CSS基础知识(5)浮动、定位

1、文档流
元素自上而下,自左而右,块元素独占一行,行内元素在一行上显示,碰到父集元素的边框换行。

2、浮动布局(想让盒子div一行显示就需要使用到浮动)

特点:
★元素浮动之后不占据原来的位置(脱标)
★(两个或两个以上盒子)浮动,盒子在一行上显示
★行内元素浮动之后转换为行内块元素。(不推荐使用,转行内元素最好使用display: inline-block;)

【1】浮动的方向
float: left | right

【2】浮动的作用
◆文本绕图
.box {
width: 400px;
height: 300px;
background: #eee;
}
.box img{
float: left;
}
◆制作导航
无序列表简写:ul>li*3>a[#]
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
/*样式表写到head中*/
<style type="text/css">
/*初始化*/
body,ul,li{
margin: 0;
padding: 0;
}
ul,li{
list-style: none;
}
.nav{
width: 800px;
height: 40px;
background: pink;
margin: 20px auto;/*居中显示*/
}
.nav ul li{
float: left;/*浮动显示*/
}
.nav ul li a{
display: inline-block;/*让a标签转为行内块元素,否则无法定义高度*/
height: 40px;
font: 14px/40px 微软雅黑;/*导航直接给a标签行高和高度*/
padding: 0 20px;/*如果使用margin就不会撑开盒子*/
text-decoration: none;/*去除下划线*/
}
.nav ul li a:hover{
background: #eee;
}

</style>
</head>
<body>
<div class= "nav">
<ul>
<li><a herf="#">火柴</a></li>
<li><a herf="#">孤独</a></li>
<li><a herf="#">遇见</a></li>
</ul>
</div>

</body>
</html>

◆网页布局
<style type="text/css">
.header,.main,.footer{
width: 500px;
}
.header{
height: 100px;
background: #000;
}
.main{
height: 300px;
background: #eee;
margin: 10px 0;
}
.content{
width: 300px;
height: 300px;
background: orange;
float: left;/*如果下面的.sidebar不设置浮动,会因为脱标而隐藏在下面*/
}
.sidebar{
width: 190px;
height: 300px;
background: green;
float: right;
}
</style>
</head>
<body>
<div class= "header"></div>
<div class= "main">
<div class= "content"></div>
<div class= "sidebar"></div>
</div>
<div class= "footer"></div>
</body>
</html>

【3】清除浮动
如果把上述.main中的高度删除
.main{
/* height: 300px;*/
background: #eee;
margin: 10px 0;
}
/*当父盒子没有定义高度,嵌套的盒子浮动之后,下边的元素发生位置错误,会导致下面的内容往上移动*/

但在做网页的时候比如一些新闻的高度你无法预测它是多长的,所以需要一种即使父盒子没有定义高度,嵌套盒子浮动后,下面的内容也不会错乱的方法。

◆清除浮动不是不用浮动,清除浮动产生的不利影响。
◆清除浮动的方法
clear: left | right | both
工作里用的最多的是clear:both;

(1)额外标签法
在最后一个浮动元素后添加标签,。
<body>
<div class= "header"></div>
<div class= "main">
<div class= "content"></div>
<div class= "sidebar"></div>
<div style="clear:both;"></div>/*如果浮动太多,此方法并不实用*/
</div>
<div class= "footer"></div>
</body>

(2)给父集元素使用overflow:hidden; bfc
如果有内容出了盒子,不能使用这个方法。
.main{
height: 300px;
background: #eee;
margin: 10px 0;
overflow: hidden;
}

(3)伪元素清除浮动 推荐使用
.clearfix:after{
content: ".";/*(相当于插入一个盒子)不过不可选取,所以高度、行高为零*/
display: block;/*让它可见*/
height: 0;
line-height: 0;
visibility: hidden;/*插入盒子但让它隐藏*是为了下面清除浮动*/
clear: both;
}
/*兼容ie浏览器*/
.clearfix{
zoom: 1;
}
.main{
height: 300px;
background: #eee;
margin: 10px 0;
}
.content{
width: 300px;
height: 300px;
background: orange;
float: left;/*如果下面的.sidebar不设置浮动,会因为脱标而隐藏在下面*/
}
.sidebar{
width: 190px;
height: 300px;
background: green;
float: right;
}
</style>
</head>
<body>
<div class= "header"></div>
<div class= "main clearfix">
<div class= "content"></div>
<div class= "sidebar"></div>
</div>
<div class= "footer"></div>
</body>
</html>

3、CSS初始化
因为浏览器兼容性问题,且不同浏览器对有些标签的默认值是不同的,如果不对CSS初始化往往会出现浏览器之间的页面差异。
腾讯
body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select{margin:0;padding:0}
body{font:12px"宋体","Arial Narrow",HELVETICA;background:#fff;-webkit-text-size-adjust:100%;}
a{color:#2d374b;text-decoration:none}
a:hover{color:#cd0200;text-decoration:underline}
em{font-style:normal}
li{list-style:none}
img{border:0;vertical-align:middle}
table{border-collapse:collapse;border-spacing:0}
新浪:
body,ul,ol,li,p,h1,h2,h3,h4,h5,h6,form,fieldset,table,td,img,div{margin:0;padding:0;border:0;}
body{background:#fff;color:#333;font-size:12px; margin-top:5px;font-family:"SimSun","宋体","Arial Narrow";}
ul,ol{list-style-type:none;}
select,input,img,select{vertical-align:middle;}
a{text-decoration:none;}
a:link{color:#009;}
a:visited{color:#800080;}
a:hover,a:active,a:focus{color:#c00;text-decoration:underline;}
淘宝:
body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fieldset, legend, button, input, textarea, th, td { margin:0; padding:0; }
body, button, input, select, textarea { font:12px/1.5tahoma, arial, \5b8b\4f53; }
h1, h2, h3, h4, h5, h6{ font-size:100%; }
address, cite, dfn, em, var { font-style:normal; }
code, kbd, pre, samp { font-family:couriernew, courier, monospace; }
small{ font-size:12px; }
ul, ol { list-style:none; }
a { text-decoration:none; }
a:hover { text-decoration:underline; }
sup { vertical-align:text-top; }
sub{ vertical-align:text-bottom; }
legend { color:#000; }
fieldset, img { border:0; }
button, input, select, textarea { font-size:100%; }
table { border-collapse:collapse; border-spacing:0; }
p{word-wrap:break-word}

4、overflow属性

overflow: visiable /*默认值,内容不会被修剪,会呈现在元素框之外*/
overflow: hidden /*内容会被修剪,并且其余内容是不可见的*/
overflow: scroll /*内容会被修剪,但浏览器会显示滚动条以便查看其余内容*/
overflow: auto /*如果内容被修剪,则浏览器会显示滚动条以便查看其余内容*/

5、定位
定位方向: left | right | top | bottom
【静态定位】
position:static; 静态定位。默认值,就是文档流。
【绝对定位】
Position:absolute;
特点:
★元素使用绝对定位之后不占据原来的位置(脱标)
★元素使用绝对定位,位置是从浏览器出发。
★嵌套的盒子,父盒子没有使用定位,子盒子绝对定位,子盒子位置是从浏览器出发。
★嵌套的盒子,父盒子使用定位,子盒子绝对定位,子盒子位置是从父元素位置出发。
★给行内元素使用绝对定位之后,转换为行内块。(不推荐使用,推荐使用display:inline-block;)

【相对定位】
Position: relative;
特点:
★使用相对定位,位置从自身出发。w
★还占据原来的位置。
★重点:子绝父相(父元素相对定位,子元素绝对定位) /*子元素根据父元素出发*/
★行内元素使用相对定位不能转行内块

【固定定位】
Position:fixed;
特点:
★固定定位之后,不占据原来的位置(脱标)
★元素使用固定定位之后,位置从浏览器出发。
★元素使用固定定位之后,会转化为行内块(不推荐,推荐使用display:inline-block;)

猜你喜欢

转载自www.cnblogs.com/Automated-testing/p/9186040.html