扶梯正确使用

by Kevin Kononenko

凯文·科诺年科(Kevin Kononenko)

乘坐自动扶梯解释CSS浮动 (CSS Floats Explained By Riding An Escalator)

如果您曾经跳过自动扶梯,那么您可以快速了解浮动。 (If you have ever jumped on an escalator, then you can quickly understand floats.)

Your <div> is almost perfect. You decide to introduce some floats to fix the relationship between a few elements.

您的<div>几乎是完美的。 您决定引入一些浮点数来修复一些元素之间的关系。

The next thing you know, your newly floated elements jump out of your carefully chosen order, and stick to the side of your div like a magnet. The phrase “unintended behavior” comes to mind.

接下来您会知道,新浮动的元素会跳出您精心选择的顺序,并像磁铁一样粘在div的一侧。 我想到了“意想不到的行为”。

I’ve personally spent hours trying to fully understand floats. I’d read an article and say, “Oh, this makes sense!” Then I’d return to my code, try it out and… fail. Back to the drawing board.

我个人花费了数小时试图完全理解花车。 我读了一篇文章说:“哦,这很合理!” 然后,我将返回代码,尝试一下,然后…失败。 回到绘图板。

I’m going to do my best to spare you this fate.

我将竭尽全力为您节省命运。

Floats create alternate flows. This is the hardest part to understand. And once you introduce them, you then need to write your code so that it accounts for up to three flows: normal, left and right. This is a whole new set of rules, as opposed to manipulating the width of elements or their positioning.

浮动产生交替的流动 。 这是最难理解的部分。 一旦介绍了它们,就需要编写代码,以便它最多说明三个流程:正常,向左和向右。 这是一组全新的规则,与操纵元素的宽度或其位置相反。

Actually, floats are pretty similar to the dynamics of riding an escalator, and I am going to show how they can be used alongside the clear property to create crystal-clear relationships within divs. This way, the next time you try to use floats in your code, you won’t encounter any surprises.

实际上,花车与乘坐自动扶梯的动力非常相似,我将展示如何将它们与clear属性一起使用,以在div中创建水晶般清晰的关系。 这样,下次您尝试在代码中使用浮点数时,就不会遇到任何意外。

要尊重通过车道 (Gotta respect the pass lane)

The default flow of elements is kind of like the picture above. Some guy is standing in the middle with his hand on the railing. He’s hogging the entire escalator. Nobody can pass him. Pretty poor escalator etiquette, really.

元素的默认流程类似于上图。 有人站在中间,他的手在栏杆上。 他正在占用整个自动扶梯。 没有人可以通过他。 确实,自动扶梯礼节很差。

He is standing behind a bunch of other people that are doing the same thing, so nobody can pass them either. There is no concept of lanes or basic human decency.

他站在一群正在做同样事情的人后面,所以没人能通过他们。 没有车道或基本的人性化的概念。

This is the scenario when you are not using floats at all.

当您根本不使用浮点数时,就是这种情况。

All right, now we’re talking! People showing some level of awareness. Love to see that.

好吧,现在我们在说话! 人们表现出一定程度的意识。 很高兴看到这一点。

We have one lane formed on the left, and another lane formed on the right. Other people can easily move around the two people that are standing still and walk up the escalator more quickly, if they want. Nobody is blocking the flow by standing in the middle.

我们在左侧形成一个车道,在右侧形成另一个车道。 如果他们愿意,其他人可以轻松地在静止不动的两个人周围移动,并更快地走上自动扶梯。 没有人站在中间来阻挡潮流。

This is the scenario when you are using floats in your div. There is a left flow and a right flow, and the elements that are not floated can easily fill the space that is not taken by the floated elements.

在div中使用浮点数时就是这种情况。 有左向流和右向流,未浮动的元素可以轻松填充未浮动元素占用的空间。

浮点数:左右 (Floats: Left and Right)

Using floats can introduce up to two new flows: left and right.

使用浮点数最多可以引入两个新流程:向左和向右。

And this allows the normal flow of elements, those without a float value, to fill in the spaces around these elements.

这样,正常的元素流(没有浮点值的元素)就可以填充这些元素周围的空间。

Floats allow you to create these new relationships between flows.

浮点数使您可以在流之间创建这些新关系。

If you had one line of people standing in the middle of the elevator, you would have limited options for new structures. But when you have a left and right column, suddenly you can specify that certain people stand on the right, other ones stay left, and another group can fill in the gaps.

如果您在电梯中间站着一排人,那么新结构的选择将非常有限。 但是,当您有左右两列时,突然可以指定某些人站在右边,其他人留在左边,而另一组人可以填补空白。

