談到佈局,首先就要想到定位,當別人問我,css的position定位有哪些取值,分別表示什麼意思?呃.....抓頭.gif,是時候迴歸本質,看定義了。
position有六個屬性值:static、relative、absolute、fixed、sticky和inherit。css
三列布局,其中一列寬度自適應,在PC端最經常使用之一,搞定了三列布局,其餘同樣的套路。html
缺點:html結構不正確,當包含區域寬度小於左右框之和,右邊框會被擠下來css3
<style> .tree-columns-layout.float .left { float: left; width: 300px; background-color: #a00; } .tree-columns-layout.float .right { float: right; width: 300px; background-color: #0aa; } .tree-columns-layout.float .center { /* left: 300px; right: 300px; */ margin: 0 300px; background-color: #aa0; overflow: auto; } </style> <section class="tree-columns-layout float"> <article class="left"> <h1>我是浮動佈局左框</h1> </article> <article class="right"> <h1>我是浮動佈局右框</h1> </article> <article class="center"> <h1>我是浮動佈局中間框</h1> </article> </section>
缺點:要求父級要有非static定位,若是沒有,左右框容易偏移出去佈局
<style> .tree-columns-layout.position { position: relative; } .tree-columns-layout.position .left { position: absolute; left: 0; top: 0; width: 300px; background-color: #a00; } .tree-columns-layout.position .right { position: absolute; right: 0; top: 0; width: 300px; background-color: #0aa; } .tree-columns-layout.position .center { margin: 0 300px; background-color: #aa0; overflow: auto; } </style> <section class="tree-columns-layout position"> <article class="left"> <h1>我是浮動定位左框</h1> </article> <article class="center"> <h1>我是浮動定位中間框</h1> </article> <article class="right"> <h1>我是浮動定位右框</h1> </article> </section>
缺點:沒什麼缺點,恐懼tableflex
<style> .tree-columns-layout.table { display: table; width: 100%; } .tree-columns-layout.table > article { display: table-cell; } .tree-columns-layout.table .left { width: 300px; background-color: #a00; } .tree-columns-layout.table .center { background-color: #aa0; } .tree-columns-layout.table .right { width: 300px; background-color: #0aa; } </style> <section class="tree-columns-layout table"> <article class="left"> <h1>我是表格佈局左框</h1> </article> <article class="center"> <h1>我是表格佈局中間框</h1> </article> <article class="right"> <h1>我是表格佈局右框</h1> </article> </section>
缺點:兼容性spa
<style> .tree-columns-layout.flex { display: flex; } .tree-columns-layout.flex .left { width: 300px; flex-shrink: 0; /* 不縮小 */ background-color: #a00; } .tree-columns-layout.flex .center { flex-grow: 1; /* 增大 */ background-color: #aa0; } .tree-columns-layout.flex .right { flex-shrink: 0; /* 不縮小 */ width: 300px; background-color: #0aa; } </style> <section class="tree-columns-layout flex"> <article class="left"> <h1>我是flex彈性佈局左框</h1> </article> <article class="center"> <h1>我是flex彈性佈局中間框</h1> </article> <article class="right"> <h1>我是flex彈性佈局右框</h1> </article> </section>
缺點:兼容性 Firefox 52, Safari 10.1, Chrome 57, Opera 44code
<style> .tree-columns-layout.grid { display: grid; grid-template-columns: 300px 1fr 300px; } .tree-columns-layout.grid .left { background-color: #a00; } .tree-columns-layout.grid .center { background-color: #aa0; } .tree-columns-layout.grid .right { background-color: #0aa; } </style> <section class="tree-columns-layout grid"> <article class="left"> <h1>我是grid柵格佈局左框</h1> </article> <article class="center"> <h1>我是grid柵格佈局中間框</h1> </article> <article class="right"> <h1>我是grid柵格佈局右框</h1> </article> </section>
缺點:須要多加一層標籤,html順序不對,佔用了佈局框的margin屬性htm
<style> .tree-columns-layout.cup:after { clear: both; content: ""; display: table; } .tree-columns-layout.cup .center { width: 100%; float: left; } .tree-columns-layout.cup .center > div { margin: 0 300px; overflow: auto; background-color: #aa0; } .tree-columns-layout.cup .left { width: 300px; float: left; margin-left: -100%; background-color: #a00; } .tree-columns-layout.cup .right { width: 300px; float: left; margin-left: -300px; background-color: #0aa; } </style> <section class="tree-columns-layout cup"> <article class="center"> <div> <h1>我是聖盃佈局中間框</h1> </div> </article> <article class="left"> <h1>我是聖盃佈局左框</h1> </article> <article class="right"> <h1>我是聖盃佈局右框</h1> </article> </section>