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

การสร้างเมนูการนำทางโดยใช้ CSS Image Sprite


ใช้รูปภาพสไปรท์เพื่อลดจำนวนคำขอ http ที่ทำให้โหลดไซต์ของเราเร็วขึ้น

ต่อไปนี้เป็นรหัสสำหรับสร้างเมนูการนำทางโดยใช้ CSS ภาพเทพดา -

ตัวอย่าง

<!DOCTYPE html>
<html>
<head>
<style>
body {
   font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   margin: 0px;
}
span {
   width: 200px;
   height: 300px;
   background-color: black;
}
nav {
   background-color: black;
   height: 50px;
   padding-top: 15px;
   padding-left: 10px;
}
nav a {
   font-size: 20px;
   color: white;
   text-decoration: none;
   margin-right: 10px;
}
.home {
   width: 32px;
   height: 32px;
   background: url("css_sprites.png") -62px -62px;
}
.search {
   width: 32px;
   height: 32px;
   background: url("css_sprites.png") -10px -62px;
}
.phone {
   width: 32px;
   height: 32px;
   background: url("css_sprites.png") -62px -10px;
}
.user {
   width: 32px;
   height: 32px;
   background: url("css_sprites.png") -10px -10px;
}
</style>
</head>
<body>
<nav>
<img class="home" />
<a href="">HOME</a>
<img class="phone" />
<a href="">Call Us</a>
<img class="user" />
<a href="">Our Team</a>
<img class="search" />
<a href="">Search</a>
</nav>
<h1>
Main content here
</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Animi, non! Numquam
reprehenderit alias, nisi eos corrupti repudiandae deleniti illo officiis!</p>
</body>
</html>

ผลลัพธ์

รหัสข้างต้นจะสร้างผลลัพธ์ต่อไปนี้ -

การสร้างเมนูการนำทางโดยใช้ CSS Image Sprite