css flexbox模型_如何將Flexbox后備添加到CSS網格

css flexbox模型

I shared how to build a calendar with CSS Grid in the previous article. Today, I want to share how to build a Flexbox fallback for the same calendar.

在上一篇文章中,我分享了如何使用CSS Grid構建日歷。 今天,我想分享如何為同一日歷構建Flexbox后備廣告。

如何提供支持 (How to provide support)

Generally, there are three ways to provide support when it comes to CSS.

通常,涉及CSS時,可以通過三種方式提供支持。

First method: Write fallback code. Overwrite fallback code.

第一種方法:編寫后備代碼。 覆蓋后備代碼。

.selector {property: fallback-value;property: actual-value;
}

Second method: Write fallback code. Overwrite fallback code in CSS Feature Queries (@supports). Reset properties inside @supports if you need.

第二種方法:編寫后備代碼。 覆蓋CSS功能查詢( @supports )中的后備代碼。 如果需要,請在@supports內重置屬性。

.selector {property: fallback-value;
}@supports (display: grid) {property: actual-value;
}

Third method: Write everything in @supports.

第三種方法:@supports編寫所有@supports

@supports not (display: grid) {.selector {property: fallback-value;}
}@supports (display: grid) {.selector {property: actual-value;}
}

These three methods are listed in order of decreasing-complexity. (If you need to overwrite code, it's more complicated). This means writing everything in @supports is the simplest of the three.

這三種方法按復雜度遞減的順序列出。 (如果您需要覆蓋代碼,則更為復雜)。 這意味著用@supports編寫所有@supports是這三個中最簡單的。

How you choose to support your project depends on browser support for:

選擇支持項目的方式取決于瀏覽器對以下方面的支持:

  1. The feature

    功能
  2. The fallback feature

    后備功能
  3. Support for Feature Queries

    支持功能查詢

檢查支持 (Checking for support)

The best place to check for support is caniuse. Here, I see that support for CSS Grid is decent. Browsers I have to worry about are:

尋求支持的最佳地點是caniuse 。 在這里,我看到對CSS Grid的支持是不錯的。 我要擔心的瀏覽器是:

  1. Opera Mini: 1.42% global usage

    Opera Mini:1.42%的全球使用量
  2. Android Browsers 2.1 to 4.4.4: 0.67% global usage

    Android瀏覽器2.1至4.4.4:全球使用率為0.67%
  3. Blackberry browser: 0.02% global usage (Not gonna worry about this one).

    黑莓瀏覽器:0.02%的全球使用率(不用擔心這一點)。

Support for the fallback (Flexbox) is also good.

對后備(Flexbox)的支持也很好。

But we have a problem: Flexbox fallback wouldn't work for Android 2.1 to 4.3 (it doesn't support wrapping). Global usage for Android 2.1 to 4.3 is 0.37%.

但是我們有一個問題:Flexbox后備廣告在Android 2.1到4.3上不起作用(它不支持換行)。 Android 2.1至4.3的全球使用率為0.37%。

Here, I have to decide:

在這里,我必須決定:

  1. Is providing Flexbox fallback for Opera Mini (1.42%), Android 4.4.4 (0.3%), and Blackberry (0.02%) worth the effort?

    為Opera Mini(1.42%),Android 4.4.4(0.3%)和Blackberry(0.02%)提供Flexbox后備功能值得嗎?
  2. Should I change fallback from Flexbox to an older feature to support Android 2.1 to 4.3 (another 0.37%)?

    我是否應該將Flexbox的后備功能更改為較舊的功能,以支持Android 2.1到4.3(另一個0.37%)?

Let's assume, for this project, I decide that Flexbox fallback is sufficient. I'm not going to worry about Android 2.1 to 4.3.

讓我們假設,對于這個項目,我認為Flexbox后備就足夠了。 我不會擔心Android 2.1至4.3。

Next, I want to check whether browsers support CSS Feature Queries.

接下來,我要檢查瀏覽器是否支持CSS功能查詢。

Here, I see:

在這里,我看到:

  1. Opera Mini supports Feature Queries

    Opera Mini支持功能查詢
  2. Android 4.4.4 supports Feature Queries

    Android 4.4.4支持功能查詢
  3. Blackberry browser doesn't support Feature Queries

    Blackberry瀏覽器不支持功能查詢
  4. IE 11 does't support Feature Queries

    IE 11不支持功能查詢

確定如何編寫后備代碼 (Deciding how to write fallback code)

Earlier, I mentioned there are three ways to write fallback code for CSS:

之前,我提到了為CSS編寫后備代碼的三種方法:

  1. Write fallback code. Overwrite fallback code.

    編寫后備代碼。 覆蓋后備代碼。
  2. Write fallback code. Overwrite fallback code in @supports.

    編寫后備代碼。 覆蓋@supports后備代碼。

  3. Write everything in @supports.

    將所有內容寫在@supports

If I write everything inside @supports, I can provide support for:

如果我在@supports內編寫所有@supports ,則可以提供以下支持:

  1. Opera Mini (1.43%)

    Opera Mini(1.43%)
  2. Android 4.4.4 (0.3%)

    Android 4.4.4(0.3%)

But I lose support for:

但是我對以下方面失去支持:

  1. IE 11 (2.3%)

    IE 11(2.3%)
  2. Blackberry (0.02%)

    黑莓(0.02%)

I do not want to forsake the 2.3% of IE users, which means Method 3 (write everything in @supports) is out.

我不想放棄2.3%的IE用戶,這意味著方法3(將所有內容寫在@supports )已經淘汰了。

If I use Method 2 (Write fallback code. Overwrite fallback code in @supports), I can provide support for:

如果我使用方法2(寫回退代碼。覆蓋@supports回退代碼),則可以提供以下支持:

  1. IE 11 (2.3%)

    IE 11(2.3%)
  2. Opera Mini (1.43%)

    Opera Mini(1.43%)
  3. Android 4.4.4 (0.3%)

    Android 4.4.4(0.3%)
  4. Blackberry browser (0.02%)

    黑莓瀏覽器(0.02%)

That's everything I need. That's why I'm gonna go with Method 2.

這就是我所需要的。 這就是為什么我要使用方法2。

Note: If you want to code along, you can use demo from my previous article as the starting point.

注意:如果要一起編碼,則可以使用上一篇文章中的演示作為起點。

禁用網格代碼 (Disabling Grid code)

First, we park the CSS Grid code under @supports (like we discussed above).

首先,我們將CSS Grid代碼停放在@supports下(就像我們上面討論的那樣)。

@supports (display: grid) {.day-of-week,.date-grid {display: grid;grid-template-columns: repeat(7, 1fr);}.date-grid button:first-child {grid-column: 6;}
}

We can disable the CSS Grid code by setting display to an invalid value (not grid). This disables the entire block of code.

我們可以通過將display設置為無效值(不是grid )來禁用CSS Grid代碼。 這將禁用整個代碼塊。

(Thank Rachel Andrew for this neat trick. I believe I learned it from her ?).

(感謝Rachel Andrew的巧妙技巧。我相信我從她那里學到了嗎?)。

@supports (display: gridx) {/*...*/
}
Initial layout.

編寫Flexbox代碼 (Writing Flexbox code)

We need to build the same seven-column grid with Flexbox. The first thing we need to do is acknowledge that Flexbox and Grid work differently. We won't be able to get a perfect replica, but we can get close.

我們需要使用Flexbox構建相同的七列網格。 我們需要做的第一件事是確認Flexbox和Grid的工作方式不同。 我們將無法獲得完美的復制品,但我們可以接近。

The first thing is set display to flex.

首先將display設置為flex

.day-of-week,
.date-grid {display: flex;
}
Results after setting display to flex.

We need the buttons in .date-grid to wrap, so we set flex-wrap to wrap.

我們需要包裝.date-grid的按鈕,所以我們將flex-wrap設置為wrap

.date-grid {flex-wrap: wrap;
}
Buttons in date grid wrapped at the edges.

We need to replicate the seven-column grid. An easy way to do this is calculate the width of the grid according to the width of each button. Here, I have already set each button to 4.5ch. This means the width of the grid should be 7 x 4.5ch.

我們需要復制七列網格。 一種簡單的方法是根據每個按鈕的寬度計算網格的寬度。 在這里,我已經將每個按鈕設置為4.5ch。 這意味著網格的寬度應為7 x 4.5ch

(We can use CSS Calc to do the math for us).

(我們可以使用CSS Calc為我們做數學運算)。

.day-of-week,
.date-grid {max-width: calc(4.5ch * 7);
}
Wrapping at 7 columns

We need the elements in .day-of-week to spread out across the available width. One simple way is to set justify-content to space-between.

我們需要.day-of-week的元素分布在可用寬度上。 一種簡單的方法是將justify-content設置justify-content space-between

.day-of-week {justify-content: space-between;
}
After setting space-between.

Here, we can see that elements in .day-of-week extend past the grid. This extension happens because we let Flexbox calculate flex-basis for us. If we want every element in .day-of-week to be have the same width, we need to set flex-basis ourselves.

在這里,我們可以看到.day-of-week中的元素延伸到網格之外。 之所以發生這種擴展,是因為我們讓Flexbox為我們計算了flex-basis 。 如果我們希望.day-of-week每個元素都具有相同的寬度,則需要自己設置flex-basis

In this case, the easiest way is to set flex-basis to the width of one grid item (or 4.5ch). Note: I adjusted font-size of each item in .day-of-week to 0.7em (for visual aesthetics). We have to account for this change.

在這種情況下,最簡單的方法是將flex-basis設置為一個網格項的寬度(或4.5ch )。 注意:我將每個項目在.day-of-week font-size調整為0.7em (出于視覺美感)。 我們必須考慮到這一變化。

.day-of-week > * {flex-basis: calc(4.5ch / 0.7);
}
Adjusted .day-of-week for size.

Finally, we need to push the 1 February to Friday. (Five columns). Since column is 4.5ch, we simply push it by 4.5ch x 5.

最后,我們需要將2月1日推到星期五。 (五列)。 由于4.5ch ,我們只需將其推入4.5ch x 5

(Again, we can use CSS Calc to help us with this).

(同樣,我們可以使用CSS Calc來幫助我們)。

.date-grid button:first-child {margin-left: calc(4.5ch * 5);
}
Pushed 1 Febuary to Friday

修復CSS網格版本 (Fixing the CSS Grid version)

We can reactivate the CSS Grid code and make any necessary changes now.

我們現在可以重新激活CSS Grid代碼并進行任何必要的更改。

@supports (display: grid) {/* ... */
}
Activating CSS Grid code

Here, we see some values fly far out to the right. This happens because we added margin-left to the first grid item. We need to reset the added margin.

在這里,我們看到一些值向右飛走。 發生這種情況是因為我們向第一個網格項添加了margin-left 。 我們需要重置增加的保證金。

@supports (display: grid) {/* ... */.date-grid button:first-child {grid-column: 6;margin-left: 0;}
}
Removed margin-left.

Another thing: We can remove max-width because we don't need it in the CSS Code. (Even though this doesn't affect the CSS Code, we still want to remove it. Always better to have less properties).

另一件事:我們可以刪除max-width因為CSS代碼中不需要它。 (即使這不會影響CSS代碼,我們仍然希望將其刪除。總是最好減少屬性)。

@supports (display: grid) {.day-of-week,.date-grid {display: grid;grid-template-columns: repeat(7, 1fr);max-width: initial;}/* ... */
}

Here's the visual difference between the Flexbox and CSS Grid versions. Not too bad!

這是Flexbox和CSS Grid版本之間的視覺差異。 還不錯!

Visual difference between the Flexbox and CSS Grid code

一件事有趣 (One fun thing)

CSS Grid is cool because it follows writing direction. We can easily change the flow from left-to-right to right-to-left.

CSS Grid很酷,因為它遵循書寫方向。 我們可以輕松地將流程從左到右更改為從右到左。

Note: I don't know if calendars are read from right to left in rtl languages. I just thought it'll fun to mention this ?).

注意:我不知道日歷是否以rtl語言從右到左讀取。 我只是覺得提到這個會很有趣?)。

Switching between ltr and rtl.

Our code for CSS Grid supports this behaviour naturally. If you want to support the same behaviour with Flexbox, you need to use CSS Logical Properties.

我們CSS Grid代碼自然支持此行為。 如果要使用Flexbox支持相同的行為,則需要使用CSS邏輯屬性 。

