【问题标题】:Image not scaling to full width of div element图像未缩放到 div 元素的全宽
【发布时间】:2021-03-03 09:50:46
【问题描述】:

我有一个 div 元素,我希望其中的图像缩放到父元素的全宽,但是由于某种原因它没有占据全宽。我可以使用 object-fit: cover;但我不想裁剪我希望它填满父级空间的图像。我还想缩放它,使其始终保持为正方形,因此它始终缩放为相等的正方形。我有点困惑为什么它不占用父母容器。它似乎想要保持其纵横比,但我希望它通过其高度和宽度属性拉伸到父容器。

html:

import React from 'react';

const ThreeGridTeaser = (props) => {

    return (
        <div className="specific-offer-container mt-0">
            <div className="three-grid-teaser-wrapper">
                <div className="three-grid-teaser-container">
                    {props.gridData.map((item, index) => (
                        <div key={index} className="three-grid-teaser-block">
                            <div className="three-grid-image-container hidden">
                                <img src={item.image} 
                                />
                            </div>
                            <div className="product-text-container hidden">
                                <ul>
                                    <li className="product-title">{item.title}</li>
                                    <li className="product-description">{item.description}</li>
                                </ul>
                            </div>
                        </div>
                    ))}
                </div>
            </div>
        </div>       
    )
}

export default ThreeGridTeaser;

SASS:

.three-grid-teaser-wrapper {
    width: 100%;
    height: auto;
    display: flex;
    justify-content: center;
}

.three-grid-teaser-container {
    width: 100%;
    justify-content: center;
    flex-wrap: wrap;
    height: auto;
    display: flex;
    grid-gap: 20px;
    gap: 20px;
    @media (max-width: 1199.98px) { 
        gap: 25px;
    } 
    @media (max-width: 767.98px) { 
        width: 100%;
    } 
}
.three-grid-teaser-block {
    display: flex; 
    width: 32%;
    height: fit-content;
    @media (max-width: 1199.98px) { 
        width: 48%;
    } 
    @media (max-width: 767.98px) { 
        width:47%;
    } 
    justify-content: center;
    flex-direction: column;
}

.hover-text-underline {
    text-decoration: underline;
    text-underline-position: under;
}

.three-grid-image-container {
    height: 300px;
    @media (max-width: 1199.98px) { 
        height: 350px;
    } 
    @media (max-width: 767.98px) { 
        height: 180px;
    }    

    width: 100%;
    max-width: 100%;
    img {
        display: block;
        max-width: 100%;
        max-height: 100%;
        width: 100%;
        height: 100%;
    }
}

.product-text-container {
    ul {
        margin-top: 10px;
        padding: 0;
        li:last-child {
            padding-top: 5px;
        }
        li {
            list-style: none;

        }
    }
}

.product-title {
    font-family: $terminaDemi;
    font-size: 12px;
    @media (max-width: 767.98px) { 
        font-size: 8px;
    } 
}

.product-description {
    font-family: $Montserrat;
    font-size: 18px;
    @media (max-width: 767.98px) { 
        font-size: 12px;
    } 
}

【问题讨论】:

  • 澄清一下,您希望每张图像都扭曲(根据需要在一个方向或另一个方向上)以填充一个正方形,而不进行裁剪?
  • 我希望图像占据它的容器

标签: javascript html css sass


【解决方案1】:

这是因为图像已达到相对于div 的最大高度。我的意思是,在一个 400px 正方形div 内,一个 300px 宽和 400px 高的图像,在这种情况下,如果你在图像中放置 100% 的宽度,它的宽度将达到的最大值是 300px,在你的情况下不想调整它的大小。

Demonstração

更新

'object-fit: contains' 使图像适合其按比例可用的空间,如果您希望它保持其比例并填充整个空间,请使用 'object-fit:cover'

看看这个 https://jsfiddle.net/gxnvuq6s/8/

【讨论】:

  • 但您当然可以拉伸图像以超出其纵横比的范围并填满父容器。
  • 是的,但我认为您想完整显示图像,而不是裁剪或类似的东西。但是您可以在图像中使用“位置:绝对”,在您的 div 中,您可以使用“溢出:隐藏”来获取通过容器隐藏的图像内容。
【解决方案2】:

您是否尝试过查看background-size: cover; 样式?然后你也可以用background-position: center center;定位你的图片

【讨论】:

  • 是的,它似乎仍然无法正确缩放图像,它会将其切断
  • hmm - 这里的问题出现了,为您的父 div 设置一个固定高度,然后为该 div 设置背景图像。但是@sydo 为您提供了一个更好的解决方案,它使用了 object-fit:cover。这样,您仍然可以在图像中添加 SEO 友好标签,使其在搜索引擎中可用
猜你喜欢
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
  • 1970-01-01
  • 2015-10-27
  • 1970-01-01
  • 2021-04-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多