一个人做饭简单食谱

by Anabella Spinelli

通过安娜贝拉·斯皮内利(Anabella Spinelli)

如何通过两个简单的寿司布局食谱来理解CSS浮动 (How to understand CSS floats with two simple sushi layout recipes)

A few weeks ago I decided I should admit to all the things I’ve never understood about basic CSS. I would try to do a deep and conscious dive into them and get them. It seemed that now, more than a couple years after learning about CSS for the first time, I could use all the experience I’ve gathered to my advantage. This time it should be easier and clearer.

几周前,我决定我应该接受关于基本CSS的所有我从未了解的事情。 我会尝试对它们进行深入而有意识的了解并得到它们。 在第一次学习CSS的几年后,现在看来,我可以利用自己积累的所有经验来发挥自己的优势。 这次应该更轻松,更清晰了。

I also thought to myself: I cannot be the only one struggling with these properties for the Nth (or first) time. Documenting my journey into the most elusive of CSS properties would make a great bunch of articles that others could leverage.

我也对自己想: 在第N次(或第一次),我不是唯一一个在这些属性上挣扎的人。 记录一下我进入最难以捉摸CSS属性的旅程,将产生很多其他人可以利用的文章。

Last month I kicked it off with an intro to the mysterious pairings of the position property. Here’s my second stop in the journey:

上个月,我开始介绍了position属性的神秘配对 。 这是我旅途中的第二站:

The float property, in the form of cooking recipes.

烹饪食谱形式的float属性。

食谱索引 (Recipes Index)

  • Sushi rows — make elements cover a full row in an even manner

    寿司行 -使元素均匀地覆盖整行

  • Clearing broth — make content found below the floats act normally

    清汤 -使漂浮物下方的内容正常运行

寿司行? (Sushi rows ?)

We'll use floats and percentage values to distribute elements evenly in the full (container) width. Just like sushi rows in a plate.

我们将使用浮点数和百分比值在整个(容器)宽度中均匀分配元素。 就像一盘寿司排成一排。

配料: (Ingredients:)

  • 1 container or board1个容器或板
  • Some sushi pieces you need to distribute side-by-side.您需要并排分发一些寿司片。
  • The % sign

    %

  • 1 float: left;

    1 float: left;

说明: (Instructions:)

Prepare your sushi pieces, that is, the elements you want to be displayed in a row. They could be makis, item cards, nigiris, icons, whatever suits your taste.

准备寿司块,即要连续显示的元素。 它们可能是makis,物品卡片,nigiris,图标,任何适合您的口味。

You can also add any non-positioning styling to them: colors, text-alignment, fonts, soy sauce.

您还可以向它们添加任何非定位样式:颜色,文本对齐方式,字体,酱油。

Put them inside a block container, like a board. In its most basic form, this should be a div (but you can use any other HTML5 semantic elements such as header, footer, section, article, main). Add a descriptive class for them. I’ll be using nigiri.

将它们放在一块大容器中,就像一块木板。 以其最基本的形式,它应该是一个div(但是您可以使用任何其他HTML5语义元素,例如页眉,页脚,部分,文章,主要)。 为他们添加一个描述性的类。 我将使用nigiri

Now, on class nigiri we'll apply some styles, including our float: left;. Take a moment and read through them:

现在,在类nigiri我们将应用一些样式,包括我们的float: left; 。 花一点时间阅读它们:

What float: left;does is tell every element to stick to one side — in this case, left — and stand next to each other in a left-to-right row.

什么float: left; 要做的是告诉每个元素都紧贴一侧(在这种情况下为左侧),并在从左到右的行中彼此相邻。

Note that we're adding height to the board. Normally we wouldn’t need this: the board would expand to fit whatever is in it. But floating elements, like our nigiris, are different. They don’t occupy real Document space and don’t affect other, non-floating elements. That’s why we’re using a fixed, pixel-sized height for the board.

请注意,我们正在增加木板的height 。 通常,我们不需要这样做:董事会可以进行扩展以适应其中的任何内容。 但是浮动元素,例如我们的妄想者,则有所不同。 它们不占用实际的文档空间,也不影响其他非浮动元素。 这就是为什么我们在板上使用固定的像素大小的高度。

Now, you should see all your elements on a single row. But something’s not quite right. They’re all piling up on the left and you probably have a lot of empty space on the right side of your board.

现在,您应该在一行中看到所有元素。 但是有些不对劲。 它们全都堆积在左侧,您的木板右侧可能有很多空白。

We need to space them evenly.

我们需要将它们均匀地隔开。

We can do that by setting the nigiri's width to be relative to their container (the board in this case) using a percentage value.

我们可以通过使用百分比值将握持器的宽度设置为相对于其容器(在本例中为木板)的宽度来实现。

Now this is the tricky part: the % you need to set will depend on 3 things

现在这是棘手的部分 :您需要设置的百分比将取决于3件事

  • how many items you have你有几件
  • how they’re structured inwardly (padding)它们如何向内构造(填充)
  • and how much space you want between them.以及它们之间需要多少空间。

Do you want them to stick to each other side by side or do they need some margin in between them? If the sushi pieces have rice padding, that will cause them to be bigger than their content. You’ll have to compensate that by decreasing their width. For this it’s also advisable to use % in the padding values.

您是否希望它们并排粘在一起,或者它们之间需要一定的余量? 如果寿司片有的填充 这将导致他们比他们的内容更大。 您必须通过减小其宽度来进行补偿。 为此,建议在填充值中使用%。

I know al this can be confusing. Here’s a handmade single-press illustration that I hope can illustrate it clearly.

我知道这可能会造成混淆。 这是一个手工制作的单按插图,我希望可以清楚地说明它。

But this is a recipe, not a math lesson. To make it easier for you, dear readers, here are some common combinations for shoulder-to-shoulder and margin-spaced elements, all with 1% rice padding:

但这是一个食谱,而不是数学课程。 亲爱的读者,为了让您更轻松,这是一些肩对肩和边缘距元素的常见组合,所有元素均使用1%的稻米填充:

You may have noticed the pattern here: we’re assuming elements come with 1% of padding. They need to compensate that by subtracting 2% (1% for each side) from the element’s percentage width. Same goes for our 1% margin breather. Now it makes more sense to not use 33.33% width for 3 elements in a row. Instead set it to 29.33% after leaving 2% the padding and 2% for the margin on each.

您可能已经注意到了这里的模式:我们假设元素带有1%的padding 。 他们需要通过从元素的百分比宽度中减去2%(每边1%)来对此进行补偿。 我们的1% 保证金呼吸也是如此。 现在,更合理的做法是不要对连续3个元素使用33.33%的宽度。 而是在分别保留2%的填充和2%的边距后将其设置为29.33%。

Sigh… that was a lot of math. Ok so now, no matter how many pieces your sushi roll gets chopped in, you know how to present them nicely in a board.

感叹……那是很多数学 好了,现在,无论您将寿司卷切成多少块,您都知道如何将它们很好地呈现在板上。

If you want to play around with this setup, here's a CodePen specially made for that.

如果您想使用此设置,这是专门为此设计的CodePen 。

And if you like CSS sushi, don't miss Sasha Tran's very inspiring CSS Sushi Board.

如果您喜欢CSS寿司,请不要错过Sasha Tran令人鼓舞的CSS Sushi Board 。

清汤? (Clearing broth ?)

The perfect soup to have with some floating sushi, while making sure your portions don’t end up swimming in it.

搭配一些浮动寿司时可以搭配的完美汤品,同时确保您的食材不会在其中游动。

配料: (Ingredients:)

  • One container or board with floating sushi一个带有漂浮寿司的容器或盘子
  • A soup or broth to follow after it.跟随汤或汤。
  • One clear: broth;

    一个clear: broth;

说明: (Instructions:)

