css flexbox模型_CSS Flexbox在全國范圍內的公路旅行中得到了解釋

css flexbox模型

by Kevin Kononenko

凱文·科諾年科(Kevin Kononenko)

CSS Flexbox在全國范圍內的公路旅行中得到了解釋 (CSS Flexbox Explained by Road Tripping Across the Country)

如果您旅行很長,那么您可以了解CSS Flexbox! (If you have ever been on a long roadtrip, then you can understand CSS Flexbox!)

The popular Flexbox model attempts to replace the giant pain known as CSS “floats”. Unfortunately, it also introduces yet another entirely new system into CSS. And you thought there were enough already!

流行的Flexbox模型試圖替代稱為CSS“ floats ”的巨大痛苦。 不幸的是,它還在CSS中引入了另一個全新的系統。 而且您認為已經足夠了!

Actually, the grid-oriented nature of Flexbox makes much more sense than constantly juggling your “float” and “block/inline-block” values.

實際上,Flexbox面向網格的本質比不斷調整“ float”和“ block / inline-block”值有意義得多。

There are already a couple good resources out there, like Flexbox Tower Defense and this more technical guide.

已經有一些不錯的資源,例如Flexbox塔防和此更多技術指南 。

In this article, I’ll take the route of explaining the Flexbox system through one of my favorite types of vacations… the road trip!

在本文中,我將通過我最喜歡的一種度假方式-公路旅行,來說明Flexbox系統!

That’s right — we’re going to use the entire United States in this analogy.

沒錯-我們將以此類推來使用整個美國。

The US actually has a grid-oriented interstate highway system that spans East-West and North-South.

實際上,美國擁有跨東西向和南北向的以網格為導向的州際公路系統。

While this map is geographically accurate, it’s pretty hard to understand. So let’s try that again.

盡管這張地圖在地理上是準確的,但很難理解。 因此,讓我們再試一次。

In this scenario, you must primarily travel along a one-directional path.

在這種情況下, 您必須主要沿著單向路徑行駛

For example, you might take the route from Seattle to Boston which only includes west to east. Or, you might take the route from Seattle to San Diego, which only covers north to south.

例如,您可能選擇了從西雅圖到波士頓的路線,其中僅包括西到東。 或者,您可能會選擇從西雅圖到圣地亞哥的路線,該路線僅覆蓋北到南。

Since the default position is top left, we’ll start from Seattle. You’ll get a chance to add side trips to your road trip towards the end! This is important because it imitates the flow of <div>s within their container.

由于默認位置在左上方,因此我們將從西雅圖開始。 您將有機會在旅途中添加旁行路線! 這很重要,因為它模仿了<div>在其容器中的流動。

Let’s hit the road!

讓我們上路吧!

flex-direction:您旅行的方向 (flex-direction: The direction of your trip)

Flex-direction determines the direction of elements within a container <div> with “display:flex;”. The default value is “row”, which means from left to right. No surprises there.

伸縮方向使用“ display:flex;”確定容器<div>中元素的方向。 默認值為“行”,表示從左到右。 沒有驚喜。

Let’s say you’re starting in Seattle, and taking a trip to Boston. That trip might look like this in HTML:

假設您要從西雅圖開始,然后去波士頓旅行。 該行在HTML中可能如下所示:

This assumes that you’re making stops at Yellowstone, Mount Rushmore and Chicago. Here is a timeline view, assuming you stop for 2 days at each location.

假設您要在黃石公園,拉什莫爾山公園和芝加哥停留。 這是時間軸視圖,假設您在每個位置停留2天。

And what if you are using “flex-direction:column;”? That means that your <div>s will align from top to bottom. Now, you’re going from Seattle to San Diego. Stops along the way might be Portland, San Francisco and Los Angeles.

如果您使用的是“ flex-direction:column;”,該怎么辦? 這意味著您的<div>將自上而下對齊。 現在,您要從西雅圖到圣地亞哥。 一路上的站點可能是波特蘭,舊金山和洛杉磯。

證明內容:您在旅途中如何安排停留地點 (justify-content: How you space your stops along the trip)

Alright, let’s return to the Seattle to Boston trip. With Flexbox, we can decide how our child <div>s spread out along the width of the container. So, if you are on a road trip, you may not necessarily evenly space out your stops. You might stop more frequently at the beginning, or at the end.

