Bootstrap 如何有固定的div的高度,而不管它的图像高度

aydmsdu9  于 9个月前  发布在  Bootstrap
关注(0)|答案(2)|浏览(92)

我有一行三列。我想有一个对每列的div图像相同的高度,而不论在每个div的图像大小。
我想得到的图像在一个div调整大小。但是我得到不同大小的div,因为myImage1.pngmyImage2.pngmyImage3.png的图像大小不同。
这是因为我在我的'bg'类中修复了填充。有人能帮我修一下吗?
这是我的HTML和CSS代码:

<div class="row IDE_container">
    <div class="col-lg-3 col-md-3 col-sm-3">
        <div class="bg">
            <img src="myImage1.png" class="IDE_div" />
        </div>
    </div>

    <div class="col-lg-2 col-md-2 col-sm-2">
        <div class="circle_bg circle_bg_sm">
            <img src="myImage2.png" class="IDE_div" />
        </div>
    </div>

    <div class="col-lg-3 col-md-3 col-sm-3">
        <div class="circle_bg">
            <img src="myImage3.png" class="IDE_div" />
        </div>
    </div>
</div>

CSS:

.bg {
    background-color: #ededed;
    border-radius: 100%;
    text-align: center;
    padding: 35% 5%;
    width: max-content;
    max-width: 100%;
    box-shadow: 0 0 3px 1px #DDD;
}

.IDE_div {
    display: inline-block;
    position: relative;
    width: 100%; 
    height: 100%;
}

.IDE_container {
    display: flex;
    align-items: baseline;
}
0wi1tuuw

0wi1tuuw1#

对于各种类型的图片,可以使用object-fit属性。

像这样添加CSS:

.IDE_div{
width: 100%;
object-fit: cover;
height: 300px; /* only if you want fixed height */
}

<div class="col-3">
    <div class="circle_bg">
        <img src="myImage3.png" class="IDE_div" />
    </div>
</div>

您可以在这里找到有关object-fitobject-position的详细信息:https://css-tricks.com/on-object-fit-and-object-position/

bweufnob

bweufnob2#

您需要在包含图像的div上设置高度或最大高度,否则它们将适应其内容(图像)的高度。由于您已将图像设置为占用100%的宽度和高度,并且没有约束其父级,因此它们将根据原始图像大小尽可能地变大。
例如:

.IDE_container {
  display: flex;
  align-items: baseline;

  div.col-3 {

    div {
      height: 200px;

      .IDE_div {
        display: inline-block;
        position: relative;
        width: 100%;
        height: 100%;

        &::before {
          content: "";
          position: absolute;
          top: 50%;
          left: 50%;
          width: var(--val);
          padding-bottom: var(--val);
          transform: translate(-50%, -50%);
          z-index: -1;
          border-radius: 50%;
        }
      }
    }
  }
}

你可以给予所有的容器一个相同的类(例如:fixed-height-bg)来更容易地设计它们。
另请注意:您不需要为不同的屏幕大小为您的列提供单独的类,因为它们总是占据相同的比例。所以,你可以用

<div class="col-3">
        <div class="circle_bg">
            <img src="myImage3.png" class="IDE_div" />
        </div>
    </div>

编辑添加:如果你想让图片保持它们的比例,你需要改变你的css看起来像这样:

img {
  height: 100%; /* height of the parent container if specified, or of the image itself */
  width: auto; /* will maintain the proportions of the image */
}

您也可以将这些值切换为高度:auto; width:100%;如果你想让图像采取全宽度。

相关问题