Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS when inline-block elements line-break, parent wrapper does not fit new width

Tags:

html

css

How do you get an inline-block element to fit its content width, if the content line-breaks because of screen width?

<!-- parent inline-block --> <div style='display: inline-block;'>     <div style='display: inline-block; width:200px;'></div>     <!--         If this child line breaks,          two 200px wide blocks are stacked vertically.         That should make the parent width 200px,         but the parent stays much wider than that     -->     <div style='display: inline-block; width:200px;'></div> </div> 

I can't think of how to phrase the question so it sounds easy, but I put together a simple JSFiddle illustrating.

#wide {    position: relative;    width: 100%;    border: 1px solid black;    padding: 5px;  }  #narrow {    position: relative;    width: 175px;    border: 1px solid black;    padding: 5px;  }  .wrap {    display: inline-block;    border: 1px solid green;    margin: auto;  }  .inlineblock {    display: inline-block;    vertical-align: top;    background: red;    min-width: 100px;    min-height: 100px;    border: 1px solid black;  }
<section id='wide'>    <div class='wrap'>      <div class='inlineblock'></div>      <div class='inlineblock'></div>    </div>  </section>  <p>    When the red inline-blocks are forced to line break, how do you make a parent with display:inline-block (the green border) snap to fit? How do you get rid of all the extra horiztonal space in the lower green bordered div?  </p>  <section id='narrow'>    <div class='wrap'>      <div class='inlineblock'></div>      <div class='inlineblock'></div>    </div>  </section>
like image 773
user2782001 Avatar asked Jan 25 '16 15:01

user2782001


People also ask

Does width work on inline elements?

Inline element properties: The height of an inline element is the height of the content. The width of an inline element is the width of the content. The height and width of an inline element cannot be set in CSS. You cannot set the height and width of block-level elements in CSS.

How do you prevent inline block elements from wrapping?

As a result, the elements can sit next to each other. The major difference between display: inline; and display: inline-block; is that, display: inline-block; also allows us to set the width and height of the element. We can prevent inline-block divs from wrapping by adding suitable Bootstrap classes.

Is inline elements in HTML are not affected by Width and height properties?

An inline element is an element that's width and height are determined by the content it contains. Inline elements, such as a <span> , will completely ignore the width and height as well the the top and bottom margin properties because, well, the content is what determines the dimensions.

How do I change the width of an inline element?

To apply width, set css property 'display' to either 'block' or 'inline-block'. block: the element will sit in a single line. In such case you may want to set float so links are in the same line; inline-block; the element will have height, width, etc, and multiple elements will sit in the same line (block).


1 Answers

You can't. By default, inline-block elements have a shrink-to-fit width:

The shrink-to-fit width is:
min(max(preferred minimum width, available width), preferred width).

Then,

  • When preferred minimum width <= preferred width <= available width, the width will be the preferred width, as you desire.
  • When available width <= preferred minimum width <= preferred width, the width will be the preferred minimum width, as you desire.
  • When preferred minimum width <= available width <= preferred width, the width will be the available width, even if you don't like it.

If you really don't want this, I guess you could add a resize event listener with JS, and set the desired width manually.

like image 163
Oriol Avatar answered Sep 25 '22 12:09

Oriol