如何创建:将文本定位在图像上

学习如何将文本放置在图像上。

图像文本

Beijing
Bottom Left
Top Left
Top Right
Bottom Right
Centered

亲自试一试

如何将文本放置在图像中

第一步 - 添加 HTML:

<div class="container">
  <img src="img_snow_wide.jpg" alt="Snow" style="width:100%;">
  <div class="bottom-left">Bottom Left</div>
  <div class="top-left">Top Left</div>
  <div class="top-right">Top Right</div>
  <div class="bottom-right">Bottom Right</div>
  <div class="centered">Centered</div>
</div>

第二步 - 添加 CSS:

/* 包含图像和文本的容器 */
.container {
  position: relative;
  text-align: center;
  color: white;
}

/* 左下角文字 */
.bottom-left {
  position: absolute;
  bottom: 8px;
  left: 16px;
}

/* 左上角文本 */
.top-left {
  position: absolute;
  top: 8px;
  left: 16px;
}

/* 右上角文本 */
.top-right {
  position: absolute;
  top: 8px;
  right: 16px;
}

/* 右下角文本 */
.bottom-right {
  position: absolute;
  bottom: 8px;
  right: 16px;
}

/* 居中文本 */
.centered {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

亲自试一试

相关页面

教程:CSS 图像

教程:CSS 定位