简体   繁体   English

防止 span 或 div 的换行

[英]Prevent wrapping of span or div

I'd like to put a group of div elements of fixed width into a container and have the horizontal scroll bar appeared.我想将一组固定宽度的div元素放入一个容器中,并出现水平滚动条。 The div / span elements should appear in a line, left to right in the order they appear in the HTML (essentially unwrapped). div / span元素应该出现在一行中,按照它们在 HTML 中出现的顺序从左到右(基本上是展开的)。

This way the horizontal scroll bar will appear and can be used instead of the vertical scroll bar for reading through the content (left to right).这样,水平滚动条就会出现,并且可以用来代替垂直滚动条来阅读内容(从左到右)。

I've tried floating them in a container and then putting a white-space: nowrap style on the container, but alas, it still seems to wrap.我试过将它们漂浮在一个容器中,然后在容器上放置一个white-space: nowrap样式,但是唉,它似乎仍然可以换行。

Ideas?想法?

It looked like this:它看起来像这样:

 .slideContainer { white-space: nowrap; }.slide { width: 800px; float: left; display: inline; }
 <div class="slideContainer"> <div class="slide">Some content</div> <div class="slide">More content</div> <div class="slide">Even More content!</div> </div>

UPDATE:更新:
See site for examples, but they were wrong about not being another way - I'm sure the article is old though.有关示例,请参见站点,但是他们认为不是另一种方式是错误的-尽管我敢肯定这篇文章很旧。

Try this:尝试这个:

 .slideContainer { overflow-x: scroll; white-space: nowrap; }.slide { display: inline-block; width: 600px; white-space: normal; }
 <div class="slideContainer"> <span class="slide">Some content</span> <span class="slide">More content. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</span> <span class="slide">Even more content!</span> </div>

Note that you can omit .slideContainer { overflow-x: scroll; }请注意,您可以省略.slideContainer { overflow-x: scroll; } .slideContainer { overflow-x: scroll; } (which browsers may or may not support when you read this), and you'll get a scrollbar on the window instead of on this container. (当您阅读本文时,哪些浏览器可能支持或可能不支持.slideContainer { overflow-x: scroll; } ,您将在 window 而不是此容器上获得滚动条。

The key here is display: inline-block .这里的关键是display: inline-block This has decent cross-browser support nowadays, but as usual, it's worth testing in all target browsers to be sure.如今,它具有不错的跨浏览器支持,但像往常一样,值得在所有目标浏览器中进行测试以确保。

It works with just this:它只适用于:

.slideContainer {
    white-space: nowrap;
}
.slide { 
    display: inline-block;
    width: 600px;
    white-space: normal;
}

I did originally have float: left;我最初确实有float: left; and that prevented it from working correctly.这阻止了它正常工作。

Thanks for posting this solution.感谢您发布此解决方案。

Particularly when using something like Twitter's Bootstrap , white-space: nowrap;特别是在使用 Twitter 的Bootstrap之类的东西时, white-space: nowrap; doesn't always work in CSS when applying padding or margin to a child div .将填充或边距应用于子div时,在 CSS 中并不总是有效。 Instead however, adding an equivalent border: 20px solid transparent;然而,添加一个等效的border: 20px solid transparent; style in place of padding/margin works more consistently.代替填充/边距的样式更一致。

As mentioned you can use:如前所述,您可以使用:

overflow: scroll;

If you only want the scroll bar to appear when necessary, you can use the "auto" option:如果只希望在必要时出现滚动条,可以使用“auto”选项:

overflow: auto;

I don't think you should be using the "float" property with "overflow", but I'd have to try out your example first.我认为您不应该将“float”属性与“overflow”一起使用,但我必须先尝试您的示例。

Looks like divs will not go outside of their body's width.看起来 div 不会 go 超出其身体宽度。 Even within another div.即使在另一个 div 中。

I threw this up to test (without a doctype though) and it does not work as thought.我把它扔了去测试(虽然没有文档类型),但它并没有像想象的那样工作。

 .slideContainer { overflow-x: scroll; }.slide { float: left; }
 <div class="slideContainer"> <div class="slide" style="background: #f00">Some content Some content Some content Some content Some content Some content</div> <div class="slide" style="background: #ff0">More content More content More content More content More content More content</div> <div class="slide" style="background: #f0f">Even More content! Even More content! Even More content!</div> </div>

What i am thinking is that the inner div's could be loaded through an iFrame, since that is another page and its content could be very wide.我在想的是内部 div 可以通过 iFrame 加载,因为那是另一个页面,它的内容可能很宽。

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

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