成人欧美一区二区三区的电影,日韩一级一欧美一级国产,国产成人国拍亚洲精品,无码人妻精品一区二区三区毛片,伊人久久无码大香线蕉综合

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

從 “布局卡殼” 到 “秒出結(jié)構(gòu)”:Grid 布局常見案例拆解,新手也能抄作業(yè)(附完整代碼)

admin
2025年9月22日 15:28 本文熱度 1034

在上一篇《CSS 布局大比拼:浮動、Flex、Grid 實(shí)戰(zhàn)對比,看完就知道該用哪個!?》文章中我們主要是對常見的布局方式做了個對比,我們今天主要是通過常見的布局場景來看看Grid怎么用。


基礎(chǔ)網(wǎng)格布局

      我們先來看看一個簡單的3x3網(wǎng)格,使用 fr 單位和 gap,第二列的寬度是另外兩列的兩倍(2fr vs 1fr)。中間的行高度會隨著內(nèi)容變化,而首尾行高度固定。每個項(xiàng)目(item)之間都有20px的間隙。


<style>        .container {            display: grid;            /* 定義3列,寬度比為 1:2:1 */            grid-template-columns1fr 2fr 1fr;            /* 定義3行,第一和第三行高度為80px,中間行自動填充 */            grid-template-rows80px auto 80px;            /* 設(shè)置行間距和列間距均為20px */            gap20px;            height100vh/* 讓容器占滿整個視口高度 */            padding20px;            box-sizing: border-box;            background-color#f0f0f0;        }
        .item {            background-color#3498db;            color: white;            border-radius5px;            display: flex;            justify-content: center;            align-items: center;            font-size1.5rem;            font-weight: bold;        }
        /* 為不同的項(xiàng)目添加不同的顏色,便于區(qū)分 */        .item:nth-child(even) {            background-color#2ecc71;        }    </style>        <div class="container">        <div class="item">1</div>        <div class="item">2</div>        <div class="item">3</div>        <div class="item">4</div>        <div class="item">5</div>        <div class="item">6</div>        <div class="item">7</div>        <div class="item">8</div>        <div class="item">9</div>    </div>



經(jīng)典的圣杯布局

      使用Grid輕松實(shí)現(xiàn)Header, Footer, Sidebar, Main Content區(qū)域,并添加簡單的響應(yīng)式。在桌面端,會看到經(jīng)典的Header、左右布局、Footer結(jié)構(gòu)。調(diào)整瀏覽器窗口寬度小于768px時(shí),布局會自動變?yōu)閱瘟卸询B,Sidebar移動到Main Content上方,非常適合移動設(shè)備瀏覽。

PC端

?

移動端

<style>        body {            margin0;            padding0;        }
        .container {            display: grid;            /* 定義網(wǎng)格區(qū)域:第一行是header,最后一行是footer,中間一行左邊是sidebar,右邊是main */            grid-template-areas:                "header header"                "sidebar main"                "footer footer";            /* 定義列:側(cè)邊欄200px,主內(nèi)容區(qū)自動填充剩余空間 */            grid-template-columns200px 1fr;            /* 定義行:Header和Footer高度固定,中間部分自動填充剩余空間 */            grid-template-rows80px 1fr 80px;            height100vh;            gap10px;            padding10px;            box-sizing: border-box;        }        /*grid-area是項(xiàng)目屬性,指定項(xiàng)目放在哪一個區(qū)域。一般搭配grid-template-area使用 */        .header {            grid-area: header;            background-color#ff7979;        }
        .sidebar {            grid-area: sidebar;            background-color#7ed6df;        }
        .main {            grid-area: main;            background-color#f6e58d;        }
        .footer {            grid-area: footer;            background-color#badc58;        }
        /* 為所有區(qū)域添加統(tǒng)一樣式 */        .header.sidebar.main.footer {            padding15px;            border-radius5px;            color#333;            font-weight: bold;        }
        /* 響應(yīng)式設(shè)計(jì):當(dāng)屏幕寬度小于768px時(shí) */        @media (max-width768px) {            .container {                /* 改變網(wǎng)格模板:單列布局 */                grid-template-areas:                    "header"                    "sidebar"                    "main"                    "footer";                /* 將列定義改為單列 */                grid-template-columns1fr;                /* 行高可以自動調(diào)整 */                grid-template-rows80px auto auto 80px;            }        }    </style>    <div class="container">        <header class="header">Header</header>        <aside class="sidebar">Sidebar</aside>        <main class="main">Main Content</main>        <footer class="footer">Footer</footer>    </div>


照片墻/瀑布流布局

      使用 grid-auto-rows 和 span 關(guān)鍵字創(chuàng)建高度不一的卡片布局。一個不均勻的網(wǎng)格布局,有些卡片較高,有些較矮,形成了類似瀑布流的效果。用grid-auto-rows 設(shè)置了基礎(chǔ)行高,而 grid-row: span X 讓某些項(xiàng)目跨越多個行軌道。repeat(auto-fit, minmax(200px, 1fr)) 確保了布局是響應(yīng)式的。


<style>        .container {            display: grid;            /* 定義自適應(yīng)的列:最小200px,最大1fr。auto-fit會自動填充容器 */            grid-template-columnsrepeat(auto-fit, minmax(200px1fr));            /* 設(shè)置隱式行的高度為固定的150px */            grid-auto-rows150px;            gap15px;            padding20px;        }
        .item {            background-color#9b59b6;            border-radius8px;            display: flex;            justify-content: center;            align-items: center;            color: white;            font-weight: bold;            font-size1.2rem;        }
        /* 通過為不同項(xiàng)目設(shè)置不同的行高跨度來模擬瀑布流 */        /* 實(shí)際項(xiàng)目中,這個高度可能由圖片的實(shí)際高度決定,這里用類名模擬 */        .tall {            grid-row: span 2/* 這個項(xiàng)目占據(jù)2行的高度 */            background-color#e74c3c;        }
        .very-tall {            grid-row: span 3/* 這個項(xiàng)目占據(jù)3行的高度 */            background-color#34495e;        }
        /* 隨機(jī)顏色,僅用于演示 */        .item:nth-child(3n) {            background-color#16a085;        }        .item:nth-child(5n) {            background-color#f39c12;        }    </style>    <div class="container">        <div class="item">1</div>        <div class="item tall">2 (Tall)</div>        <div class="item">3</div>        <div class="item very-tall">4 (Very Tall)</div>        <div class="item">5</div>        <div class="item tall">6 (Tall)</div>        <div class="item">7</div>        <div class="item">8</div>        <div class="item tall">9 (Tall)</div>        <div class="item">10</div>        <div class="item very-tall">11 (Very Tall)</div>        <div class="item">12</div>    </div>   


電商產(chǎn)品卡片

      結(jié)合Flexbox和Grid,并使用 auto-fit 和 minmax() 實(shí)現(xiàn)完全響應(yīng)式,典型的電商產(chǎn)品列表,Grid布局 (products-grid) 負(fù)責(zé)外部的響應(yīng)式網(wǎng)格,會根據(jù)容器寬度自動調(diào)整每行顯示的產(chǎn)品數(shù)量(至少250px寬)。每個產(chǎn)品卡片的內(nèi)部則使用Flexbox進(jìn)行精細(xì)的排版,確保按鈕始終位于卡片底部。無需媒體查詢即可實(shí)現(xiàn)響應(yīng)式。


<style>        body {            background-color#ecf0f1;            padding20px;            font-family: ;        }
        .products-grid {            display: grid;            /* 魔法所在:自動適應(yīng)列數(shù),每列最小250px,最大1份額 */            grid-template-columnsrepeat(auto-fit, minmax(250px1fr));            gap25px;            max-width1200px;            margin0 auto;        }
        .product-card {            /* 卡片內(nèi)部使用Flexbox進(jìn)行垂直布局 */            display: flex;            flex-direction: column;            background-color: white;            border-radius10px;            overflow: hidden;            box-shadow0 4px 8px rgba(0000.1);            transition: transform 0.3s ease;        }
        .product-card:hover {            transformtranslateY(-5px);        }
        .product-image {            height180px;            background-color#ddd;            /* 模擬圖片 */            display: flex;            align-items: center;            justify-content: center;            color#777;            font-size0.9rem;        }
        .product-info {            padding20px;            /* 讓按鈕保持在卡片底部 */            display: flex;            flex-direction: column;            flex-grow1;        }
        .product-title {            margin-top0;            color#2c3e50;        }
        .product-price {            color#e74c3c;            font-size1.2rem;            font-weight: bold;            margin10px 0;        }
        .product-description {            color#7f8c8d;            font-size0.9rem;            flex-grow1/* 描述文字占據(jù)剩余空間,將按鈕推到底部 */        }
        .add-to-cart-btn {            background-color#3498db;            color: white;            border: none;            padding10px 15px;            border-radius5px;            cursor: pointer;            margin-top15px;            font-weight: bold;            transition: background-color 0.2s ease;        }
        .add-to-cart-btn:hover {            background-color#2980b9;        }    </style>    <div class="products-grid">        <!-- 卡片1 -->        <div class="product-card">            <div class="product-image">產(chǎn)品圖片</div>            <div class="product-info">                <h3 class="product-title">優(yōu)質(zhì)產(chǎn)品名稱</h3>                <p class="product-price">¥199.00</p>                <p class="product-description">這是一段關(guān)于這個產(chǎn)品的精彩描述,說明它的特點(diǎn)和優(yōu)點(diǎn)。</p>                <button class="add-to-cart-btn">加入購物車</button>            </div>        </div>
        <!-- 復(fù)制更多卡片以查看網(wǎng)格效果 -->        <div class="product-card">            <div class="product-image">產(chǎn)品圖片</div>            <div class="product-info">                <h3 class="product-title">另一個很棒的產(chǎn)品</h3>                <p class="product-price">¥259.00</p>                <p class="product-description">這是另一個產(chǎn)品的描述,可能更長一些,以展示卡片高度的靈活性。</p>                <button class="add-to-cart-btn">加入購物車</button>            </div>        </div>
        <div class="product-card">            <div class="product-image">產(chǎn)品圖片</div>            <div class="product-info">                <h3 class="product-title">超級產(chǎn)品三號</h3>                <p class="product-price">¥99.00</p>                <p class="product-description">簡潔的描述。</p>                <button class="add-to-cart-btn">加入購物車</button>            </div>        </div>
        <div class="product-card">            <div class="product-image">產(chǎn)品圖片</div>            <div class="product-info">                <h3 class="product-title">旗艦產(chǎn)品</h3>                <p class="product-price">¥599.00</p>                <p class="product-description">這是我們最高端的產(chǎn)品,擁有所有你能想到的功能和卓越的性能表現(xiàn)。</p>                <button class="add-to-cart-btn">加入購物車</button>            </div>        </div>    </div>


雜志式不規(guī)則布局

      使用基于線的定位,讓網(wǎng)格項(xiàng)跨越多個行列,創(chuàng)建視覺沖擊力強(qiáng)的布局,這個布局打破了整齊的網(wǎng)格,創(chuàng)建了一個視覺重心。最大的「特色文章」區(qū)塊橫跨4列2行,立即吸引用戶的注意力。其他文章區(qū)塊大小不一,交錯排列,形成了動態(tài)且有趣的雜志或新聞網(wǎng)站首頁布局。關(guān)鍵在于使用 grid-column 和 grid-row 并指定起始和結(jié)束的網(wǎng)格線來精確控制每個項(xiàng)目的位置和大小。

<style>        .magazine-layout {            display: grid;            /* 定義一個6列的靈活網(wǎng)格 */            grid-template-columnsrepeat(61fr);            /* 定義4行,高度自動 */            grid-auto-rowsminmax(150px, auto);            gap15px;            max-width1200px;            margin0 auto;            padding20px;        }
        .featured {            /* 占據(jù)從第1條線到第5條線(即前4列) */            grid-column1 / 5;            /* 占據(jù)從第1條線到第3條線(即前2行) */            grid-row1 / 3;            background-color#e74c3c;        }
        .secondary {            /* 占據(jù)第5列到最后一列(第7條線) */            grid-column5 / 7;            background-color#3498db;        }
        .tertiary {            /* 占據(jù)3列(可根據(jù)需要調(diào)整起始位置) */            grid-column: span 2;            background-color#2ecc71;        }
        .tall {            /* 這個項(xiàng)目占據(jù)2行 */            grid-row: span 2;            background-color#f39c12;        }
        /* 通用項(xiàng)目樣式 */        .magazine-layout > div {            padding20px;            border-radius8px;            color: white;            font-weight: bold;            font-size1.2rem;            display: flex;            align-items: center;            justify-content: center;        }    </style>    <div class="magazine-layout">        <div class="featured">特色文章 (占據(jù)4x2)</div>        <div class="secondary">次要文章 (占據(jù)2x1)</div>        <div class="tertiary tall">文章 3 (占據(jù)2x2)</div>        <div class="tertiary">文章 4 (占據(jù)2x1)</div>        <div class="tertiary">文章 5 (占據(jù)2x1)</div>        <div class="tertiary tall">文章 6 (占據(jù)2x2)</div>        <div class="tertiary">文章 7 (占據(jù)2x1)</div>        <div class="tertiary">文章 8 (占據(jù)2x1)</div>    </div>


使用小貼士

  • 瀏覽器前綴:IE11 需要 -ms-grid 與 -ms-grid-column/row,可 autoprefixer 自動補(bǔ)。
  • 調(diào)試:Chrome DevTools → Elements → Layout → Grid  Overlay,能直觀看到軌道。
  • 降級:老舊瀏覽器用 @supports not (display:grid){} 回退到 Flex 或浮動。


閱讀原文:https://mp.weixin.qq.com/s/Trp31UUbZzMCdb76TSkzFw


該文章在 2025/9/22 15:31:28 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved