第二步: HTML 代码
首先,我们会给容器 div 一个 .roundedBox
类 :
<div class="roundedBox"></div>
现在,我们必须再增加四个 div ,这会在将来创建圆角的时候用到。之后必须给每个加载一个类 .corner,同时也标识一个类来指定它们格子的位置。
<div class="roundedBox"> <strong>My content in roundedBox Type 1</strong> <div class="corner topLeft"></div> <div class="corner topRight"></div>
<div class="corner bottomLeft"></div> <div class="corner bottomRight"></div> </div>
一切搞定? 嗯,让我们把注意力再转移到 CSS 代码上来。
第三步: CSS 代码
如你所知, (或者您会在这里快速学习到) 绝对定位元素通常都依照相对定位的父元素进行定位。. If this element is not defined, they will take as their parent relatively-positioned element, the body tag.如果这个父元素无法界定,那么它会去最近作相对定位的那个父元素,直至 body 标签。 哈?! - 好了,如果到此为止您仍没有掌握,不用担心,我们将在第二部分了解它。(翻译得有点拗,附上原文:Ok, if you didn’t get this, don’t worry, you’ll catch it in an second.)
让我们先来定义下所有的圆角:
所有的圆角都必须定义绝对定位,并且注明高度跟宽度。 我的圆角定义的宽度跟高度都是 17px. 
.corner { position:absolute; width:17px; height:17px; }
如果您是第一次切割矩形圆角,那么宽度跟高度很可能会不一样 (咄!)。
现在开始定义 div 容器样式:
.roundedBox {position:relative;}
任何定义有类 .roundedBox 的元素内,绝对定位元素都会相对于这个元素进行定位,而不是标签 body。 我们也必须设置一些padding值,如果没有设置,圆角将会覆盖我们的文本,这肯定不是我们想要的效果。 重要提示: top 和 bottom padding 值必须 等价于圆角的 height。left 和 right padding 值必须等价于圆角的宽度。 正如您已经知道的,我的圆角宽度跟高度是相等的,因此,四个边角的padding 值也是相等的:
.roundedBox {position:relative; padding:17px; margin:10px 0;}
我也通过 margin 给我们的 div 流出了一定的空隙 最后,让我们对没有圆角作单独定义
我们会对每个圆角作绝对定位设置,并且定位背景图的位置 (根据我们的 sprite):
.roundedBox {position:relative; padding:17px; margin:10px 0;}
.corner {position:absolute; width:17px; height:17px;}
.topLeft {top:0; left:0; background-position:-1px -1px;} .topRight {top:0; right:0; background-position:-19px -1px;} .bottomLeft {bottom:0; left:0; background-position:-1px -19px;} .bottomRight {bottom:0; right:0; background-position:-19px -19px;}
您可能已经注意到,我们的样式仍然还没有下载 sprite。我们一般不会去定义它们的原因是,我们会使用另外的方法。
出处:译言
责任编辑:bluehearts
上一页 CSS Sprites + 圆角 [1] 下一页 CSS Sprites + 圆角 [3]
◎进入论坛网页制作、WEB标准化版块参加讨论,我还想发表评论。
|