Home Web Front-end HTML Tutorial CSS 「Sprite」雪碧图应用_html/css_WEB-ITnose

CSS 「Sprite」雪碧图应用_html/css_WEB-ITnose

Jun 24, 2016 am 11:23 AM

  在写网页过程中,会遇到这种需要多个图标展示:

  

  如上图中的「女装」文字左边的图标。容易想到的解决方法是为每张图片加入CSS 「Sprite」雪碧图应用_html/css_WEB-ITnose标签,但这样做会增加HTTP请求数量,影响网站加载速度。比这更优的解决方案是:雪碧图sprite。

  所谓雪碧图就是把我们所需要的所有小图片用CSS sprite或者PS工具拼接成一张大图片,然后通过元素的background-image、background-positon属性完成图片的定位。

  为此,我做了个Demo。

  

   在学习的过程中,遇到了背景图片定位的问题,也就是background-position属性使用以及如何在这张大图片中定位到要显示的小图标。

   问题简化就是:

   

  答案是把这张图片设置为div的背景图。然后移动图片使图片3在div区域显示。

  假设4张小图大小一样,都是25x25像素,div也是25x25像素。因为把大图片设置为div的背景图时,默认图片的左上角顶点是与div区域的顶点重合的,所以要将「小图片3」向上垂直移动,水平方向保持不变。因此设置background-position:0px,-25px;(图片像素的定位可用CSS sprite工具查看)。

  就是这么个简单的问题,但我之前出于两点理解失误始终理解不了为什么像素值是负数。比如一个无序列表

    中每一项的
  • 都设置背景图片为这个大图片,当显示的时候,就像在一张完整的图片每个区域开个洞,各自看这张大图的局部,我这样理解就解释不了两个
  • 怎么都显示一样的小图片。我之所以有这样的理解是因为我忽略了每个li独自拥有这张大图,然后再移动这张大图,想显示哪个区域就显示哪个区域。再,为什么像素值是负数呢?我网上搜,发现也有不理解的网友认为直接把这个像素值为负数记住就行,有网友认为是div顶点动,图片不动,但也解释不通。后来查到的两句话点醒了我:
  • 怎么说呢,图片定位技术,实际上相当于开了一个窗户,你透过窗户看外面的景色。你的窗户大小是不会变的,但是景色却能改变位置,整张雪碧图,就是一个大的风景,通过控制他的定位,你就可以看到不同的风景,所以,可以先做适合大小的图片,然后再整合到一起去,通过负定位进行上移或左移,达到能让这个窗户显示自己正确背景的位置。
  • 以图片左上角为原点,整张图片向左和向上移为负,整张图片向右和向下移为正。
  •   有错误之处,还请指正。

      参考资料:

      W3C CSS背景属性

      显示切图的一部分作为背景显示

      CSS sprite在线工具

      本文Demo下载:

      模仿淘宝导航条(提取码:e0f5)

      

      

     

Statement of this Website
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Is HTML easy to learn for beginners? Is HTML easy to learn for beginners? Apr 07, 2025 am 12:11 AM

HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

Understanding HTML, CSS, and JavaScript: A Beginner's Guide Understanding HTML, CSS, and JavaScript: A Beginner's Guide Apr 12, 2025 am 12:02 AM

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

What is an example of a starting tag in HTML? What is an example of a starting tag in HTML? Apr 06, 2025 am 12:04 AM

AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

HTML, CSS, and JavaScript: Essential Tools for Web Developers HTML, CSS, and JavaScript: Essential Tools for Web Developers Apr 09, 2025 am 12:12 AM

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML: The Structure, CSS: The Style, JavaScript: The Behavior HTML: The Structure, CSS: The Style, JavaScript: The Behavior Apr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web Design The Future of HTML: Evolution and Trends in Web Design Apr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The Future of HTML, CSS, and JavaScript: Web Development Trends The Future of HTML, CSS, and JavaScript: Web Development Trends Apr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

See all articles