码迷,mamicode.com
首页 > Web开发 > 详细

HTML与CSS布局技巧总结

时间:2016-04-16 19:18:08      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

    很多人对CSS的布局有困惑,实际的应用场景中由于布局种类多难以选择。今天我花些时间总结下自己对CSS布局的理解,分析下了解各种布局的优劣,同时希望能分享给初入前端的朋友们一些在布局上的经验,如果有那些地方总结的不好,欢迎大家指正。言归正传,现在就来揭开各种布局的面纱。

单列布局

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"parent"</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"child"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
</<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li></ul>

水平居中

       水平居中的布局方式是最常见的一种,常常用于头部、内容区、页脚,它主要的作用是控制盒子在整个页面以水平居中的方式呈现。

技术分享

使用margin:0 auto来实现

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.child</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">width</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"><span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">800</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">margin</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span> auto</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>

       优势:兼容性好 
       劣势:需要指定盒子 宽度


1.使用table来实现

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.child</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> table</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">margin</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span> auto</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>

       优势:不需要父容器parent,只需要对自身进行设置 
       劣势:IE6、7需要调整结构


2.使用inline-block和text-align来实现

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">text-align</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> center</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.child</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> inline-block</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li></ul>

       优势:兼容性好 
       劣势:需要同时设置子元素和父元素


3.使用绝对定位absolute来实现

       使用绝对定位来实现水平居中布局有两种情况,一种子容器无宽度,另一种子容器有宽度。无宽度可以用一下代码,如果是有宽度,则可以设置margin-left负值为容器宽度的一半。

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> relative</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.child</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> absolute</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">left</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>%</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">transform</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: border-box;">translateX(-<span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>%)</span></span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li></ul>

       优势:无需设置容器宽度,在移动端可以使用 
       劣势:兼容性差,需要IE9及以上浏览器的支持


4.使用flex布局来实现

       flex有两种方法来实现水平居中,父容器设置display:flex, 一种直接在父容器中设置justify-content属性值center。第二种在子容器中使用margin: 0 auto

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> flex</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">justify-content</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> center</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>
<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> flex</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.child</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">margin</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span> auto</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li></ul>

       优势:实现起来简单,尤其是使用在响应式布局中 
       劣势:兼容性差,如果大面积的使用该布局可能会影响效率


垂直居中

技术分享

这边说的垂直居中是子容器无高的垂直居中,并非单行文本垂直居中line-height

1.使用绝对定位absolute来实现(同水平居中的使用方法,优劣一样)

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> relative</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.child</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> absolute</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">top</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>%</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">transform</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: border-box;">translateY(-<span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>%)</span></span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li></ul>

2.使用flex来实现

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> flex</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">align-items</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> center</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>

3.使用display:table-cell来实现

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> table-cell</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">vertical-align</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> middle</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">height</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">400</span>px</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>

       总结:将水平居中和垂直居中两种布局方法相互的结合起来就可以实现水平居中布局。这边只举一个用绝对定位来实现水平垂直居中布局的方法,别的方法大家可以尝试自己练习。(以下介绍各种布局时都是基于上面水平和垂直居中的方法,所有对于它们的优劣就不再分析。)

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> relative</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.child</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> absolute</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">top</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>%</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">left</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>%</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">transform</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-function" style="margin: 0px; padding: 0px; box-sizing: border-box;">translate(-<span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>%,-<span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>%)</span></span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li></ul>

多列布局

多列布局也是非常常见的,适用于一侧定宽,另一侧自适应的布局。

技术分享

浮动布局

       前段时间我总结过关于两列浮动布局方法,这里我就不再从新总结了,如果有兴趣的朋友可以参考前端时间关于浮动布局的方法(总结)这篇博客。

多列等分布局

多列等分布局常常出现在内容中,多数为同功能、同阶级内容的并排显示。 
技术分享

HTML代码

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"parent"</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"column"</span>><span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">1</span></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"column"</span>><span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">2</span></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"column"</span>><span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">3</span></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"column"</span>><span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">4</span></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
</<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">4</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">5</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">6</li></ul>

1.使用flex来实现多列布局

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> flex</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.column</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">flex</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">1</span></span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.column</span>+ <span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.column</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">margin-left</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">20</span>px</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li></ul>

2.使用table来实现多列布局

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> table</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">table-layout</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> fixed</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">width</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">100</span>%</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.column</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> table-cell</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">padding-left</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">20</span>px</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li></ul>

3.使用float来实现多列布局

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.column</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">float</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> left</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">width</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">25</span>%</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">padding-left</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">20</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">box-sizing</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> border-box</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>

       提示:使用table和float实现多列布局的时候需要注意,如果要设置背景颜色则必须将.column盒子作为父容器在其里面添加一个子容器,在设置背景颜色,如果直接在.column容器中设置背景颜色会由于padding而无法产生盒子之间的间距。

九宫格布局

