CSS使用总结
使用CSS的三种方式
1.外部样式表
样式应用于多页面。
<head>
<link rel="stylesheet" type="text/css" href="test.css" />
</head>
2.内部样式表
使用 <style>
标签在文档头部定义内部样式表。
<head>
<style type="text/css">
body {margin:0; padding:0;}
</style>
</head>
3.内联样式
利用所有标签都有的style属性。
<p style="color: blue; margin-left: 20px;font-size:12px">
优先级:属性选择器>id选择器>类选择器>元素选择器
</p>
选择器
ID选择器
id选择器可以为标有特定id的HTML元素指定特定的样式。
格式:#id名{ 属性:值;}
类选择器
把某一个样式应用到相同的HTML元素上。
格式1:.类名{ 属性:值;}
格式2:元素.类名{ 属性:值;}
元素(派生)选择器
通过依据元素在其位置的上下文关系来定义样式,你可以使标记更加简洁。
格式:元素 { 属性:值;}
属性选择器
对带有指定属性的HTML元素设置样式。
格式:[属性名]{属性:值;}
布局常用属性
什么叫布局?
将有限的视觉元素进行有机的排列组合。
常用属性:
margin:外边距。(居中:{margin:0 auto})
padding:内边距。
width:宽度。
height:高度。
background:背景。
position:元素定位。
font-size:字体尺寸。
font-weight:字体粗细。
text-align:对齐方式。
line-height:行间的距离(行高)。
float:浮动。
overflow:规定当内容溢出元素框时发生的事情。
clear:规定元素的哪一侧不允许其他浮动元素。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>混合布局</title>
<style type="text/css">
body{ margin:0; padding:0; font-size:30px; font-weight:bold}
div{ text-align:center; line-height:50px}
.top{ height:100px;background:#9CF}
.main{ margin:0 auto;}
.left{ width:20%; height:600px; background:#ccc; float:left;}
.right{ width:80%; height:600px;background:#FCC; float:left}
.r_sub_left{ width:20%; height:600px; background:#9C3; float:left}
.r_sub_right{ width:80%; height:600px; background:#9FC; float:right;}
.footer{ height:50px; background:#9F9; clear: both;}
</style>
</head>
<body>
<div class="top">top</div>
<div class="main">
<div class="left">left</div>
<div class="right">
<div class="r_sub_left">sub_left</div>
<div class=" r_sub_right">sub_right</div>
</div>
</div>
<div class="footer">footer</div>
</body>
展示: