css flexbox模型_Flexbox教程:了解如何使用CSS Flexbox編寫響應式導航欄

css flexbox模型

In this article, I’ll explain how to create a navbar which adapts to various screen sizes using Flexbox along with media queries.

在本文中,我將解釋如何使用Flexbox和媒體查詢來創建適應各種屏幕尺寸的導航欄。

This tutorial can also be found as an interactive screencast in my free Flexbox course at Scrimba.

也可以在我在Scrimba的免費Flexbox課程中以交互式屏幕錄像的形式找到本教程。

To read more about the course, check out this article.

要了解更多有關課程,看看這個文章。

設置 (The setup)

Let’s begin with the markup for a very simple navbar:

讓我們從一個非常簡單的導航欄的標記開始:

<nav>  <ul class="container">  <li>Home</li>  <li>Profile</li>  <li class="search">  <input type="text" class="search-input" placeholder="Search">  </li>  <li>Logout</li>  </ul>  
</nav>

The <ul> element is our flex container and the <li> elements are our flex items. To turn it into a Flexbox layout we’ll do:

<ul>元素是我們的flex容器,而<li>元素是我們的flex項。 要將其轉換為Flexbox布局,我們將執行以下操作:

.container {  display: flex;  
}

Which will result in the following layout:

這將導致以下布局:

I’ve added some styling, but that has nothing to do with Flexbox.

我添加了一些樣式,但這與Flexbox無關。

As you can see, we have a bit of extra space on the right-hand side. This is because Flexbox lays out the items going from left to right, and each item is only as wide as its content forces it to be.

如您所見,右側有一些額外的空間。 這是因為Flexbox將從左到右排列項目,并且每個項目的寬度僅取決于其內容的大小。

Since the flex container by default is a block level element (and is wider than the four items) we get the gap at the end.

由于默認情況下,flex容器是一個塊級元素(并且比這四個元素還要寬),因此我們在最后得到了差距。

The reason the search items is wider than the others is because input fields are by default set to size="20", which different browsers and operating systems interpret in different ways.

搜索項之所以比其他項寬,是因為默認情況下將輸入字段設置為size="20" ,不同的瀏覽器和操作系統以不同的方式解釋輸入字段。

響應度1 (Responsiveness 1)

To give our navbar basic responsiveness, we’ll simply give the search item a flex value of 1.

為了使我們的導航欄具有基本的響應能力,我們只需將搜索項的flex值設為1。

.search {  flex: 1;  
}

This results in the search item expanding and shrinking with the width of the container, meaning we won’t get the extra space in the right-hand side.

這導致搜索項隨著容器的寬度而擴大和縮小,這意味著我們將不會在右側獲得額外的空間。

While it makes sense to have the search item grow while the others stay fixed, you could argue that it can become too wide compared to the others. So if you prefer to have all the items grow with the width of the container instead, you can simply give all the items a flex value of 1.

雖然使搜索項增長而其他項保持固定是有意義的,但您可以辯稱,與其他項相比,搜索項可能變得太寬。 因此,如果您希望所有項目都隨容器的寬度而增長,則只需將所有項目的flex值設為1。

.container > li {  flex: 1;  
}

Here’s how that plays out:

播放結果如下:

You can also give the items different flex values, which would make them grow with different speeds. Feel free to experiment with that in this Scrimba playground.

您還可以為項目指定不同的flex值,這將使它們以不同的速度增長。 可以在此Scrimba游樂場中隨意嘗試。

For the rest of the tutorial, we’ll continue with the first solution, where the search items are the only one with a flex value.

在本教程的其余部分中,我們將繼續第一個解決方案,其中搜索項是唯一具有flex值的項。

響應能力2 (Responsiveness 2)

Our navbar works well on wide screens. However, on more narrow ones it gets into problems, as you can see here:

我們的導航欄可在寬屏幕上正常運行。 但是,在更狹窄的范圍內會遇到問題,如您在此處看到的:

At some point, it’s not viable to have all items on the same row, as the container becomes too narrow. To solve this we’ll add a media query where we’ll split our four items into two rows. The media query will kick when the screen is 600px wide:

