CSS and other proportional division, in the CSS layout is more important, the following share several common methods and explore compatibility.
One: Floating layout + percent
The code is as follows |
Copy Code |
Emmet HTML code: ul.float-ul>li*5>.con>h3{equal headings}+p{equal content} . float-ul{width:100%; overflow:hidden; zoom:1;} . float-ul li{float:left; width:20%;}
|
This style of compatibility is good, but can not achieve when the number of columns in the proportion also changes, you must manually modify, of course, you can also use a JS to adjust.
Two: In-line Element (Inline-block) + percent
Refer to the previously written inline-block replace float code, which also enables you to implement the attributes of block-level elements such as layout-centered.
The code is as follows |
Copy Code |
. inline-ul{font-size:0 *word-spacing: -1px;} . inline-ul Li{display:inline-block; *display:inline; *zoom:1; font-size:14px; vertical-align:top; word-spacing:norma L Letter-spacing:normal; width:20%;} @media screen and (-webkit-min-device-pixel-ratio:0) { . inline-ul{letter-spacing: -5px; }
|
Three: Display:table + Display:table-cell
We know that the table can be divided according to the content, CSS also has a style is display:table to achieve a similar table layout, but do not support IE8 the following browsers.
The code is as follows |
Copy Code |
. table-cell{display:table; width:100%;} . Table-cell Li{display:table-cell;}
|
Four: Use CSS3 Display:flex.
Old syntax:
The code is as follows |
Copy Code |
. Flex-ul{display:-webkit-box; display:box;} . flex-ul li{-webkit-box-flex:1; box-flex:1;} New syntax: . Flex-ul{display:-webkit-flex; display:flex; width:100%;} . flex-ul li{-webkit-flex:1; flex:1;} |
This method only applies to the Advanced browser, IE10 the following or forget. Specific introduction
V: Using grid systems
such as the bootstrap grid system
The code is as follows |
Copy Code |
<div class= "Container" > <div class= "Row" > <div class= "Col-md-3" ></div> ... </div> </div> . col-md-3 {Float:left} @media (min-width:992px) { . col-md-3 {width:25%} /* 3/12 of the parent container * * }
|
The disadvantage is the same as the first float, which needs to jump out of the width adjustment based on the number of columns.
Example
The code is as follows |
Copy Code |
<style type= "Text/css" > Ul{list-style:none; margin:0; padding:0;} . float-ul{width:100%; overflow:hidden; zoom:1;} . float-ul li{float:left; width:20%;} . inline-ul{font-size:0 *word-spacing: -1px;} . inline-ul Li{display:inline-block; *display:inline; *zoom:1; font-size:14px; vertical-align:top; word-spacing:norma L Letter-spacing:normal; width:20%;} @media screen and (-webkit-min-device-pixel-ratio:0) { . inline-ul{letter-spacing: -5px; } . table-cell{display:table; width:100%;} . Table-cell Li{display:table-cell;} . Flex-ul{display:-webkit-flex; Display:flex; width:100%;} . flex-ul li{-webkit-flex:1; flex:1;} /*.flex-ul{display:-webkit-box; display:box;} . flex-ul li{-webkit-box-flex:1; box-flex:1;} */ </style> <body> ul.float-ul>li*5>.con>h3{}+p{divided into equal content} <ul class= "Float-ul" > <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> </ul> <ul class= "Inline-ul" > <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> </ul> <ul class= "Table-cell j_equallist" > <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> </ul> <ul class= "Flex-ul" > <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> <li> <div class= "Con" > <p> equal content </p> </div> </li> </ul> <script type= "Text/javascript" > Window.onload = function () { function Setequal (CLS) { var a = Document.queryselectorall (CLS); for (var i = 0, L = a.length i<l; i++) { var b = a[i]; var child = B.children; var c = child.length; for (var j = 0; j<c; i++) { Child[i].style.width = ' 20% '; } } var child = A.children; for (var i = 0, L = child.length i<l; i++) { child[i].style.width = 100/l+ "%"; // } } Setequal ('. J_equallist '); }
</script> |
Upper and lower three lines of layout, up and down high, the middle bar adaptive browser height, and content vertically centered
The code is as follows |
Copy Code |
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/> <title> the next three lines of layout, up and down high, the middle bar adaptive browser height, and the content vertically centered </title> <style type= "Text/css" > * { margin:0; padding:0; } Html Body #box { height:100%; font:small/1.5 "Song Body", serif; } Body { Text-align:center; } #box { Text-align:left; Background: #666; display:table; width:80%; margin:0 Auto; position:relative; } #box > Div { Display:table-row; } #header, #footer { Background: #fcc; height:50px; } #main { Background: #ccf; } #main #wrap { Display:table-cell; Background: #ffc; Vertical-align:middle; } #text { Text-align:center; } </style> <!--[If ie]> <style type= "Text/css" > #header, #footer { width:100%; Z-index:3; Position:absolute; } #footer { bottom:0; } #main { height:100%; Z-index:1; position:relative; } #main #wrap { Position:absolute; top:50%; width:100%; Text-align:left; } #main #text { position:relative; width:100%; top:-50%; Background: #ccc; } </style> <! [endif]--> <body> <div id= "box" > <div id= "Header" >header</div> <div id= "Main" > <div id= "Wrap" > <div id= "Text" > <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> <p> content </p> </div> </div> </div> <div id= "Footer" >footer</div> </div> </body> |
Summarize:
If a compatible ie6-ie7 is required, the first and second types can be used if the number of columns is fixed. If the number of columns is not fixed, you can add a few JS support.
If you only consider moving, consider a third, compatibility is better than the flex support below.
If you simply do not consider the lower version of the browser, you can consider the use of the fourth.