Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize div to fit screen

When the text is added on top,the div moves down and the text on bottom is not visible. I want the divs to resize so that everything fits into container keeping the width and height to 100%.

Is there are any way to do this with CSS or do I need JavaScript?

body,html {
  margin: 0;
  padding: 0;
}

#container {
  position: absolute;
  width:100%;
  height: 100%;
  overflow: hidden;
}
        
.img {
  background: blue;
  width: 100%;
  height: 50%;
  display: inline-block;
  vertical-align: top;    
}
<div id="container">
  <p>Text 1</p>
  <div class="img"></div>
  <div class="img"></div>
  <p>Text 2</p>
</div>
like image 426
FBR Avatar asked Oct 03 '16 11:10

FBR


People also ask

How do I make a div fit content?

You can simply use the CSS display property with the value inline-block to make a <div> not larger than its contents (i.e. only expand to as wide as its contents).


1 Answers

You could use CSS flex for this.

It could look something like this:

body,html{
            margin: 0;
            padding: 0;
        }
        #container{
            position: absolute;
            width:100%;
            height: 100%;
            display:flex;
            flex-direction: column;
        }
        .img{
            background: blue;
            width: 100%;
            height: 50%;
            vertical-align: top;    
        }
<div id = 'container'>
    <p>Text 1</p>
    <div class="img"></div>
    <div class="img"></div>
    <p>Text 2</p>
</div>

Fiddle

like image 168
Goombah Avatar answered Sep 20 '22 01:09

Goombah