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

肯德基圣代中間空心

by Kevin Kononenko

凱文·科諾年科(Kevin Kononenko)

建造冰淇淋圣代解釋CSS位置 (CSS Positioning Explained By Building An Ice Cream Sundae)

如果您之前做過冰淇淋圣代,那么您可以了解CSS的位置。 (If you’ve made an ice cream sundae before, then you can understand CSS positioning.)

Your divs are zooming around the screen like Roman candles.

您的div像羅馬蠟燭一樣在屏幕上縮放。

They’re diving deep into their container, then coming back up to the surface like a whale.

他們正潛入容器中,然后像鯨魚一樣回到水面。

They’re pushing the other elements out of the way, then leaving the container entirely like an impatient businessman.

他們將其他要素排除在外,然后像一個急躁的商人一樣完全離開了容器。

And somehow, this happens in a new and exciting way every time you change that one nasty line of CSS: the position property.

而且,每當您更改CSS的那一行討厭的行: position屬性時 ,都會以一種新穎而令人興奮的方式發生。

Every single person who has learned CSS has been there. CSS positioning seems to be nonsensical until hours of trial and error finally give you a vague understanding.

每個學習過CSS的人都曾去過那里。 CSS的定位似乎是荒謬的,直到經過數小時的試驗和錯誤終于使您模糊理解。

This post will end the confusion, once and for all. The position property doesn’t seem to have an obvious relation to any real-world concept… until you consider the humble ice cream sundae.

這篇文章將一勞永逸地結束混亂。 在您考慮不起眼的冰淇淋圣代之前,position屬性似乎與任何現實概念都沒有明顯的關系。

We’ll cover the following position properties:

我們將介紹以下位置屬性:

  • Absolute

    絕對
  • Static (default)

    靜態(默認)
  • Fixed

    固定
  • Relative

    相對的

And, for the sake of clarity, an ice cream sundae will consist of 4 major components:

而且,為清楚起見,圣代冰淇淋將由4個主要部分組成:

  • The glass

    玻璃
  • Ice cream scoops

    冰淇淋勺
  • Whipped cream

    鮮奶油
  • Cherries

    櫻桃

冰淇淋圣代HTML (The Ice Cream Sundae as HTML)

If you had to explain the structure of an ice cream sundae with HTML, it would probably look like this.

如果必須用HTML解釋冰淇淋圣代的結構,它可能看起來像這樣。

Or, in picture form:

或者,以圖片形式:

Before we dive into CSS, we can make a couple observations:

在深入研究CSS之前,我們可以做一些觀察:

  1. The number of ice cream scoops is limited by the glass height. We cannot keep stacking ice cream scoops forever. Eventually, the whole beautiful setup would topple over.

    冰淇淋勺的數量受玻璃杯高度的限制。 我們不能永遠堆疊冰淇淋勺。 最終,整個漂亮的設置將翻倒。
  2. You can put the cherries wherever you darn please. Cherries do not obey the flow and stacking of the scoops.They are smaller, and can fit in nooks and crannies where you cannot fit an ice cream scoop. And, more cherries will not upset the order of the scoops.

    您可以將櫻桃放到任何地方。 櫻桃不服從瓢的流動和堆積,它們較小,可以放在無法容納冰淇淋勺的角落和縫隙中。 而且,更多的櫻桃不會破壞瓢的順序。
  3. The whipped cream sits on top, no matter how many scoops there are. Have you ever seen an ice cream sundae with the whipped cream in the middle, and none on top? Neither have I.

    無論有多少勺,打好的奶油都坐在上面。 您是否曾經見過冰淇淋圣代冰淇淋,中間夾著鮮奶油,而頂部卻沒有? 我也沒有。

位置相對/靜態:玻璃和鏟子 (Position Relative/Static: The Glass and Scoops)

As shown in the first picture, our leaning tower of ice cream can only hold 5 scoops before toppling over. Let’s say that these 5 scoops have a total height of 500 pixels, and each scoop sits directly on top of one another. Our fullSundae div, in that case, would have a height of 500px to indicate that it can only handle those 5 scoops, and no more. This is an example of the default position, static. We use this to show that the height is unrelated to any container div.

如第一幅圖所示,我們的冰淇淋斜塔只能倒下5個勺子。 假設這5個瓢的總高度為500像素,并且每個瓢都直接位于另一個之上。 在這種情況下,我們的fullSundae div的高度將為500px,以表明它只能處理這5個瓢,而不能再處理了。 這是默認位置static的示例。 我們用它來表明高度與任何容器div無關。

The simple thing to do next would be to just give each iceCreamScoop a height of 100px, which would match the height of the fullSundae div. That would be no fun, because the glass div would be 300px by default. Let’s look at it another way.

接下來要做的簡單事情是,將每個iceCreamScoop的高度設置為100px,這將與fullSundae div的高度匹配。 那不會很有趣,因為默認情況下glass div為300px。 讓我們換一種方式來看。

