使用 CSS 图像精灵创建导航菜单
使用图像精灵可以减少 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>输出
以上代码将产生以下输出 −

Advertisements
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP