Css multi-bar Adaptive Layout and css Adaptive Layout
In page refactoring, we often need to implement multi-column layout, such as a combination of fixed width of n columns and adaptive width of m columns, absolute layout + padding + percentage width is a relatively violent solution that is easy to think of, but as an "engineer" in the future, we need some elegant methods. Let's talk about the layout of the two columns. The example above is as follows:
<div class='container' > <div class='div1' >1</div> <div class='div2' >2</div></div>
If there are two sub-elements in a container, we want element 1 to be Px in width, and element 2 to be filled with the remaining container width, the violent method is the absolute layout + padding + percentage width mentioned above. The key css:
// The absolute layout of the brute force method + padding + percentage
. Container {padding-left: 200px; position: relative;}. div1 {height: 200px; position: absolute; left: 0 ;}. div2 {width: 100% ;}
I am an example, and stamp me
Of course, here. div2 is a block-level element and can be used without a width.
The second method is to use the mysterious "BFC" under the specific conditions of the dom element to clear the float. If you do not know about the search, the key css is directly used.
// Elegant method float + BFC
. Div1 {width: 200px; float: left;}. div2 {overflow: hidden ;}
I am an example, and stamp me
Is it elegant. Because. div1 width is not triggered. div2's BFC feature. setting its margin-left: 200px can also achieve the same page effect. if div1 is changed, you need to manually change it. div2 margin-left is not flexible enough. If you are interested, try it on your own.
The third method is to use the flex layout of css3, which is also known as the layout of the New Generation-a streaming layout.
// Avant-garde method flex
. Container {display: flex;}. div1 {width: 200px;}. div2 {flex: 1 ;}
I am an example, and stamp me
Isn't it very easy? If you want to know more original rational things, we suggest you observe the blog of great god. Here we only summarize the knowledge of 0.0, And the flex layout may require a prefix in Some browsers, browser prefixes can be automatically added to the http://pleeease.io/play/ (good bookmarks)
Method 4: the front-end circles in the "ancient times" were popular in table layout, and css
// Ancient method: table + table-cell
. Container {display: table; width: 100%;}. div1 {width: 200px; display: table-cell;}. div2 {display: table-cell ;}
I am an example, and stamp me
Actually, it looks quite simple, haha .. However, it may be because the name is not tall enough or is replaced by other layout methods.
The above examples are all for the layout of two columns. If there are more than two columns, the float + BFC method is not very good, because only one column can be filled with the remaining width by clearing the floating adaptive mode, I suggest using the flex layout, the principle is the same, set the width of the fixed width, auto scaling sets the flex value based on the ratio. If the table layout is not flexible enough, the fixed-width table-cell sets the width. If the width is not set, the remaining width will be evenly allocated.
To sum up the above, flex is the most flexible. In fact, there are many other advantages of flex, such as its implementation.
Vertical and horizontal centered Element
. Container {display: flex; align-items: center; // The child element is vertically centered. justify-content: center; // The child element is horizontally centered}
Of course, align-items and justify-content are also written differently in the old browser, but most modern browsers support this method. To be conservative, still go to http://pleeease.io/play/, http://caniuse.com/check
Vertical multi-column Adaptive Layout, etc.
.container{display: flex;flex-direction: column;height: 500px;}.div1{width: 100%; flex:1;}.div2{width: 100%;height: 200px;}.div3{width: 100%;flex:1;}
Learn more about other magical usage. If there are any errors, please point out