使用CSS浮动 - 是否需要宽度? [英] Floating with CSS - Is a width required?

查看:146
本文介绍了使用CSS浮动 - 是否需要宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我学习CSS的时候,我读到,当你浮动元素,每个元素的宽度是必需的。这是真的吗?

When I was learning CSS some time back I read that when you float elements that a width for each element was required. Is this true?

现在我问的原因是因为我想浮动变化宽度的元素;但显然这不工作,因为较长的元素将包装到下一行。我拿出了CSS&它显示我想要它......元素只是占用空间&如果对于容器的宽度太长,则它将只显示在下一行。

The reason I ask now is because I wanted to float varying width elements; but obviously this wasn't working as the longer elements would just wrap to the next line. I took out the width for the elements in the CSS & it displays how I want it to..... the elements just take up the space & if it's too long for the width of the container then it will just display on the next line.

推荐答案

不需要宽度。

当我使用静态宽度浮动元素时,通常会创建一个类似列的布局

When I float elements with a static width it's usually to create a column-like layout

.sidebar { float: left: width: 30%; }
.content { float: left: width: 70%; }

但是对于希望项目占用尽可能少的空间并包装到下一个

but for times when you want items to take up as little space as possible and wrap to the next line, width isn't necessary.

并且,和往常一样,当你浮动时,确保你 clear:both left right

And, as always, when you float, make sure you clear:both or left or right

这篇关于使用CSS浮动 - 是否需要宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