Support for CSS Logical Properties.

Since support for CSS Logical Properties is not-so-great, we need to provide fallback for it. (Best way is to through Method 1: Write fallback; overwrite fallback).

由于對CSS邏輯屬性的支持不太好,因此我們需要為其提供備用。 (最好的方法是通過方法1:寫回退;覆蓋回退)。

.date-grid button:first-child {margin-left: calc(4.5ch * 5);margin-inline-start: calc(4.5ch * 5);
}@supports (display: grid) {/* ... */.date-grid button:first-child {grid-column: 6;margin-left: 0;margin-inline-start: 0;}
}

That's it! Here's a Codepen for the final code:

而已! 這是最終代碼的Codepen:

See the Pen Building a Calendar with CSS Grid (and fallback with Flexbox) by Zell Liew (@zellwk) on CodePen.

請參閱CodePen上的Zell Liew( @zellwk ) 撰寫的 使用CSS網格構建日歷(以及使用Flexbox進行后退 )的筆 。



Thanks for reading. This article was originally posted on my blog. Sign up for my newsletter if you want more articles to help you become a better frontend developer.

謝謝閱讀。 本文最初發布在我的博客上 。 如果您想要更多的文章來幫助您成為更好的前端開發人員,請注冊我的時事通訊 。

翻譯自: https://www.freecodecamp.org/news/how-to-add-flexbox-fallback-to-css-grid/

css flexbox模型

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/391240.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/391240.shtml
英文地址,請注明出處:http://en.pswp.cn/news/391240.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python:封裝連接數據庫方法

config.py # 數據庫測試環境 name *** password ****** host_port_sid 10.**.*.**:1521/bidbuat OracleOperation.py import cx_Oracle import configclass OracleOperation(object):# 執行下面的execute_sql方法時會自動執行該初始化方法進行連接數據庫def __init__(self):…

貝塞爾修正_貝塞爾修正背后的推理:n-1

貝塞爾修正A standard deviation seems like a simple enough concept. It’s a measure of dispersion of data, and is the root of the summed differences between the mean and its data points, divided by the number of data points…minus one to correct for bias.標…

RESET MASTER和RESET SLAVE使用場景和說明【轉】

【前言】在配置主從的時候經常會用到這兩個語句,剛開始的時候還不清楚這兩個語句的使用特性和使用場景。 經過測試整理了以下文檔,希望能對大家有所幫助; 【一】RESET MASTER參數 功能說明:刪除所有的binglog日志文件,…

Kubernetes 入門(1)基本概念

1. Kubernetes簡介 作為一個目前在生產環境已經廣泛使用的開源項目 Kubernetes 被定義成一個用于自動化部署、擴容和管理容器應用的開源系統;它將一個分布式軟件的一組容器打包成一個個更容易管理和發現的邏輯單元。 Kubernetes 是希臘語『舵手』的意思&#xff0…

Python程序互斥體

Python程序互斥體 有時候我們需要程序只運行一個實例,在windows平臺下我們可以很簡單的用mutex實現這個目的。 ??在開始時,程序創建了一個命名的mutex,這個mutex可以被其他進程檢測到。 這樣如果程序已經啟動,再次運行時的進程就…

890

890 轉載于:https://www.cnblogs.com/Forever77/p/11528605.html

android 西班牙_分析西班牙足球聯賽(西甲)

android 西班牙The Spanish football league commonly known as La Liga is the first national football league in Spain, being one of the most popular professional sports leagues in the world. It was founded in 1929 and has been held every year since then with …

Goalng軟件包推薦

2019獨角獸企業重金招聘Python工程師標準>>> 前言 哈嘍大家好呀! 馬上要迎來狗年了大家是不是已經懷著過年的心情了呢? 今天筆者給大家帶來了一份禮物, Goalng的軟件包推薦, 主要總結了一下在go語言中大家開源的優秀的軟件, 大家了解之后在后續使用過程有遇到如下軟…

Kubernetes 入門(2)基本組件

1. C/S架構 Kubernetes 遵循非常傳統的客戶端服務端架構,客戶端通過 RESTful 接口或者直接使用 kubectl 與 Kubernetes 集群進行通信,這兩者在實際上并沒有太多的區別,后者也只是對 Kubernetes 提供的 RESTful API 進行封裝并提供出來。 左側…