好了,讓我們回到西雅圖到波士頓的旅程。 使用Flexbox,我們可以決定子<div>沿容器的寬度分布的方式。 因此,如果您在旅途中,不一定會均勻地停下來。 您可能會在開始或結束時更頻繁地停止。

The default value for justify-content is “flex-start”, which means your <div>s spread from the left-most side. This is similar to choosing to do all of your stops on the roadtrip at the beginning. This would include places like Glacier National Park, Yellowstone and Mount Rushmore.

justify-content的默認值為“ flex-start ”,這意味著您的<div>從最左側展開。 這類似于選擇一開始在旅途中的所有停留。 其中包括冰川國家公園,黃石公園和拉什莫爾山。

On a map:

在地圖上:

Okay, this is admittedly a little unrealistic. You probably would not want to drive 20 hours from South Dakota straight to Boston. The same could be said of “flex-end”, when all of the <div>s hug the right side of the container. This would include stops at places like the wonderful city of Cleveland, Niagara Falls and the MLB Hall of Fame.

好的,這無疑是不現實的。 您可能不希望從南達科他州開車直達波士頓20小時。 當所有<div>都擁抱容器的右側時,“ flex-end ”也可以這樣說。 這將包括在美麗的克利夫蘭市,尼亞加拉大瀑布和美國職業棒球大聯盟名人堂等地方停留。

On map:

在地圖上:

Another example is “center”, where the <div>s align themselves to the middle of the container <div>. That would mean visits to Mount Rushmore, the Mall of America in Minnesota and Chicago.

另一個示例是“ center ”,其中<div>使其與容器<div>的中間對齊。 那將意味著參觀拉什莫爾山,明尼蘇達州和芝加哥的美國購物中心。

align-items:您想在全國穿哪條公路? (align-items: Which highway do you want to take across the country?)

Alright, so far we have mainly been discussing the northern route across the country. In HTML terms, that means we are just going along the top of the <div>. But, one magical property of Flexbox is that we can easily move to the middle or bottom of the <div> without any CSS trickery.

好吧,到目前為止,我們主要討論的是橫跨美國的北部路線。 用HTML術語來說,這意味著我們只是在<div>的頂部。 但是,Flexbox的一項神奇特性是,我們可以輕松地移至<div>的中間或底部,而無需任何CSS技巧。

The align-items property defaults to “flex-start”, but if we change that to “center”, our <div>s will vertically align to the center of the container. This is kind of like starting your road trip in San Francisco, heading over to Las Vegas, then Denver, then St. Louis and ending in Washington D.C.

align-items屬性默認為“ flex-start”,但是如果將其更改為“ center ”,則我們的<div>將垂直對齊容器的中心。 這有點像在舊金山開始您的公路旅行,然后前往拉斯維加斯,然后是丹佛,然后是圣路易斯,然后在華盛頓特區結束

Here’s the map:

這是地圖:

And, in HTML:

并且,在HTML中:

If you wanted to set your align-items value to “flex-end”, your <div>s would align to the bottom of the container. You would be taking the southern route across the United States, and stopping in places like Sedona, Austin and New Orleans before ending in Jacksonville.

如果要將align-items值設置為“ flex-end ”,則<div>將與容器的底部對齊。 您將沿美國南部路線行駛,然后在塞多納,奧斯丁和新奧爾良等地停留,然后在杰克遜維爾結束。

自我調整:在另一條高速公路上停下來 (align-self: Have one stop on a different highway route)

You can apply “align-self” to individual child <div>s in order to have them move vertically within the container, regardless of the align-items property. So if you are taking the trip from Seattle to Boston, you can make a stop in Las Vegas, which is in the middle of the country. Then you can continue on to Mount Rushmore or wherever else on the normal horizontal flow.

您可以將“ align-self”應用于單個子<div>,以使它們在容器中垂直移動,而不管align-items屬性如何。 因此,如果您從西雅圖到波士頓旅行,可以在美國中部的拉斯維加斯停留。 然后,您可以繼續前往拉什莫爾山(Mount Rushmore)或正常水平流動的其他任何地方。

順序:按特定順序停靠 (order: Make your stops in a specific order)

