search
Homephp教程PHP开发css中使用一张图片显示多个背景

     不知你有没有经历过,看到网页上的一些漂亮的图标,如列表图标,于是把它下载,打开一看,竟然不是我们下载的单个图标,而是一张大图,上面集合着许多小图标!
这里便使用了CSS中一张图片显示多个背景的方法
这些小图标为什么不是分别存储成单独的图标,而是集合放在一张图片上?这样做的目的就是减少http请求次数,节省时间和带宽。
强烈推荐使用这种方法!
当然,实现的方法是有多种,下面将通过一个例子来说一下我常使用的方法。
1、显示完整的图片

如图(1)

css中使用一张图片显示多个背景

代码:

<style type="text/css">  
<!--  
.bg{ 
WIDTH: 181px; 
HEIGHT: 61px; 
} 
.bg .logo { 
POSITION: relative; WIDTH: 181px; BACKGROUND: url(./logo.gif) 0px 0px; FLOAT: left; HEIGHT: 60px; 
} 
} 
--> 
</style>  
<body> 
<div class="bg"> 
<div class="logo"></div> 
</body>

关键代码:

POSITION: relative; WIDTH: xpx; BACKGROUND: url(image) x1px y1px; FLOAT: left; HEIGHT: ypx;

POSITION属性说明:
relative--相对定位
WIDTH--图片显示的宽度
image --背景图片(这里是图片的地址)
x1px--图片水平位置偏移量,起点为(0,0),如取负值将向反方向偏移
y1px---图片竖直位置偏移量
FLOAT--图片浮动方向,如left,right
HEIGHT--图片显示的高度

现在我们将在上面的位置为参照物显示图片不同区域。
1.1、只显示图片中的“W‘

如图(2):

css中使用一张图片显示多个背景

图示显示部分WIDTH: 75px,HEIGHT: 60px,图片水平向左偏移3px.代码:

.bg .logo { 
      
    POSITION: relative; WIDTH: 75px; BACKGROUND: url(./logo.gif) -3px 0px; FLOAT: left; HEIGHT: 60px; 
      
}

1.2、只显示图片中的一个“B“,如图(3)

css中使用一张图片显示多个背景
图示显示部分WIDTH: 38px,HEIGHT: 60px,图片水平向左偏移78px,竖直向下偏120px,代码:

.bg .logo { 
    POSITION: relative; WIDTH: 38px; BACKGROUND: url(./logo.gif) -78px 120px; FLOAT: left; HEIGHT: 60px 
}



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.