【问题标题】:centered image but text is under image居中的图像,但文字在图像下方
【发布时间】:2015-02-07 22:30:52
【问题描述】:

使用这个 css 我可以使图像居中,但是,图像现在浮动在文本之上。最初我的顶部周围都是 0,但是这些尺寸使图像正确居中。如何将下一行文本保留在图像下方而不是下方?

img.center {
    position: absolute;
    top: 40px; bottom:50px; left: 105px; right:105px;
    margin: auto;
}

【问题讨论】:

  • 添加 jsfiddle 示例
  • 如果您的内容是静态的,您可以在包含您的文本的元素上设置顶部,但是如果您的图像会得到更新,那么绝对定位是很难维护的。

标签: html css image center


【解决方案1】:

绝对定位使图像“脱离流程”。要将其保留在文档流中,您可以使用以下内容:

如果你知道父母的身高:

.parent {
    line-height: 100px;
}
.parent img {
    vertical-align: middle;
}

如果你不知道高度:

.parent {
    display: table;
}
.parent img {
    display: table-cell;
    vertical-align: middle;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 2021-05-29
    • 2013-12-25
    • 2021-09-09
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多