美文网首页
2018-11-29

2018-11-29

作者: 清酒金杯空对月 | 来源:发表于2018-12-05 20:28 被阅读0次

本来父元素是由子元素抻开的,子元素设置了浮动之后,子元素就脱离文档流了,父元素无法抻开,就会形成高度塌陷

解决高度塌陷

BFC
父元素的垂直外边距不会和子元素重叠
开启BFC的元素不会被浮动元素所覆盖
开启BFC的元素可以包含浮动的子元素

如何开启BFC

设置元素浮动
设置元素的绝对定位
设置元素为inline-block
将元素的overflow设置为一个非visible的值(副作用最小的)

.box{
border:10px red solid;
overflow:hidden;
}

兼容IE6浏览器
hasLayout zom:1;

overflow(溢出)

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

.box{
width:100px;
height:100px;
background-color:red;
overflow:visible;
}

2.文档流

每个页面都是文档,每个文档也是一个网页
块元素在文档流中如果设置宽和高就默认为设置的宽和高,设置padding会影响盒子的大小,如果没设置宽就默认父*
元素宽的100%,高的话是由内容决定,内容有多高,它就有多高
内联元素只占自身的宽和高

3.浮动
.box2{
width: 100px;
height: 100px;
background-color: red;
/display: inline-block;/
float: left;
}

left 元素向左浮动。
right 元素向右浮动。
none 默认值。元素不浮动,并会显示在其在文本中出现的位置。

内联浮动

所有的内联元素如果脱离文档流就会变成块元素,内联元素是不支持宽和高,如果变成块元素就会生效。

z-index
.box1{
width: 200px;
height: 200px;
background-color: #0bcd96;
position:relative;
z-index:3;
}
.box2{
width: 200px;
height: 200px;
background-color: #1e389a;
position: absolute;
left: 100px;
top: 100px;
}
.box3{
width: 200px;
height: 200px;
background-color: #6a1752;
position: relative;
z-index:2;
}

如果没有开启定位的元素或者是定位为默认值,会忽略z-index的声明
父元素的层级再怎么高也不会盖住子元素,可以理解为(水涨船高)

opacity(透明)
.box1{
width: 200px;
height: 200px;
background-color: #0bcd96;
position:relative;
opacity: 50%;
filter: alpha(opacity=50);
}
.box2{
width: 200px;
height: 200px;
background-color: #1e389a;
position: absolute;
left: 100px;
top: 100px;
opacity:0.5px;
filter: alpha(opacity=50);
}
.box3{
width: 200px;
height: 200px;
background-color: #6a1752;
position: relative;
opacity: 50%;
filter: alpha(opacity=50);
}

opacity只能设置0-1之间的数值
filter: alpha(opacity=50)兼容IE浏览器

背景(background-image)
.box1{
width: 500px;
height: 500px;
margin:0 auto;
background-color: #0bcd96;
background-image: url(../css/logo_new@2x.png);
background-repeat:no-repeat;
}

background-repeat:no-repeat 图片只显示一次
background-repeat:repeat-x 背景将在水平方向重复
background-repeat:repeat-y 背景将在垂直方向重复

相关文章

网友评论

      本文标题:2018-11-29

      本文链接:https://www.haomeiwen.com/subject/dmxgcqtx.html