Once you have your row of floating sushi pieces ready, place your soup container below them in the html.

准备好行的浮动寿司块后,将您的汤容器放在html中的下面。

Our sushi is meant to gracefully float "above" the Document flow and not affect other elements. If we’re not careful they might just end up floating in soup and sushi-ramen is not something the world is ready for.

我们的寿司旨在优雅地漂浮在“文档”流上方,而不影响其他元素。 如果我们不小心的话,它们可能最终会流落在汤中,而寿司拉面并不是世界所准备的。

Remember that floating elements don’t have real Document height. This also means that they don’t “push” the soup down. Now look at this horrific mess:

请记住,浮动元素没有实际的文档高度。 这也意味着他们不会“下汤”。 现在看看这个可怕的烂摊子:

To prevent this atrocity, we need to add our clear: broth;… I mean both;!

为了防止这种暴行,我们需要添加clear: broth; ……我的意思是both ;

We have two options here:

我们在这里有两个选择:

We can simply put the soup in a bowl or container and give the bowl a style of clear: both;. This will sort of get the job done, but it will result in things like margin-top not working at all on the bowl.

我们可以简单地将汤放入碗或容器中,使碗具有clear: both;的风格clear: both; 。 这样就可以完成工作, 但是会导致像margin-top这样的事情根本无法解决。

So if we want the sushi pieces to be completely protected from soup flooding — and not lose any features in the way — we need them to be contained in a plate with a high edge. To achieve that we’ll add an :after pseudo-element to the sushi plate (that is, the container of our little floaters):

因此,如果我们希望完全保护寿司片免受汤液泛滥的侵害,并且又不失去任何功能,则我们需要将它们装入高边缘的盘子中。 为此,我们将添加一个:after 寿司板的伪元素 (即我们的小漂浮物的容器):

Here below there's another example for you play around with. I’ve made the plate visible using height and background color. Even though that’s not necessary for the soup to be well placed, it just makes it look fancier ?

在下面,还有另一个示例供您使用。 我已经使用高度和背景色使该板可见。 尽管这不是必需的汤很好地放置,它只是使它看起来爱好者?

Think of this as making the sushi dish have a very high southern wall in order to prevent any soup from flooding in. But… like… a nice wall.

可以认为这是为了使寿司盘具有很高的南墙,以防止汤汁泛滥。但是……就像……一面漂亮的墙。

All right, I’m super glad that you made it this far and I hope this tiny recipe book helped you get a better idea on how floats work… and how we can work with floats. Stay tuned for more deep-dive-into-basic-but-elusive-things like this ?

好吧,我很高兴您能做到这一点,并且我希望这本简短的食谱书可以帮助您更好地了解浮子如何工作……以及我们如何使用浮子。 敬请关注更多类似这样的基本知识,但又难以捉摸

翻译自: https://www.freecodecamp.org/news/how-to-understand-css-floats-with-two-simple-sushi-layout-recipes-dded925706b9/

