จากที่ไหนที่จะทำให้ข้อความตั้งบนรูปภาพ

เรียนรู้ว่าเป็นอย่างไรที่จะทำให้ข้อความตั้งบนรูปภาพ

ข้อความรูปภาพ

กรุงปักกิ่ง
Bottom Left
Top Left
Top Right
Bottom Right
Centered

ทดลองด้วยตัวเอง

ฝากข้อความบนรูปภาพ

ขั้นที่ 1 - เพิ่ม 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>

ขั้นที่ 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 定位