caioj1522: [NOIP提高組2005]過河

狀態壓縮的經典題。 按照一般做法,DP一維時間O(n),顯然跑不過。考慮到石子較少,實際上有很長一段是一定可以跳到的,設兩個石頭分別在i點和j點,跳躍的路程為S到T。那么從i點可以跳到iS到iT。從j-T到j-S可以跳到J。顯然當…

Dev控件使用CheckedListBoxControl獲取items.count為0 的解決方法

CheckedListBoxControl,我使用DataSource屬性,給其綁定了一個List對象。界面顯示都挺正常的,當若干個項的復選框被選中的后,它的checkedListBoxControl1.CheckedItems也是正常的。 唯獨的問題是在代碼中得到的checkedListBoxContr…

如何啟動軟件YouTube頻道

Hi, I’m Beau and I run the freeCodeCamp.org YouTube channel. 嗨,我是Beau,我運行了freeCodeCamp.org YouTube頻道 。 For the first few years of our channel’s life, we had less than 100,000 subscribers. When we published new videos, we …

【powerdesign】從mysql數據庫導出到powerdesign,生成數據字典

使用版本powerdesign16.5,mysql 5.5,windows 64 步驟: 1.下載mysql驅動【注意 32和64的驅動都下載下來,具體原因查看第三步 依舊會報錯處】 下載地址:https://dev.mysql.com/downloads/connector/odbc/5.3.html 請下…

php amazon-s3_推薦亞馬遜電影-一種協作方法

php amazon-s3Item-based collaborative and User-based collaborative approach for recommendation system with simple coding.推薦系統的基于項目的協作和基于用戶的協作方法,編碼簡單。 推薦系統概述 (Overview of Recommendation System) There are many met…

[高精度乘法]BZOJ 1754 [Usaco2005 qua]Bull Math

模板題目&#xff0c;練練手~ #include <iostream> #include <algorithm> #include <cstring> #include <cstdio> using namespace std;int s1[2333]; int s2[2333]; int Out[2333]; string one,two;void Debug(){for(int i0;i<one.length();i){pri…

python:使用Djangorestframework編寫post和get接口

1、安裝django pip install django 2、新建一個django工程 python manage.py startproject cainiao_monitor_api 3、新建一個app python manage.py startapp monitor 4、安裝DRF pip install djangorestframework 5、編寫視圖函數 views.py from rest_framework.views import A…

Kubernetes 入門(3)集群安裝

1. kubeadm簡介 kubeadm 是 Kubernetes 官方提供的一個 CLI 工具&#xff0c;可以很方便的搭建一套符合官方最佳實踐的最小化可用集群。當我們使用 kubeadm 搭建集群時&#xff0c;集群可以通過 K8S 的一致性測試&#xff0c;并且 kubeadm 還支持其他的集群生命周期功能&#…

Angular Material 攻略 04 Icon

Icon 網頁系統中的Icon雖然說很簡單&#xff0c;但是其中的學問還是有很多的&#xff0c;我們常用的Icon庫有FontAwesome、Iconfont等&#xff0c;我們選擇了Angular Material這個組件庫&#xff0c;就介紹Material Icons吧。 對Icon感興趣的同學可以看一下這里 Material Desig…

【9303】平面分割

Time Limit: 10 second Memory Limit: 2 MB 問題描述 同一平面內有n&#xff08;n≤500&#xff09;條直線&#xff0c;已知其中p&#xff08;p≥2&#xff09;條直線相交與同一點&#xff0c;則這n條直線最多能將平面分割成多少個不同的區域&#xff1f; Input 兩個整數n&am…

簡述yolo1-yolo3_使用YOLO框架進行對象檢測的綜合指南-第一部分

簡述yolo1-yolo3重點 (Top highlight)目錄&#xff1a; (Table Of Contents:) Introduction 介紹 Why YOLO? 為什么選擇YOLO&#xff1f; How does it work? 它是如何工作的&#xff1f; Intersection over Union (IoU) 聯合路口(IoU) Non-max suppression 非最大抑制 Networ…