2

I want to set the width of divs to their contents and keep the divs under each other. Using display:inline-block effectively floats the divs next to each other. How can I do this?

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
IlludiumPu36
  • 4,196
  • 10
  • 61
  • 100

2 Answers2

1

Working jsFiddle Demo

Consider the following markup:

<div class="text">apple</div>
<div class="text">banana</div>
<div class="text">kiwi</div>
<div class="text">orange</div>

Float and clear all, here is the CSS:

.text {
    background: yellow;
    float: left;
    margin-bottom: 3px;
    clear: both;
}
  • The poster of this question wants the display inline property hence wanting the box to be the size of its content. They should only have to use clear both. – Judson Terrell May 29 '13 at 04:35
  • @JudsonTerrell How you would set the `width` of a `block` element to the width of the content within it? You must explicitly make it `inline` or `inline-block`, or make it `float`, or change the `position` of it to `absolute`. If they aren't floated, so clear is useless. –  May 29 '13 at 04:37
  • I meant along with inline block. Another option is to put a div between them with only clear both. – Judson Terrell May 29 '13 at 04:40
  • @JudsonTerrell `float` property will do nothing with `inline-block` elements. –  May 29 '13 at 04:41
0

Put clear: both on them this will keep them from floating, and yes you do need float "something"

#div1, #div2{

    display: inline;
    float: left;
    clear: both;
    border: 1px solid grey;

}

<div id="div1">some content here that is bigger</div>
<div id="div2">some content here</div>
Judson Terrell
  • 4,204
  • 2
  • 29
  • 45