This allows you to create more readable and understandable code, because the float property also gives an indication of an element’s relationship to surrounding elements.

这使您可以创建更具可读性和可理解性的代码,因为float属性还可以指示元素与周围元素的关系。

清除财产 (The Clear Property)

There is one more wrinkle that we have not discussed yet: the clear property. “Clear” allows elements to specify where they should align in comparison to the floated elements.

我们还没有讨论过另外一种皱纹: 透明特性 。 “清除”允许元素指定与浮动元素相比应对齐的位置。

In the first picture of the “Floats” section, the two escalator riders were courteously standing on each side of the escalator. This allows others to pass them and move freely as they wish.

在“浮标”部分的第一张图片中,两个自动扶梯骑手有礼貌地站在自动扶梯的每一侧。 这使其他人可以通过他们并自由移动。

Let’s say that instead of having one floated left element and one floated right element, we instead had 3 floated left elements and 1 on the right. The three floated left elements would stack up in a line on the left if we also give them the “clear: left” property. But if they horizontally aligned with the floated right element, it could make it very difficult or even impossible for the normal flow of elements to pass:

假设我们没有一个浮动的左元素和一个浮动的右元素,而是有3个浮​​动的左元素和一个右侧的元素。 如果我们还为它们提供了“ clear:left”属性,则三个浮动的left元素将堆积在左侧的一行中。 但是,如果它们与浮动的右元素水平对齐,则可能使元素的正常流通过变得非常困难,甚至不可能:

“Clear: left” tells each person floating left that they should align themselves behind the first element that is floated left. Depending on the size of the bottom two people, it could be challenging for any normal elements to squeeze through and occupy the space on the top right. So even good escalator practices can still lead to blockages.

“清除:左”告诉每个向左浮动的人,他们应该将自己对齐到向左浮动的第一个元素的后面。 根据底部两个人的大小,任何普通元素都可能会挤过并占据右上角的空间。 因此,即使良好的自动扶梯做法也可能导致阻塞。

One of the most frequent uses of the clear property is “clear:both”. This allows you to reset the flow of elements, as opposed to continuing to maintain a right, left and normal flow. It’s kind of like that guy on the escalator who takes up the whole space because he brought his suitcase.

clear属性最常使用的一种是“ clear:both”。 与继续保持向右,向左和正常流动相反,这使您可以重置元素流。 就像自动扶梯上的那个家伙一样,因为他带来了手提箱而占据了整个空间。

With “clear:both”, it doesn’t matter where that one guy is standing in orientation to his suitcase. It doesn’t matter who is standing left or right above him. He’s still blocking the entire escalator. People who get on after him will need to start a new flow of elements, which could include any of the three flows: left, right, or normal.

有了“ clear:both”,无论一个人站在他的手提箱的方位上都没关系。 谁站在他的左上方或右上方都没关系。 他仍然挡住了整个自动扶梯。 追随他的人将需要开始新的元素流程,其中可能包括三个流程中的任何一个:左,右或正常。

He has escaped the three-flow system and reset the rules. Bad for people that want to run up the escalator? Sure. But it’s good if you want to stop anyone from passing.

他逃脱了三流系统并重设了规则。 对想要启动自动扶梯的人不利吗? 当然。 但是,如果您想阻止任何人通过,那就很好了。

Notice how this is different from the one gentleman at the beginning who stood in the middle of the escalator, behind a line of people who were doing the same. That was a one-flow system. “Clear: both” acknowledges that there may be up to three flows, and blocks the passage of any element that follows.

请注意,这与起初坐在自动扶梯中间的那一位绅士有什么不同,那一群人正在做同样的事情。 那是一个单流系统。 “清除:两者”确认最多可能有三个流,并阻止随后的任何元素通过。

If you enjoyed this post, you may also enjoy my other explanations of challenging CSS and JavaScript topics, such as positioning, Model-View-Controller, and callbacks.

如果您喜欢这篇文章,那么您可能还会喜欢我对具有挑战性CSS和JavaScript主题的其他解释 ,例如定位,Model-View-Controller和回调。

And if you think this might help other people in the same position as you, give it a “heart”!

而且,如果您认为这可以帮助与您处于同一位置的其他人,请给它一个“心脏”!

This post originally appeared on the CodeAnalogies blog.

该帖子最初出现在CodeAnalogies博客上

翻译自: https://www.freecodecamp.org/news/css-floats-explained-by-riding-an-escalator-57fa55232333/

扶梯正确使用

