简体   繁体   English

如何避免div显示在新行中

[英]How to avoid div to be displayed on a new line

I have a list of div displayed on the same line. 我在同一行上显示了一个div列表。 When the page is not wide enough to display all the div on the same line, I don't want extra div to be displayed on the next line. 当页面的宽度不足以在同一行上显示所有div时,我不希望在下一行显示额外的div。 How can I avoid displaying extra div on a new line? 如何避免在新行上显示多余的div?

Here is a jsFiddle: http://jsfiddle.net/YCFZM/ 这是一个jsFiddle: http : //jsfiddle.net/YCFZM/

Try to reduce width on the page and you'll see some div displayed on a new line. 尝试减小页面的宽度,您会在新行上看到一些div。

Thanks. 谢谢。

Updated gist with solution: http://jsfiddle.net/Meligy/YCFZM/16/ 解决方案更新了要点: http//jsfiddle.net/Meligy/YCFZM/16/

Main changes: 主要变化:

  • Removed extra quote in HTML at id=""sidebar" which was breaking the gist 删除了HTML中id=""sidebar"处的多余引号,这打破了要旨
  • Removed height: 0px; 移除height: 0px; from div#sidebar CSS 来自div#sidebar CSS
  • Added white-space:nowrap; 添加了white-space:nowrap; to div#sidebar CSS div#sidebar CSS
  • Removed float:left from div.sidebar-item CSS div.sidebar-item CSS移除了float:left
  • Added white-space: nowrap; display:inline-block; 添加white-space: nowrap; display:inline-block; white-space: nowrap; display:inline-block; to div.sidebar-item CSS div.sidebar-item CSS

Optionally you can also remove text-align:center; 您也可以选择删除text-align:center; from div#sidebar CSS if you meant to make the text be to the left. 如果您想使文本在左侧,请从div#sidebar CSS中获取。

在样式表中,将display属性与文档上的内联值一起使用

As far as my knowledge goes the only solution to this is to have a fixed width for the container. 据我所知,唯一的解决方案是为容器设置固定的宽度。 Or to have an element in the container which has a fixed width 或者在容器中放置宽度固定的元素

You need to set a min-width which is large enough to encompass the divs. 您需要设置一个min-width ,该min-width必须足以容纳div。 Also, you had an extra quotation mark in your jsfiddle which was preventing your styling from being applied and for some reason the height property was set to 0 . 另外,您在jsfiddle中加了一个引号,这阻止了样式的应用,并且由于某种原因, height属性设置为0 I've adapted your jsfiddle here to display the behaviour I think your after. 我在这里对您的jsfiddle进行了调整,以显示我想像的行为。

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

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