在某些時候,將所有項目都放在同一行是不可行的,因為容器變得太狹窄。 為了解決這個問題,我們將添加一個媒體查詢,將四個項目分為兩行。 當屏幕為600像素寬時,媒體查詢將啟動:

@media all and (max-width: 600px) {  .container {  flex-wrap: wrap;  }  .container > li {  flex-basis: 50%;  }}

First, we allow the Flexbox layout to wrap with flex-wrap. This is by default set to nowrap, so we’ll have to change it to wrap.

首先,我們允許Flexbox布局用flex-wrap 。 默認情況下,它被設置為nowrap ,因此我們必須對其進行wrap

The next thing we do it set the items’ flex-basis value to 50%. This tells Flexbox to make each item take up 50% of the available width, which results in two items per row, like this:

接下來,我們將商品的flex-basis值設置為50%。 這告訴Flexbox使每個項目占用可用寬度的50%,這導致每行有兩個項目,如下所示:

Note: I’ve also centred the placeholder text in the search input field.

注意:我還將占位符文本放在搜索輸入字段的中心。

Now we have two different states. However, this layout still doesn’t work on very small screens, like mobile screens in portrait mode.

現在我們有兩個不同的狀態。 但是,此布局仍然無法在非常小的屏幕上使用,例如縱向模式下的移動屏幕。

If we continue shrinking the screen, it’ll end up like the image below.

如果我們繼續縮小屏幕,它將最終像下圖所示。

What’s happened here is that the second row can’t fit two items anymore.

這里發生的是第二行不能再容納兩個項目。

The logout and the search items are simply too wide, as you can’t shrink them down to below their minimum width, which is the width they need in order to fill the content inside of them.

注銷和搜索項太寬了,因為您不能將它們縮小到其最小寬度以下,這是它們填充內部內容所需的寬度。

The home and profile items are still able to appear on the same row though, so Flexbox will allow them to do so. This isn’t optimal, as we want all of our rows to behave in the same way.

盡管首頁和個人資料項目仍然可以顯示在同一行中,所以Flexbox允許它們這樣做。 這不是最佳選擇,因為我們希望所有行的行為都相同。

響應能力3 (Responsiveness 3)

So as soon as one of the rows can’t fit two items in the width, we want none of the rows to have two items in the width. In other words, on very small screens we’ll actually make navbar vertical. We’ll stack the items on top of each other.

因此,只要其中一行不能容納兩個寬度的項目,我們就不希望任何行具有兩個寬度的項目。 換句話說,在很小的屏幕上,我們實際上會將導航欄設置為垂直。 我們將各個項目堆疊在一起。

To achieve this we simply need to change our 50% width to 100%, so that each row only fits a single item. We’ll add this breakpoint at 400px.

為此,我們只需要將50%的寬度更改為100%,以便每行僅適合一個項目。 我們將在400px處添加此斷點。

@media all and (max-width: 400px) {  .container > li {  flex-basis: 100%;  }  .search {  order: 1;  }  
}

In addition to this, I’d like to place the search item at the bottom, which is why I’m also targeting the search and give it an order value of 1.

除此之外,我想將搜索項放在底部,這就是為什么我也以搜索為目標并為其賦予order值1的原因。

This results in the following:

結果如下:

The reason order: 1; results in the search item being placed at the bottom are because flex items by default have a value of zero, and whatever item has a higher value than that will be placed after the others.

原因order: 1; 之所以將搜索項放置在底部,是因為彈性項默認情況下的值為零,而任何具有比其他項更高的值。

To see how it all plays out, here’s the gif from the top of the article:

要查看其效果如何,請參見文章頂部的gif:

Congrats! You now know how to create a fully responsive navbar using Flexbox and media queries.

恭喜! 現在,您知道如何使用Flexbox和媒體查詢創建完全響應的導航欄。

If you’re interested in learning more about Flexbox, I’d recommend you to check out my free course at Scrimba.

如果您有興趣了解有關Flexbox的更多信息,建議您閱讀Scrimba的免費課程。



Thanks for reading! My name is Per Borgen, I'm the co-founder of Scrimba – the easiest way to learn to code. You should check out our responsive web design bootcamp if want to learn to build modern website on a professional level.

謝謝閱讀! 我叫Per Borgen,我是Scrimba的共同創始人–學習編碼的最簡單方法。 如果要學習以專業水平構建現代網站,則應查看我們的響應式Web設計新手訓練營 。

翻譯自: https://www.freecodecamp.org/news/how-to-create-a-fully-responsive-navbar-with-flexbox-a4435d175dd3/

css flexbox模型

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

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

相關文章

oracle數字類型ef映射,Entity Framework 學習中級篇5—使EF支持Oracle9i - ♂風車車.Net - 博客園...

從Code MSDN上下載下來的EFOracleProvider不支持Oracle9i.但是,目前我所使用的還是Oracle9i。為此,對EFOracleProvider修改了以下&#xff0c;以便使其支持Oracle9i.下面說說具體修改地方.(紅色部分為添加或修改的代碼部分)一&#xff0c;修改EFOracleProvider1,修改EFOraclePr…

Oracle 數據庫之最:你見過最高的 SQL Version 是多少?

Oracle數據庫中執行的SQL&#xff0c;很多時候會因為種種原因產生多個不同的執行版本&#xff0c;一個游標的版本過多很容易引起數據庫的性能問題&#xff0c;甚至故障。 有時候一個SQL的版本數量可能多達數萬個&#xff0c;以下是我之前在"云和恩墨大講堂”分享過的一個案…

mybatis傳參問題總結

一、 傳入單個參數 當傳入的是單個參數時&#xff0c;方法中的參數名和sql語句中參數名一致即可 List<User> getUser(int id);<select id"getUser" parameterType"java.lang.Integer" resultType"com.lee.test.pojo.User">select *…

C 怎么讀取Cpp文件_opencv從yaml文件中讀取矩陣(c++)

PS:由于我是新手&#xff0c;因此記錄的比較羅里吧嗦&#xff0c;本文也屬于一個沒有任何技術的編程積累。在SLAM系統中&#xff0c;經常需要從配置文件中讀取參數文件&#xff0c;讀取整型&#xff0c;浮點型都是比較常見的操作&#xff0c;在讀取矩陣卡了一下&#xff0c;記錄…

oracle中的判斷大小,sql語句判斷大小

如何用sql語句查看某個數據庫中的表的大小--讀取庫中的所有表名select name from sysobjects where xtypeu--讀取指定表的所有列名select name from syscolumns where id(select max(id) from sysobjects where xtypeu and name表名)獲取數據庫表名和字段sqlserver中各個系統表…

超越Android:探索Kotlin的應用領域

by Adam Arold亞當阿羅德(Adam Arold) 超越Android&#xff1a;探索Kotlin的應用領域 (Going beyond Android: exploring Kotlin’s areas of application) If you have written something in Kotlin, chances are that you wrote it for Android. Kotlin, however, has other…

3.SFB標準版前端安裝

SFB服務器準備部分&#xff1a;1.修改服務器名稱&#xff0c;sfb加入域&#xff0c;用域管理員賬戶登錄2.配置服務器IP地址&#xff0c;DNS3.安裝Windows組件Add-WindowsFeature NET-Framework-Core, RSAT-ADDS, Windows-Identity-Foundation, Web-Server, Web-Static-Content,…

向spark standalone集群提交任務

文檔鏈接 #切換到spark安裝目錄,執行下面一條命令,192.168.0.10是master的ip, examples/src/main/python/pi.py 是python 文件的路徑 ./bin/spark-submit --master spark://192.168.0.106:7077 examples/src/main/python/pi.py任務已經執行完畢,耗時10秒 轉載于:https://www.c…

SQLite學習手冊

一、聚合函數&#xff1a; SQLite中支持的聚合函數在很多其他的關系型數據庫中也同樣支持&#xff0c;因此我們這里將只是給出每個聚集函數的簡要說明&#xff0c;而不在給出更多的示例了。這里還需要進一步說明的是&#xff0c;對于所有聚合函數而言&#xff0c;distinct關鍵字…

oracle全局索引 效率,關于插入,全局索引和局部索引的情況,那種效率高

分區表上的索引表可以按range&#xff0c;hash&#xff0c;list分區&#xff0c;表分區后&#xff0c;其上的索引和普通表上的索引有所不同&#xff0c;oracle對于分區表上的索引分為2類&#xff0c;即局部索引和全局索引&#xff0c;下面分別對這2種索引的特點和局限性做個總結…