扶梯正确使用_乘坐自动扶梯解释CSS浮动相关推荐

  1. 肯德基圣代中间空心_建造冰淇淋圣代解释CSS位置

    肯德基圣代中间空心 by Kevin Kononenko 凯文·科诺年科(Kevin Kononenko) 建造冰淇淋圣代解释CSS位置 (CSS Positioning Explained By B ...

  2. r语言解释回归模型的假设_模型假设-解释

    r语言解释回归模型的假设 Ever heard of model assumptions? What are they? And why are they important? A model is ...

  3. 传智播客_急先锋_html、css、js_郝强勇老师

    传智播客_急先锋_html.css.js_郝强勇老师 我的GitHub地址:https://github.com/heizemingjun 我的博客园地址:http://www.cnblogs.com ...

  4. 一个人做饭简单食谱_如何通过两个简单的寿司布局食谱来理解CSS浮动

    一个人做饭简单食谱 by Anabella Spinelli 通过安娜贝拉·斯皮内利(Anabella Spinelli) 如何通过两个简单的寿司布局食谱来理解CSS浮动 (How to unders ...

  5. css浮动(float)及清除浮动的几种实用方法

    CSS浮动是现在网页布局中使用最频繁的效果之一,而浮动可以帮我们解决很多问题,那么就让我们一起来看一看如何使用浮动. 一.css浮动(float) (1)html文档流 自窗体自上而下分成一行一行,并 ...

  6. css清除浮动的几种方法_web前端学习路线分享CSS浮动-清除浮动篇

    web前端学习路线分享CSS浮动-清除浮动篇,为什么要清除浮动 这里所说的清除浮动,并不是不要浮动了,而是清除浮动与浮动之间的影响.那么到底会有什么影响呢? 1.高度塌陷 举个例子我们看一下. 我们在 ...

  7. 关于CSS浮动(float,clear)的通俗讲解(经验分享)

    很早以前就接触过CSS,但对于浮动始终非常迷惑,可能是自身理解能力差,也可能是没能遇到一篇通俗的教程. 前些天小菜终于搞懂了浮动的基本原理,迫不及待的分享给大家. 写在前面的话: 由于CSS内容比较多 ...

  8. (13) css浮动补充

    一.字围现象 与前面压盖效果结构类似,同级元素中前面的元素浮动,后面的元素不浮动,不浮动的元素内部还有一些文字,浮动的蓝盒子会压盖住黄绿盒子的一部分,但是文字内容不会被盖住,蓝色盒子中的文字会让开蓝盒 ...

  9. (图文详细)最通俗易懂的CSS 浮动float属性详解

    (图文详细)最通俗易懂的CSS 浮动float属性详解 声明:本文属于搬砖大神的文章到自己的博客上,原文地址为:https://www.cnblogs.com/iyangyuan/archive/20 ...

最新文章

  1. linux 日志 停止滚动,linux – syslog在日志轮换后停止记录
  2. 深圳湾口岸明起开通公交大巴
  3. /proc/meminfo 学习
  4. Start vm by command line
  5. jakarta ee_Jakarta EE贡献–入门
  6. 10815 - Andy's First Dictionary
  7. 发布后500访问错误 —— dll引用错误
  8. exfat最佳单元大小_ICLR2019最佳论文!神经网络子网络压缩10倍,精确度还能保持不变...
  9. LightTable:更改Julia代码字体和console中字体
  10. MySql学习笔记(六):扫描范围
  11. 08——<use><defs>标签创建图形引用
  12. Codeforces——C. Yet Another Walking Robot
  13. 天龙八部 - 其它 - 属性伤害的计算方法
  14. Python双均线策略回测(2021-10-12)
  15. VVC中的熵编码-JVET提案Q2002
  16. FineReport填报--批量删除
  17. ROWNUM和ROWID的认识
  18. Google 开源最新机器学习系统 TensorFlow
  19. 前端生成gif动态图下载
  20. Swift之网络编程-网络监测

热门文章

  1. Hbase的过滤器查询
  2. java file_Java IO: File
  3. 一:搭建一套免费的serverless网站
  4. 微信小程序获取openid和session_key并且把openid存入数据库
  5. iOS动画进阶 - 手摸手教你写ShineButton动画
  6. [译] ASP.NET 生命周期 – ASP.NET 上下文对象(六)
  7. mysql中模糊查询的四种用法介绍
  8. 用 jQuery 的 AJAX 功能发现的一个错误/注意点:HTTP Error 411
  9. Linux练习(显示环境变量)
  10. 基于shiro的权限设计