Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS flexbox: variable height footer beneath variable height content area

Tags:

css

flexbox

I need to make a simple layout where the top area is a variable-sized image and the bottom area should be a variable-height footer, where the content of the footer is

My best attempt so far:

* { padding: 0; margin: 0; }

html, body {
  height: 100%;
  min-height: 100%;
}

.body {
  display: flex;
  min-height: 100%;
}

nav {
  width: 150px;
  flex-shrink: 0;
  background-color: gray;
}

main {
  flex-grow: 1;
	display: flex;
	flex-direction: column;
}

.image {
  flex-grow: 1;
  border: 1px solid blue;
  
  background-image: url('https://www.hawaiimagazine.com/sites/default/files/field/image/plumeria%202%20Eric%20Tessmer%20Flickr.jpg');
	background-size: contain;
	background-repeat: no-repeat;
	background-position: top center;
}

footer {
  border: 1px solid red;
}
<div class="body">
  <nav>side nav</nav>
  <main>
    <div class="image"></div>
    <footer>
      I need this to stay directly below the image but not go beneath the fold
    </footer>
  </main>
</div>

(Resize the preview window both horizontally and vertically, and you'll see that even when the image does not fill the height, the footer does not raise to fill it, either.)

I've tried many different ways, including without flexbox, with grid, and with an <img> tag -- and any of those would be fine in the end. (Just no JS...) I almost had success with an <img> tag and using object-fit: contain but it would push the footer beneath the fold.

EDIT: Updated attempt here (still not solved though):

* { padding: 0; margin: 0; }

html, body {
  height: 100%;
  min-height: 100%;
}

.body {
  display: flex;
  min-height: 100%;
}

nav {
  width: 150px;
  flex-shrink: 0;
  background-color: gray;
}

main {
  flex-grow: 1;
	display: flex;
	flex-direction: column;
}

img {
  max-width: 100%;
	max-height: 80vh;
  object-fit: contain;
}

footer {
  height: 20vh;
  border: 1px solid red;
}
<div class="body">
  <nav>side nav</nav>
  <main>
    <img src="https://www.hawaiimagazine.com/sites/default/files/field/image/plumeria%202%20Eric%20Tessmer%20Flickr.jpg">
    <footer>
      I need this to stay directly below the image but not go beneath the fold.<br>
      UPDATED ATTEMPT: This footer is now fixed-height. If possible I'd like to avoid this.
    </footer>
  </main>
</div>

(this example shows the footer staying below the content, but now the footer is fixed-height, which is not desirable)

How can we use pure CSS to cause the content of the footer to stay below the image, while not extending beneath the fold when the image is tall?

(EDIT: And I should clarify, I did set the sizing to contain on purpose; I need to see the full image -- it's for a photo gallery. So cropping is not really an option here.)

like image 786
Matt Avatar asked Jun 06 '26 19:06

Matt


1 Answers

change the background-size: contain to background-size: cover

it worked for me

like image 126
DerDjamel Avatar answered Jun 09 '26 10:06

DerDjamel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!