實現(xiàn)關(guān)閉按鈕方法:1、創(chuàng)建一個標(biāo)簽;2、使用“標(biāo)簽id名::before {content: "2716";}”屬性,將按鈕設(shè)置成叉按鈕;3、使用“position: absolute;”配合上下左右進(jìn)行定位,放置到指定位置。
本教程操作環(huán)境:windows7系統(tǒng)、CSS3&&HTML5版、Dell G3電腦。
在本文中將分享用CSS3設(shè)計出來的樣式來呈現(xiàn)出關(guān)閉按鈕的樣子,這樣以后想設(shè)計簡單的對話框,就不需要求美工來幫忙畫按鈕的圖片了。先來看看樣式:
/* basic style */ .close { /* still bad on picking color */ background: orange; color: red; /* make a round button */ border-radius: 12px; /* center text */ line-height: 20px; text-align: center; height: 20px; width: 20px; font-size: 18px; padding: 1px; }/* use cross as close button */ .close::before { content: "2716"; } /* place the button on top-right */ .close { top: -10px; right: -10px; position: absolute; }
然后直接把它加到HTML元素上就可以看到效果了:
<div style="height: 100px; width: 100px; border: 1px solid black; position: relative;"> <span class="close"></span> </div>
效果:
這主要是利用了在CSS的::before
偽屬性可以直接添加內(nèi)容的特點,這樣就可以直接設(shè)置樣式在class上而不用做其它操作。當(dāng)然這里用::after
效果也是一樣的。唯一要注意的是如果要指定關(guān)閉按鈕在特定位置,就需要使用到position:absolut
;,相應(yīng)的其父元素就不能是默認(rèn)的position:static
;。
對于按鈕中的叉按鈕,用的是Unicode
中的一種標(biāo)識,以下這些也可以使用:
只是使用了不同字符后,可能居中位置會有偏差需要重新調(diào)整。
推薦學(xué)習(xí):css視頻教程