Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize image with CSS without altering image quality

I have a Bootstrap carousel with the below code; how can I size this image to completely fill the carousel with losing image quality and also ensure the image height does not exceed the width or height of the carousel? image

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.1/css/bootstrap.min.css" rel="stylesheet"/>
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
  <ol class="carousel-indicators">
    <li data-target="#carouselExampleIndicators" data-slide-to="0" class="active"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="1"></li>
    <li data-target="#carouselExampleIndicators" data-slide-to="2"></li>
  </ol>
  <div class="carousel-inner">
    <div class="carousel-item active" style="width:400px;">
      <img class="d-block w-100" src="{{articles.image.url}}" alt="First slide" style=" width:100%;height:auto;">
      <div class="carousel-caption d-none d-md-block">
        <h5>Latest</h5>
        <p>{{articles.title}}</p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">{% lorem 1 p %}
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">{% lorem 1 p %}
    </div>
  </div>
  <a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
    <span class="carousel-control-prev-icon" aria-hidden="true"></span>
    <span class="sr-only">Previous</span>
  </a>
  <a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
    <span class="carousel-control-next-icon" aria-hidden="true"></span>
    <span class="sr-only">Next</span>
  </a>
</div>
like image 931
ade desmond Avatar asked Jun 04 '18 11:06

ade desmond


3 Answers

Use object fit property in your css, and give a fixed width and height to your image tag or respective class so that every image will have same width and height, Now Your Image won't be distorted.

 .d-block w-100 {
   width:100%;
   height:550px;
   object-fit:cover;
   object-position:50% 50%;
  }
like image 132
Surya Neupane Avatar answered Sep 29 '22 17:09

Surya Neupane


You can make the image 100% width and height auto.

like image 23
Friday Ameh Avatar answered Sep 29 '22 17:09

Friday Ameh


Old code:

<div class="carousel-inner">
    <div class="carousel-item active" style="width:400px;">
      <img class="d-block w-100" src="{{articles.image.url}}" alt="First slide" style=" width:100%;height:auto;">
      <div class="carousel-caption d-none d-md-block">
        <h5>Latest</h5>
        <p>{{articles.title}}</p>
      </div>
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=666&fg=444&text=Second slide" alt="Second slide">{% lorem 1 p %}
    </div>
    <div class="carousel-item">
      <img class="d-block w-100" src=".../800x400?auto=yes&bg=555&fg=333&text=Third slide" alt="Third slide">{% lorem 1 p %}
    </div>
  </div>

Edit the carousel-item with:

width: 100%;
height: auto; 
like image 39
billy.farroll Avatar answered Sep 29 '22 15:09

billy.farroll