图像中文本的垂直和水平居中位置,而不考虑分辨率的比例

斯凯勒·斯派思

我想将文本垂直和水平居中放置到图像中。我不在乎它是否是纯HTML / CSS。

到目前为止,我已经尝试过在StackOverflow上找到的以下解决方案:

.image {
    position: relative;
    width: 100%; /* for IE 6 */
}

h2 {
    position: absolute;
    top: 200px;
    left: 0;
    width: 100%;
}
<body>
    <div class="image">
        <img src="https://www.google.com/images/srpr/logo11w.png">
        <h2>Text</h2>
    </div>
</body>
来宾271314

尝试利用css :before伪元素,calc()

<!DOCTYPE html>
<html>

<head>
  <style>
    .image {
      position: relative;
      width: 100%;
      /* for IE 6 */
    }
    .image:before {
      position: absolute;
      left: calc(538px / 2 - 12px);
      top: calc(190px / 2 - 12px);
      width: 24px;
      content: "Text";
      display: block;
      font-size: 1.5em;
      font-weight: bold;
    }
  </style>
</head>

<body>
  <div class="image">
    <img src="https://www.google.com/images/srpr/logo11w.png">
  </div>
</body>

</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章