Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent wrapping of span or div

Tags:

html

css

I'd like to put a group of div elements of fixed width into a container and have the horizontal scroll bar appeared. The div/span elements should appear in a line, left to right in the order they appear in the HTML (essentially unwrapped).

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.

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.

like image 541
cgp Avatar asked Mar 25 '09 00:03

cgp


People also ask

How do I stop span from wrapping?

If you want to prevent the text from wrapping, you can apply white-space: nowrap; Notice in HTML code example at the top of this article, there are actually two line breaks, one before the line of text and one after, which allow the text to be on its own line (in the code).

How do you keep a floating element from wrapping?

Use "overflow: hidden" to avoid floating elements from wrapping a container's text. We use flexbox for this use case now. The reason behind this is that " overflow: hidden " will give you a new block formatting context. You could use other attributes as well, but overflow: hidden works nicely without interfering much.

How do you prevent inline block divs from wrapping?

Add white-space: nowrap; to your . layout style declaration. This will do exactly what you need: preventing the divs from wrapping.

What's the difference between span and div?

Span and div are both generic HTML elements that group together related parts of a web page. However, they serve different functions. A div element is used for block-level organization and styling of page elements, whereas a span element is used for inline organization and styling.


2 Answers

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; } (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.

The key here is display: inline-block. This has decent cross-browser support nowadays, but as usual, it's worth testing in all target browsers to be sure.

like image 72
Ron DeVera Avatar answered Sep 28 '22 17:09

Ron DeVera


It works with just this:

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

I did originally have float : left; and that prevented it from working correctly.

Thanks for posting this solution.

like image 31
Demolishun Avatar answered Sep 28 '22 17:09

Demolishun