我可以有一个具有设定高度的响应式图像吗?

发布于 2025-01-12 15:25:31 字数 294 浏览 4 评论 0原文

我在页面构建器中工作 对于一家商店,我正在创造。我可以改变 CSS,这很棒。

我正在努力调整这 4 列行中图像的响应大小。由于图像的高度不同,我必须设置高度并具有响应宽度。有什么办法让它正确缩放吗?

宽度是自动的,高度是根据屏幕尺寸设置的高度。

您可以看到,当我调整大小时,它会与盒子分离,然后有时会被压扁。

1 2

I working in a page builder
For a shop, I am creating. I can change the CSS which is great.

I’m struggling to get a responsive resize of the images in this 4 column row. Since the images are different heights I have to have to set a height and have responsive width. Is there any way to get it to scale correctly?

The width is auto and the height is a set height based on the size of the screen.

You can see that when I resize it separates from the box and then sometimes get squished.

1
2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

债姬 2025-01-19 15:25:31

object-fit 属性

我通过使用 display : flex;object-fit : cover; 属性完成了您的设计。我认为直接在图像上的这个 object-fit 属性是唯一缺少的属性,可以让你的图像在屏幕分辨率不同的情况下仍然看起来不错。

请注意 object-position : center; 的使用,这使得调整大小始终在图像的中心进行。


索引.html

<div class="foo">
    <div class="bar">
        <img src="https://picsum.photos/200/300" alt="">
        <div>
            <h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>

    <div class="bar">
        <img src="https://picsum.photos/500/200" alt="">
        <div>
            <h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>
    
    <div class="bar">
        <img src="https://picsum.photos/200/700" alt="">
        <div>
            <h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>

    <div class="bar">
        <img src="https://picsum.photos/500/400" alt="">
        <div>
            <h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>
</div>

样式.css

body {
    background-color: #f7f7f7;
    font-family: Arial, Helvetica, sans-serif;
}

.foo {
    width: 100%;
    display: flex;
    gap: 20px;
}

.bar {
    background-color: #ffffff;
    display: flex;
    flex-direction: column;
    width: 100%;
    border-radius: 15px;
    overflow: hidden;
    box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
}

.bar > img {
    width: 100%;
    height: 200px;
    object-fit: cover;
    object-position: center;
}

h4 {
    color:#9ccf74;
}

.bar > div {
    padding: 10px;
    height: 100px;
}

object-fit property

I did your design by using display : flex; and object-fit : cover; properties. I think that this object-fit property directly on the image is the only lacking property to make your images still looking good despite the screen resolution.

Notice the use of object-position : center; which makes the resizing always axed on the center of the image.


index.html

<div class="foo">
    <div class="bar">
        <img src="https://picsum.photos/200/300" alt="">
        <div>
            <h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>

    <div class="bar">
        <img src="https://picsum.photos/500/200" alt="">
        <div>
            <h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>
    
    <div class="bar">
        <img src="https://picsum.photos/200/700" alt="">
        <div>
            <h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>

    <div class="bar">
        <img src="https://picsum.photos/500/400" alt="">
        <div>
            <h4>Lorem, ipsum dolor sit amet consectetur adipisicing.</h1>
            <p>$42</p>
        </div>
    </div>
</div>

style.css

body {
    background-color: #f7f7f7;
    font-family: Arial, Helvetica, sans-serif;
}

.foo {
    width: 100%;
    display: flex;
    gap: 20px;
}

.bar {
    background-color: #ffffff;
    display: flex;
    flex-direction: column;
    width: 100%;
    border-radius: 15px;
    overflow: hidden;
    box-shadow: rgba(100, 100, 111, 0.2) 0px 7px 29px 0px;
}

.bar > img {
    width: 100%;
    height: 200px;
    object-fit: cover;
    object-position: center;
}

h4 {
    color:#9ccf74;
}

.bar > div {
    padding: 10px;
    height: 100px;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文