Four commonly used analytical methods and principles: floating, floating embedded div, positioning, flex.
float
<style type="text/css">
.wrap {background: #eee; padding: 20px; }
p {margin: 0; }
.left {width: 200px; height: 200px; float: left; background: coral; }
.right {width: 200px; height: 200px; float: right; background: lightblue; }
.middle {margin: 0 200px; background: lightpink; }
</style>
<div class="wrap">
<p class="left">我在左边</p>
<p class="right">我在右边</p>
<p class="middle">我排最后,但是跑到中间来了</p>
</div>
principle:
- Non-floating elements and the floating elements not in the same three-dimensional space, if the clear float position of the element to below it will float.
- Float height is 0, the floating box level than
block
high-level block hbox than theinline/inline-block
lower level of the box.
Embedded floating div
<style type="text/css">
.wrap {background: #eee; padding: 20px; }
p {margin: 0; }
.left {width: 200px; height: 200px; float: left; background: coral; margin-left: -100%;}
.right {width: 200px; height: 200px; float: left; background: lightblue; margin-left: -200px;}
.middle {width: 100%; height: 200px;float: left; background: lightpink; }
span{
display: inline-block;
margin: 0 200px;
}
</style>
<div class="wrap">
<p class="middle">
<span class="inner">
我在中间
</span>
</p>
<p class="left">我在左边</p>
<p class="right">我在右边</p>
</div>
principle:
- Three floating elements, wherein the elements relating to a line of 100% covered by the negative
margin
to put the left and right elements. - Themes which then sets a sub-elements, sub-elements
margin: 0 200px
, to prevent the contents went around two floating elements are covered below.
Locate
<style type="text/css">
.wrap {background: #eee; position: relative;}
p {margin: 0; }
.left {width: 200px; height: 200px; background: coral; position: absolute;left: 0; top: 0;}
.right {width: 200px; height: 200px; background: lightblue; position: absolute;right: 0; top: 0;}
.middle {height: 200px; background: lightpink; margin: 0 200px;}
</style>
<div class="wrap">
<p class="middle">我在中间,我用 margin 抵消左右两块定位元素占据空间</p>
<p class="left">我在左边,我是定位元素</p>
<p class="right">我在右边,我是定位元素</p>
</div>
principle:
- Two right and left positioning elements, can be placed anywhere.
- With the intermediate element
margin: 0 200px
to prevent the contents went around two positioning elements are covered below.
flex
<style type="text/css">
.wrap {background: #eee; display: flex}
p {margin: 0; }
.left {width: 200px; height: 200px; background: coral; }
.right {width: 200px; height: 200px; background: lightblue; }
.middle {height: 200px; background: lightpink; flex: 1;}
</style>
<div class="wrap">
<p class="left">我在左边</p>
<p class="middle">我在中间,flex: 1 自动占据剩余空间</p>
<p class="right">我在右边</p>
</div>
principle:
-
flex
Layout, the default sub-elements arranged horizontally. -
flex: 0 1 auto
-> default, the parent does not follow the footprint amplification, followed smaller, their original width -
flex: 1 1 auto
-> auto, follow the footprint parent amplification, while following small, their original width -
flex: 0 0 auto
-> none, the parent does not follow the footprint enlarged, while not follow small, their original width -
flex: 1 1 1
-> auto, follow the footprint parent amplification, while following becomes smaller, and automatically fills the remaining space