flexbox布局_Flexbox vs Grid-如何構建最常見HTML布局

flexbox布局

There are so many great CSS resources all over the internet. But what if you just want a simple layout and you want it NOW?

互聯網上有很多很棒CSS資源。 但是,如果您只是想要一個簡單的布局而現在就想要呢?

In this article, I describe the 5 most common web page layouts and how to build them using both Flexbox and Grid.

在本文中,我將介紹5種最常見的網頁布局以及如何使用Flexbox和Grid來構建它們。

這將如何工作 (How this will work)

There is a link below every layout for the full HTML and CSS code on CodePen.

每個布局下方都有一個鏈接,用于CodePen上的完整HTML和CSS代碼。

Note that I'm using SASS for composing style definitions, so if you want to do the same on your local, install SASS using:

請注意,我使用SASS來編寫樣式定義,因此,如果要在本地進行相同的操作,請使用以下命令安裝SASS:

npm i sass -g

基本卡模板 (Basic card template)

I used the above card as the base of the web page layout. It's composed of three elements in a vertical direction, so normal div blocks would work well. However, I will later need to make the middle element - the text paragraph - stretch.

我使用以上卡片作為網頁布局的基礎。 它由垂直方向上的三個元素組成,因此普通的div塊會很好地工作。 但是,稍后我將需要拉伸中間元素(文本段落)。

Here both Flexbox and Grid do the job seamlessly. I prefer Flexbox as it's more straightforward to me.

在這里,Flexbox和Grid都能無縫地完成工作。 我更喜歡Flexbox,因為它對我來說更直接。

Winner: Flexbox

獲獎者:Flexbox

CodePen Flexbox, CodePen Grid

CodePen Flexbox , CodePen網格

Now let's start creating our different layouts.

現在開始創建不同的布局。

1垂直和水平居中卡 (1 Vertically and horizontally centered card)

With Flexbox, we need one element that centers horizontally, and another (the child element) that centers vertically.

使用Flexbox,我們需要一個元素水平居中,另一個(子元素)垂直居中。

The order of items is defined by flex-direction. How the element positions itself in the available space is set by align-self on the element or align-items on its parent.

項目的順序由flex-direction定義。 元素如何在可用空間中定位自己是由元素上的align-self或其父元素上的align-items設置的。

With Grid, we need three columns and three rows. Then we position the card in the middle cell.

使用網格,我們需要三列三行。 然后,將卡放置在中間單元中。

The horizontal centering is easy. We define three columns and their sizes using grid-template-columns: auto 33% auto as the card should be as wide as 1/3 of the visible area.

水平居中很容易。 我們使用grid-template-columns: auto 33% auto定義三列及其大小grid-template-columns: auto 33% auto因為卡片的寬度應為可見區域的1/3。

The problem is, we don't know the vertical dimensions. We want the top and bottom rows to take up the remaining space which is not possible with grid. The card is centered, but its height depends on the height of the window.

問題是,我們不知道垂直尺寸。 我們希望最上面和最下面的行占用剩余空間,而網格是不可能的。 該卡居中,但其高度取決于窗口的高度。

However, we can solve this with an additional wrapping element around the card and center it using margin.

但是,我們可以通過在卡周圍使用其他包裝元素來解決此問題,并使用margin對其進行居中。

Winner: Flexbox

獲獎者:Flexbox

CodePen Flexbox, CodePen Grid

CodePen Flexbox , CodePen網格

2兩張卡垂直和水平居中 (2 Two cards vertically and horizontally centered )

Often we need to center more than just one element. These two cards should also maintain the same height if one or the other contains longer text.

通常,我們需要將多個元素居中。 如果一個或另一個包含較長的文本,則這兩個卡也應保持相同的高度。

With Flexbox, we need to wrap both cards in another element and use it to center both cards at once.

使用Flexbox,我們需要將兩個卡包裝在另一個元素中,并使用它一次將兩個卡居中。

We can't use align-items here as that applies to the Y-axis in this case. We need to define how the remaining space on the X-axis should be distributed with justify-content: center. That ensures both cards are horizontally centered.

我們不能在這里使用align-items ,因為在這種情況下,它適用于Y軸。 我們需要定義如何使用justify-content: center分配X軸上的剩余空間。 這樣可以確保兩張卡都水平居中。

If we omit the variable height problem of Grid, we can achieve the same result even without any additional wrapping elements. This time we define grid with five columns with grid-template-columns: auto 33% 50px 33% auto. The rest stays the same as in the previous example.

如果省略Grid的可變高度問題,即使沒有任何其他包裝元素,我們也可以達到相同的結果。 這次,我們用五列的grid-template-columns: auto 33% 50px 33% auto定義了grid grid-template-columns: auto 33% 50px 33% auto 。 其余部分與前面的示例相同。

Winner: Flexbox

獲獎者:Flexbox

CodePen Flexbox, CodePen Grid

CodePen Flexbox , CodePen網格

3張相同寬度和高度的卡 (3 Multiple cards with same width and height)

This is another typical use case for blogs, e-commerce sites, or generally any site that displays some kind of listing. We want the cards to have the same width and height. Height needs to be inferred from the greatest element in the list.

這是博客,電子商務網站或通常顯示某種列表的任何網站的另一個典型用例。 我們希望卡片具有相同的寬度和高度。 需要從列表中最大的元素推斷出高度。

This can be done in Flexbox using flex-wrap: wrap. Elements will wrap to the next line if their width exceeds the remaining space of each line. However, the same height is only preserved in the scope of a single line, unless you define it explicitly.

這可以在Flexbox中完成 使用flex-wrap: wrap 。 如果元素的寬度超過每行的剩余空間,則它們將換行到下一行。 但是,除非明確定義,否則同一高度僅保留在單行范圍內。

Grid shows its true power here. This layout can be created using grid-auto-rows: 1fr which enforces the same height on all rows.

在這里展示其真正的力量。 可以使用grid-auto-rows: 1fr創建此布局,它在所有行上強制使用相同的高度。

Winner: Grid

優勝者:網格

CodePen Flexbox, CodePen Grid

CodePen Flexbox , CodePen網格

4垂直和水平居中交替顯示文本和圖像 (4 Alternating text and images vertically and horizontally centered)

In this example, we have text with CTA buttons accompanied by an image on the other side. Both components should be vertically centered, as their size can vary.

在此示例中,我們帶有帶有CTA按鈕的文本,另一側帶有圖像。 兩個組件都應垂直居中,因為它們的大小可以變化。

This is a piece of cake for Flexbox. Every row is an article element split into two wrapping containers, .img and .content. They are required for even size distribution (flex-basis: 50%).

對于Flexbox來說,這是小菜一碟。 每行都是一個article元素,分為兩個包裝容器.img.content 。 它們對于均勻的尺寸分布是必需的( flex-basis: 50% )。

Vertical centering of the inner content is defined by align-items: center.

內部內容的垂直居中由align-items: center定義。

The alternation is achieved by reversing the direction of Flexbox by flex-direction: row-reverse on every odd article.

交替是通過將flexbox的方向通過flex-direction: row-reverse在每個奇數文章上按flex-direction: row-reverse來實現的。

Grid handles this use-case nicely too. We don't need to define one giant grid, but rather one for each article.

也很好地處理了這個用例。 我們不需要定義一個巨型網格,而是為每篇article定義一個。

It defines equally wide columns that are vertically centered using align-items: center.

它定義了使用align-items: center垂直居中的等寬列。

The alternation is defined on the cell-level by switched values for grid-column.

交替是在單元格級別上通過grid-column切換值定義的。

Winner: tie

優勝者:并列

CodePen Flexbox, CodePen Grid

CodePen Flexbox , CodePen網格

5個帶菜單的水平接頭 (5 Horizontal header with menu)

To achieve this design using Flexbox, both sides of the header need to be represented by a single element.

為了使用Flexbox實現此設計,標頭的兩側都需要由單個元素表示。

The logo and company name form one anchor on the left, and the menu is a single nav element on the right. Flexbox positions them with justify-content: space-between.

徽標和公司名稱在左側形成一個anchor ,菜單在右側是單個nav元素。 Flexbox將它們放置在justify-content: space-between

With Grid, we need two columns - one for the logo and the other for the menu. The menu is another grid that distributes the size of columns evenly using grid-template-columns: repeat(4, minmax(0, 1fr)).

對于Grid,我們需要兩列-一列用于徽標,另一列用于菜單。 菜單是另一個網格,它使用grid-template-columns: repeat(4, minmax(0, 1fr)) columns均勻地分配列的大小grid-template-columns: repeat(4, minmax(0, 1fr))

The problem here is that if we want to add another item to the menu, we also need to adjust the CSS.

這里的問題是,如果我們想在菜單中添加另一個項目,我們還需要調整CSS。

Winner: Flexbox

獲獎者:Flexbox

CodePen Flexbox, CodePen Grid

CodePen Flexbox , CodePen網格

最終獲勝者是... (And the winner is...)

The final score is 5:2 in favor of Flexbox, but this does not mean that it becomes the ultimate CSS winner. There are situations when you need to use one or the other, sometimes even both together, to achieve what you need.

最終得分是Flexbox的5:2,但這并不意味著它成為最終CSS贏家。 在某些情況下,您需要使用一個或另一個(有時甚至同時使用)來實現所需的功能。

If you need flexible and conditional positioning, use Flexbox. If you want to create listings or similar structures that require equally sized elements or have a table form, use Grid.

如果需要靈活的條件定位,請使用Flexbox。 如果要創建需要相等大小的元素或具有表格形式的清單或類似結構,請使用網格。

As a front-end developer, you won't get away with not knowing both.

作為前端開發人員,您將不會一無所知。

Reference guide Flexbox, Reference guide Grid

參考指南Flexbox , 參考指南Grid

P.S. If I missed a layout you use on a daily basis, let me know on Twitter and I'll prepare a sequel :-)

PS:如果我錯過了您每天使用的版式,請在Twitter上告訴我,我將準備續集:-)

翻譯自: https://www.freecodecamp.org/news/flexbox-vs-grid-how-to-build-the-most-common-html-layouts/

flexbox布局

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

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

相關文章

789. 逃脫阻礙者

789. 逃脫阻礙者 你在進行一個簡化版的吃豆人游戲。你從 [0, 0] 點開始出發,你的目的地是 target [xtarget, ytarget] 。地圖上有一些阻礙者,以數組 ghosts 給出,第 i 個阻礙者從 ghosts[i] [xi, yi] 出發。所有輸入均為 整數坐標 。 每一…

計算機視覺-自定義對象檢測器

1、模板匹配 運行指令:python template_matching.py --source 3.jpg --template 2.jpg import argparse import cv2ap argparse.ArgumentParser() ap.add_argument("-s", "--source", requiredTrue, help"Path to the source image"…

Java 微信公眾號導出所有粉絲(openId)

由于公眾號換了公司主體,需要做遷移,玩家的openId數據需要做處理。 (我是按我要的json格式,將粉絲導成了1萬條數據的一個json文件) 文件格式: {"info":[{"openId":"ogVous494ltuNmO4zHb1seHeGLSk"}…

javascript閉包_JavaScript閉包教程–帶有JS閉包示例代碼

javascript閉包Closures – many of you JavaScript devs have probably heard this term before. When I started my journey with JavaScript, I encountered closures often. And I think theyre one of the most important and interesting concepts in JavaScript. 閉包–…

1646. 獲取生成數組中的最大值

1646. 獲取生成數組中的最大值 給你一個整數 n 。按下述規則生成一個長度為 n 1 的數組 nums &#xff1a; nums[0] 0 nums[1] 1 當 2 < 2 * i < n 時&#xff0c;nums[2 * i] nums[i] 當 2 < 2 * i 1 < n 時&#xff0c;nums[2 * i 1] nums[i] nums[i …

docker保存日志文件到本地

其實很簡單 docker logs 你需要添加的額外參數 容器id >文件名稱 然后查看這個文件就可以了&#xff0c;也可以通過ftp協議下載到本地

防反射JavaScript –如何讓您的JS等待

Debounce methods do not execute when invoked. Instead, they wait for a predetermined time before executing. If the same method is called again, the previous is cancelled and the timer restarts.防抖動方法在調用時不執行。 而是&#xff0c;它們在執行之前等待預…

21天學通Visual.Basic pdf

下載地址&#xff1a;網盤下載《21天學通Visual Basic(第2版)》是Visual Basic 6.0的入門教程&#xff0c;主要針對沒有程序設計基礎的讀者&#xff0c;詳細介紹了Visual Basic 6.0的基本概念和編程技術。書中通過大量的范例及綜合練習來介紹Visual Basic 6.0的基本概念、語言特…

297. 二叉樹的序列化與反序列化

297. 二叉樹的序列化與反序列化 序列化是將一個數據結構或者對象轉換為連續的比特位的操作&#xff0c;進而可以將轉換后的數據存儲在一個文件或者內存中&#xff0c;同時也可以通過網絡傳輸到另一個計算機環境&#xff0c;采取相反方式重構得到原數據。 請設計一個算法來實現…

交互式圖表_如何構建羅馬數字轉換器和交互式羅馬數字圖表

交互式圖表The Roman numerals are no longer an essential part of our daily lives. But we do use them when designing monuments, clocks, and even for sporting events.羅馬數字不再是我們日常生活中必不可少的部分。 但是我們在設計紀念碑&#xff0c;鐘表甚至體育賽事…

Python 08 面向對象

Python 面向對象 1、編程范式 2、面向對象特性 3、屬性、方法 4、三大特性 5、高級方法 6、類的特殊成員方法 7、反射 8、異常處理 9、單例模式 一、編程范式 編程&#xff1a;程序員用特定的語法數據結構算法組成的代碼來告訴計算機如何執行任務的過程 &#xff0c; 實現一個…

eclipse手動添加SVN插件

https://www.cnblogs.com/hcl1991/p/5888461.html 1.手動下載svn插件&#xff08;百度SVNsite-1.8.18&#xff09; 2.將下載好的SVNsite-1.8.18.zip 解壓 3.在eclipse安裝目錄的plugins新建SVN文件夾 4.將SVNsite-1.8.18解壓包下的features和plugins拷貝到新建的SVN文件夾下 5.…

440. 字典序的第K小數字

440. 字典序的第K小數字 給定整數 n 和 k&#xff0c;找到 1 到 n 中字典序第 k 小的數字。 注意&#xff1a;1 ≤ k ≤ n ≤ 109。 示例 : 輸入: n: 13 k: 2 輸出: 10 解釋: 字典序的排列是 [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]&#xff0c;所以第二小的數字是…

微信小程序 設置背景占滿整個頁面

微信小程序,默認的根節點是<page></page>&#xff0c;所有內容都包裹在這個標簽里&#xff0c;如何讓頁面的背景占滿整個屏幕&#xff1f;&#xff1f; <page><view class"bg">....</view> </page> .bg {background-image:ur…

udemy下載課程無法播放_最好的Udemy Web開發課程+熱門免費課程

udemy下載課程無法播放Heres a list of some of the most popular web development courses on Udemy:以下是Udemy上一些最受歡迎的Web開發課程的列表&#xff1a; Best General Web Development Courses on Udemy:關于Udemy的最佳常規Web開發課程&#xff1a; The Complete …

滲透測試初學者_滲透測試許可證:面向初學者的道德黑客課程

滲透測試初學者A penetration test is an authorized cyberattack on a computer system, performed to evaluate the security of the system. 滲透測試是對計算機系統的授權網絡攻擊&#xff0c;旨在評估系統的安全性。 Weve released a full pentesting course on the free…

Codeforces 915 E Physical Education Lessons

題目描述 This year Alex has finished school, and now he is a first-year student of Berland State University. For him it was a total surprise that even though he studies programming, he still has to attend physical education lessons. The end of the term is …

JDK 11 還有一個處于計劃階段的 JEP:讓其支持 TLS 1.3

開發四年只會寫業務代碼&#xff0c;分布式高并發都不會還做程序員&#xff1f; >>> JDK 11 最近有什么消息&#xff1f;我們不妨來看一下它的進展情況&#xff0c;包括最新的 JEP 提案。Java 的新版本發布計劃意味著總會有一款新的 JDK 即將推出。根據他們的計劃&a…

498. 對角線遍歷

498. 對角線遍歷 給定一個含有 M x N 個元素的矩陣&#xff08;M 行&#xff0c;N 列&#xff09;&#xff0c;請以對角線遍歷的順序返回這個矩陣中的所有元素&#xff0c;對角線遍歷如下圖所示。 示例: 輸入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ] 輸出: [1,2,4,7,5…

軟件測試測試用例編寫_不要先編寫所有軟件測試-只需編寫一個

軟件測試測試用例編寫Test Driven Development (TDD) is sometimes described as “writing tests first”. The TDD mantra states that we should not write code before we have written automated tests that exercise that code. Writing code first is considered subopt…