How can I make the image fill the remaining space inside the div?


Jason

I have a <div>window that takes up 60% of the space and it contains two things:

  • narrow header row
  • I want an image that occupies the rest of the div.

How to do this with pure CSS (no Javascript)? I've been trying a lot of things with no luck.

This is the closest I can get; the image sneaks beyond the green borderdiv.container

html, body {
  height: 100%;
  overflow: hidden;
  margin: 0px;
}
div.container {
  height: 60%;
  border: 2px solid green;
  box-sizing: border-box;
}
div.rest {
  height: 40%;
  border: 2px solid red;
  box-sizing: border-box;
}
div.img-container {
  height: 100%; /* this is wrong, but what do I do? */
}
div.img-container img {
  max-height: 100%;
  max-width: 100%;
  height: auto;
  width: auto;
  opacity: 0.5;
}
<html>
<body>
<div class="container">
<div class="header">hieronymus bosch last judgement</div>
<div class="img-container"><img src="https://i.imgur.com/TT6drhn.jpg"></div>
</div>
<div class="rest">
<h1>blah blah blah</h1>
</div>
</body>
</html>

Here is what I tried to use and flexfailed .

html, body {
  height: 100%;
  overflow: hidden;
  margin: 0px;
}
div.container {
  height: 60%;
  border: 2px solid green;
  box-sizing: border-box;
  display: flex;
  flex-direction: column
}
div.rest {
  height: 40%;
  border: 2px solid red;
  box-sizing: border-box;
}
div.img-container {
  flex: 1;
}
div.header {
  flex: 0;
}
div.img-container img {
  max-height: 100%;
  max-width: 100%;
  height: auto;
  width: auto;
  opacity: 0.5;
}
<html>
<body>
<div class="container">
<div class="header">hieronymus bosch last judgement</div>
<div class="img-container"><img src="https://i.imgur.com/TT6drhn.jpg"></div>
</div>
<div class="rest">
<h1>blah blah blah</h1>
</div>
</body>
</html>

j-petty bourgeoisie

@Christian's approach works if you know the height of the header element, but you can also use flex.

This allows the element to grow dynamically to fill the remaining space, so your header can be any height.

html,
body {
  height: 100%;
  overflow: hidden;
  margin: 0px;
}

