overflow: hidden; not working on image inside grid

Viewed 357

I have an image inside of a grid, I want the image to fill the grid container, and hide any overflow, see code:

    <div class="serviceFlexChildLeft serviceGridImage">
      <img id="broadcastProductionImage1" src="content/images/homepage/broadcastProduction2.png">
      <img id="broadcastProductionImage2" src="content/images/homepage/broadcastProduction3.png">
      <div id="broadcastSlash"></div>
    </div>
.serviceGridImage {
    display: grid;
    height: 40rem;
    grid-template: 50% 50% / 100%;
    overflow: hidden;
}

#broadcastProductionImage1 {
    object-fit: cover;
    justidy-self: center;
    align-self: center;
    grid-area: 1 / 1 / 2 / 2;
    overflow: hidden;
}

#broadcastProductionImage2 {
    object-fit: cover;
    justify-self: center;
    align-self: center;
    grid-area: 2 / 1 / 3 / 2;
    overflow: hidden;
}

you can see I've tried putting overflow: hidden on both the image itself, and on the grid parent, neither seem to have any effect, I've used both object-fit: cover; and min-width: 100%; min-height: 100%; to attempt achieving my desired results, neither seems to yield what I want, it does appear to work once I define either width or height without the min, but those both mess up the images ability to dynamically scale and fit the container.

suggestions?

1 Answers

The problem is, that u set a overflow: hidden directly to your images, but overflow: hidden works differently.
For example you create a div with a height and a width and give it overflow: hidden, your images inside won't overlap anymore.


Of course you set overflow:hidden to your .serviceGridImage but this contains both images, so nothing is overflowing .serviceGridImage right now. Your images overflowing the grid, not the div.

You can fix your current problem if you give the images a max-height value.

.serviceGridImage {
    display: grid;
    height: 40rem;
    grid-template: 50% 50% / 100%;
    overflow: hidden;
}

#broadcastProductionImage1 {
    object-fit: cover;
    justify-self: center;
    align-self: center;
    grid-area: 1 / 1 / 2 / 2;
    overflow: hidden;
}

#broadcastProductionImage2 {
    object-fit: cover;
    justify-self: center;
    align-self: center;
    grid-area: 2 / 1 / 3 / 2;
    overflow: hidden;
}

.serviceGridImage img {
    max-height: 100%;
}
<div class="serviceFlexChildLeft serviceGridImage">
      <img id="broadcastProductionImage1" src="https://images.unsplash.com/photo-1613470651441-9f59d6918dd6?ixid=MXwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw0fHx8ZW58MHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60">
      <img id="broadcastProductionImage2" src="https://images.unsplash.com/photo-1613465892991-39d8d5fb2d3a?ixid=MXwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw1fHx8ZW58MHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60">
      <div id="broadcastSlash"></div>
    </div>

Or change your HTML structure like this:

.serviceGridImage {
    display: grid;
    height: 40rem;
    grid-template: 50% 50% / 100%;
    overflow: hidden;
}
.image {
  display: grid;
  grid-area: 1 / 1 / 2 / 2;
  overflow: hidden;
}

.image.second {
   grid-area: 2 / 1 / 3 / 2; 
}

#broadcastProductionImage1 {
    object-fit: cover;
    justify-self: center;
    align-self: center;
    
    overflow: hidden;
}

#broadcastProductionImage2 {
    object-fit: cover;
    justify-self: center;
    align-self: center;
    overflow: hidden;
}
<div class="serviceFlexChildLeft serviceGridImage">
      <div class="image">
        <img id="broadcastProductionImage1" src="https://images.unsplash.com/photo-1613470651441-9f59d6918dd6?ixid=MXwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw0fHx8ZW58MHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60">
      </div>
      <div class="image second">
        <img id="broadcastProductionImage2" src="https://images.unsplash.com/photo-1613465892991-39d8d5fb2d3a?ixid=MXwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw1fHx8ZW58MHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60">
      </div>
      <div id="broadcastSlash"></div>
    </div>

Related