user3877230 user3877230 - 2 months ago 6
CSS Question

Stretch DIV inside a DIV to 100% height?

I want to stretch a DIV-Container inside a div to the height of 100%. The DIV-Container i want to stretch has a image inside via

<img src="http://thisisatestforstack.com/image.png"/>


I also want to center the Image vertically afterwards. I prepared a picture which makes my wishes more clear:

enter image description here

This is my Code right now:

<div id="master">
<div id="text" style="width: 60%; float: left;">
This is an example.<br/>
This is an example.<br/>
This is an example.<br/>
This is an example.<br/>
This is an example.<br/>
This is an example.<br/>
This is an example.<br/>
This is an example.<br/>
This is an example.<br/>
This is an example.<br/>
This is an example.<br/>
This is an example.
</div>

<div id="image" style="width: 40%; float: left;">
<img src="http://thisisatestforstack.com/image.png"/>
</div>
</div>

Answer

Using flexbox layout you can achieve that as:

Code Snippet

#master {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
  -webkit-box-pack: center;
  -webkit-justify-content: center;
  -ms-flex-pack: center;
  justify-content: center;
}
#text {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
}
#image {
  -webkit-box-flex: 1;
  -webkit-flex: 1;
  -ms-flex: 1;
  flex: 1;
}
.item-top {
  width: 60%;
  float: left;
  -webkit-align-self: flex-start;
  -ms-flex-item-align: start;
  align-self: flex-start;
}
<div id="master">
  <div id="text" class="item-top">
    This is an example.
    <br/>This is an example.
    <br/>This is an example.
    <br/>This is an example.
    <br/>This is an example.
    <br/>This is an example.
    <br/>This is an example.
    <br/>This is an example.
    <br/>This is an example.
    <br/>This is an example.
    <br/>This is an example.
    <br/>This is an example.
  </div>

  <div id="image" style="width: 40%; float: left;">
    <img src="https://www.google.co.in/logos/doodles/2016/teachers-day-2016-india-6215218130583552-res.png" />
  </div>
</div>

Comments