Saad Khan
Saad Khan

Reputation: 77

HTML no-wrap conflicting with Divs text wrapping

I'm trying to use the white-space: nowrap in CSS to allow scrolling on overflow.

However, this prevents the innerHTML inside the divs to wrap.

If I remove the white-space: nowrap, the innerHTML works as it should but the divs don't scroll, instead they go onto new line.

With no-wrap:

enter image description here

Without no-wrap:

enter image description here

Upvotes: 0

Views: 502

Answers (2)

vsync
vsync

Reputation: 130215

Here's a working example of a few div elements, with a horizontal scroll when they overflow their parent node, which is a section in this demo.

In the below demo, I thought it best to use flexbox, which gives some nice benefits.

section{  
  display: flex;
  overflow: hidden;
  overflow-x: auto;
  flex-wrap: nowrap;
  border: 1px solid green;
  height: 150px;
}

div{  
  flex: 400px;
  min-width: 200px;
  margin: 5px;
  padding: 5px;
  background: silver;
}
<section>
  <div>some very very long text which should probably break because it is very long.</div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</section>

Upvotes: 0

Pedro Caseiro
Pedro Caseiro

Reputation: 495

Leave the

white-space: nowrap;

in the outer div and in each inner div:

white-space: normal; 

for example:

.outer-div{
  white-space: nowrap;
}
.outer-div>div{
  white-space: normal;
}

Upvotes: 2

Related Questions