So far, every stop has corresponded with the order of the elements in the HTML. In other words, if the Mount Rushmore stop is specified third in the HTML, that means it will be the third stop on the road trip.

到目前為止,每個停止都與HTML中元素的順序相對應。 換句話說,如果在HTML中將拉什莫爾山站指定為第三站,則這將是旅途中的第三站。

The “order” property is a numerical value that allows us to change the order of the HTML elements. Without Flexbox, we would need to use a confusing series of floats, or just change the HTML.

“ order”屬性是一個數值,使我們可以更改HTML元素的順序。 如果沒有Flexbox,我們將需要使用一系列令人困惑的浮點數,或者僅更改HTML。

With “order”, we can turn around on our road trip and visit a place that is not on the way to the end point. Would you do this in real life? Only if you enjoy an extra 15 hours in the car!

使用“訂單”,我們可以繞開公路旅行,并前往未到達終點的地方。 您會在現實生活中這樣做嗎? 僅在您享受額外15個小時的車程時!

Let’s say we’re taking the northern trip, Seattle to Boston. Here’s that HTML again.

假設我們正在北行,從西雅圖到波士頓。 這又是HTML。

But, after starting in Seattle, we want to first go to South Dakota for the world’s largest square dance festival. We would use the “order” property to make sure the Mount Rushmore visit comes right after Seattle.

但是,從西雅圖開始之后,我們想先去南達科他州參加世界上最大的廣場舞蹈節。 我們將使用“ order”屬性來確保拉什莫爾山的訪問恰好在西雅圖之后。

Order defaults to 0, so we might want to give seattleVisit a value of -2, and mountRushmoreVisit a value of -1 to make sure it comes 2nd. Then the rest of the elements will follow in a normal flow.

Order默認為0,因此我們可能要給seattleVisit賦予-2的值,并為mountRushmoreVisit賦予-1的值以確保它排在第二位。 然后,其余元素將按照正常流程進行。

Notice — this is now strictly a timeline view, not using the geographic order as the other maps were using.

注意-現在嚴格來說,這是一個時間軸視圖,沒有像其他地圖一樣使用地理順序。

結論 (Conclusion)

Time for a little quiz! Here are some sample destinations, in HTML.

時間小測驗! 這是HTML中的一些示例目標。

But, if the following is your planned route… what would the CSS need to look like?

但是,如果以下是您計劃的路線,那么CSS需要什么樣?

  • Start in San Francisco

    從舊金山開始
  • 2nd stop: Las Vegas

    第二站:拉斯維加斯
  • 3rd Stop: Mount Rushmore

    第三站:拉什莫爾山
  • 4th Stop: Backtrack to Denver

    第四站:回丹佛
  • End by driving all the way to Washington D.C

    一路駛向華盛頓特區結束

The answer:

答案:

Here’s the reasoning behind this:

這是其背后的原因:

  • 4 out of 5 items are along our central route, so we “align-items” to center.

    5個項目中有4個位于我們的中心路線上,因此我們將“項目對齊”到中心。

  • The three stops are generally in the middle of the United States from a West-East perspective, so we “justify-content to center as well.

    從東西方的角度來看,這三個站點通常位于美國中部,因此我們也將“合理化內容”定中心

  • Mount Rushmore is on the northern route, so we use align-self on that one <div>.

    拉什莫爾山(Mount Rushmore)位于北部路線,因此我們在該<div>上使用align-self

  • The order CSS property is why we must backtrack to Denver, and why our actual trip does not follow the order of the children <div>s in the HTML. Order allows us to change the sequence of <div>s. In this case, we move Denver to the second to last stop, so we must give it an order greater than 0, but less than the order of the final <div> so that we still end in D.C.

    CSS 順序屬性是為什么我們必須回溯到丹佛的原因,以及為什么我們的實際旅行沒有遵循HTML中子元素<div>的順序。 Order允許我們更改<div>的順序。 在這種情況下,我們將丹佛移至倒數第二個停靠點,因此我們必須它一個大于0但小于最后一個<div>的順序的順序,以便我們仍以DC結尾

If you enjoyed this post, you may also enjoy my other explanations of challenging CSS and JavaScript topics, such as positioning, Model-View-Controller, and callbacks.

