简体   繁体   English

使图像具有相同的高度

[英]Make images take the same height

I'm making a grid of 4 images in a row using DaisyUI card.我正在使用DaisyUI卡连续制作 4 张图像的网格。 However some images have white space at the bottom due to different image heights.然而,由于图像高度不同,一些图像底部有空白。 Is there a way I can make all of them take the same height without compromising image quality?有没有一种方法可以在不影响图像质量的情况下让它们都具有相同的高度?

Here's the code:这是代码:

import React from "react";

const Book = ({ book }) => {
  return (
    <div className="card card-compact w-56 bg-base-100 shadow-xl">
      <figure>
        <img src={book.img} alt="Books" />
      </figure>
    </div>
  );
};

export default Book;

I haven't used custom CSS for this.我没有为此使用自定义 CSS。 I'm using Tailwind.我正在使用顺风。 Help appreciated: Here's a snippet: The yellow book is a good example of the issue帮助表示赞赏:这是一个片段:黄皮书是这个问题的一个很好的例子

If you can use custom CSS, try this:如果你可以使用自定义 CSS,试试这个:

.card > figure, .card > img {
    width: 100%;
    height: 100%;
}

.card > img {
    object-fit: cover;
}

This will make the figure and img elements use up 100% of the height and width of their respective containers, and make the img element make sure that it will retain its aspect ratio while scaling up.这将使figureimg元素使用它们各自容器的 100% 高度和宽度,并使img元素确保在放大时保持其纵横比。

Simplest way - This will keep the image size as it is and fill the other area with space, this way all the images will take same specified space regardless of the image size without stretching最简单的方法 - 这将保持图像大小不变,并用空间填充其他区域,这样所有图像都将占用相同的指定空间,无论图像大小如何而不拉伸

.img{
   width:100px;
   height:100px;

/*Scale down will take the necessary specified space that is 100px x 100px without stretching the image*/
    object-fit:scale-down;

}
  • It is possible when you pick images with same aspect ratio.当您选择具有相同宽高比的图像时,这是可能的。
  • You can add h-full w-full to make full size image but this will stretch your image in case of height.您可以添加h-full w-full来制作全尺寸图像,但这会在高度的情况下拉伸您的图像。 So, it is better you use images of same aspect ratio.因此,最好使用相同宽高比的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM