如何做 - 将文本置于图像之上
了解如何将文本放置在图像上。
图像文本

左下角
左上角
右上角
右下角
居中
如何将文本放置在图像中
步骤 1) 添加 HTML
示例
<div class="container">
<img src="img_snow_wide.jpg" alt="雪景" style="width:100%;">
<div class="bottom-left">左下角</div>
<div class="top-left">左上角</div>
<div class="top-right">右上角</div>
<div class="bottom-right">右下角</div>
<div class="centered">居中</div>
</div>
步骤 2) 添加 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 定位的信息,请阅读我们的 CSS 定位 教程。