python excelwriter保存路徑_Python和Excel 終于可以互通了!!

點擊“開發者技術前線”&#xff0c;選擇“星標&#x1f51d;”在看|星標|留言, 真愛作者&#xff1a;小天真_5eed 鏈接&#xff1a;https://www.jianshu.com/p/6ecf414f3372今天為大家分享一篇使用python將大量數據導出到Excel中的技巧心得&#xff0c;可以讓Python和Excel…

nodejs 調用微服務器_無服務器NodeJS:構建下一個微服務的快速,廉價方法

nodejs 調用微服務器by Filipe Tavares由Filipe Tavares 無服務器NodeJS&#xff1a;構建下一個微服務的快速&#xff0c;廉價方法 (Serverless NodeJS: the fast, inexpensive way to build your next microservice) I love Node.js. I’ve re-discovered Javascript through…

(藍橋杯)2018JAVA B組 日志分析

日志統計 小明維護著一個程序員論壇。現在他收集了一份"點贊"日志&#xff0c;日志共有N行。其中每一行的格式是&#xff1a; ts id 表示在ts時刻編號id的帖子收到一個"贊"。 現在小明想統計有哪些帖子曾經是"熱帖"。如果一個帖子曾在任意一個長…

MySQL 導出數據

2019獨角獸企業重金招聘Python工程師標準>>> 1、導出整個數據庫 mysqldump -u 用戶名 -p 數據庫名 > 存放位置比如&#xff1a; mysqldump -u root -p project > c:/a.sql 2.導出一個表的結構&#xff0c;并且帶表中的數據 mysqldump -u 用戶名 -p 數據庫名 …

哎 心好累

雨天后的周六還要上班&#xff0c;避開了所有上班的交通方式&#xff0c;沒有比這更需要車的時候&#xff0c;哎&#xff0c;感覺心好累 好好努力買車吧&#xff0c;覺得再這樣只能是徒勞了。 困得和傻逼一樣 單片機又要換型號&#xff0c;后面一堆事兒&#xff0c;哎 再見-dsp…

Abbey加入了FreeCodeCamp團隊,擔任編輯

by Quincy Larson昆西拉爾森(Quincy Larson) Abbey加入了FreeCodeCamp團隊&#xff0c;擔任編輯 (Abbey is joining the freeCodeCamp team as an editor) Every article you’ve read here on the freeCodeCamp community Medium publication has been edited with care by a…

單片機STM8S測量電壓電路_單片機電路設計中的10個難點

單片機是嵌入式系統的核心元件&#xff0c;使用單片機的電路要復雜得多&#xff0c;但在更改和添加新功能時&#xff0c;帶有單片機的電路更加容易實現&#xff0c;這也正是電器設備使用單片機的原因。那么在單片機電路的設計中需要注意的難點有哪些&#xff1f;嵌入式ARM開發 …

oracle ebs 數據源,Oracle EBS環境下查找數據源(Form篇)

關于在Oracle EBS環境下如何查找數據源的文章幾年前就已經開始整理&#xff0c;但是其中關于OAF方面的一直沒有整理&#xff0c;導致這份文檔一直殘缺不全&#xff0c;有很多次同事都向我索要相關文檔都未能如愿以償&#xff0c;新的一屆培訓工作再次啟動&#xff0c;為了自己也…

net-speeder

有的同學反映自己的***速度慢&#xff0c;丟包率高。這其實和你的網絡服務提供商有關。據我所知一部分上海電信的同學就有這種問題。那么碰到了坑爹的網絡服務商&#xff0c;我們應該怎么辦呢&#xff1f; duangduang~~~~~~有請今天的主角&#xff1a;Net-Speeder登場&#xff…

linux 實用指令

通過init 來制定/切換不同的運行指令 查看linux 系統下&#xff0c;電腦的運行級別 vim /etc/inittab 如何找回丟失的root密碼&#xff1f; 進入到單用戶模式&#xff0c;然后修改root密碼 進入到單用戶模式&#xff0c;root不需要密碼也可以登錄 如果開機就是init 0 辦法&…