CSS 使用Sprites技术实现圆角效果

墨蓝 2021-11-22 09:34 411阅读 0赞

首先来简单说一下什么是Sprites,Sprites是一种网页图片应用处理方式。它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一幅一幅地慢慢显示出来了。对于当前网络流行的速度而言,不高于200KB的单张图片的所需载入时间基本是差不多的,所以无需顾忌这个问题。
第一步:创建我们的 Sprite
用PS等工具合成如图所示的图片(以一个像素的红线来区分)

第二步:编写HTML代码
首先,我们会给容器 div 一个 .roundedBox类 :

  1. 第二步:编写HTML代码
  2. 首先,我们会给容器 div 一个 .roundedBox :

现在,我们必须再增加四个div ,这会在将来创建圆角的时候用到。之后必须给每个加载一个类 .corner,同时也标识一个类来指定它们格子的位置。

  1. <div class="roundedBox">
  2. <strong>My content in roundedBox Type 1</strong>
  3. <div class="corner topLeft"></div>
  4. <div class="corner topRight"></div>
  5. <div class="corner bottomLeft"></div>
  6. <div class="corner bottomRight"></div>
  7. </div>

第三步:编写CSS样式
绝对定位元素通常都依照相对定位的父元素进行定位。如果这个父元素无法界定,那么它会去最近作相对定位的那个父元素,直至 body 标签。
让我们先来定义下所有的圆角
所有的圆角都必须定义绝对定位,并且注明高度跟宽度。 我的圆角定义的宽度跟高度都是 17px.

  1. .corner{position:absolute;width:17px;height:17px;}

现在开始定义 div 容器样式:

  1. .roundedBox {position:relative;}

任何定义有类 .roundedBox 的元素内,绝对定位元素都会相对于这个元素进行定位,而不是标签 body。 我们也必须设置一些padding值,如果没有设置,圆角将会覆盖我们的文本,这肯定不是我们想要的效果。 重要提示: top 和 bottom padding 值必须 等价于圆角的 height。left 和 right padding 值必须等价于圆角的宽度。 正如您已经知道的,我的圆角宽度跟高度是相等的,因此,四个边角的padding 值也是相等的:

  1. .roundedBox {position:relative; padding:17px; margin:10px 0;}

让我们对没有圆角作单独定义
我们会对每个圆角作绝对定位设置,并且定位背景图的位置 (根据我们的 sprite):

  1. .roundedBox {position:relative; padding:17px; margin:10px 0;}
  2. .corner {position:absolute; width:17px; height:17px;}
  3. .topLeft {top:0; left:0; background-position:-1px -1px;}
  4. .topRight {top:0; right:0; background-position:-19px -1px;}
  5. .bottomLeft {bottom:0; left:0; background-position:-1px -19px;}
  6. .bottomRight {bottom:0; right:0; background-position:-19px -19px;}

最后,给 #type1 匹配一个背景色,使之融合于 sprite 中的圆角:

  1. #type1 {background-color:#CCDEDE;}
  2. #type1 .corner {background-image:url(../image/corners.gif);}

全部的代码:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>无标题文档</title>
  6. <style type="text/css">
  7. .roundedBox {position:relative; padding:17px; margin:10px 0;}
  8. .corner {position:absolute; width:17px; height:17px;}
  9. .topLeft {top:0; left:0; background-position:-1px -1px;}
  10. .topRight {top:0; right:0; background-position:-19px -1px;}
  11. .bottomLeft {bottom:0; left:0; background-position:-1px -19px;}
  12. .bottomRight {bottom:0; right:0; background-position:-19px -19px;}
  13. #type1 {background-color:#CCDEDE;}
  14. #type1 .corner {background-image:url(../image/corners.gif);}
  15. </style>
  16. </head>
  17. <body>
  18. <div class="roundedBox" id="type1">
  19. <strong>My content in roundedBox Type 1</strong>
  20. <div class="corner topLeft"></div>
  21. <div class="corner topRight"></div>
  22. <div class="corner bottomLeft"></div>
  23. <div class="corner bottomRight"></div>
  24. </div>
  25. </body>
  26. </html>

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,411人围观)

还没有评论,来说两句吧...

相关阅读

    相关 css sprite

    CSSSprites在国内很多人叫css精灵,是一种网页图片应用处理方式。它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会

    相关 CSS sprites

    CSS Sprites其实就是把网页中一些背景图片整合到一张图片文件中, 再利用CSS的“background-image”,“background- rep

    相关 CSS圆角效果

    上一篇博文中提到了css2.0的圆角效果,但是没有实现,今天闲着没事儿做了一个,想想还是css3来的痛快,用css2做一个圆角确实还是挺痛苦的,其他不说了,先截个图    

    相关 CSS实现边框圆角

    CSS实现边框圆角 通过设置border-radius可以实现圆角边框的效果。 1.分别设置四个角的半径值 在border-radius属性中,依次书写四个角的半

    相关 CSS 使用Sprites技术实现圆角效果

    首先来简单说一下什么是Sprites,Sprites是一种网页图片应用处理方式。它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片