如何用 CSS 制作浮动顶部?
我知道CSS只支持浮动属性的左值和右值,但是有没有一种技术可以实现浮动顶部?我会尽力解释。我有以下代码:
<div style="float:left">
<div style="float:left">
<div style="float:left">
....
使用此代码,每个 div 都会向左浮动,直到达到页面的右限。我想做同样的事情,但垂直,这样每个 div 都放置在前一个 div 的底部,然后,当达到页面的底部限制时,创建一个新列。有没有办法只使用CSS(并且可能编辑HTML代码)来做到这一点?
I know that CSS only supports left and right values for the float property, but is there a technique to implement a floating top? I will try to explain. I have the following code:
<div style="float:left">
<div style="float:left">
<div style="float:left">
....
With this code every div is floated to left until the right limit of the page is reached. I want to do the same thing but vertically, so that every div is placed at the bottom of the previous one and then, when the bottom limit of the page is reached, a new column is created. Is there a way to do this using only CSS (and maybe editing the HTML code)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
只需使用
vertical-align
:Simply use
vertical-align
:仅使用 CSS 来实现此目的的唯一方法是使用 CSS 3,它不适用于所有浏览器(仅适用于最新一代,如 FF 3.5、Opera、Safari、Chrome)。
事实上,CSS 3 有一个很棒的属性:column-count,您可以像这样使用:
如果 #myContent 正在包装您的其他 div。
更多信息请参见:http://www.quirksmode.org/css/multicolumn.html
正如您可以在那里读到的,使用它有严重的限制。在上面的示例中,如果内容溢出,它只会添加到另一列。
在mozilla 中,您可以使用column-width 属性,该属性允许您根据需要将内容划分为任意多的列。
否则,您必须在 Javascript 或后端的不同 div 之间分发内容。
The only way to do this with CSS only is by using CSS 3 which is not going to work on every browser (only the latest generation like FF 3.5, Opera, Safari, Chrome).
Indeed with CSS 3 there's this awesome property : column-count that you can use like so :
If #myContent is wrapping your other divs.
More info here : http://www.quirksmode.org/css/multicolumn.html
As you can read there, there are serious limitations in using this. In the example above, it will only add up to one another column if the content overflows.
in mozilla you can use the column-width property that allows you to divide the content in as many columns as needed.
Otherwise you'll have to distribute the content between different divs in Javascript or in the backend.
Hugogi Raudel 提出了一种有趣的方法,通过
CSS
来实现这一目标。假设这是我们的HTML
标记:您可以使用此
CSS
代码实现 3 行列:这是最终结果:
我们在这里所做的是为行中的每个项目添加适当的边距。此方法的限制是您必须确定列行数。它不会是动态的。我确信它有用例,所以我在这里包含了这个解决方案。
Hugogi Raudel has came up with an interesting way to to achieve this by
CSS
. suppose here is ourHTML
markup:You can achieve a 3-row column using this
CSS
code:And here is the end result:
What we are doing here is adding a appropriate margin for each item in the row. This approach limitation is that you have to determine the column row count. it's not going to be dynamic. I'm sure it has use cases so I included this solution here.
没有
float:top
,只有float:left
和float:right
如果你想在彼此下面显示 div,你必须这样做:
There is no
float:top
, onlyfloat:left
andfloat:right
If you wish to display div underneath each other you would have to do:
我尝试这个只是为了好玩 - 因为我也想要一个解决方案。
小提琴: http://jsfiddle.net/4V4cD/1/
html:
css:
我可以看到这适用于更高/更宽的 div。只能从侧面思考一下。我想定位将成为一个问题。 transform-origin 应该可以帮助一些人。
I tried this just for fun - because I too would like a solution.
fiddle: http://jsfiddle.net/4V4cD/1/
html:
css:
I You can see this will work with taller/wider divs. Just have to think sideways. I imagine positioning will become an issue. transform-origin should help some with it.
这有效,适用于
ul
:This works, apply in
ul
:我认为完成您所讨论的内容的最佳方法是拥有一组 div 作为您的列,并按照您所描述的方式填充这些内容。然后用您正在谈论的内容垂直填充这些 div。
I think the best way to accomplish what you're talking about is to have a set of divs that will be your columns, and populate those in the way you described. Then fill those divs vertically with the content you're talking about.
但是如果div的元素数量不固定或者你不知道可以有多少,你仍然需要JS。使用 jQuery
:even
,:odd
But if the number of div's elements is not fixed or you don't know how much it could be, you still need JS. use jQuery
:even
,:odd
我只是用 JQuery 做的。
我在 Firefox 和 IE10 中进行了测试。
在我的问题中,这些项目有不同的高度。
I just make with JQuery.
I tested in Firefox and IE10.
In my problem the items have different heights.
要使用 CSS3 来实现这一点,只要我正确理解你,就不那么难了。假设 HTML DIV 如下所示:
CSS 如下所示。以下 CSS 的作用是让您的 DIV 执行向左浮动,这会将其“粘”在父 DIV 元素的左侧。然后,您使用“top:0”,它会将其“粘贴”到浏览器窗口的顶部。
To achieve this using CSS3, it will not be that hard as long as I am understanding you properly. Let us say that the HTML DIV's looks like this:
And the CSS would be as followed. What the following CSS will do is make your DIV execute a float left, which will "stick" it to the left of the Parent DIV element. Then, you use a "top: 0", and it will "stick it " to the top of the browser window.
对我有用的技巧是在 div 对齐期间更改写入模式。
The trick that worked for me was to change the writing-mode for the duration of the div-alignment.
这是一个在 FF、Opera、Chrome 中有效(几乎完美)的解决方案:
技巧是
p
和column- 上的
。它带有动态列数。page-break-inside:void;
body
上的宽度a
中的文本可以是多行,并且块可以具有不同的高度。也许有人有 Edge 和 IE 的东西
Here is a solution which works (almost perfect) in FF, Opera, Chrome:
The trick is
page-break-inside: avoid;
onp
andcolumn-width
onbody
. It comes with dynamic column count. Text ina
may be multi-line and blocks may have different heights.Maybe someone has something for Edge and IE
您也许可以使用同级选择器做一些事情,例如:
没有尝试过,但这可能会使第四个 div 向左浮动,也许可以做您想做的事情。再次不完全支持。
You might be able to do something with sibling selectors e.g.:
Not tried it but this might float the 4th div left perhaps doing what you want. Again not fully supported.
我知道这个问题很老了。但今天任何想要这样做的人都可以。
I know this question is old. But anyone looking to do it today here you go.
这对我有用:
This works for me: