Reskk Reskk - 1 month ago 9
CSS Question

Flexbox - Image overflowing

I'm trying to create a flex container that has 2 items in it - a text div and an image div.

However I'm having real problems keeping the image within the container itself. When I resize the browser to a small width the image just overflows out of the container and I can't seem to understand why it is happening. Surely the items should stay within their container?

Code is here:

<div class="featured-blog">
<div class="featured-blog-main">
<h2>Featured Blog</h2>

<div class="blog-flex">

<div class="blog-text">
<h3>Easter Island</h3>
<p>Turpis sed tempus integer erat, pellentesque tortor nisl, eros viverra, integer vehicula taciti sapien sed nisl dui, nec litora tincidunt cum non lobortis sollicitudin. Et odio duis cum magna pretium. Enim augue odio. Non nec velit sed lacus in. Enim vitae pellentesque nec phasellus, quis in vitae, leo in eros donec, pede volutpat. Donec nunc mi vel, quis malesuada, sed proin curabitur orci ipsum volutpat, eu eu id blandit ultricies sodales</p></div>

<div class="blog-img"> <img src="http://www-tc.pbs.org/wgbh/nova/assets/img/fate-of-easter-island/image-01-large.jpg"></div>

</div> <!-- blog-flex-->
</div> <!-- featured-blog-main -->
</div> <!--featured-blog -->



.featured-blog {
margin: 0 60px;
padding: 100px 0;
border-top: 2px solid gainsboro; }

.blog-flex {
display: flex; }

.blog-text {
margin: 0 30px; }


The codepen is here:

http://codepen.io/reskk/pen/ALdpbz

I've done some looking around and tried a few solutions: using flex-basis, flex-grow etc. and none seem to work.

Is there a way for me to make these two divs responsive whilst keeping them inside their flex container?

Or if I'm doing something wrong in the way I've set it up can someone please let me know?

Thanks,

Reskk

Answer

Just add max-width: 100%; and height: auto; to your <img> to make it responsive.

h2 {
  text-align: center;
}
h3 {
  margin: 0 0 30px;
  font-size: 1.8em;
}
.featured-blog {
  margin: 0 60px;
  padding: 100px 0;
  border-top: 2px solid gainsboro;
}
.featured-blog-main {
  background: rgba(0, 0, 0, 0.3);
}
.blog-flex {
  display: flex;
}
.featured-blog p {} .blog-text {
  /*  width: 80%;*/
  margin: 0 30px;
  flex: 1;
}
.blog-img img {
  max-width: 100%;
  height: auto;
}
<div class="featured-blog">
  <div class="featured-blog-main">
    <h2>Featured Blog</h2>
    <div class="blog-flex">
      <div class="blog-text">
        <h3>Easter Island</h3>
        <p>Turpis sed tempus integer erat, pellentesque tortor nisl, eros viverra, integer vehicula taciti sapien sed nisl dui, nec litora tincidunt cum non lobortis sollicitudin. Et odio duis cum magna pretium. Enim augue odio. Non nec velit sed lacus in.
          Enim vitae pellentesque nec phasellus, quis in vitae, leo in eros donec, pede volutpat. Donec nunc mi vel, quis malesuada, sed proin curabitur orci ipsum volutpat, eu eu id blandit ultricies sodales</p>
      </div>
      <div class="blog-img">
        <img src="http://www-tc.pbs.org/wgbh/nova/assets/img/fate-of-easter-island/image-01-large.jpg">
      </div>
    </div>
  </div>
</div>

Comments