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模型

css flexbox模型_Flexbox教程:了解如何使用CSS Flexbox编写响应式导航栏相关推荐

  1. Angular最新教程-第六节编写响应式导航栏

    这节课我们讲解如何使用bootstrap 4 编写响应式布局. 参考图我们还是参照Angular中文社区http://www.angularjs.cn/ 图中标注红色的部分,我自己不是很喜欢,所以做了 ...

  2. 【前端实例代码】如何使用 HTML 和 CSS 快速创建一个响应式导航栏

    效果图: 大屏: 小屏: bilibili在线视频演示地址: [前端实例代码]如何使用 HTML 和 CSS 快速创建一个响应式导航栏nav_哔哩哔哩_bilibili完整代码在这里:https:// ...

  3. css --- 应用媒介查询制作响应式导航栏

    以上导航会自动适应各个尺寸的屏幕 代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...

  4. SAP UI5 应用开发教程之三十三 - SAP UI5 应用的响应式布局特性(Responsiveness)试读版

    一套适合 SAP UI5 初学者循序渐进的学习教程 教程目录 SAP UI5 本地开发环境的搭建 SAP UI5 应用开发教程之一:Hello World SAP UI5 应用开发教程之二:SAP U ...

  5. html5响应式导航条,10个响应式设计的导航菜单源码-附教程

    10个响应式设计的导航菜单源码-附教程 Sponsor 在2013年里,响应式Web设计将会开始普及,我们应该学习这些新技术,尤其作为网页设计师和前端开发人员,学习CSS3样式表和HTML5是必不可少 ...

  6. css flexbox模型_Flexbox和CSS Grid之间的主要区别

    css flexbox模型 by Shaira Williams 由莎拉·威廉姆斯(Shaira Williams) Flexbox和CSS Grid之间的主要区别 (The main differe ...

  7. html5 css 响应式_在HTML5 / CSS3中编写响应式简历

    本文是我们的" Web响应式设计系列"的一部分,该系列由工具,资源和教程组成,可帮助您为所有平台的用户创建网站. 单击此处查看同一系列的更多文章. 业务部分的几乎每个人都在某个时间 ...

  8. Bulma - 免费开源的纯 CSS 前端 UI 框架,专注于构建移动优先的响应式 web 界面

    简单易用的 CSS 框架,虽然只有一个 CSS 文件,但功能很强大,在国外很受开发者欢迎,推荐给大家. 关于 Bulma CSS 框架 Bulma 是一个简单.很容易自定义的 CSS UI 框架,提供 ...

  9. css 背景效果_前端教程 :20个CSS的常用套路附demo的效果实现与源码

    前言 本文是笔者写CSS时常用的套路.不论效果再怎么华丽,万变不离其宗. 1.交错动画 有时候,我们需要给多个元素添加同一个动画,播放后,不难发现它们会一起运动,一起结束,这样就会显得很平淡无奇.那么 ...

最新文章

  1. WIKI与BLOG殊途同归(转)
  2. 立体匹配成像算法BM,SGBM,GC,SAD一览
  3. ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory
  4. 重磅!66 个机器学习硬核资源,请务必收藏!
  5. angular HttpClient 配置
  6. 关于ssl免费证书设置
  7. JAVA ReentrantLock 分析
  8. Java中的多重继承与组合vs继承
  9. element-ui的表单校验;el-form表单校验;el-form表单自定义校验;手机号校验;车牌号校验;车牌号正则校验;
  10. 工作2年跳槽阿里,面试官会问哪些?(免费领取Java面试题)
  11. 中医经典《伤寒论》-原文
  12. java 读取 解析微软Project .mpp 文件
  13. 中介者(Mediator)模式实例
  14. DBpedia Introduction
  15. 联想IdeaPad Z460在Win10环境下BIOS刷白名单
  16. converting character set: invalid arguements
  17. Unity之webGL问题汇总
  18. 彻底解决jdbc数据库连接超时重试-communication link failure的正确姿势
  19. pycharm运行程序时在Python console窗口中运行
  20. 如果记录数据库表修改记录

热门文章

  1. 我了解到的面试的一些小内幕!顺利通过阿里Android岗面试
  2. html5跨平台桌面打包,Html5到跨平台app应用
  3. springboot集成redis使用redis作为session报错ClassNotFoundException类RememberMeServices
  4. Codeforces 408D Long Path (DP)
  5. fiddler抓包1-抓小程序https包
  6. 从简单的信道预计说起
  7. 在Hadoop集群上,搭建HBase集群
  8. hadoop2.2.0 core-site.xml--security properties
  9. void Update ( ) 更新 void FixedUpdate ( )
  10. asp.net的MessageBox