首页 > 其他分享 >css38 CSS Image Sprites

css38 CSS Image Sprites

时间:2024-06-01 16:54:44浏览次数:12  
标签:hover navsprites img gif Image css38 width background Sprites

https://www.w3schools.com/css/css_image_sprites.asp

 

Image Sprites

An image sprite is a collection of images put into a single image.

A web page with many images can take a long time to load and generates multiple server requests.

Using image sprites will reduce the number of server requests and save bandwidth.


Image Sprites - Simple Example

Instead of using three separate images, we use this single image ("img_navsprites.gif"):

navigation images

With CSS, we can show just the part of the image we need.

In the following example the CSS specifies which part of the "img_navsprites.gif" image to show:

Example

#home {
  width: 46px;
  height: 44px;
  background: url(img_navsprites.gif) 0 0;
}
<!DOCTYPE html>
<html>
<head>
<style>
#home {
  width: 46px;
  height: 44px;
  background: url(img_navsprites.gif) 0 0;
}

#next {
  width: 43px;
  height: 44px;
  background: url(img_navsprites.gif) -91px 0;
}
</style>
</head>
<body>

<img id="home" src="img_trans.gif" width="1" height="1">
<img id="next" src="img_trans.gif" width="1" height="1">

</body>
</html>

 

Example explained:

  • <img id="home" src="img_trans.gif"> - Only defines a small transparent image because the src attribute cannot be empty. The displayed image will be the background image we specify in CSS
  • width: 46px; height: 44px; - Defines the portion of the image we want to use
  • background: url(img_navsprites.gif) 0 0; - Defines the background image and its position (left 0px, top 0px)

This is the easiest way to use image sprites, now we want to expand it by using links and hover effects.


Image Sprites - Create a Navigation List

We want to use the sprite image ("img_navsprites.gif") to create a navigation list.

We will use an HTML list, because it can be a link and also supports a background image:

Example

#navlist {
  position: relative;
}

#navlist li {
  margin: 0;
  padding: 0;
  list-style: none;
  position: absolute;
  top: 0;
}

#navlist li, #navlist a {
  height: 44px;
  display: block;
}

#home {
  left: 0px;
  width: 46px;
  background: url('img_navsprites.gif') 0 0;
}

#prev {
  left: 63px;
  width: 43px;
  background: url('img_navsprites.gif') -47px 0;
}

#next {
  left: 129px;
  width: 43px;
  background: url('img_navsprites.gif') -91px 0;
}
<!DOCTYPE html>
<html>
<head>
<style>
#navlist {
  position: relative;
}

#navlist li {
  margin: 0;
  padding: 0;
  list-style: none;
  position: absolute;
  top: 0;
}

#navlist li, #navlist a {
  height: 44px;
  display: block;
}

#home {
  left: 0px;
  width: 46px;
  background: url('img_navsprites.gif') 0 0;
}

#prev {
  left: 63px;
  width: 43px;
  background: url('img_navsprites.gif') -47px 0;
}

#next {
  left: 129px;
  width: 43px;
  background: url('img_navsprites.gif') -91px 0;
}
</style>
</head>
<body>

<ul id="navlist">
  <li id="home"><a href="default.asp"></a></li>
  <li id="prev"><a href="css_intro.asp"></a></li>
  <li id="next"><a href="css_syntax.asp"></a></li>
</ul>

</body>
</html>

 

Example explained:

  • #navlist {position:relative;} - position is set to relative to allow absolute positioning inside it
  • #navlist li {margin:0;padding:0;list-style:none;position:absolute;top:0;} - margin and padding are set to 0, list-style is removed, and all list items are absolute positioned
  • #navlist li, #navlist a {height:44px;display:block;} - the height of all the images is 44px

Now start to position and style for each specific part:

  • #home {left:0px;width:46px;} - Positioned all the way to the left, and the width of the image is 46px
  • #home {background:url(img_navsprites.gif) 0 0;} - Defines the background image and its position (left 0px, top 0px)
  • #prev {left:63px;width:43px;} - Positioned 63px to the right (#home width 46px + some extra space between items), and the width is 43px
  • #prev {background:url('img_navsprites.gif') -47px 0;} - Defines the background image 47px to the right (#home width 46px + 1px line divider)
  • #next {left:129px;width:43px;} - Positioned 129px to the right (start of #prev is 63px + #prev width 43px + extra space), and the width is 43px
  • #next {background:url('img_navsprites.gif') -91px 0;} - Defines the background image 91px to the right (#home width 46px + 1px line divider + #prev width 43px + 1px line divider)

 

Image Sprites - Hover Effect

Now we want to add a hover effect to our navigation list.

Tip: The :hover selector can be used on all elements, not only on links.

Our new image ("img_navsprites_hover.gif") contains three navigation images and three images to use for hover effects:

navigation images

Because this is one single image, and not six separate files, there will be no loading delay when a user hovers over the image.

We only add three lines of code to add the hover effect:

Example

#home a:hover {
  background: url('img_navsprites_hover.gif') 0 -45px;
}

#prev a:hover {
  background: url('img_navsprites_hover.gif') -47px -45px;
}

#next a:hover {
  background: url('img_navsprites_hover.gif') -91px -45px;
}
<!DOCTYPE html>
<html>
<head>
<style>
#navlist {
  position: relative;
}

#navlist li {
  margin: 0;
  padding: 0;
  list-style: none;
  position: absolute;
  top: 0;
}

#navlist li, #navlist a {
  height: 44px;
  display: block;
}

#home {
  left: 0px;
  width: 46px;
  background: url('img_navsprites_hover.gif') 0 0;
}

#prev {
  left: 63px;
  width: 43px;
  background: url('img_navsprites_hover.gif') -47px 0;
}

#next {
  left: 129px;
  width: 43px;
  background: url('img_navsprites_hover.gif') -91px 0;
}

#home a:hover {
  background: url('img_navsprites_hover.gif') 0 -45px;
}

#prev a:hover {
  background: url('img_navsprites_hover.gif') -47px -45px;
}

#next a:hover {
  background: url('img_navsprites_hover.gif') -91px -45px;
}
</style>
</head>
<body>

<ul id="navlist">
  <li id="home"><a href="default.asp"></a></li>
  <li id="prev"><a href="css_intro.asp"></a></li>
  <li id="next"><a href="css_syntax.asp"></a></li>
</ul>

</body>
</html>

 

Example explained:

    • #home a:hover {background: url('img_navsprites_hover.gif') 0 -45px;} - For all three hover images we specify the same background position, only 45px further down

标签:hover,navsprites,img,gif,Image,css38,width,background,Sprites
From: https://www.cnblogs.com/emanlee/p/18226117

相关文章

  • css37 CSS Image Gallery
    https://www.w3schools.com/css/css_image_gallery.asp CSScanbeusedtocreateanimagegallery. ImageGalleryThefollowingimagegalleryiscreatedwithCSS:<!DOCTYPEhtml><html><head><style>div.gallery{margin:5px;......
  • delphi Image32 之 快速入门
     官方快速入门,加上了一些注解[从WORD粘贴后失去了样式]TImage32类是关键。TImage32 对象包含单个图像,所有图像操作都作用于此对象。usesImg32; //引用单元...img:=TImage32.Create; //创建TImage32对象//执行一些其它操作img.Free; //用完了要释放图像存储......
  • QImage和QPixmap的区别和使用
    一、基本概念和特点QImage概念:QImage是Qt库中用于处理图像数据的一个类。它提供了直接访问和操作图像像素的接口。特点:可以独立于屏幕分辨率和设备处理图像。支持读取和保存多种图像格式,如PNG、JPEG、BMP等。可以在没有图形界面的情况下使用,例如服务器端图像处理。内部存......
  • 如何在 ImageMap 热点上应用边框和更改边框颜色
    我渲染了一幅图像,并提供了定义了图像映射的热点。我还添加了鼠标悬停事件监听器,以便使用focus()来突出显示热点。我希望在加载图像后显示边框,并在用户点击热点时改变边框的颜色。点击功能可通过标签上的事件监听器实现,但要使边框显示出来似乎有点棘手。如果您对此有任何帮......
  • delphi 图形图像处理 Image32
    delpher 越来越少了,但不能掩盖它的优秀,很外前看到了Image32,但发现用它的人很少,这段时间整理了它的资料,重新组合了一个DEMO,也可以说是个小工具,分享出来。----下面的内容不能直接从WORD中复制过来,只能一点点粘贴,Image32 关于Image32说明文档是这样描述的:  用Delphi......
  • SwiftUI中AsyncImage的使用(一个高效的异步下载图片组件)
    iOS开发者经常会遇到需要在应用中显示网络图像的场景,无论是获取和显示用户头像,展示产品图像,等等。在原来的UIKit中,如果我们要用系统的API还是稍微有点麻烦,很多开发的朋友都选择了第三方的框架去处理网络图片的请求缓存等等。AsyncImage是SwiftUI中一个强大的功能,它简化了在......
  • WPF Image enlarge via MouseWheel, selected image center does not shift
    //xaml<Windowx:Class="WpfApp123.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • GEE C26 GEEDiT—Digitizing from Satellite Imagery从卫星影像数字化
     一、TheGoogleEarthEngineDigitisationTool(GEEDiT)  GEE数字化工具谷歌地球引擎数字化工具(GEEDiT)允许用户定义地球上任何地方的一个点,并通过一个简单的图形用户界面(GUI)对每个卫星的数据进行过滤,以获得用户定义的时间框架、最大可接受的云量,以及预定义或自定义图......
  • WPF Image ZoomIn ZoomOut pan
    <Windowx:Class="WpfApp120.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • LiT: Zero-shot transfer with locked-image text tuning
    郑重声明:原文参见标题,如有侵权,请联系作者,将会撤销发布!ProceedingsoftheIEEE/CVFConferenceonComputerVisionandPatternRecognition(CVPR),2022 Abstract 1.Introduction 2.Relatedwork 3.Methods 3.1.Contrastivepre-training 3.2.C......