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.mzph.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 办法&…