Stop wrapping contained divs?

<>

This question already has an answer here:

Answers


Use a wrapper div with the total width of the 4 elements, and set the container to hidden overflow, html example...

<div class="container">
  <div class="wrapper">
    <div class="element"> Element 1 content </div>
    <div class="element"> Element 2 content </div>
    <div class="element"> Element 3 content </div>
    <div class="element"> Element 4 content </div>
  </div>
</div>

and for the css

.container { width:200px; overflow-x:auto; overflow-y:hidden; }
.wrapper { width: 400px; }
.element { width:100px; float:left; }

There are two methods:

1) Clearfix - added to the container.

.clearfix:after {
    visibility: hidden;
    display: block;
    font-size: 0;
    content: " ";
    clear: both;
    height: 0;
}

2) Clearing DIV - placed after the last floating element.

<div class="clear"></div>

.clear {
   clear:both;
   font-size:0;
}

You can save some markup in your situation be using an unordered list instead:

<ul id="container">  
    <li> Element 1 content </li>  
    <li> Element 2 content </li>  
    <li> Element 3 content </li>  
    <li> Element 4 content </li>  
</ul>

This way you can style the contents without and extra class name. It makes more semantic sense as well.

.container li {
   ...
}

Divs are block level elements - which means they create a new line. If you want block level elements next to eachother then they will need to be floated with float:left or float:right. Remember to clear them with clear: both when you want stuff to start going onto new lines.

To prevent them from wrapping you can use the overflow: parameter.

I would also suggest using the CSS direct child selector instead of specifying the same class element over and over again. div.container > div { color: red; }


Firstly for these fields to use those CSS styles - they would need to be #container and #element respectively. Either that or change it to use a wrapping div to handle the scrolling.

Markup:

<div id="container">
<div class="wrappingcontainer">
    <div class="element"> Element 1 content </div>
    <div class="element"> Element 2 content </div>
    <div class="element"> Element 3 content </div>
    <div class="element"> Element 4 content </div>
</div>

CSS:

    #container { width:200px; overflow-x:auto; overflow-y:hidden; }
   .element { width:100px; float:left; }
   .wrappingcontainer{ width: 400px; }

Demo available here!


You can just set height: like so

.container { width:200px; height:20px; overflow-x:auto; overflow-y:hidden; }
.element { width:100px; height:20px;  float:left; }


<div class="container">
<div class="element"> Element 1 content </div>
<div class="element"> Element 2 content </div>
<div class="element"> Element 3 content </div>
<div class="element"> Element 4 content </div>
</div>

Need Your Help

How to assign string to bytes array

go

I want to assign string to bytes array:

How do I convert decimal to hexadecimal in Perl?

perl

How can I convert a number, $d = 1024, in decimal to 0xFF in hex in Perl?