一个人做饭简单食谱

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

  1. 一个人做饭简单食谱_通过这5条简单食谱学习SQL

    一个人做饭简单食谱 SQL (Structured Query Language) is a powerful and expressive language for dealing with dat ...

  2. 用java做一个简单记事本_用记事本写一个简单的java程序

    用记事本写一个简单的java程序 第一步: 安装好jdk,并设置好环境变量. 桌面-计算机(右键)-属性-高级系统设置-环境变量-path-在变量值后加上:和jdk安装路径加上(路径即为C:\Prog ...

  3. python简单编程语言_功能强大而又简单易学的编程语言Python

    Python是一种面向对象.直译式计算机程序设计语言,也是一种功能强大的通用型语言(维基百科).自从上次写那个批量Blast小程序的时候接触了Python,发现这个玩意儿真是好用,后来还用它弄了个动态 ...

  4. 怎么用python编简单游戏_用Python实现一个简单的算术游戏详解

    用Python实现一个简单的算术游戏 #!/usr/bin/env python from operator import add, sub from random import randint, c ...

  5. a - 数据结构实验之串一:kmp简单应用_串的两种模式匹配方式(BF/KMP算法)

    串的两种模式匹配方式(BF/KMP算法) 前言 串,又称作字符串,它是由0个或者多个字符所组成的有限序列,串同样可以采用顺序存储和链式存储两种方式进行存储,在主串中查找定位子串问题(模式匹配)是串中最 ...

  6. 用html5做一个简单网页_用Python做一个简单的翻译工具

    编程本身是跟年龄无关的一件事,不论你现在是十四五岁,还是四五十岁,如果你热爱它,并且愿意持续投入其中,必定会有所收获. 本文就来自编程教室一位"小"读者的投稿(互助学习1群里的同学 ...

  7. php写简单接口_使用PHP如何编写简单的App接口

    这篇文章主要介绍了使用PHP编写简单的App接口的一些个人的步骤以及经验总结,有需要的小伙伴可以参考下 本篇是笔记尝试写的第一个PHP接口,并在iOS开发中尝试应用测试.今天给大家分享如何自己写接口来 ...

  8. 怎样对java网站进行简单修改_用Java写一个简单的毕业设计,功能就增删改查的一些基本功能就可以,需要哪些技术?...

    JAVA实现对吗.我来BB几句吧. 首先,你描述的问题,解决方案是属于java web的知识. 毕设的程度呢.第一种方案: 你可以入手很成熟很成熟的ssm架构.就是Spring+mybatis plu ...

  9. matlab watershed函数简单实现_函数指针方法实现简单状态机(附代码)

    之前写过一篇状态机的实用文章,很多朋友说有几个地方有点难度不易理解,今天给大家换种简单写法,使用函数指针的方法实现状态机. 状态机简介 有限状态机FSM是有限个状态及在这些状态之间的转移和动作等行为的 ...

最新文章

  1. 在CentOS 6.3 64bit上安装最新版tsar并监控ATS 5.3
  2. 《细胞》重磅!科学家培育全球首个人类自组织心脏类器官,可自主跳动能自我修复...
  3. mysql userstat_mysql 中记录用户登录错误日志方法小结
  4. C#中读取xml文件指定节点
  5. 根据各工序时间画aoe网_曲靖被动防护网RXI150被动网厂家
  6. pxe+kickstart实现无人值守网络安装rhel5.4
  7. 分析 Go time.After 引起内存暴增 OOM 问题
  8. eclipse中的WEB项目打包部署到tomcat .
  9. 让不同的库元件继承自共同的类
  10. 工频干扰频谱测量_力参数传感器的电阻应变测量系统
  11. 使用Python Chord包画出好看的弦图
  12. android app消息推送,如何进行app消息推送(push)?
  13. 小红书的浏览量很低是为什么?有什么提高的方法吗?
  14. 包含源文件 —— 是奇技淫巧还是饮鸩止渴?
  15. 昨晚,谷歌发布了一个可怕的人工智能!
  16. Java8 通过foreach 遍历List,同时输出下标
  17. 基于Mysql+Servlet+JSP的作业提交系统
  18. urllib和urllib2区别
  19. 理解什么是接口测试?怎样做接口测试?
  20. 阿里云·数加,阿里下一代数据集成实践

热门文章

  1. ClickHouse系列-架构概述
  2. C++ Windows窗口程序:子窗口控件之按钮类button
  3. python英雄联盟万图视频制作
  4. Shell编程四剑客之AWK(基础篇)
  5. mysql databasemetadata_DatabaseMetaData的用法(转)
  6. Oracle cursor 元数据,第七章 数据库元数据(Database Metadata)
  7. AnolisOS 入门三:软件安装
  8. HJ41 称砝码 ——华为机试练习题
  9. 小说中人称转换作用_小说中人称转换作用
  10. PHPCMS V9手机移动端(支持单图和多图)图片上传,(PHPCMS V9前台使用layui的上传组件代替默认的SWFupload上传图片)