CSS Layout Chapter
In the front-end CSS learning process, layout is a very important part, therefore, I also through the collection and collation of information to write this blog, to brief about the layout of the various ways, to their own knowledge about CSS layout to do a comb, but also hope to give other people need to learn the layout of the knowledge of some help.
achieve left and right layouts
Assuming the width of the left part of the layout is fixed, the right part width varies with the width of the browser.
Useflexmethod to achieve left and right layout
<div class="container"> <div class="initial">左侧部分宽度固定</div> <div class="flex1">右侧部分宽度随浏览器宽度的变化而自适应变化 </div></div>
.container { /* background-color: red; */ display: flex; height: 100px;}.initial { width: 300px; background-color: red;}.flex1 { background-color: palevioletred; flex:1;}
Usefloatmethod to achieve left and right layout
<div class="container2 clearfix"> <div class="initial2">左侧部分宽度固定</div> <div class="float1">右侧部分宽度随浏览器宽度的变化而自适应变化 </div></div>
.container2 { height: 100px;}.clearfix::after { content: ‘‘; display: block; clear: both;}.initial2 { height: inherit; width: 300px; float: left; background-color: rosybrown;}.float1 { height: inherit; margin-left: 300px; background-color: gray;}
implementing the left-right layout
Assuming that the layout is fixed in two parts, and that part of the width varies with the width of the browser
Useflexmethod to implement the left-right layout
<div class="container3"> <div class="three_1">第一列</div> <div class="three_2">第二列</div> <div class="three_3">第三列</div></div>
.container3 { display: flex; height: 100px;}.three_1 { width: 200px; height: inherit; background-color: indigo;}.three_2 { flex: 1; height: inherit; background-color:darkcyan;}.three_3 { width: 200px; height: inherit; background-color: darkorchid;}
Usefloatmethod to implement the left-right layout
<div class="container4" <div class="three_1_1">第一列</div> <div class="three_3_3">第三列</div> <div class="three_2_2">第二列</div></div>
.container4 { height: 100px;}.three_1_1 { width: 200px; float: left; height: inherit; background-color: forestgreen;}.three_2_2 { margin-left: 200px; margin-right: 200px; height: inherit; background-color: tomato;}.three_3_3 { width: 200px; float: right; height: inherit; background-color: salmon;}
Achieve horizontal centeringInline element is centered horizontally
<div class="center-children"> This is center</div>
.center-children { text-align: center;}
Block-level elements are centered horizontally
Block-level elements are centered horizontally and are divided into two different situations
- Block-level element width-height has been determined
The following is the use of margin to set the horizontal center
<div class="center"> I‘m a block level element and am centered.</div>
.center { margin: 0 auto; width: 200px; background: black; padding: 20px;}
The following is the use of position to set the horizontal center
<div class="father"> <div class="center"> I‘m a block level element and am centered. </div></div>
.father { position: relative;}.center { position: absolute; background-color: red; width: 960px; left: 50%; transform: translate(-50%,0); text-align: center;}
- Block-level element width height not determined
<div class="father"> <div class="center"> I‘m a block level element and am centered.<br> I‘m a block level element and am centered. </div></div>
.father { text-align: center;}.center { display: inline-block}
Achieve vertical centeringInline elements are centered vertically
- Center single-line inline element
flexinline elements are vertically centered using layouts (regardless of compatibility with older browsers)
<div class="center"> I‘m a block level element and am centered.<br> I‘m a block level element and am centered.</div>
.center { display: flex; align-items: center;}
Using line-height to achieve inline element centering vertically
<div class="center"> I‘m a block level element and am centered.</div>
.center { height: 32px; line-height: 32px;}
- Multi-line inline elements are centered vertically
Use flex layouts to center inline elements vertically (regardless of compatibility with older browsers)
<div class="center"> <p> I‘m a block level element and am centered. I‘m a block level element and am centered. </p></div>
.center { display: flex; justify-content: center; flex-direction: column;}
Block-level elements are centered vertically
- Block-level element height has been determined
<main> <div> I‘m a block-level element with a fixed height, centered vertically within my parent. </div> </main>
main { background: white; height: 300px; width: 300px; position: relative; resize: vertical; overflow: auto;}main div { position: absolute; top: 50%; height: 100px; margin-top: -50px; background: black; color: white;}
- Block-level element height not determined
<main> <div> I‘m a block-level element with an unknown height, centered vertically within my parent. </div> </main>
main { position: relative;}main div { position: absolute; top: 50%; transform: translateY(-50%);}
<main> <div> I‘m a block-level element with an unknown height, centered vertically within my parent. </div> </main>
main { display: flex; flex-direction: column; justify-content: center;}
About Grid
In addition, the layout of CSS has now been developed to the grid, he is more convenient and fast and efficient.
In the case of compatibility, we should be familiar with the previous layout methods, but with a development perspective, we can look at the more powerful layout of the grid, to understand the current trend of CSS development.
There are also many articles about grids on the web, and we can learn about them, such as the Nuggets article.
The analysis and collation of CSS layout is over, I hope to help more friends.
CSS Layout article