css flexbox模型_5分钟内学习CSS Flexbox-初学者教程
css flexbox模型
快速介绍流行的布局模块 (A quick introduction to the popular layout module)
In this post, you’ll learn the basics of CSS Flexbox, which has become a must-have skill for web developers and designers in the last couple of years.
在本文中,您将学习CSS Flexbox的基础知识,在过去的两年中,它已成为Web开发人员和设计人员的必备技能。
We’ll be using a navbar as an example, as this is a very typical use case for Flexbox. This will introduce you to its most-used properties of the module while leaving out the ones which aren’t as critical.
我们将以导航栏为例,因为这是Flexbox的一个非常典型的用例。 这将向您介绍其最常用的模块属性,而忽略那些不太重要的属性。
I’ve also created a free 12-part course on Flexbox. Check it out here if you’re interested!
我还在Flexbox上创建了免费的12部分课程。 如果您有兴趣, 请在这里查看 !
Now let’s get started!
现在开始吧!
您的第一个Flexbox布局 (Your first Flexbox layout)
The two main components of a Flexbox layout are the container and the items.
Flexbox布局的两个主要组件是容器和项目 。
Here’s the HTML for our example, which contains a container with three items:
这是我们示例HTML,其中包含一个包含三个项目的容器:
<nav class="container"> <div>Home</div> <div>Search</div> <div>Logout</div>
</nav>
Before we turn this into a Flexbox layout, the elements will be stacked on top of each other like this:
在将其转换为Flexbox布局之前,元素将像这样相互堆叠:
I’ve added a little bit of styling, but that has nothing to do with Flexbox.
我添加了一些样式,但这与Flexbox无关。
To turn this into a Flexbox layout, we’ll simply give the container the following CSS property:
要将其转换为Flexbox布局,我们只需为容器提供以下CSS属性:
.container { display: flex;
}
This will automatically position the items nicely along the horizontal axis.
这将自动沿水平轴很好地定位项目。
If you want to check out the actual code, you can head over to this Scrimba playground.
如果您想查看实际代码,可以前往此Scrimba游乐场。
Now let’s shuffle these items around a bit.
现在,让我们对这些项目进行一些调整。
对齐内容并对齐项目 (Justify content and Align items)
Justify-content and align-items are two CSS properties which help us distribute the items in the container. They control how the items are positioned along the main axis and cross axis.
Justify-content和align-items是两个CSS属性,可帮助我们在容器中分发项目。 它们控制物品沿主轴和横轴的放置方式。
In our case (but not always) the main axis is horizontal and the cross axis is vertical:
在我们的情况下(但并非总是如此),主轴是水平的,而交叉轴是垂直的:
In this article, we’ll only look at justify-content
, as I’ve found to be using this one much more than align-items
. However, in my Flexbox course, I explain both properties in detail.
在本文中,我们将仅讨论justify-content
,因为我发现它比align-items
使用得多。 但是,在我的Flexbox课程中 ,我将详细解释这两个属性。
Let’s center all the items along the main axis by using justify-content
:
让我们使用justify-content
沿主轴将所有项居中:
.container { display: flex; justify-content: center;
}
Or we can set it to space-between
, which will add space between the items, like this:
或者我们可以将其设置为space-between
,这将在项目之间添加空间,如下所示:
.container { display: flex; justify-content: space-between;
}
Here are the values you can set for justify-content:
这是您可以为justify-content:
设置的值justify-content:
flex-start (default)
flex-start( 默认 )
- flex-end柔性端
- center中央
- space-between间隔
- space-around周围空间
- space-evenly空间均匀
I’d recommend you to play around with these and see how they play out on the page. That should give you a proper understanding of the concept.
我建议您试用这些内容,并在页面上查看它们的显示方式。 那应该使您对该概念有适当的了解。
控制单个项目 (Controlling a single item)
We can also control single items. Let’s say we, for example, want to keep the first two items on the left side, but move the logout
button to the right side.
我们还可以控制单个项目 。 假设我们想将前两个项目保留在左侧,但是将logout
按钮移到右侧。
To do this we’ll use the good old technique of setting the margin to auto
.
为此,我们将使用将边距设置为auto
旧技术。
.logout { margin-left: auto;
}
If we’d want both the search
item and the logout
item to be pushed to the right-hand side, we’ll simply add the margin-left
to the search
item instead.
如果我们希望将search
项和logout
项都推到右侧,则只需在search
项中添加margin-left
即可。
.search { margin-left: auto;
}
It’ll push the search item as far to the right-hand side as possible, which again will push the log out item with it:
它将搜索项目尽可能地推到右侧,再次将注销项目推到右边:
flex属性 (The flex property)
So far, we’ve only had fixed-width items. But what if we want them to be responsive? To achieve that we have a property called flex
. It makes it a lot easier than the old way of using percentages.
到目前为止,我们只有固定宽度的项目。 但是,如果我们希望他们做出回应,该怎么办? 为此,我们有一个称为flex
的属性。 与使用百分比的旧方法相比,它要容易得多。
We’ll simply target all the items and give them a flex
value of 1
.
我们将简单地定位所有项目并将其flex
值赋予1
。
.container > div { flex: 1;
}
As you can see, it stretches the items to fill the entire container.
如您所见,它会拉伸项目以填充整个容器。
In many cases, you’d probably want one of the items to take up the extra width, and thereby only set one of them to have flexible width. We can, for example, make the search
item take up all the extra space:
在许多情况下,您可能希望其中一项占用额外的宽度,从而仅将其中一项设置为具有灵活的宽度。 例如,我们可以使search
项占用所有额外的空间:
.search { flex: 1;
}
Before we end this article, I want to mention that the flex property is actually a shorthand three properties: flex-grow, flex-shrink and flex-basis. However, learning those takes more than five minutes, so it’s outside of the scope of this tutorial.
在结束本文之前,我想提到一下flex属性实际上是三个属性的简写形式: flex-grow , flex-shrink和flex-basis 。 但是,学习这些内容需要五分钟以上的时间,因此超出了本教程的范围。
If you’re interested in learning them, though, I’m explaining all three properties thoroughly in my free Flexbox course.
但是,如果您有兴趣学习它们,我将在我的免费Flexbox课程中全面解释这三个属性。
Now that you’ve learned the basics you’ll definitely be ready to take my full-length course and become a Flexbox master!
现在,您已经学习了基础知识,您一定会准备参加我的全长课程并成为Flexbox的高手!
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/learn-css-flexbox-in-5-minutes-b941f0affc34/
css flexbox模型
相关文章:

「linux网络管理」OSI模型
学习linux网络管理,笔记整理,促进记忆。 OSI(开放系统互联模型)包含七层,由应用层向物理层递进,分别有不同的协议和数据处理方式。 应用层--> 表示层--> 会话层--> 传输层--> 网络层--> 数据…

微信小程序下拉刷新和上拉加载的实现
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 一: 下拉刷新 下拉刷新两个步骤就能实现。 1.在要实现下拉刷新的页面的json配置文件里面加上 "enablePullDownRefresh": true, //开启下拉刷新"backgro…

Spring整合Hibernate的步骤
为什么要整合Hibernate?1、使用Spring的IOC功能管理SessionFactory对象 LocalSessionFactoryBean2、使用Spring管理Session对象 HibernateTemplate3、使用Spring的功能实现声明式的事务管理 整合Hibernate的步骤:1、配置SessionFactory(能够…

初创企业购买企业邮箱_支持#NetNeutrality =支持设计师及其创建的初创企业
初创企业购买企业邮箱by Lukasz Lysakowski卢卡斯吕萨科夫斯基(Lukasz Lysakowski) 支持#NetNeutrality 支持设计师及其创建的初创企业 (Supporting #NetNeutrality Supporting Designers and the Startups They Create) I believe in Net Neutrality, and I wrote a brief e…

【转】Android source build/envsetup.sh学习笔记
原文网址:http://blog.csdn.net/mliubing2532/article/details/7567164 如果你只需要修改某一个模块的内容,但是却每次都要执行make, 最后等待很长时间。使用模块编译,那只需要在你所在的模块的目录或者其子目录,执行mm࿰…

微信小程序之上传附件
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 目前微信API开放上传图片,录音文件,视频文件,but 只能下载并打开附件,不能上传附件,以后会不会开放附件上传目前还不确定&…

微软在.NET官网上线.NET 架构指南频道
微软在Visual Studio 2017 正式发布的时候也上线了一个参考应用https://github.com/dotnet/eShopOnContainers , 最近微软给这个参考应用写了完善的文档,放在.NET官网的.NET架构频道https://www.microsoft.com/net/architecture。 整个.NET 架构按照4个部分展开&…

初学者css常见问题_5分钟内学习CSS Grid-初学者教程
初学者css常见问题Grid layouts are fundamental to the design of websites, and the CSS Grid module is the most powerful and easiest tool for creating it. I personally think it’s a lot better than for example Bootstrap (read why here).网格布局是网站设计的基础…

HBase保存的各个字段意义解释
// Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ // nutch2.2.1集成HBase0.94.25, 可以查询nutch的conf文件中的gora-hbase-mapping.xml查看原文件 <gora-orm><table name"webpage"><family name"p" ma…

AJAX基础篇
微信小程序开发交流qq群 581478349 承接微信小程序开发。扫码加微信。 正文: 整理一下AJAX相关的知识,帮助自己复习的同时希望还能够帮助到新加入前端阵营的小伙伴们。 1.AJAX是什么? AJAX 异步的JavaScript和XML 2.AJAX的作用&…

在运筹学中什么样的解决方案是最优的
问题: 1.在运筹学中什么样的解决方案是最优的 2.线性代数解决的是什么问题? 3.运筹学与线性代数的关系? 4.如何使用matlab 解决运筹学中的问题? 转载于:https://www.cnblogs.com/lwy1103/p/6676373.html

npm构建脚本_NPM脚本简介
npm构建脚本by Mohammed Ajmal Siddiqui由Mohammed Ajmal Siddiqui NPM脚本简介 (Introduction to NPM Scripts) NPM scripts are among my favorite features of NPM. They are simple. They reduce the need for tools. Hence they reduce the number of configuration file…

SQL 中循环、for循环、游标
我们使用SQL语句处理数据时,可能会碰到一些需要循环遍历某个表并对其进行相应的操作(添加、修改、删除),这时我们就需要用到咱们在编程中常常用的for或foreach,但是在SQL中写循环往往显得那么吃力,翻遍网上…

Unix Linux大学教程(三):过滤器、正则表达式、vi
第16章 过滤器:简介和基本操作 删除数据列用colrm:colrm [startcol [endcol]] 如果没有endcol则删除从startcol至行末尾所有的列。 第17章 过滤器:比较和抽取 比较任意两个文件:cmp file1 file2 显示不同字节数及所在行。 比…

微信小程序获取多选框选中值和选中值对应的id
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 官方文档中只有获取多选框的值的方法,但是我需要获取选中的值同时还要获取选中值对应的id,但是又不能操作DOM获取,相信和我有同样需求的伙伴们都会为此发愁࿰…

调试代码遗留_陷入遗留代码地狱吗? 这里有一些想法可以帮助您处理情况
调试代码遗留by Felipe Lopes通过Felipe Lopes 陷入遗留代码地狱吗? 这里有一些想法可以帮助您处理情况 (Stuck in legacy code hell? Here are some few thoughts to help you manage the situation) I’m gonna tell you a little story about how I ended up i…

求二维数组最大子数组
结对队友:胡康臻、杨寒寒 1、设计思想: 首先定义产生二维数组,定义可输入二维数组行和列,各位数随机产生; 然后进行最大子数组的求和比较,从每行的第一个数为子数组的起点开始进行不同的子数组遍历比较&…

UVa 11021 (概率 递推) Tribles
Tribble是麻球? 因为事件都是互相独立的,所以只考虑一只麻球。 设f(i)表示一只麻球i天后它以及后代全部死亡的概率,根据全概率公式: f(i) P0 P1 * f(i-1) P2 * f(i-1)2 ... Pn * f(n)n 每个麻球死亡是独立的,所以…

积分页面布局记录
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 、 // pages/user/my_integral/record/record.js var app getApp(); var util require("../../../utils/util.js") Page({data: {url_data: ,username: ,usertop: ,tab: 1…

uber_Uber是如何制成的
uberby Dmytro Brovkin由Dmytro Brovkin Uber是如何制成的 (How Uber was made) Uber has transformed the world. Indeed, its inconceivable to think of a world without the convenience of the innovative ride sharing service. Tracing its origins in a market which …

HTML 标签包含规范,规避脱标流,图片和文字垂直居中对齐,
1 标签包含规范 ◆div可以包含所有的标签。 ◆p标签不能包含div h1等标签。 ◆h1可以包含p,div等标签。 ◆行内元素尽量包含行内元素,行内元素不要包含块元素。 2 规避脱标流 ◆尽量使用标准流。 ◆标准流解决不了的使用浮动。 ◆浮动解决不了…

Java Socket发送与接收HTTP消息简单实现
在上次Java Socket现实简单的HTTP服务我 们实现了简单的HTTP服务,它可以用来模拟HTTP服务,用它可以截获HTTP请求的原始码流,让我们很清楚的了解到我们向服务发的HTTP消息的结 构,对HTTP请求消息有个清晰的认识。这一节我想写了一个…

微信小程序日期相减得出天数
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: // 日期结算 num_data: function (e) {var start_date new Date(this.data.start_date.replace(/-/g, "/"));var end_date new Date(this.data.end_date.replace(/-/g, &q…

es6 ... 添加属性_如何在10分钟内免费将HTTPS添加到您的网站,以及为什么您现在不止需要这样做......
es6 ... 添加属性by Ayo Isaiah通过Ayo Isaiah 如何在10分钟内免费将HTTPS添加到您的网站,以及为什么现在比以往更需要这样做 (How to add HTTPS to your website for free in 10 minutes, and why you need to do this now more than ever) Last week, Google ann…

程序员跳槽全攻略——读书笔记
有同学说,我技术很好啊,又会机器学习又会编译原理,凭什么那些写Javascript的薪水比我高一倍? 谁让你在一家建站公司上班呢。对一家做网站的公司而言,机器学习和编译原理是不能为它带来收益的,而Javascript写…
[转] Gradle: 此时不应有 Androidandroid-studiosdk oolslib\find_java.exe。解决方法
上述问题主要是java路径的问题,这里主要给出解决方案,至于为什么这么解决的,大家可以学学bat语言。想问的可以留言我。 dx.bat 根据安装目录,我的是D:\Program Files (x86)\Android\android-studio\sdk\build-tools\android-4.2.2…

微信小程序和微信小程序之间的跳转和传参示例代码附讲解
微信小程序开发交流qq群 173683895 承接微信小程序开发。扫码加微信。 正文: 一:微信小程序跳转 使用限制 需要用户触发跳转 从 2.3.0 版本开始,若用户未点击小程序页面任意位置,则开发者将无法调用此接口自动跳转至其他小…

电子界卡组构建2019_2018–2019年构建现代Android应用程序的路线图
电子界卡组构建2019Kriptofolio应用程序系列—简介 (Kriptofolio app series — Introduction) Welcome to this series of blog posts where I will be creating a modern Android app. I will use the best tools and practices available in the year 2018–2019. I am doin…

python操作mysql数据库实现增删改查
Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口。 Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: GadFlymSQLMySQLPostgreSQLMicrosoft SQL Server 2000InformixInterbaseOr…

弹性布局,自动按比例居中
1. 让行类盒子及盒子的元素 自动按比例居中效果图 html <view classaaa><view classbbb>aaaaaaaaa</view><view classbbb>aaaaaaaaa</view><view classbbb>bb</view><view classbbb>aaaaaaaaa</view> </view> c…