【问题标题】:CSS set height related to the widthCSS设置高度与宽度相关
【发布时间】:2018-07-04 17:37:20
【问题描述】:

我正在使用 HTML 和 CSS 制作专辑页面,我想将图像的高度设置为宽度的 9/16。

HTML 代码

<div class = "col-md-12" id = "album-list">
    <div class = "col-md-4">
        <div id = "album-card" style = "border: 1px solid grey;">
            <img class = "image_thumbnail" src = "images/sampleImage.jpg" alt = "Thumbnail" />
            <div style = "padding-top: 10px; padding-bottom: 5px;">
                <p class = "card-text"><strong>TITLE HERE</strong></p>
            </div>
        </div>
    </div>
    <!-- More Columns -->
</div>

CSS 样式

.image_thumbnail {
    width: 100%;
}

我在stackoverflow上看到一些帖子有类似的问题,答案是使用padding-bottom根据宽度设置高度。但是,由于我在#album-card 周围设置了边框,所以#album-card 的高度比预期的要长。

如何设置宽度的高度 9/16??

【问题讨论】:

标签: html css


【解决方案1】:

要转换图像:

var images = document.querySelectorAll('.image_thumbnail');
for (var i = 0; i < images.length; i++) {
    images[i].height = images[i].width * 0.5625;
}

这将选择页面上具有相应类的所有图像,并将相对于它们的宽度直接转换高度(如:16 / 9 = 0.5625)。

【讨论】:

    【解决方案2】:

    借助 css3 中的 calc(),您可以计算宽度。

    .image_thumbnail {
        width: calc((100%*9)/16);
    }
    

    我是Referance calc(),这可能对你有帮助。

    【讨论】:

    • 我知道这应该根据经验起作用,但仍然不起作用..不知道为什么
    • 这是将高度设置为高度的 9/16。 @GreenRoof
    • @GreenRoof,你可以在 jquery 中使用calccss(),我想这会解决你的问题......!
    猜你喜欢
    • 2014-03-15
    • 2011-08-24
    • 2010-10-29
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2013-05-17
    相关资源
    最近更新 更多