1

I want to create div with overflow effect. here is my code

<div style="width:100%;clear:both;overflow-x:auto">
   <div style="width:50%; float:left; background:#ccc;height:200px"></div>
   <div style="width:50%; float:left; background:#333;height:200px"></div>
   <div style="width:50%; float:left; background:#cc90fc;height:200px"></div>
   <div style="width:50%; float:left; background:#000;height:200px"></div>
</div>

i want these div don't come below while overflow, the container div will have a scroll bar, any suggestion.

3 Answers 3

3

You need to give whitespace:nowrap to parent container, and display:inline-block to children elements

When whitespace:nowrap is given, the content inside doesn't go to the next line.

<div style="width:100%;overflow-x:auto;white-space: nowrap; font-size:0">
  <div style="width:50%; display:inline-block;  background:#ccc;height:200px; font-size:initial"></div>
  <div style="width:50%; display:inline-block;  background:#333;height:200px; font-size:initial"></div>
  <div style="width:50%; display:inline-block;  background:#cc90fc;height:200px; font-size:initial"></div>
  <div style="width:50%; display:inline-block;  background:#000;height:200px; font-size:initial"></div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

1

To avoid gap between display:inline-block element you should use font-size:0; in container elements. Also add vertical-align to keep the child element in same line. See below:

<div style="width:100%;overflow-x:auto; white-space: nowrap; font-size: 0;">
  <div style="width:50%; display:inline-block; background:#ccc; height:200px; font-size: 12px; vertical-align: top;">afadsf afdafdsaf</div>
  <div style="width:50%; display:inline-block; background:#333; height:200px; font-size: 12px;  vertical-align: top;"></div>
  <div style="width:50%; display:inline-block; background:#cc90fc; height:200px; font-size: 12px;  vertical-align: top;"></div>
  <div style="width:50%; display:inline-block; background:#000; height:200px; font-size: 12px; vertical-align: top;"></div>
</div>

2 Comments

I can still see the gap in between, i guess it's not practical to be a fixed size.
@ZohirSalakCeNa I updated my answer. font-size: 0 is key issue to remove white space in between the child elements.
0

I'm not 100% sure what you're asking, but if you don't want the div to show when it overflows vertically (up and down) instead of horizontally (left to right) you should be using the overflow-y style instead of the overflow-x style.

Change style="width:100%;clear:both;overflow-x:auto to style="width:100%;clear:both;overflow-y:auto in your style attribute.

1 Comment

That being said, you also need to define the height of the parent div. (I'm assuming there isn't an element wrapping it that defines the size)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.