Skip to content Skip to sidebar Skip to footer

Show Only Certain Portions Of Image Inside A Div

How could I only show the image inside the div? Please refer to image below wherein only part of the image inside the blue highlighted part should be visible while the one boxed in

Solution 1:

The overflow:hidden also fills the padding that bootstrap uses to add the gutters.

So I moved the img-sub to the <a> element (also fixed up some css)

https://jsfiddle.net/y573zfja/3/

HTML:

          </div>
          <div class="col-md-4">
            <a href="/posts/1" class="img-sub"><img class="" src="https://i.ytimg.com/vi/JPA_rzHDy6o/maxresdefault.jpg" /></a>
          </div>
          <div class="col-md-4">
            <a href="/posts/1" class="img-sub"><img class="" src="https://i.ytimg.com/vi/JPA_rzHDy6o/maxresdefault.jpg" /></a>
          </div>
        </div>
        <div class="row">
          <div class="col-md-4">
            <a href="/posts/1" class="img-sub"><img class="" src="https://i.ytimg.com/vi/JPA_rzHDy6o/maxresdefault.jpg" /></a>
          </div>
          <div class="col-md-4">
            <a href="/posts/1" class="img-sub"><img class="" src="https://i.ytimg.com/vi/JPA_rzHDy6o/maxresdefault.jpg" /></a>
          </div>
          <div class="col-md-4">
            <a href="/posts/1" class="img-sub"><img class="" src="https://i.ytimg.com/vi/JPA_rzHDy6o/maxresdefault.jpg" /></a>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>

CSS:

.img-sub {
  height: 150px;
  overflow: hidden;
  width: 100%;
  display:block;
  position:relative;
}

.img-sub img {
  height: 100%;
  position: absolute;
  margin: 0 auto;
  left: -50%;
  width: auto;
}

Solution 2:


Solution 3:

In your CSS change this:

.img-sub img {
      height: 100%;
      position: absolute;
      margin: 0 auto;
      left: -100%;
      right: -100%;
      width: auto;
    }

To this:

.img-sub img {
      height: 100%;
      margin: 0 auto;
    }

Post a Comment for "Show Only Certain Portions Of Image Inside A Div"