技术分享 
HTML代码

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"parent"</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"row"</span>>
        <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"item"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
        <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"item"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
        <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"item"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    </<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"row"</span>>
        <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"item"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
        <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"item"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
        <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"item"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    </<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"row"</span>>
        <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"item"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
        <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"item"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
        <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"item"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    </<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
</<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">4</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">5</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">6</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">7</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">8</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">9</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">10</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">11</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">12</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">13</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">14</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">15</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">16</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">17</li></ul>

1.使用flex来实现九宫格布局

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> flex</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">flex-direction</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> column</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">width</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">300</span>px</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.row</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">height</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">100</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> flex</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">border</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">1</span>px solid red</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.item</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">width</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">100</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">background-color</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-hexcolor" style="margin: 0px; padding: 0px; box-sizing: border-box;">#ccc</span></span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">border</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">1</span>px solid red</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li></ul>

2.使用table来实现九宫格布局

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> table</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">table-layout</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> fixed</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">width</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">100</span>%</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.row</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> table-row</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.item</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">display</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> table-cell</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">width</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">33.3</span>%</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">height</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">200</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">border</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">1</span>px solid red</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li></ul>

全屏布局

技术分享

HTML代码

<code class="hljs applescript has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"parent"</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"top"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"left"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"right"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
    <<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span> <span class="hljs-type" style="margin: 0px; padding: 0px; box-sizing: border-box;">class</span>=<span class="hljs-string" style="margin: 0px; padding: 0px; color: rgb(0, 136, 0); box-sizing: border-box;">"bottom"</span>></<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>>
</<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">div</span>></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">4</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">5</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">6</li></ul>

使用绝对定位实现全屏布局

<code class="hljs css has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); box-sizing: border-box;">html</span>,<span class="hljs-tag" style="margin: 0px; padding: 0px; color: rgb(0, 0, 0); box-sizing: border-box;">body</span>,<span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.parent</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">height</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">100</span>%</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">overflow</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> hidden</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
        <span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.top</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> absolute</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">top</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span></span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">left</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span></span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">right</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span></span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">height</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span></span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">background-color</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> black</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">height</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">100</span>px</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
        <span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.left</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> absolute</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">top</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">100</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">left</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span></span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">bottom</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">width</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">200</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">background-color</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> orange</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
        <span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.right</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> absolute</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">top</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">100</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">left</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">200</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">right</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span></span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">bottom</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">background-color</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> grey</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">overflow</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> hidden</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span>
        <span class="hljs-class" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(155, 112, 63);">.bottom</span><span class="hljs-rules" style="margin: 0px; padding: 0px; box-sizing: border-box;">{<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">position</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> absolute</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">left</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span></span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">right</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span></span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">bottom</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">0</span></span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">height</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> <span class="hljs-number" style="margin: 0px; padding: 0px; box-sizing: border-box;">50</span>px</span></span>; <span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;"><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">background-color</span>:<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 102, 102);"> pink</span></span>;<span class="hljs-rule" style="margin: 0px; padding: 0px; box-sizing: border-box;">}</span></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">4</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">5</li></ul>

响应式布局

meta标签的使用

<code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-tag" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102); box-sizing: border-box;"><<span class="hljs-title" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 0, 136);">meta</span> <span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 0, 102);">name</span>=<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 136, 0);">"viewport"</span> <span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(102, 0, 102);">content</span>=<span class="hljs-value" style="margin: 0px; padding: 0px; box-sizing: border-box; color: rgb(0, 136, 0);">"width=device-width, initial-scale=1"</span>/></span></code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li></ul>

使用媒体查询

<code class="hljs lasso has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">@media screen <span class="hljs-literal" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">and</span> (<span class="hljs-keyword" style="margin: 0px; padding: 0px; color: rgb(0, 0, 136); box-sizing: border-box;">max</span><span class="hljs-attribute" style="margin: 0px; padding: 0px; box-sizing: border-box;">-width</span>: <span class="hljs-number" style="margin: 0px; padding: 0px; color: rgb(0, 102, 102); box-sizing: border-box;">480</span>px){
         <span class="hljs-comment" style="margin: 0px; padding: 0px; color: rgb(136, 0, 0); box-sizing: border-box;">/*屏幕小于480px的样式*/</span>
}</code><ul class="pre-numbering" style="margin: 0px; padding: 6px 0px 40px; box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">1</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">2</li><li style="margin: 0px; padding: 0px 5px; box-sizing: border-box;">3</li></ul>

       总结:这些布局方法有些经常用到,有些由于兼容性的问题在具体项目相中使用的情况相对较少,不过对于移动端来说,以上总结的布局都是实用。今天特意花些时间来整理这些布局,一是为了巩固知识,二是希望这些方法能够分享给前端的初学者,让他们对布局有更深入的了解,当然这些并非是CSS中的所有布局方法,以后发现有什么布局没有总结到的,我会继续更新、分享,如果哪位同行对布局方法有所补充,或者发现博客中存在问题,欢迎相互交流。

HTML与CSS布局技巧总结

标签:

原文地址:http://blog.csdn.net/linzhijing_csdn/article/details/51161759

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!