div.container {
  height: 60%;
  border: 2px solid green;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

div.rest {
  height: 40%;
  border: 2px solid red;
  box-sizing: border-box;
}

div.img-container {
  flex: 1;
  position: relative;
}

div.img-container img {
  opacity: 0.5;
  height: 100%;
  position: absolute;
}
<html>

<body>
  <div class="container">
    <div class="header">hieronymus bosch last judgement</div>
    <div class="img-container"><img src="https://i.imgur.com/TT6drhn.jpg"></div>
  </div>
  <div class="rest">
    <h1>blah blah blah</h1>
  </div>
</body>

</html>

Related


How can I make the div fill the remaining horizontal space?

Alexander Shenko I have 2 divs: one on the left side of the page and one on the right. The one on the left has a fixed width and I want the one on the right to fill the remaining space. #search { width: 160px; height: 25px; float: left; background-colo

How can I make the div fill the remaining horizontal space?

Alexander Shenko I have 2 divs: one on the left side of the page and one on the right. The one on the left has a fixed width and I want the one on the right to fill the remaining space. #search { width: 160px; height: 25px; float: left; background-colo

How can I make the div fill the remaining space?

Fred I have a web page with header, content and footer. The content has a background image. I want the image to fill the remaining space between the header and footer. Some divs are children of content divs, and images in them sometimes contain content and oth

How can I make the div fill the remaining space?

Fred I have a web page with header, content and footer. The content has a background image. I want the image to fill the remaining space between the header and footer. Some divs are children of content divs, and images in them sometimes contain content and oth

How can I make the div fill the remaining space?

123321123321 Suppose I have this basic html layout: Header Automatically resize DIV footer. Header and footer have a fixed pixel size. <div id="header"> Lorem Ipsum </div> <div id="auto"> This div should automatically change size </div> </div id="footer"> Lo

How can I make the div fill the remaining horizontal space?

Alexander Shenko I have 2 divs: one on the left side of the page and one on the right. The one on the left has a fixed width and I want the one on the right to fill the remaining space. #search { width: 160px; height: 25px; float: left; background-colo

How can I make the div fill the remaining space?

Fred I have a web page with header, content and footer. The content has a background image. I want the image to fill the remaining space between the header and footer. Some divs are children of content divs, and images in them sometimes contain content and oth

How can I make the div fill the remaining space?

Fred I have a web page with header, content and footer. The content has a background image. I want the image to fill the remaining space between the header and footer. Some divs are children of content divs, and images in them sometimes contain content and oth

How can I make the div fill the remaining space?

Fred I have a web page with header, content and footer. The content has a background image. I want the image to fill the remaining space between the header and footer. Some divs are children of content divs, and images in them sometimes contain content and oth

How can I make the div fill the remaining space?

123321123321 Suppose I have this basic html layout: Header Automatically resize DIV footer. Header and footer have a fixed pixel size. <div id="header"> Lorem Ipsum </div> <div id="auto"> This div should automatically change size </div> </div id="footer"> Lo

How to make div inside table fill remaining space

Touan Given the HTML structure below, how can I td.expandfill the remaining vertical space #outer? I've tried various solutions (including Flexbox) but nothing worked for me. Unfortunately, I'm stuck with both tables, so assume the HTML structure cannot be cha

How to make div inside table fill remaining space

Touan Given the HTML structure below, how can I td.expandfill the remaining vertical space #outer? I've tried various solutions (including Flexbox) but nothing worked for me. Unfortunately, I'm stuck with both tables, so assume the HTML structure cannot be cha

How to make div inside table fill remaining space

Touan Given the HTML structure below, how can I td.expandfill the remaining vertical space #outer? I've tried various solutions (including Flexbox) but nothing worked for me. Unfortunately, I'm stuck with both tables, so assume the HTML structure cannot be cha

How to make div inside table fill remaining space

Touan Given the HTML structure below, how can I td.expandfill the remaining vertical space #outer? I've tried various solutions (including Flexbox) but nothing worked for me. Unfortunately, I'm stuck with both tables, so assume the HTML structure cannot be cha

How to make div inside table fill remaining space

Touan Given the HTML structure below, how can I td.expandfill the remaining vertical space #outer? I've tried various solutions (including Flexbox) but nothing worked for me. Unfortunately, I'm stuck with both tables, so assume the HTML structure cannot be cha

How to make div inside table fill remaining space

Touan Given the HTML structure below, how can I td.expandfill the remaining vertical space #outer? I've tried various solutions (including Flexbox) but nothing worked for me. Unfortunately, I'm stuck with both tables, so assume the HTML structure cannot be cha

How can I make the view fill the remaining space?

George I have a layout with views A and B, ___ |A| |B| --- The height of A can vary widely. I want B to have ParentHeight-A.height, and want to do this via xml (I know I can resize them programmatically, but would like to avoid that). How can I achieve this u

How can I make the view fill the remaining space?

George I have a layout with views A and B, ___ |A| |B| --- The height of A can vary widely. I want B to have ParentHeight-A.height, and want to do this via xml (I know I can resize them programmatically, but would like to avoid that). How can I achieve this u

How can I make the view fill the remaining space?

George I have a layout with views A and B, ___ |A| |B| --- The height of A can vary widely. I want B to have ParentHeight-A.height, and want to do this via xml (I know I can resize them programmatically, but would like to avoid that). How can I achieve this u

How can I set a div under the header to fill the remaining space?

amazing prnt.sc/ha3dag The black div is the title and is 40 pixels high. The blue div below it needs to fill the remaining space without exceeding the screen height and causing overflow-y. Mr. Nellinger You haven't given us much to do. When you post a question

How to make a div fill the remaining horizontal space

Jason Z. I have two dividends. One floats left and the other floats right. The width of the one on the left is set to 18% with a minimum width of 217px. I want the div on the right to take up the remaining space, while also being able to resize to fit the wind

How to make a div fill the remaining horizontal space

Jason Z. I have two dividends. One floats left and the other floats right. The width of the one on the left is set to 18% with a minimum width of 217px. I want the div on the right to take up the remaining space, while also being able to resize to fit the wind