简体   繁体   English

如何将div与文本换行连续放置?

[英]How do I place divs in a row with text-wrap?

I am making an application where tags can be added to posts, much like stackoverflow's tagging system. 我正在开发一个可以在帖子中添加标签的应用程序,就像stackoverflow的标签系统一样。 I want to display a list of all common tags, each in a little box that fits the tag perfectly (like the <input type="submit"> tag stretches to fit the size of the word). 我想显示所有常见标签的列表,每个标签都放在一个非常适合标签的小框中(例如<input type="submit">标签会拉伸以适合单词的大小)。 How can I do this and have the <div> s align themselves horizontally with text-wrapping in their containing div? 如何做到这一点,并使<div>与包含其的div中的文本换行水平对齐? Ie something like this, where each word has <div id="tag"> : 也就是说,每个单词都有<div id="tag">

tag1 thisisalongtag
biology physics
thiswordwraps science
one two three four

The easiest solution is to style the tag elements with display: inline-block , which allows text-wrapping while preventing margin-collapsing. 最简单的解决方案是使用display: inline-block设置标签元素的样式,该功能允许在不保证边距折叠的情况下进行文本包装。

display:inline-block

doesn't work on all browsers (older IEs... including 7) 不适用于所有浏览器(旧版IE,包括7)

try this instead: 试试这个代替:

http://jsfiddle.net/mtwDX/ http://jsfiddle.net/mtwDX/

tags are inline but with white-space:nowrap; 标签是内联的,但带有空格:nowrap; and a proper line-height set 并设置适当的线高

hope this helps 希望这可以帮助

I'm not sure that I completely understand your question but there are different ways to achieve this "tag" effect. 我不确定我是否完全理解您的问题,但是有多种方法可以实现这种“标记”效果。

You could set the div's to float:left, clear:left and give it a little padding (5px on all sides). 您可以将div的值设置为float:left,clear:left并为其添加一些填充(所有边均为5px)。 Then put all of your tags in a fixed-width container and they should tile themselves horizontally until they run out of room, at which point they will wrap down to the next "line" below. 然后,将所有标签放入固定宽度的容器中,它们应水平平铺,直到用完为止,这时它们将向下滚动至下一个“行”。

An even simpler way is to just use inline elements, such as anchor tags, which are being used on this site (stackoverflow). 甚至更简单的方法是仅使用此站点上使用的内联元素,例如锚标记(stackoverflow)。 The elements naturally flow horizontally until they run out of room and then wrap. 元素自然地水平流动,直到它们用完然后缠绕。 If you inspect them you'll see there is not much to their styles. 如果检查它们,您会发现它们的样式没有太多。 And they are behaving the way an inline element is expected to behave. 并且它们表现出内联元素的预期行为。

Sometimes the solution is simpler than you think. 有时解决方案比您想象的要简单。 You might not need to bother with div's or floating. 您可能不需要麻烦div或float。 But without more information it's hard to say exactly what will work for you. 但是,如果没有更多信息,很难确切地说出什么对您有用。

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

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