简体   繁体   English

图像不受父 div 高度的限制,只有宽度

[英]Image not constrained by parent div height, only width

my gatsby image is correctly constrained by width of the parent div, but not by the height property.我的 gatsby 图像正确地受父 div 的宽度约束,但不受 height 属性的约束。

Width of image will not exceed the parent div (pink border):图片的宽度不会超过父 div(粉色边框):

在此处输入图像描述

However, if i increase browser size, the height of image goes beyond parent div (pink border):但是,如果我增加浏览器大小,图像的高度会超出父 div(粉红色边框):

在此处输入图像描述

Below is the code - how can I fix this?下面是代码 - 我该如何解决这个问题? Thanks in advance!提前致谢!

    <div 
  style={{
    display: `flex`,
    justifyContent: `flex-start`,
    borderStyle: `solid`,
    borderColor: `blue`,
    height:`70vh`,
    width:`100%`,
    marginTop: `3rem`
}}>
  <div className="imageContainer" 
    style={{
      width:`50%`,
      height:`100%`,
      borderStyle: `solid`,
      borderColor: `pink`,
  }}>
    <StaticImage
      src="../images/about.jpg"
      layout="constrained"
      width={1200}
      height={1100}
      quality={90}
      formats={["auto", "webp", "avif"]}
      alt="home image"
      style={{ 
        marginBottom: 0,
      }}
    />
  </div>

</div>

So do you want the image inside the pink border always follow the width and the height of the pink border div?那么你是否希望粉红色边框内的图像始终遵循粉红色边框div的宽度和高度?

If yes, I think you can achieve it with this css below, add it to StaticImage className props.如果是的话,我想你可以用下面的这个 css 来实现它,将它添加到 StaticImage className 道具。

.static-image {
  height: 100%;
  width: 100%;
  object-fit: cover;
}

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

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