Home > OS >  How to avoid overflow of a bootstrap card image in card-body?
How to avoid overflow of a bootstrap card image in card-body?

Time:05-07

I am using bootstrap cards and I could manage the card image from overflowing outside the card area but the bottom of the image overflows in the card-body region. Not sure if we have any CSS attribute that can avoid overflowing content into the current div.

#services img {
    object-fit: cover;
    height    : 15rem;
    transition: transform 0.2s;
}

#services .card {
    overflow: hidden;
    padding : 0px 0px;
}

#services img:hover {
    transform: scale(1.1);
}
<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

  <title>Hello, world!</title>

</head>

<body>

  <div  id="services">

    <div >

      <div >
        <div >
          <img src="https://media.istockphoto.com/photos/hot-air-balloons-flying-over-the-botan-canyon-in-turkey-picture-id1297349747?b=1&k=20&m=1297349747&s=170667a&w=0&h=oH31fJty_4xWl_JQ4OIQWZKP8C6ji9Mz7L4XmEnbqRU=" alt="...">
          <div >
            <h5 >Card Title</h5>
            <p >Some quick example text to build on the card title and make up the bulk
              of the card's content.</p>
          </div>
        </div>
      </div>
    </div>
  </div>

</body>

</html>

CodePudding user response:

Try out this snippet.

This is fairly simple as you have to wrap the image inside a div and add overflow-hidden utility class to the wrapper.

overflow utilities: Overflow utilities

#services img {
    object-fit: cover;
    height    : 15rem;
    transition: transform 0.2s;
}

#services img:hover {
    transform: scale(1.1);
}
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"/>

  <div  id="services">
    <div >
      <div >
        <div >
          <div >
            <img  src="https://media.istockphoto.com/photos/hot-air-balloons-flying-over-the-botan-canyon-in-turkey-picture-id1297349747?b=1&k=20&m=1297349747&s=170667a&w=0&h=oH31fJty_4xWl_JQ4OIQWZKP8C6ji9Mz7L4XmEnbqRU=" alt="...">
          </div>
          <div >
            <h5 >Card Title</h5>
            <p >Some quick example text to build on the card title and make up the bulk
              of the card's content.</p>
          </div>
        </div>
      </div>
    </div>
  </div>

CodePudding user response:

#services img {
    object-fit: cover;
    transition: transform 0.2s;
    width: 100%;
}

#services .card {
    overflow: hidden;
    padding : 0px 0px;
}

#services img:hover {
    transform: scale(1.1);
}

#services .card-img {
    height    : 15rem;
    overflow: hidden;
    width: 100%;
}
<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">

  <!-- Bootstrap CSS -->
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">

  <title>Hello, world!</title>

</head>

<body>

  <div  id="services">

    <div >

      <div >
        <div >
          <div >
          <img src="https://media.istockphoto.com/photos/hot-air-balloons-flying-over-the-botan-canyon-in-turkey-picture-id1297349747?b=1&k=20&m=1297349747&s=170667a&w=0&h=oH31fJty_4xWl_JQ4OIQWZKP8C6ji9Mz7L4XmEnbqRU="  alt="...">
          </div>
          <div >
            <h5 >Card Title</h5>
            <p >Some quick example text to build on the card title and make up the bulk
              of the card's content.</p>
          </div>
        </div>
      </div>
    </div>
  </div>

</body>

</html>

This should work:

  1. Wrap the image into a div
  2. Give width 100% to this div and height (ex: 15rem), and make overflow:hidden of this div. So, anything that goes outside of div will be hidden.

CodePudding user response:

you should put your image in div tag like blow:

<div >
            <img src="https://picsum.photos/id/237/200/300" alt="...">
        </div>

then style like below:

#services .card-image img {
    object-fit: cover;
    height    : 15rem;
    width: 100%;
    transition: transform 0.2s;
}

#services .card {
    overflow: hidden;
    padding : 0px 0px;
}

#services .cardimage img:hover {
    transform: scale(1.1);
}

.card-image{
    width: 100%;
    height    : 15rem;
    overflow: hidden;
}
  • Related