Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> CSS

วิธีสร้าง Breadcrumb Navigation (บ레드ครัมบ์) ด้วย CSS

สร้าง Breadcrumb Navigation (แถบแสดงตำแหน่งหน้าเว็บ) ใช้ <ul> <li> และ Pseudo-element ::before สร้างเครื่องหมาย "/" คั่นระหว่างรายการ

ตัวอย่างโค้ด

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body {    margin: 0px;    margin-top: 10px;    padding: 0px; } .breadcrumb {    background-color: rgb(39, 39, 39);    overflow: auto;    height: auto; } li {    display: inline-block;    text-align: center;    padding: 10px;    font-size: 17px; } .links {    text-decoration: none;    color: rgb(178, 137, 253); } .links:hover {    text-decoration: underline; } .breadcrumb ul li:before {    padding: 8px;    color: white;    content: "/\00a0";    text-decoration: none; } ul:last-child {    color: white;    font-weight: bolder;    font-family: monospace; } </style> </head> <body> <h1>Breadcrumb Navigation Example</h1> <div class="breadcrumb"> <ul> <li><a class="links" href="#">Root</a></li> <li><a class="links" href="#">Home</a></li> <li><a class="links" href="#">User</a></li> <li><a class="links" href="#">Desktop</a></li> <li>Games</li> </ul> </div> <h2>Hover over the links to see effect</h2> </body> </html>

คำอธิบาย

  • li { display: inline-block; } - เรียงแนวนอน
  • li:before { content: "/\00a0"; } - เพิ่ม "/" + space (non-breaking) ก่อนแต่ละรายการ
  • รายการสุดท้าย (ul:last-child - selector ผิด ควรเป็น li:last-child) ไม่มีลิงก์ แสดงข้อความธรรมดา ตัวหนา สีขาว
  • Hover ลิงก์ -> Underline

แก้ไข Selector

/* ผิด */ ul:last-child { ... }
/* ถูก */ li:last-child { ... }

ผลลัพธ์

แถบสีเข้ม: Root / Home / User / Desktop / Games (รายการสุดท้ายไม่มีลิงก์ ตัวหนาสีขาว)