表格元素及相关样式
1、<table>标签:声明一个表格
2、<tr>标签:定义表格中的一行
3、<td>和<th>标签:定义一行中的一个单元格,td代表普通单元格,th表示表头单元格
表格相关样式属性
border-collapse 设置表格的边线合并,如:border-collapse:collapse;
<style>
.table01{
width:500px;
height:200px;
border:1px solid black;
/* 设置表格的边线合并 */
border-collapse:collapse;
/* 设置表格水平居中 */
margin:0px auto;
}
.table01 th{
border:1px solid black;
background:lightskyblue;
color:white;
}
.table01 td{
border:1px solid black;
/* 设置文字水平居中 */
text-align:center;
}
</style>
<body>
<!-- table>(tr>td*5)*4 -->
<table class="table01">
<tr>
<th>序号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>班级</th>
</tr>
<tr>
<td>1</td>
<td>张山</td>
<td>男</td>
<td>18</td>
<td>python10</td>
</tr>
<tr>
<td>2</td>
<td>张山</td>
<td>男</td>
<td>18</td>
<td>python10</td>
</tr>
<tr>
<td>3</td>
<td>张山</td>
<td>男</td>
<td>18</td>
<td>python10</td>
</tr>
</table>
</body>
定位
文档流
文档流,是指盒子按照html标签编写的顺序依次从上到下,从左到右排列,块元素占一行,行内元素在一行之内从左到右排列,先写的先排列,后写的排在后面,每个盒子都占据自己的位置。
关于定位
我们可以使用css的position属性来设置元素的定位类型,postion的设置项如下:
-
relative 生成相对定位元素, 本质是相对于自身进行定位.
-
.box01{ background:lightgreen; /* 设置相对定位 本质是相对于自身进行定位*/ position:relative; left:50px; top:50px; } -
absolute 生成绝对定位元素,元素脱离文档流,不占据文档流的位置,可以理解为漂浮在文档流的上方,相对于上一个设置了定位的父级元素来进行定位,如果找不到,则相对于body元素进行定位。
-
.box01{ background:lightgreen; /* 设置绝对定位 */ position:absolute; left:131px; top:50px; }注意:父级要设置为相对定位,(不设置偏移,父级自身就不发生位置改变),子级就以它为参照做绝对定位,如果父级未设置定位,则子集会相对于body进行定位 -
fixed 生成固定定位元素,元素脱离文档流,不占据文档流的位置,可以理解为漂浮在文档流的上方,相对于浏览器窗口进行定位。
-
.box01{ background:lightgreen; /* 设置固定定位 */ position:fixed; right:131px; bottom:50px; }
定位元素的偏移
定位的元素还需要用left、right、top或者bottom来设置相对于参照元素的偏移值。
定位元素层级
定位元素是浮动的正常的文档流之上的,可以用 z-index 属性来设置元素的层级
伪代码如下:
.box01{
......
position:absolute; /* 设置了绝对定位 */
left:200px; /* 相对于参照元素左边向右偏移200px */
top:100px; /* 相对于参照元素顶部向下偏移100px */
z-index:10 /* 将元素层级设置为10,如果不设置z-index属性,定位元素的层级安装代码书写的顺序,先写的排列在下面,后写的排列在上面*/ }
水平垂直居中的弹框
<style>
.pop{
width:500px;
height:300px;
border:1px solid black;
background:white;
position: fixed;
/* 弹框水平垂直居中的写法: */
left:50%;
top:50%;
margin-left:-251px;
margin-top:-151px;
/* 设置弹框的层级,设置比较大的值,盖住其他所有的元素*/
z-index:9999;
}
.pop h3{
margin:5px;
background: lightskyblue;
line-height:40px;
}
.mask{
position:fixed;
left:0px;
top:0px;
width:100%;
height:100%;
background:black;
z-index:9998;
/* 设置背景的透明度 */
opacity:0.3;
/* 透明度兼容写法,兼容IE */
filter:alpha(opacity=30);
}
/* 设置pop_con的隐藏,可以同时将弹框和背景隐藏起来 */
.pop_con{
display:none;
}
</style>
<body>
<h1>网页内容</h1>
<div class="pop_con" style="display:block">
<div class="pop">
<h3>弹框标题</h3>
<p>弹框的文字内容</p>
</div>
<div class="mask"></div>
</div>
</body>
新增相关样式属性
/* 设置元素透明度,将元素透明度设置为0.3,此属性需要加一个兼容IE的写法 */ opacity:0.3; /* 兼容IE */ filter:alpha(opacity=30);
CSS权重
CSS权重指的是样式的优先级,有两条或多条样式作用于一个元素,权重高的那条样式对元素起作用,权重相同的,后写的样式会覆盖前面写的样式。
权重的等级
可以把样式的应用方式分为几个等级,按照等级来计算权重
1、内联样式,如:style=””,权重值为1000
2、ID选择器,如:#content,权重值为100
3、类,伪类,如:.content、:hover 权重值为10
4、标签选择器,如:div、p 权重值为1
权重的计算实例
1、实例一:
<style type="text/css">
div{
color:red;
}
</style>
......
<div style="color:blue">这是一个div元素</div>
<!--
两条样式同时作用一个div,上面的样式权重值为1,下面的行间样式的权重值为1000,
所以文字的最终颜色为blue
-->
2、实例二:
<style type="text/css">
body #content .main_content h2{
color:red;
}
#content .main_content h2{
color:blue;
}
</style>
......
<div id="content">
<div class="main_content">
<h2>这是一个h2标题</h2>
</div>
</div>
<!--
第一条样式的权重计算: 1+100+10+1,结果为112;
第二条样式的权重计算: 100+10+1,结果为111;
h2标题的最终颜色为red
-->