【问题标题】:Center image within it's container [duplicate]在它的容器中居中图像[重复]
【发布时间】:2018-10-31 13:56:04
【问题描述】:

我似乎无法将我的图像居中,它的定位是相对于它的相对父级。

我用inspect element尝试了很多方法,但它似乎不想工作。

请在此处查看示例:http://dassiedev.dassieartisan.com/furniture

这是我的 CSS:

    .tm-switch-image-container {
    position: relative;
    overflow: hidden;
    height: 276px;
}
.tm-switch-image-container img {
    width: 276px;
    height: 276px;
    max-width: none;
}

【问题讨论】:

    标签: css margin text-align


    【解决方案1】:

    要在div 中居中img,请给img 一个display: inline-block 规则,然后给div 一个text-align: center 规则。这将使其水平居中。

    .tm-switch-image-container {
        position: relative;
        overflow: hidden;
        height: 276px;
        text-align: center;
    }
    
    .tm-switch-image-container img {
        width: 276px;
        height: 276px;
        display: inline-block;
    }
    

    【讨论】:

    • 这并没有完全得到 OP 正在寻找的结果(至少我相信如此)。您必须另外删除 .tm-switch-image-container img 上的宽度和高度样式,以使家具居中显示。
    • @JasonKlas 如果图像只是水平居中,并且容器的宽度大于 276px,那么这应该可以。
    • “并且容器比 276px 更宽”这解释了它,谢谢!我的显示器无法在不缩小它们的情况下水平容纳 3 张图像。
    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 2017-01-29
    • 2019-03-25
    • 1970-01-01
    • 2012-07-01
    • 2015-03-16
    • 2019-02-05
    • 2012-04-16
    相关资源
    最近更新 更多