Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS CSS Flexbox Layout Understanding Flexbox Creating a Flex Container

Stefan Cutajar
Stefan Cutajar
7,747 Points

Why does images overflow it's flex container?

Hi, I'm practicing a little bit on a website I'm building for practice and I noticed that images inside flex containers overflow is there anyway to fix this please?

5 Answers

Stefan Cutajar
Stefan Cutajar
7,747 Points

Thanks for the reply,However what I'm trying to do is put the images next to each other not below each other. I asked this question because guil mentioned that flex items stretches to the size of its containers when the container is given a height but this dont seem to work with images for some reason. Also note that flex-direction and justify-content only work on flex container so you cant use it on the class middle-images unless you set its display to flex or inline-flex aswell

img{ max-width:100%; } might work

Stefan Cutajar
Stefan Cutajar
7,747 Points

No it doesn't :/ I've tried that already.

can you share your code maybe it would be easier to understand the problem?

Stefan Cutajar
Stefan Cutajar
7,747 Points

Sure,

<div class="wrapper">
<div class="flex">
<div class="middle"> <!-- Middle Content -->
    <h1>The Restaurant</h1>
    <p>
Lorem ipsum dolor sit amet, ad omnis nulla duo, sit ne platonem voluptatum intellegam. Vis eirmod ceteros ex, est cu commodo tibique suavitate. Eos ei partem deserunt senserit, nec invidunt volutpat et. Mel volumus atomorum in, nec congue quodsi ad, in vix omittam recusabo</p>
</div>
<div class="middle-images"> <!-- Images next to text -->
    <img src="one.jpg" alt="one">
    <img src="two.jpg" alt="two">
</div>
</div>

              ```CSS

.wrapper {
    width: 80vw;
    margin: 0 auto;
}
.flex {
    display: flex;
    border: 1px solid black;
    height: 500px;
    flex-direction: row;
}
.middle-images {
    display: flex;
    flex-wrap: wrap;
}

.middle-images > img {
    width: 100%;

}

hi stefan sorry for the previous code i've misunderstood a couple of things and a little bit confused obviously:) Setting the width:100% or max-width:100% makes the 2 images stretch 100% of their parent div and causes overflow. This might not be the exact solution you want, if it's not i apoligize in advance

https://thimbleprojects.org/hazalg/375370/

good luck:)