【问题标题】:resizing and fitting an image to a parent div in react在反应中调整图像大小并将其拟合到父 div
【发布时间】:2021-07-09 02:24:34
【问题描述】:

大家好,我是新来的反应。我正在建立一个网站并完成了大部分工作,但是我遇到了问题。我想在我的 scss 文件夹中编写代码,以便使用给定的大小和高度声明 div 元素,然后声明该 div 的子元素,以便在图像上调用 div 将调整大小并居中以适应宽度和父 div 的高度。所以父 div 指定大小,子元素调整图像大小并居中,我们可以在网站上调用任何我们想要的图像上的子元素来调整图像大小并将图像居中到父 div 的尺寸。

我尝试了类似的东西

.container {
    margin: 10px;
    width: 115px;
    height: 115px;
    line-height: 115px;
    text-align: center;
    border: 1px solid red;
}
.resize_fit_center {
    max-width:100%;
    max-height:100%;
    vertical-align: middle;
}

然后从名为“_other.scss”的文件中调用

我做到了

<div class='container'>
    <a href='#'>
    <img class='resize_fit_center'
      src='http://i.imgur.com/H9lpVkZ.jpg' />
    </a>
</div>

但这不起作用,特别是如果图像位于其他预先指定的较大容器中。关于如何在“scss”中制作标准文件并在图像上调用它以便在应用图像时可以调整大小并居中到父 div 的大小的任何想法?

【问题讨论】:

    标签: css reactjs next.js


    【解决方案1】:

    图像如您所愿居中。但是为什么要限制容器的宽度呢?

    .container {
        margin: 10px;
        width: 115px; // <== remove this line, you will see the image had been centered already.
        height: 115px;
        line-height: 115px;
        text-align: center;
        border: 1px solid red;
    }
    .resize_fit_center {
        max-width:100%;
        max-height:100%;
        vertical-align: middle;
    }
    

    Here is the demo

    【讨论】:

    • 您好,我限制了容器宽度,因为我希望容器大小保持不变。无论图像如何,我都希望图像在恒定大小的容器上居中并重新缩放。如果我们删除该行,则容器会以不同方式扩展,如您的示例所示。我将为许多图像使用这些元素,并且我希望它们都在相同大小的容器中
    • @lambdaepsilon 我明白了,所以你需要将容器居中,而不是图像 ^^。
    猜你喜欢
    • 2015-10-03
    • 2012-09-23
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-05-07
    • 2021-04-10
    • 1970-01-01
    • 2019-04-18
    相关资源
    最近更新 更多