如果您喜歡這篇文章,那么您可能還會喜歡我對具有挑戰性CSS和JavaScript主題的其他解釋 ,例如定位,Model-View-Controller和回調。

And if you think this might help other people in the same position as you, give it a “heart”!

而且,如果您認為這可以幫助與您處于同一位置的其他人,請給它一個“心臟”!

翻譯自: https://www.freecodecamp.org/news/css-flexbox-explained-by-road-tripping-across-the-country-1217b69c390e/

css flexbox模型

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

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

相關文章

oracle 10g 白皮書,Oracle 10g標準版與企業版

beautiful 于 2007-03-06 00:43:37發表:最后還有一些關于oracle產品的FAQ&#xff1a;1. Oracle數據庫軟件目前在售的版本號&#xff1f;A&#xff1a;目前在售的是Oracle 9i 和Oracle 10g2. 10g是不是比9i更好&#xff1f;A&#xff1a;一個新版本的軟件推出以后&#xff0c;總…

Linux 小筆記

1、查看linux 版本 按ctrlshiftt 快捷鍵&#xff0c;打開終端&#xff0c;輸入sudo uname --m &#xff0c;按下enter 如果顯示i686,你安裝了32位操作系統 如果顯示 x86_64&#xff0c;你安裝了64位操作系統 轉載于:https://www.cnblogs.com/1995hxt/p/5436683.html

不會發布npm包?進來看看?

前言 npm(Node Package Manager)&#xff0c;一個Node的包管理器&#xff0c;平時我們常用的公共模塊&#xff08;插件&#xff09;或者叫做包大多都放在上面&#xff0c;所以接下來要封裝的插件&#xff0c;我們就簡單稱它為npm包&#xff0c;本文從就從這個簡單的例子開始&am…

如何讓帝國CMS7.2搜索模板支持動態標簽調用

帝國cms站內搜索一般不支持動態標簽調用&#xff0c;如果要調用如何實現呢&#xff1f;修改兩個地方就可以實現了。打開 /e/search/result/index.php 文件&#xff0c;找到&#xff08;文件改了&#xff0c;不會調用也是徒勞&#xff01;看看這個帝國cms搜索關鍵字調用標簽(sho…

access字體變為斜體_Linux折騰記(四):Linux桌面系統字體配置詳解

字體顯示效果測試文字&#xff1a;復制代碼代碼如下:這一段是為了測試宋體字的顯示效果&#xff0c;包括宋體里面自帶的英文字體&#xff0c;“This is english,how does it look like?”。這一行是小字。后面幾個字是加粗的宋體。標點符號“&#xff0c;。&#xff1a;&#…

oracle between and monday,oracle——時間

時間數據1. 插入時間數據插入語法命令&#xff1a;insert into floor values (to_date(年-月-日 時:分:秒,YYYY-MM-DD HH24:MI:SS));完整的時間插入insert into floor values (to_date(2010-07-12 09:10:12,YYYY-MM-DD HH24:MI:SS));查詢顯示&#xff1a;2010-07-12 09:10:12.0…

Nova 組件詳解 - 每天5分鐘玩轉 OpenStack(26)

本節開始&#xff0c;我們將詳細講解 Nova 的各個子服務。 前面架構概覽一節知道 Nova 有若干 nova-* 的子服務&#xff0c;下面我們將依次學習最重要的幾個。今天先討論 nova-api 和 nova-conductor。 nova-api Nova-api 是整個 Nova 組件的門戶&#xff0c;所有對 Nova 的請…

肯德基圣代中間空心_建造冰淇淋圣代解釋CSS位置

肯德基圣代中間空心by Kevin Kononenko凱文科諾年科(Kevin Kononenko) 建造冰淇淋圣代解釋CSS位置 (CSS Positioning Explained By Building An Ice Cream Sundae) 如果您之前做過冰淇淋圣代&#xff0c;那么您可以了解CSS的位置。 (If you’ve made an ice cream sundae befo…

00

&#xff08;1&#xff09;設置gcc 把所有gcc版本解壓到/home/flinn/tools/目錄下&#xff0c;以免切換編譯器export PATHPATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/flinn/tools/4.4.3/bin &#xff08;2&#xff09;編譯&#xff1…

12_04_Linux軟件管理之四yum

2019獨角獸企業重金招聘Python工程師標準>>> RPM安裝&#xff1a; 二進制格式&#xff1a; 源程序--》編譯--》二進制格式 有些特性是編譯時選定的&#xff0c;如果編譯時未選定此特性&#xff0c;將無法使用&#xff1b; rpm包的版本會落后于源碼包&#xff0c;甚至…

datastage 函數_DataStage常用函數大全

1/38DataStage常用函數大全DATASTAGE常用函數大全.................................................................................................1一、類型轉換函數................................................................................................…

linux 解析elf文件格式,Linux下ELF文件解析

1. windows PE文件與Linux ELF文件概述在windows中可執行文件是pe文件格式&#xff0c;Linux中可執行文件是ELF文件&#xff0c;其文件格式是ELF文件格式&#xff0c;在Linux下的ELF文件除了可執行文件(Excutable File),可重定位目標文件(RellocatableObject File)、共享目標文…

富爸爸窮爸爸害了我_這是我必須告訴爸爸的-在我們的時間用完之前

富爸爸窮爸爸害了我by Bram Bos通過Bram Bos 這是我必須告訴爸爸的-在我們的時間用完之前 (This is what I must tell my dad — before our time runs out) I was a young boy in the 1980s. Like the typical Generation-X kid, I grew up in the days of the home computer…

應用容器公共免費部署平臺

從網上信息&#xff0c;發現了一個公共的容器部署平臺 openshift.com&#xff0c;可以將我們封裝好的docker鏡像部署到平臺上&#xff0c; 這樣就不需要擁有一臺云服務器了。對于測試環境非常有用。 首先當然是需要注冊。這里全英文 第二&#xff0c;注冊之后需要選擇你想要的套…

西門子rwd68溫控器說明書_西門子RWD68說明書

西門子RWD68說明書顯示第一界面Y1XX模擬量輸出電壓值YIXX傳感器此時實際溫度&#xff1b;同時按—鍵五秒顯示第二界面PS4主控制回路參數&#xff1b;按—鍵顯示第三界面PS3輔助回路參數但僅在室外補償時出現&#xff1b;按—鍵顯示第四界面PS2按—鍵顯示第五界面PS1控制曲線運用…

linux 內存管理優化,Linux性能優化實戰 內存篇 閱讀筆記

第十五講 基礎篇&#xff1a;Linux內存是怎么工作的(2020.6.8)這一講相關的內容正好之前看csapp的時候總結了一下&#xff0c;可以直接貼出來作為總結了。Linux的內存工作原理&#xff0c;這又是一個特別大的話題。一切向著盡量利用物理資源的方向在發展&#xff0c;在沒有虛擬…

傅里葉變換與大數乘法

我們知道&#xff0c;兩個 N 位數字的整數的乘法&#xff0c;如果使用常規的算法&#xff0c;時間復雜度是 O(N2)。然而&#xff0c;使用快速傅里葉變換&#xff0c;時間復雜度可以降低到 O(N logN loglogN)。 假設我們要計算以下兩個 N 位數字的乘積&#xff1a; a (aN-1aN-2…

idea 啟動界面導入項目_如何為您的項目啟動有效的登錄頁面

idea 啟動界面導入項目by Christian-Peter Heimbach通過克里斯蒂安彼得海姆巴赫 如何為您的項目啟動有效的登錄頁面 (How to launch an effective landing page for your project) I began my career 10 years ago doing online marketing and advertising for video games. S…

linux程序已經在后臺運行凍結了_如何使程序在Linux后臺運行

經常在Linux上面運行程序都有這樣的體驗&#xff1a;某個程序運行的時候&#xff0c;會產生大量的log(提示)信息&#xff0c;但實際上我們只想讓它跑一下而已&#xff0c;log暫時不需要或者后面才有需要。同時run多個相同或者不同程序的時候&#xff0c;占了好多的命令行界面&a…

數字時代的精益組織

精益IT應該幫助簡化和改善我們為客戶創造價值的方式&#xff0c;并提出面向未來的更好的解決方案。未來的組織將專注于同行業的產品或業務流——其他的一切&#xff0c;包括專家和管理者在內&#xff0c;都是為了讓一線工作人員可以第一時間就做好&#xff0c;而又不會遇到任何…