本教程演示了CSS網格佈局中控制項目的放置和對齊方式。我們將探索訂購項目並單獨對齊,並作為一個組。 以前,我們介紹了網格佈局基礎知識,包括元素放置和網格自動置換算法。
>要進行最佳演示,請使用最新版本的Firefox(版本52或更高版本)或Chrome(版本57或更高版本),均提供本機網格佈局支持。
密鑰概念:
屬性指示網格中的項目序列,覆蓋默認的DOM順序。這對於眾多或動態添加的項目特別有用。
order
(單個項目)和justify-self
將網格中的所有項目對齊。 justify-items
justify-self
列軸對齊使用justify-items
(單個項目)和align-self
,align-items
和start
values。
end
>總體網格對齊由應用於網格容器的center
(行軸)和stretch
(列軸)控制。 這些接受justify-content
,align-content
,start
,end
,center
,stretch
和space-around
。
space-between
space-evenly
>
order
屬性指定項目位置序列。 默認情況下,項目遵循其DOM順序。 但是,
較低的order
值首先出現;值相等order
值的項目保持其DOM順序。
order
>示例:order
html:
> css(說明性片段):
<code class="language-html"><div class="container"> <div class="item a">A</div> <div class="item b">B</div> <div class="item c">C</div> <div class="item d">D</div> <div class="item e">E</div> <div class="item f">F</div> <div class="item g">G</div> <div class="item h">H</div> <div class="item i">I</div> <div class="item j">J</div> </div></code>
<code class="language-css">.c { grid-row-start: 1; grid-row-end: 2; } .e { grid-row-start: 1; grid-row-end: 3; } .b, .j { order: 2; } .a, .i { order: 3; }</code>>記住:使用
>更改項目順序可能會影響可訪問性。 屏幕讀取器和鍵盤導航可能無法反映視覺上的更改。
> row-axis對齊(order
,
justify-self
justify-items
justify-self
>對齊單個項目,而justify-items
將所有項目對齊沿行軸。兩者都使用start
,end
,center
和stretch
。
列軸對齊(,align-self
):align-items
>對齊單個項目,align-self
>使用align-items
,start
,end
,center
和stretch
。
總體網格對齊(justify-content
,align-content
):
justify-content
(行軸)和align-content
(列軸)將整個網格對齊其容器內。 除了start
,end
,center
和stretch
之外,他們還接受space-around
>,space-between
>。
space-evenly
CSS網格中的掌握項目排序和對齊方式提供精確的佈局控件。 請記住重新排序項目時可訪問性注意事項。 >
(為簡潔而刪除了常見問題部分。提供的常見問題已經在重寫的教程中覆蓋了。以上是如何在網格佈局中訂購和對齊項目的詳細內容。更多資訊請關注PHP中文網其他相關文章!