Since the glass div contains three of the five scoops, and all scoops are equal height, we can see that the glass div is 60% of the height of the fullSundae div. This is an opportunity for position relative! You can set the glass div to position relative and give it a height of 60%. The glass div will look at the height of the entire fullSundae div, and take up 60% of that space. The percentage is relative to the height of the container div, which was stated explicitly as 500px.

由于玻璃div包含五個勺中的三個,并且所有勺都是相等的高度,因此我們可以看到玻璃div是整個圣代div高度的60%。 這是相對職位的機會! 您可以將玻璃div設置為相對位置,并將其高度設置為60%。 玻璃div將看整個圣代div的高度,并占據該空間的60%。 百分比是相對于容器div的高度(明確表示為500px)而言的。

You can go even further. If you set each iceCreamScoop within the glass div to position relative, each scoop will calculate its height based on the height of the glass div. The glass can fit three scoops, so each scoop should have a height of 33.3%. Here is all of that in code.

您可以走得更遠。 如果將玻璃div中的每個iceCreamScoop設置為相對位置 ,則每個瓢將基于玻璃div的高度計算其高度。 該玻璃杯可容納三個勺子,因此每個勺子的高度應為33.3%。 這就是代碼中的所有內容。

固定位置:鮮奶油 (Position Fixed: The Whipped Cream)

Position fixed should be the easiest one. A position fixed element will be stuck in place no matter how far the body extends. In ice cream terms, this is the whipped cream on top. No matter how many ice cream scoops that you try and stack, the whipped cream will still be on top, with the exact same relationship to the scoops. It is positioned relative to the body, not to the containing div.

固定位置應該是最簡單的一種。 不管身體延伸多遠, 位置固定元件都將卡在適當的位置。 用冰淇淋來講,這是奶油在上面。 無論您嘗試堆疊多少個冰淇淋勺,都將鮮奶油放在頂部,與勺的關系完全相同。 它相對于主體而不是相對于包含div的位置。

The whipped cream is independent of the series of ice cream scoops. The amount of whipped cream does not affect the maximum number of scoops that you can have in the full sundae. It stays in a consistent spot on the page.

攪打的奶油與冰淇淋勺系列無關。 鮮奶油的量不會影響您在完整圣代冰淇淋中可容納的最大勺數。 它在頁面上保持一致。

You commonly see position fixed in headers and footers. These are the elements that stick in position, even when you scroll the page or div.

您通常會在頁眉和頁腳中看到固定的位置。 即使滾動頁面或div,這些元素也會保留在原位。

絕對位置:櫻桃 (Position Absolute: The Cherries)

There’s a reason I saved Position Absolute for last: it can lead to unmaintainable code if you use it too frequently. You have been warned. But, it works perfectly for the cherries in this example.

我之所以最后保存“絕對位置”是有原因的:如果您過于頻繁地使用它,可能會導致代碼無法維護。 你被警告了。 但是,在此示例中,它非常適合櫻桃。

You can put cherries almost anywhere in this ice cream sundae. You can put a bunch on top, and it will not topple over. You can wedge them in the glass itself, and you will still be able to fit the same amount of ice cream. They do not obey the same rules as the position static and position relative elements. Furthermore, you can remove them without disturbing any other elements.

您可以將櫻桃放在此冰淇淋圣代中的幾乎任何位置。 您可以在上面放一束,它不會翻倒。 您可以將它們楔入玻璃杯中,仍然可以放入相同量的冰淇淋。 它們不遵循與位置靜態元素和位置相對元素相同的規則。 此外,您可以刪除它們而不會干擾任何其他元素。

These are the key components of position absolute. Position absolute elements do not disturb the placement of other elements, but you must state their position explicitly. If you do not, they default to the upper left corner of the body. Or, if one of their parents has position relative, they go to the top left corner of that div.

這些是絕對職位的關鍵組成部分。 位置絕對元素不會干擾其他元素的放置,但是您必須明確聲明其位置。 如果不這樣做,它們默認為主體的左上角。 或者,如果他們的父母中有一位是親戚,他們將轉到該div的左上角。

Check out this cherry-filled sundae. There are cherries added in a bunch of places, and they do not disturb the flow of the other elements. But, keep in mind that you cannot stack the cherries like you stack ice cream scoops. Cherries do not stack. You must place each explicitly.

看看這個充滿櫻桃的圣代冰淇淋。 在很多地方都添加了櫻桃,它們不會干擾其他元素的流動。 但是,請記住,您不能像堆疊冰淇淋勺那樣堆疊櫻桃。 櫻桃不堆積。 您必須明確放置每個。

One last note: position absolute is calculated based on the nearest parent that is NOT position static. If every parent is position static, it is calculated based on the entire body. So, in the case above, the cherries within the glass are positioned based on the height of the glass div, not based on the height of the fullSundae div. The glass div has position relative.

最后一點:絕對位置是根據不是位置靜態值的最接近的父級來計算的。 如果每個父對象都是靜態位置,則會基于整個身體進行計算。 因此,在上述情況下,玻璃中的櫻桃是根據玻璃div的高度而不是整個圣代div的高度來定位的。 玻璃div具有相對位置。

Now go practice it, and when you feel a little more comfortable… you deserve some ice cream!

現在開始練習,當您感到更加舒適時……您應該得到一些冰淇淋!

EDIT: You can now build an ice cream sundae with CSS positioning in this interactive tutorial!

編輯:您現在可以在此交互式教程中使用CSS位置構建冰淇淋圣代!

Did you enjoy this post? Give it a “heart” so it can help others as well!

你喜歡這個帖子嗎? 給它一個“心”,這樣它也可以幫助別人!

This post originally appeared on the CodeAnalogies blog.

該帖子最初出現在CodeAnalogies博客上

翻譯自: https://www.freecodecamp.org/news/css-positioning-explained-by-building-an-ice-cream-sundae-831cb884bfa9/

肯德基圣代中間空心

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

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

相關文章

00

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

12_04_Linux軟件管理之四yum

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

datastage 函數_DataStage常用函數大全

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

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

1. windows PE文件與Linux ELF文件概述在windows中可執行文件是pe文件格式,Linux中可執行文件是ELF文件,其文件格式是ELF文件格式,在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…

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

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

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

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

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

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

傅里葉變換與大數乘法

我們知道,兩個 N 位數字的整數的乘法,如果使用常規的算法,時間復雜度是 O(N2)。然而,使用快速傅里葉變換,時間復雜度可以降低到 O(N logN loglogN)。 假設我們要計算以下兩個 N 位數字的乘積: 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上面運行程序都有這樣的體驗:某個程序運行的時候,會產生大量的log(提示)信息,但實際上我們只想讓它跑一下而已,log暫時不需要或者后面才有需要。同時run多個相同或者不同程序的時候,占了好多的命令行界面&a…

數字時代的精益組織

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

9th week blog

1、第一個計算機語言:Fortran Fortran I誕生于1957年,由IBM設計,是世界上第一個真正的計算機語言。 Fortran I運行于IBM 704計算機上。 Fortran I支持:變量(變量名最多6個字符)、If和do語句、格式化IO。 不…

cas單點登錄系統:客戶端(client)詳細配置(包含統一單點注銷配置)

最近一直在研究cas登錄中心這一塊的應用,分享一下記錄的一些筆記和心得。后面會把cas-server端的配置和重構,另外還有這幾天再搞nginxcas的https反向代理配置,以及cas的證書相關的知識分享出來。 Cas由兩部分組成,Cas Server和Cas…

open 端口打開Linux,linux – nmap顯示打開的端口,但netstat沒有

如果我使用nmap掃描我的服務器,則表明已打開21端口.但是當我登錄到這個服務器并運行netstat時,我什么也看不見.$nmap -sT serverStarting Nmap 4.76 ( http://nmap.org ) at 2009-06-24 11:54 MSDInteresting ports on server (x.x.x.x):Not shown: 994 filtered portsPORT STA…

技能學習重要的古語_學習方法:最重要的開發人員技能

技能學習重要的古語by Preethi Kasireddy通過Preethi Kasireddy 學習方法:最重要的開發人員技能 (Learning How to Learn: The Most Important Developer Skill) Being an efficient learner is at least as important as being an efficient coder.成為高效的學習…

Git submodule 的筆記

這次不造輪子,寫寫文章。file最近想把自己的 blog 整理到 github 上,但由于其中依賴了一些主題以及插件,這時候 git submodule 就能很好的處理這種情況了。 submodule 是什么? submodule 顧名思義,子模塊。在一個項目依…

Leetcode690.Employee Importance員工的重要性

給定一個保存員工信息的數據結構,它包含了員工唯一的id,重要度 和 直系下屬的id。 比如,員工1是員工2的領導,員工2是員工3的領導。他們相應的重要度為15, 10, 5。那么員工1的數據結構是[1, 15, [2]],員工2的數據結構是…

linux進程管理類命令大全,Linux進程管理類命令

一、htop命令選項-d #:指定延遲時間間隔;-u UserName:僅顯示指定用戶的進程;-s COLUME:以指定字段進行排序;子命令:l:顯示選定的進程打開的文件列表;s:跟蹤選…

C#操作Excel

1.System.Data.DataTable連接數據庫 ExcelFile 是excel的完整路徑//OleDbConnection conExcel new OleDbConnection("ProviderMicrosoft.Jet.OLEDB.4.0;Data Source" ExcelFile ";Extended PropertiesExcel 8.0");//if (conExcel.State ConnectionStat…