简体   繁体   English

当没有更多空间时将 Div 换行到新行

[英]Wrap Div to new line when there is no more space

I am working on a web application (asp.net mvc3)我正在开发一个 Web 应用程序 (asp.net mvc3)

I have a Main div.我有一个主 div。 I want to add lots of div inside that main div.我想在主 div 中添加很多 div。

But I want them to be like this: Divs should appear next to each others on a line, and when there is no more space left, the next div will wrap to a new line.但我希望它们是这样的:div 应该在一行中彼此相邻出现,当没有更多空间时,下一个 div 将换行。 (Similar to writing a text, when there is no more space on this line the next word will wrap to a new line) (类似于写文本,当这一行没有更多空间时,下一个单词将换行)

I tried to use display: inline;我尝试使用display: inline; to make the appear next to each others, but how can I make them wrap when they reach the end of the Main div?使它们彼此相邻,但是当它们到达 Main div 的末尾时,如何使它们环绕?

Is there a way to do it without hard coding the positions?有没有办法在不硬编码职位的情况下做到这一点? because divs are added dynamically so i don't know how big will they be or their number因为 div 是动态添加的,所以我不知道它们有多大或它们的数量

Thanks谢谢

Try display: inline-block - http://jsfiddle.net/7FJRr/1/ 尝试display: inline-block - http://jsfiddle.net/7FJRr/1/

UPDATE If IE7 is still a concern: 更新如果IE7仍然是一个问题:

div {
    display: inline-block;
    zoom: 1; 
    *display: inline;
}

Is this what you mean? 你是这个意思吗?

http://jsbin.com/uzoruq/edit#javascript,html,live http://jsbin.com/uzoruq/edit#javascript,html,live

I used float:left to arrange the content divs 我用float:left来安排内容div

I am not aware of solutions that will wrap a div but this will put the divs inline 我不知道将包装div的解决方案,但这会使div内联

EDIT 编辑

If you are willing to use spans you could do this: 如果您愿意使用跨度,您可以这样做:

http://jsbin.com/uzoruq/2/edit http://jsbin.com/uzoruq/2/edit

I used display:inline for the spans. 我使用display:inline for the display:inline

It's possible to do it with the help of flex.可以在 flex 的帮助下做到这一点。

display: flex;
flex-wrap: wrap;

For example:例如:

 .flex{ width: 5rem; background: yellow; display: flex; flex-wrap: wrap; justify-content: space-between; } .flex > div{ width: 2rem; background: black; color: white; margin-top: 1rem; margin-bottom: 1rem; }
 <div class="flex"> <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> </div>

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

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