CSS布局
默认情况下,所有的网页标签都在标准流布局中
从上到下,从左到右
脱离标准流的方法有
float属性、position属性 和 left、right、top、bottom属性
float属性的常用取值有
left:脱离标准流,浮动在父标签的最左边
right:脱离标准流,浮动在父标签的最右边
Position属性
值 | 描述 |
---|---|
absolute | 生成绝对定位的元素,相对于 static 定位以外的第一个父元素进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。 |
fixed | 生成绝对定位的元素,相对于浏览器窗口进行定位。元素的位置通过 "left", "top", "right" 以及 "bottom" 属性进行规定。 |
relative | 生成相对定位的元素,相对于其正常位置进行定位。因此,"left:20" 会向元素的 LEFT 位置添加 20 像素。 |
static | 默认值。没有定位,元素出现在正常的流中(忽略 top, bottom, left, right 或者 z-index 声明)。 |
inherit | 规定应该从父元素继承 position 属性的值。 |
<html>
<head>
<meta charset="UTF-8">
<title>练习练习</title>
<style>
.group1{
width: 150px;
color: white;
text-align: center;
text-shadow: black 2px 2px;
box-shadow: black 2px 2px 5px;
margin-bottom: 10px;
padding-top: 10px;
padding-bottom: 10px;
border-radius: 15px;
float: right;
}
#test1 {
background-color: rgba(255,0,0,1);
float: left;
}
#test2 {
background-color: rgba(255,0,0,0.8);
float: left;
}
#test3 {
background-color: rgba(255,0,0,0.6);
float: left;
}
#test4 {
background-color: rgba(255,0,0,0.4);
float: left;
}
#test5 {
background-color: rgba(255,0,0,0.2);
float: left;
}
#main {
padding: 10px;
}
.group2 {
background-color: aquamarine;
width: 100px;
margin: 5px;
text-align: center;
padding: 10px;
float: left;
position: inherit;
}
#main2 {
width: 50%;
height: 50px;
background-color: darkcyan;
padding: 20px;
position: static;
}
#bottom {
position: fixed;
left: 0px;
bottom: 0px;
right: 0px;
height: 50px;
background-color: deepskyblue;
color: white;
text-align: center;
}
</style>
</head>
<body>
<div id="main" class="group1">
<div id="test1" class="group1">CSS3新属性</div>
<div id="test2" class="group1">CSS3新属性</div>
<div id="test3" class="group1">CSS3新属性</div>
<div id="test4" class="group1">CSS3新属性</div>
<div id="test5" class="group1">CSS3新属性</div>
</div>
<div id="main2">
<div class="group2">CSS3新属性</div>
<div class="group2">CSS3新属性</div>
<div class="group2">CSS3新属性</div>
<div class="group2">CSS3新属性</div>
<div class="group2">CSS3新属性</div>
</div>
<div id="bottom">Flashloft</div>
</body>
</html>
水平居中和垂直居中
水平居中:
行内标签和行内块级标签使用:父级标签设置text-align:center
块级标签使用:margin:0 auto
垂直居中:
适用于行内标签和行内-块级标签:父级标签设置line-height
适用于块级标签:position left top tanslate
#bottom {
position: fixed;
left: 0px;
bottom: 0px;
right: 0px;
height: 50px;
text-align: center;
line-height: 50px;
background-color: deepskyblue;
color: white;
position: fixed;
}
#logo {
width: 200px;
height: 20px;
line-height: 20px;
background-color: cadetblue;
margin: auto auto;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%) ;
}
图片垂直居中:
#panel-footer img {
width: 30px;
height: 30px;
vertical-align: middle;
}
box-sizing
以特定的方式定义匹配某个区域的特定元素,默认值为 content-box,将 box-sizing 设置为 “border-box”,可以把边框和内边距放入框中
transform
-webkit-transition
-webkit-transform-origin
-webkit-transform