คุณสมบัติCSS พื้นหลังตำแหน่ง
ตัวอย่าง
วิธีวางตำแหน่งภาพพื้นหลัง:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center;
}
ตัวอย่าง "ลองใช้เอง" เพิ่มเติมด้านล่าง
ความหมายและการใช้งาน
คุณสมบัติbackground-position
กำหนดตำแหน่งเริ่มต้นของภาพพื้นหลัง
เคล็ดลับ:ตามค่าเริ่มต้นรูปภาพพื้นหลังจะอยู่ที่มุมซ้ายบนขององค์ประกอบ และทำซ้ำทั้งในแนวตั้งและแนวนอน
ค่าเริ่มต้น: | 0% 0% |
---|---|
รับการถ่ายทอด: | ไม่ |
เคลื่อนไหวได้: | ใช่. อ่านเกี่ยวกับแอนิเมชั่น |
รุ่น: | CSS1 |
ไวยากรณ์ JavaScript: | วัตถุ .style.backgroundPosition="center" |
รองรับเบราว์เซอร์
ตัวเลขในตารางระบุเบราว์เซอร์รุ่นแรกที่สนับสนุนคุณสมบัติอย่างสมบูรณ์
Property | |||||
---|---|---|---|---|---|
background-position | 1.0 | 4.0 | 1.0 | 1.0 | 3.5 |
หมายเหตุ: IE8 และรุ่นก่อนหน้าไม่รองรับภาพพื้นหลังหลายภาพในองค์ประกอบเดียว
ไวยากรณ์ CSS
background-position: value;
มูลค่าทรัพย์สิน
Value | Description | Play it |
---|---|---|
left top left center left bottom right top right center right bottom center top center center center bottom |
If you only specify one keyword, the other value will be "center" | |
x% y% | The first value is the horizontal position and the second value is the vertical. The top left corner is 0% 0%. The right bottom corner is 100% 100%. If you only specify one value, the other value will be 50%. Default value is: 0% 0% | |
xpos ypos | The first value is the horizontal position and the second value is the vertical. The top left corner is 0 0. Units can be pixels (0px 0px) or any other CSS units. If you only specify one value, the other value will be 50%. You can mix % and positions | |
initial | Sets this property to its default value. Read about initial | |
inherit | Inherits this property from its parent element. Read about inherit |
ตัวอย่างเพิ่มเติม
ตัวอย่าง
วิธีจัดตำแหน่งภาพพื้นหลังให้อยู่กึ่งกลางด้านบน:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: center top;
}
ตัวอย่าง
วิธีจัดตำแหน่งภาพพื้นหลังให้อยู่ด้านล่างขวา:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: bottom right;
}
ตัวอย่าง
วิธีวางตำแหน่งภาพพื้นหลังโดยใช้เปอร์เซ็นต์:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50% 50%;
}
ตัวอย่าง
วิธีวางตำแหน่งภาพพื้นหลังโดยใช้พิกเซล:
body
{
background-image: url('w3css.gif');
background-repeat: no-repeat;
background-attachment: fixed;
background-position: 50px 150px;
}
ตัวอย่าง
ใช้คุณสมบัติพื้นหลังที่แตกต่างกันเพื่อสร้างภาพ "ฮีโร่":
.hero-image {
background-image: url("photographer.jpg"); /* The
image used */
background-color: #cccccc; /* Used if the image is
unavailable */
height: 500px; /* You must set a specified height */
background-position: center; /* Center the image */
background-repeat: no-repeat; /* Do not repeat the image */
background-size: cover; /* Resize the background image to cover the entire container */
}
หน้าที่เกี่ยวข้อง
กวดวิชาCSS: พื้นหลัง CSS
การอ้างอิง CSS: คุณสมบัติภาพพื้นหลัง
การอ้างอิง HTML DOM: คุณสมบัติ backgroundPosition