Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting inline-block element's height to fill the parent

Tags:

css

I have a container with two items. One of those items is a select element, so I need to set the size attribute via HTML. I want the other item in the container to stretch its height to fit the container. I can't figure it out. I don't want to explicitly set the height of the container because I don't know the size of that select box.

.container {    padding: 5px;    border: 1px solid black;  }    .container .column {    display: inline-block;    width: 40%;    background-color: #AAA;    padding: 5px;    margin: 5px;    vertical-align: top;    height: 100%;  }    select {    width: 100%;  }
<div class="container">    <div class="column">Stretch to fill?</div>    <div class="column">      <select size="15">              <option>Option 1</option>              <option>Option 2</option>          </select>    </div>    <div>
like image 925
Brian Genisio Avatar asked May 06 '13 18:05

Brian Genisio


People also ask

Can we set height for inline elements?

Inline element properties: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. Inline elements flow left to right, meaning inline elements appear on the same line unless the line wraps or there's an explicit line break ( <br/> )

How do inline-block elements add up to 100 width?

If you have a border or padding set for your divs, then you've created additional pixels that will prevent your divs from adding up to a 100% width. To fix this, make sure you've added box-sizing: border-box to the div's styles.

How do you make a block-level element inline?

You can set a block-level element to display like an inline element by setting the display property to inline. You can also cause inline elements to behave like block-level elements using the display property.

Can we change the height and width of inline-block?

You can't set the width or height. inline-block It's formatted just like the inline element, where it doesn't start on a new line. BUT, you can set width and height values. block The element will start on a new line and occupy the full width available.


2 Answers

If table-cell is an option, here's a way to do it:

.container {    display: table;    width: 100%;    padding: 5px;    border: 1px solid black;  }    .container .column {    display: table-cell;    width: 40%;    background-color: #AAA;    padding: 5px;    border: 5px solid white;    vertical-align: top;    height: 100%;  }    select {    width: 100%;  }
<div class="container">    <div class="column">Stretch to fill?</div>    <div class="column">      <select size="15">              <option>Option 1</option>              <option>Option 2</option>          </select>    </div>    <div>
like image 85
Antony Avatar answered Oct 21 '22 20:10

Antony


If I understand what you are saying, you are facing the 100% height columns problem. I'm sorry to tell you there is no actual solution but "hacks".

Here you can find several of those workarounds. I like to use the one true layout method.

By the way, this is thinking you don't want to use the experimental css3 columns properties.

like image 22
Gaston Sanchez Avatar answered Oct 21 '22 19:10

Gaston Sanchez