A very important part of the front-end layout is the construction of the page framework, which is also the most basic part. In the construction of the page framework, there are centered layout, multi-column layout and global layout. Today we will summarize the CSS layout in front-end useful information.
1) Use inline-block+text-align
(1) Principle and usage
Principle: First change the sub-box from a block-level element to an inline block element, and then set the inline block element to be centered to achieve horizontal centering.
Usage: Set display:inline-block for the child box and text-align:center for the parent box.
(2) Code examples
<p class="parent"> <p class="child>DEMO</p> </p>
.child{ display:inline-block; }.parent{ text-align:center; }
(3) Advantages and disadvantages
Advantages: Good compatibility, even compatible ie6, ie7
Disadvantage: the text in the child will also be horizontally centered, you can add text-align:left; in .child to restore
2) Use table+margin
(1) Principle and usage
Principle: First set the sub-box to a block-level table for display (similar to
和 | ),再通过设置属性,使表格单元格内容垂直居中以达到垂直居中。 用法:先将父框设置为display:table-cell,再设置vertical-align:middle。 (2)代码实例 <p class="parent"> <p class="child>DEMO</p> </p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { display:table-cell; vertical-align:middle; } Copy after login (3)优缺点
2)使用absolute+transform
(2)代码实例 <p class="parent"> <p class="child>DEMO</p> </p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { position:relative; }.child { position:absolute; top:50%; transform:translateY(-50%); } Copy after login (3)优缺点
3)使用flex+align-items
(1)代码实例 <p class="parent"> <p class="child>DEMO</p> </p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { position:flex; align-items:center; } Copy after login (3)优缺点
水平垂直居中1)使用absolute+transform
(2)代码实例 <p class="parent"> <p class="child>DEMO</p> </p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { position:relative; }.child { position:absolute; left:50%; top:50%; transform:tranplate(-50%,-50%); } Copy after login (3)优缺点
2)使用inline-block+text-align+table-cell+vertical-align
(2)代码实例 <p class="parent"> <p class="child>DEMO</p> </p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { text-align:center; display:table-cell; vertical-align:middle; }.child { display:inline-block; } Copy after login (3)优缺点
3)使用flex+justify-content+align-items
(2)代码实例 <p class="parent"> <p class="child>DEMO</p> </p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { display:flex; justify-content:center; align-items:center; } Copy after login (3)优缺点
多列布局定宽+自适应1)使用float+overflow
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .left { float:left; width:100px; margin-right:20px; }.right { overflow:hidden; } Copy after login (3)优缺点
2)使用float+margin
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .left { float:left; width:100px; }.right { margin-left:120px; } Copy after login (3)优缺点
3)使用float+margin(改良版)
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="rigth-fix"> <p class="right"> <p>right</p> <p>right</p> </p> </p></p> Copy after login .left { float:left; width:100px; position:relative; }.right-fix { float:right; width:100%; margin-left:-100px; }.right { margin-left:120px; } Copy after login (3)优缺点
4)使用table
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { display:table; width:100%; table-layout:fixed; }.left { width:100px; padding-right:20px; }.right,.left { display:table-cell; } Copy after login 5)使用flex
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { display:flex; }.left { width:100px; margin-right:20px; }.right { flex:1; } Copy after login (3)优缺点
两列定宽+一列自适应(1)原理、用法
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="center"> <p>center</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login .left,.center { float:left; width:100px; margin-right:20px; }.right { overflow:hidden; } Copy after login 不定宽+自适应1)使用float+overflow
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .left{ float: left; margin-right: 20px; }.right{ overflow: hidden; }.left p{ width: 200px; } Copy after login (3)优缺点
2)使用table
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent{ display: table; width: 100%; }.left,.right{ display: table-cell; }.left{ width: 0.1%; padding-right: 20px; }.left p{ width:200px; } Copy after login (3)优缺点
3)使用flex
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { display:flex; }.left { margin-right:20px; }.right { flex:1; }.left p{ width: 200px; } Copy after login (3)优缺点
两列不定宽+一列自适应(1)原理、用法
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="center"> <p>center</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login .left,.center{ float: left; margin-right: 20px; }.right{ overflow: hidden; }.left p,.center p{ width: 100px; } Copy after login 等分布局公式转化:l = w * n + g * (n-1) -> l = w * n + g * n - g -> l + g = (w + g) * n Copy after login 因此,我们需要解决两个问题:
1)使用float
(2)代码实例 <p class="parent"> <p class="column"><p>1</p></p> <p class="column"><p>2</p></p> <p class="column"><p>3</p></p> <p class="column"><p>4</p></p></p> Copy after login Copy after login .parent{ margin-left: -20px;//l增加g } .column{ float: left; width: 25%; padding-left: 20px; box-sizing: border-box;//包含padding区域 w+g } Copy after login (3)优缺点
2)使用table
(2)代码实例 <p class="parent-fix"> <p class="parent"> <p class="column"><p>1</p></p> <p class="column"><p>2</p></p> <p class="column"><p>3</p></p> <p class="column"><p>4</p></p> </p></p> Copy after login .parent-fix{ margin-left: -20px;//l+g } .parent{ display: table; width:100%; table-layout: fixed; } .column{ display: table-cell; padding-left: 20px;//w+g} Copy after login (3)优缺点
3)使用flex
(2)代码实例 <p class="parent"> <p class="column"><p>1</p></p> <p class="column"><p>2</p></p> <p class="column"><p>3</p></p> <p class="column"><p>4</p></p></p> Copy after login Copy after login .parent{ display: flex; }.column{ flex: 1; }.column+.column{ margin-left:20px; } Copy after login (3)优缺点
定宽+自适应+两块高度一样高1)使用float
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login p{ background: none!important; }.left,.right{ background: #444; }.parent{ overflow: hidden; }.left,.right{ padding-bottom: 9999px; margin-bottom: -9999px; }.left{ float: left; width: 100px; margin-right: 20px; }.right{ overflow: hidden; } Copy after login (3)优缺点
2)使用table
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { display:table; width:100%; table-layout:fixed; } .left { width:100px; padding-right:20px; } .right,.left { display:table-cell; } Copy after login 3)使用flex
(2)代码实例 <p class="parent"> <p class="left"> <p>left</p> </p> <p class="right"> <p>right</p> <p>right</p> </p></p> Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login Copy after login .parent { display:flex; } .left { width:100px; margin-right:20px; } .right { flex:1; } Copy after login (3)优缺点
4)使用display
(2)代码实例 <p class="parent"> <p class="left">left</p> <p class="right">right </p> </p> Copy after login .parent { width: 100%; display: -webkit-box; } .left { width:100px; margin-right: 20px; } .right { -webkit-box-flex: 1; } Copy after login (3)优缺点
全屏布局全屏布局的特点
全屏布局的方法1)使用position
(2)代码实例 <p class="parent"> <p class="top">top</p> <p class="left">left</p> <p class="right"> <p class="inner">right</p> </p> <p class="bottom">bottom</p> </p> Copy after login html,body,.parent{ margin:0; height:100%; overflow:hidden; }body{ color:white; }.top{ position:absolute; top:0; left:0; right:0; height:100px; background:blue; }.left{ position:absolute; left:0; top:100px; bottom:50px; width:200px; background:red; }.right{ position:absolute; left:200px; top:100px; bottom:50px; right:0; background:pink; overflow: auto; }.right .inner{ min-height: 1000px; }.bottom{ position:absolute; left:0; right:0; bottom:0; height:50px; background: black; } Copy after login (3)优缺点
2)使用flex
(2)代码实例 <p class="parent"> <p class="top">top</p> <p class="middle"> <p class="left">left</p> <p class="right"> <p class="inner">right</p> </p> </p> <p class="bottom">bottom</p> </p> Copy after login Copy after login html,body,.parent{ margin:0; height:100%; overflow:hidden; }body{ color: white; } .parent{ display: flex; flex-direction: column; }.top{ height:100px; background: blue; }.bottom{ height:50px; background: black; }.middle{ flex:1; display:flex; }.left{ width:200px; background: red; }.right{ flex: 1; overflow: auto; background:pink; }.right .inner{ min-height: 1000px; } Copy after login (3)优缺点
1)使用flex
(2)代码实例 <p class="parent"> <p class="top">top</p> <p class="middle"> <p class="left">left</p> <p class="right"> <p class="inner">right</p> </p> </p> <p class="bottom">bottom</p> </p> Copy after login Copy after login html,body,.parent{ margin:0; height:100%; overflow:hidden; }body{ color:white; } .parent{ display:flex; flex-direction:column; }.top{ background:blue; }.bottom{ background:black; }.middle{ flex:1; display:flex; }.left{ background: red; }.right{ flex:1; overflow:auto; background: pink; }.right .inner{ min-height:1000px; } Copy after login 全屏布局相关方案的兼容性、性能和自适应一览表
当然,最最最最最后,如果您喜欢这片文章,可以疯狂点赞!! The above is the detailed content of A collection of solutions for CSS page layout. For more information, please follow other related articles on the PHP Chinese website!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
|
---|