Adding image to a flex container overrides 100vh and creates a scroll bar. How to fix?
Hello,
I have the following HTML. I want to present as a simple page with hero and footer taking up the entirety of the view port.
To do so, I used the following CSS:
The problem I am having is that when I comment out the image, the page displays as I would like. But when the image is added, it expands beyond the <div> container and creates a scrollbar. I want the image to grow to fit the container, but not to cause the container itself to grow. I have spent some time trying to figure it out, Google, ChatGPT but haven't found a solution - any help would be great!
Thank you in advance,
Bread
8 Replies
it's easier to diagnose things if you share your code in something like codepen, so people can see the code live.
That said, you can probably fix this with:
Ah thank you for the tip! My first post here. Here is a link to the Codepen: https://codepen.io/Datasapien/pen/PoVZMQY
you are missing some
;
in your css for a startKevin Powell
YouTube
The problems with viewport units
Conquering Responsive Layouts (free course): https://courses.kevinpowell.co/conquering-responsive-layouts
Join the Discord: https://kevinpowell.co/discord
Viewport units often feel like this cheat code that makes things so much easier when you first discover them, but they actually are problematic for a number of reasons, from creating horizon...
also you don't remove the default body margin
Oh yes, good catch, thank you! I've added them in, and now taken out the absolute positioning for the image as it was then ruining the layout completely haha. Thanks 🙂
Ah, from my first lines of CSS you mean? The * {} part? I will remove the margin: 0px now, but would you mind telling me why it is bad practice? Thank you btw 🙂
Ah missed that, I don't do it but you do you. I embrace defaults and only overwrite it when needed.
Long as you cleared it, but more know the pit falls of vh/vw units
Yeah I watched that video, really useful so thank you. I think I am using Display: flex too much for this page too, so I will try simplify a little. Thanks for the help 😄