เค้าโครง CSS - คุณสมบัติดัชนี z
คุณสมบัติz-index
ระบุลำดับสแต็กขององค์ประกอบ
ดัชนี z คุณสมบัติ
เมื่อองค์ประกอบอยู่ในตำแหน่ง พวกเขาสามารถซ้อนทับองค์ประกอบอื่นๆ
คุณสมบัติz-index
ระบุลำดับสแต็กขององค์ประกอบ (องค์ประกอบใดควรวางไว้ด้านหน้าหรือด้านหลังองค์ประกอบอื่นๆ)
องค์ประกอบสามารถมีลำดับกองบวกหรือลบ:
นี่คือหัวเรื่อง
เนื่องจากรูปภาพมีดัชนี z อยู่ที่ -1 รูปภาพจะถูกวางไว้ด้านหลังข้อความ
ตัวอย่าง
img
{
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
หมายเหตุ: z-index
ใช้ได้เฉพาะกับองค์ประกอบที่มีตำแหน่ง (ตำแหน่ง: สัมบูรณ์ ตำแหน่ง: สัมพันธ์ ตำแหน่ง: คงที่ หรือตำแหน่ง: เหนียว) และรายการแบบยืดหยุ่น
(องค์ประกอบที่เป็นลูกโดยตรงของการแสดงผล: องค์ประกอบแบบยืดหยุ่น)
ตัวอย่างดัชนี z อื่น
ตัวอย่าง
ที่นี่เราจะเห็นว่าองค์ประกอบที่มีลำดับกองซ้อนมากกว่ามักจะอยู่เหนือองค์ประกอบที่มีลำดับกองที่ต่ำกว่าเสมอ:
<html>
<head>
<style>
.container {
position: relative;
}
.black-box {
position: relative;
z-index: 1;
border: 2px solid black;
height: 100px;
margin: 30px;
}
.gray-box {
position: absolute;
z-index: 3;
background: lightgray;
height: 60px;
width: 70%;
left: 50px;
top: 50px;
}
.green-box {
position: absolute;
z-index: 2;
background: lightgreen;
width: 35%;
left: 270px;
top: -15px;
height:
100px;
}
</style>
</head>
<body>
<div class="container">
<div
class="black-box">Black box</div>
<div class="gray-box">Gray
box</div>
<div class="green-box">Green box</div>
</div>
</body>
</html>
ไม่มี z-index
หากองค์ประกอบสองตำแหน่งซ้อนทับกันโดยไม่มีการz-index
ระบุ องค์ประกอบที่กำหนดสุดท้ายในโค้ด HTMLจะแสดงที่ด้านบน
ตัวอย่าง
ตัวอย่างเดียวกับด้านบน แต่ที่นี่ไม่ได้ระบุดัชนี z:
<html>
<head>
<style>
.container {
position: relative;
}
.black-box {
position: relative;
border: 2px solid black;
height: 100px;
margin: 30px;
}
.gray-box {
position: absolute;
background: lightgray;
height: 60px;
width: 70%;
left: 50px;
top: 50px;
}
.green-box {
position: absolute;
background: lightgreen;
width: 35%;
left: 270px;
top: -15px;
height:
100px;
}
</style>
</head>
<body>
<div class="container">
<div
class="black-box">Black box</div>
<div class="gray-box">Gray
box</div>
<div class="green-box">Green box</div>
</div>
</body>
</html>
คุณสมบัติ CSS
Property | Description |
---|---|
z-index | Sets the stack order of an element |