日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
新時代布局中一些有意思的特性!

在最新的 Chrome Canary 中,一個有意思的 CSS 語法 Container Queries 得到了支持。

Chrome Canary[1]:開發(fā)者專用的每日構(gòu)建版,站上網(wǎng)絡(luò)科技最前沿。當然,不一定穩(wěn)定~

而在最近幾個 Chrome 版本中,有一些挺有意思的屬性相繼得到支持,本文就將介紹一下,在今天,新時代布局中,我們能逐漸去使用的一些有意思的新特性,通過本文,你將能了解到:

  • flex 布局中的 gap 屬性
  • 控制容器寬高比屬性 aspect-ratio
  • firefox 下的 CSS Grid 瀑布流布局(grid-template-rows: masonry)
  • CSS 容器查詢(Container Queries)

flex 布局中的 gap 屬性

gap 并非是新的屬性,它一直存在于多欄布局 multi-column 與 grid 布局中,其中:

  • column-gap 屬性用來設(shè)置多欄布局 multi-column 中元素列之間的間隔大小
  • grid 布局中 gap 屬性是用來設(shè)置網(wǎng)格行與列之間的間隙,該屬性是 row-gap 和 column-gap 的簡寫形式,并且起初是叫 grid-gap

譬如我們有如下一個 grid 布局:

 
 
 
 
  1.  
  2.     1
 
  •     2
  •  
  •     3
  •  
  •     4
  •  
  •     5
  •  
  •  
     
     
     
     
    1. .grid-container { 
    2.     display: grid; 
    3.     border: 5px solid; 
    4.     padding: 20px; 
    5.     grid-template-columns: 1fr 1fr 1fr; 
    6. .item { 
    7.     width: 100px; 
    8.     height: 100px; 
    9.     background: deeppink; 
    10.     border: 2px solid #333; 

    效果如下:

    grid 布局

    通過給 grid-container 添加 gap 屬性,可以非常方便的設(shè)置網(wǎng)格行與列之間的間隙:

     
     
     
     
    1. .grid-container { 
    2.     display: grid; 
    3.     border: 5px solid; 
    4.     padding: 20px; 
    5.     grid-template-columns: 1fr 1fr 1fr; 
    6. +   gap: 5px; 

    而從 Chromium 84 開始,我們可以開始在 flex 布局中使用 gap 屬性了!Can i use -- gap property for Flexbox[2]

    Can i use -- gap property for Flexbox

    它的作用與在 grid 布局中的類似,可以控制水平和豎直方向上 flex item 之間的間距:

     
     
     
     
    1. .flex-container { 
    2.     width: 500px; 
    3.     display: flex; 
    4.     flex-wrap: wrap; 
    5.     gap: 5px; 
    6.     justify-content: center; 
    7.     border: 2px solid #333; 
    8.  
    9. .item { 
    10.     width: 80px; 
    11.     height: 100px; 
    12.     background: deeppink; 

    gap 屬性的優(yōu)勢在于,它避免了傳統(tǒng)的使用 margin 的時候需要考慮第一個或者最后一個元素的左邊距或者右邊距的煩惱。正常而言,4 個水平的 flex item,它們就應(yīng)該只有 3 個間隙。gap 只生效于兩個 flex item 之間。

    控制容器寬高比屬性 aspect-ratio

    保持元素容器一致的寬高比(稱為長寬比)對于響應(yīng)式 Web 設(shè)計和在某些布局當中至關(guān)重要?,F(xiàn)在,通過 Chromium 88 和 Firefox 87,我們有了一種更直接的方法來控制元素的寬高比 -- aspect-ratio。Can i use -- aspect-ratio[3]

    Can i use -- aspect-ratio

    首先,我們只需要設(shè)定元素的寬,或者元素的高,再通過 aspect-ratio 屬性,即可以控制元素的整體寬高:

     
     
     
     
     
  •  
     
     
     
     
    1. div { 
    2.     background: deeppink; 
    3.     aspect-ratio: 1/1; 
    4. .width { 
    5.     width: 100px; 
    6. .height { 
    7.     height: 100px; 

    都可以得到如下圖形:

    其次,設(shè)定了 aspect-ratio 的元素,元素的高寬其中一個發(fā)生變化,另外一個會跟隨變化:

     
     
     
     
    1.  
    2.     
      寬高比1:1
       
    3.     
      寬高比2:1
       
    4.     
      寬高比3:1
       
     
     
     
     
     
    1. .container { 
    2.     display: flex; 
    3.     width: 30vw; 
    4.     padding: 20px; 
    5.     gap: 20px; 
    6. .container > div { 
    7.     flex-grow: 1; 
    8.     background: deeppink; 
    9. .container > div:nth-child(1) { 
    10.     aspect-ratio: 1/1; 
    11. .container > div:nth-child(2) { 
    12.     aspect-ratio: 2/1; 
    13. .container > div:nth-child(3) { 
    14.     aspect-ratio: 3/1; 

    當容器大小變化,每個子元素的寬度變寬,元素的高度也隨著設(shè)定的 aspect-ratio 比例跟隨變化:

    CodePen Demo -- aspect-ratio Demo[4]

    firefox 下的 CSS Grid 瀑布流布局(grid-template-rows: masonry)

    grid-template-rows: masonry 是 firefox 在 firefox 87 開始支持的一種基于 grid 布局快速創(chuàng)建瀑布流布局的方式。并且 firefox 一直在推動該屬性進入標準當中。

    從 firefox 87 開始,在瀏覽器輸入網(wǎng)址欄輸入 about:config 并且開啟 layout.css.grid-template-masonry-value.enabled 配置使用。Can i use -- grid-template-rows: masonry[5]

    正常而言,我們想要實現(xiàn)瀑布流布局還是需要花費一定的功夫的,即便是基于 grid 布局。在之前,我們通過 grid 布局,通過精細化控制每一個 grid item,也可以實現(xiàn)一些偽瀑布流布局:

     
     
     
     
    1.  
    2.   1
     
  •   2
  •  
  •   3
  •  
  •   
  •   
  •   
  •   
  •   
  •  
  •  
     
     
     
    1. .g-container { 
    2.     height: 100vh; 
    3.     display: grid; 
    4.     grid-template-columns: repeat(4, 1fr); 
    5.     grid-template-rows: repeat(8, 1fr); 
    6.  
    7. .g-item { 
    8.     &:nth-child(1) { 
    9.         grid-column: 1; 
    10.         grid-row: 1 / 3; 
    11.     } 
    12.     &:nth-child(2) { 
    13.         grid-column: 2; 
    14.         grid-row: 1 / 4; 
    15.     } 
    16.     &:nth-child(3) { 
    17.         grid-column: 3; 
    18.         grid-row: 1 / 5; 
    19.     } 
    20.     &:nth-child(4) { 
    21.         grid-column: 4; 
    22.         grid-row: 1 / 6; 
    23.     } 
    24.     &:nth-child(5) { 
    25.         grid-column: 1; 
    26.         grid-row: 3 / 9; 
    27.     } 
    28.     &:nth-child(6) { 
    29.         grid-column: 2; 
    30.         grid-row: 4 / 9; 
    31.     } 
    32.     &:nth-child(7) { 
    33.         grid-column: 3; 
    34.         grid-row: 5 / 9; 
    35.     } 
    36.     &:nth-child(8) { 
    37.         grid-column: 4; 
    38.         grid-row: 6 / 9; 
    39.     } 

    效果如下:

    CSS Grid 實現(xiàn)偽瀑布流布局

    CodePen Demo -- CSS Grid 實現(xiàn)偽瀑布流布局[6]

    在上述 Demo 中,使用 grid-template-columns、grid-template-rows 分割行列,使用 grid-row 控制每個 grid item 的所占格子的大小,但是這樣做的成本太高了,元素一多,計算量也非常大,并且還是在我們提前知道每個元素的高寬的前提下。

    而在有了 grid-template-rows: masonry 之后,一切都會變得簡單許多,對于一個不確定每個元素高度的 4 列的 grid 布局:

     
     
     
     
    1. .container { 
    2.   display: grid; 
    3.   grid-template-columns: repeat(4, 1fr); 

    正常而言,看到的會是這樣:

    簡單的給容器加上 grid-template-rows: masonry,表示豎方向上,采用瀑布流布局:

     
     
     
     
    1. .container { 
    2.   display: grid; 
    3.   grid-template-columns: repeat(4, 1fr); 
    4. + grid-template-rows: masonry; 

    便可以輕松的得到這樣一種瀑布流布局:

    如果你在使用 firefox,并且開啟了 layout.css.grid-template-masonry-value.enabled 配置,可以戳進下面的 DEMO 感受一下:

    CodePen Demo -- grid-template-rows: masonry 實現(xiàn)瀑布流布局[7]

    當然,這是一個最簡單的 DEMO,關(guān)于更多 grid-template-rows: masonry 相關(guān)知識,你可以詳細的看看這篇文章:Native CSS Masonry Layout In CSS Grid[8]

    CSS 容器查詢(Container Queries)

    什么是 CSS 容器查詢[9](Container Queries)?

    在之前,對于同個樣式,我們?nèi)绻M鶕?jù)視口大小得到不一樣效果,通常使用的是媒體查詢。

    但是,一些容器或者組件的設(shè)計可能并不總是與視口的大小有關(guān),而是與組件在布局中的放置位置有關(guān)。

    所以在未來,新增了一種方式可以對不同狀態(tài)下的容器樣式進行控制,也就是容器查詢。在最新的 Chrome Canary[10] 中,我們可以通過 chrome://flags/#enable-container-queries 開啟 Container Queries 功能。

    假設(shè)我們有如下結(jié)構(gòu):

     
     
     
     
    1.  
    2.      
    3.         Title 
    4.         

      Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus vel eligendi, esse illum similique sint!!

       
    5.      
    6.  

    正常情況下的樣式如下:

     
     
     
     
    1. .g-container { 
    2.     display: flex; 
    3.     flex-wrap: nowrap; 
    4.     border: 2px solid #ddd; 
    5.  
    6.     .child { 
    7.         flex-shrink: 0; 
    8.         width: 200px; 
    9.         height: 100px; 
    10.         background: deeppink; 
    11.     } 
    12.      
    13.     p { 
    14.         height: 100px; 
    15.         font-size: 16px; 
    16.     } 

    結(jié)構(gòu)如下:

    在未來,我們可以通過 @container query 語法,設(shè)定父容器 .wrap 在不同寬度下的不同表現(xiàn),在上述代碼基礎(chǔ)上,新增下述代碼:

     
     
     
     
    1. .wrap { 
    2.     contain: layout inline-size; 
    3.     resize: horizontal; 
    4.     overflow: auto; 
    5. .g-container { 
    6.     display: flex; 
    7.     flex-wrap: nowrap; 
    8.     border: 2px solid #ddd; 
    9.     .child { 
    10.         flex-shrink: 0; 
    11.         width: 200px; 
    12.         height: 100px; 
    13.         background: deeppink; 
    14.     } 
    15.     p { 
    16.         height: 100px; 
    17.         font-size: 16px; 
    18.     } 
    19. // 當 .wrap 寬度小于等于 400px 時下述代碼生效  
    20. @container (max-width: 400px) { 
    21.     .g-container { 
    22.         flex-wrap: wrap; 
    23.         flex-direction: column; 
    24.     } 
    25.     .g-container .child { 
    26.         width: 100%; 
    27.     } 

    注意這里要開啟 @container query,需要配合容器的 contain 屬性,這里設(shè)置了 contain: layout inline-size,當 .wrap 寬度小于等于 400px 時,@container (max-width: 400px) 內(nèi)的代碼則生效,從橫向布局 flex-wrap: nowrap 變換成了縱向換行布局 flex-wrap: wrap:

    如果你的瀏覽器已經(jīng)開啟了 chrome://flags/#enable-container-queries,你可以戳這個代碼感受一下:

    CodePen Demo -- CSS @container query Demo[11]

    媒體查詢與容器查詢的異同,通過一張簡單的圖看看,核心的點在于容器的寬度發(fā)生變化時,視口的寬度不一定會發(fā)生變化:

    這里僅僅是介紹了 @container query 的冰山一角,更多內(nèi)容你可以戳這里了解更多:say-hello-to-css-container-queries[12]

    最后

    好了,本文到此結(jié)束,希望對你有幫助 ????

    更多精彩 CSS 技術(shù)文章匯總在我的 Github -- iCSS[13] .

    參考資料


    名稱欄目:新時代布局中一些有意思的特性!
    標題鏈接:http://www.dlmjj.cn/article/cdjchdg.html

    其他資訊