简体   繁体   English

CSS / JS:将元素均匀分布在多行中

[英]CSS/JS: Evenly spreading elements across multiple lines

How would you go about evenly spreading elements across multiple lines, for example: 您如何将元素均匀分布在多行中,例如:


wrapper div: <div style="text-align: center">
elements inside: <div style="display: inline-block;
                 padding-left: 10px; padding-right: 10px;">Element</div>

Instead of only having one element on the next line (wrapped): 而不是在下一行(包装)只有一个元素:

Element 1        Element 2        Element 3        Element 4        Element 5
                          Element 6

It does this, spreading out the elements: 它这样做,散布元素:

Element 1        Element 2        Element 3
Element 4        Element 5        Element 6

If the width of the elements and their count is not fixed, I would write a code which generates custom markup depending on the situation. 如果元素的宽度及其数量不固定,我将编写一个代码,根据情况生成自定义标记。

However it may get tricky because with the web medium, it's hard (impossible actually) to calculate the actual size in pixels of a text block (no way of knowing what font will a browser use and get its metrics). 但是,这可能会变得棘手,因为使用Web介质很难(实际上是不可能)计算文本块的实际大小(以像素为单位)(无法知道浏览器将使用哪种字体并获取其度量)。

I'd suggest deciding on the number of columns in advance (2 or 3) then using elements with their width set to 50% or 33% and text-align property to center . 我建议事先确定列数(2或3),然后使用其宽度设置为50%33%并将text-align属性设置为center元素。

如何指定“宽度”样式?

<div style="width: 50px; display: inline-block; padding-left: 10px; padding-right: 10px;">Element</div>

Float them all left, width to 50% or 33% or whatever you need. 将它们全部浮动,宽度达到50%或33%或任何您需要的宽度。

Make sure you clear the float afterwards. 确保随后清除浮子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM