CSS 使用Sprites技術(shù)實(shí)現(xiàn)圓角效果
首先來簡單說一下什么是Sprites,Sprites是一種網(wǎng)頁圖片應(yīng)用處理方式。它允許你將一個(gè)頁面涉及到的所有零星圖片都包含到一張大圖中去,這樣一來,當(dāng)訪問該頁面時(shí),載入的圖片就不會(huì)像以前那樣一幅一幅地慢慢顯示出來了。對(duì)于當(dāng)前網(wǎng)絡(luò)流行的速度而言,不高于200KB的單張圖片的所需載入時(shí)間基本是差不多的,所以無需顧忌這個(gè)問題。
第一步:創(chuàng)建我們的 Sprite
用PS等工具合成如圖所示的圖片(以一個(gè)像素的紅線來區(qū)分)
第二步:編寫HTML代碼
首先,我們會(huì)給容器 div 一個(gè) .roundedBox類 :
<div class="roundedBox"></div>
現(xiàn)在,我們必須再增加四個(gè)div ,這會(huì)在將來創(chuàng)建圓角的時(shí)候用到。之后必須給每個(gè)加載一個(gè)類 .corner,同時(shí)也標(biāo)識(shí)一個(gè)類來指定它們格子的位置。
<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樣式
絕對(duì)定位元素通常都依照相對(duì)定位的父元素進(jìn)行定位。如果這個(gè)父元素?zé)o法界定,那么它會(huì)去最近作相對(duì)定位的那個(gè)父元素,直至 body 標(biāo)簽。
讓我們先來定義下所有的圓角
所有的圓角都必須定義絕對(duì)定位,并且注明高度跟寬度。 我的圓角定義的寬度跟高度都是 17px.
.corner{position:absolute;width:17px;height:17px;}
現(xiàn)在開始定義 div 容器樣式:
.roundedBox {position:relative;}
任何定義有類 .roundedBox 的元素內(nèi),絕對(duì)定位元素都會(huì)相對(duì)于這個(gè)元素進(jìn)行定位,而不是標(biāo)簽 body。 我們也必須設(shè)置一些padding值,如果沒有設(shè)置,圓角將會(huì)覆蓋我們的文本,這肯定不是我們想要的效果。 重要提示: top 和 bottom padding 值必須 等價(jià)于圓角的 height。left 和 right padding 值必須等價(jià)于圓角的寬度。 正如您已經(jīng)知道的,我的圓角寬度跟高度是相等的,因此,四個(gè)邊角的padding 值也是相等的:
.roundedBox {position:relative; padding:17px; margin:10px 0;}
讓我們對(duì)沒有圓角作單獨(dú)定義
我們會(huì)對(duì)每個(gè)圓角作絕對(duì)定位設(shè)置,并且定位背景圖的位置 (根據(jù)我們的 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;}
最后,給 #type1 匹配一個(gè)背景色,使之融合于 sprite 中的圓角:
#type1 {background-color:#CCDEDE;}
#type1 .corner {background-image:url(../image/corners.gif);}
全部的代碼:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標(biāo)題文檔</title>
<style type="text/css">
.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;}
#type1 {background-color:#CCDEDE;}
#type1 .corner {background-image:url(../image/corners.gif);}
</style>
</head>
<body>
<div class="roundedBox" id="type1">
<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>
</body>
</html>
上一篇:input submit、button和回車鍵提交數(shù)據(jù)詳解
欄 目:CSS/HTML
下一篇:div的offsetLeft與style.left區(qū)別
本文標(biāo)題:CSS 使用Sprites技術(shù)實(shí)現(xiàn)圓角效果
本文地址:http://www.jygsgssxh.com/a1/CSS_HTML/11502.html
您可能感興趣的文章
- 01-11純CSS實(shí)現(xiàn)鼠標(biāo)放上去改變文字內(nèi)容
- 01-11CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享
- 01-11CSS3中Transition屬性詳解以及示例分享
- 01-11詳細(xì)分析css float 屬性以及position:absolute 的區(qū)別
- 01-11使用css實(shí)現(xiàn)全兼容瀏覽器的三角形
- 01-11使用css實(shí)現(xiàn)全兼容tooltip提示框
- 01-11從零學(xué)CSS系列之文本屬性
- 01-11用css截取字符的幾種方法詳解(css排版隱藏溢出文本)
- 01-11CSS3+Js實(shí)現(xiàn)響應(yīng)式導(dǎo)航條
- 01-11CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)


閱讀排行
本欄相關(guān)
- 04-02html5算法,html5協(xié)議
- 04-02html5圖形,html5圖形縮放
- 04-02html5登錄模板,html5登錄注冊(cè)模板
- 04-02html5的例子,HTML示例
- 04-02html5的canvas,html5的canvas的作用
- 04-02關(guān)于html5播放視頻代碼的信息
- 04-02html5chm手冊(cè),html操作手冊(cè)
- 04-02朋友圈html5,朋友圈發(fā)泄情緒的句子
- 04-02html5的新特性有哪些,HTML5的新特性有哪
- 04-02html5掃二維碼,html5調(diào)用手機(jī)攝像頭掃描
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 04-02jquery與jsp,用jquery


