純CSS實(shí)現(xiàn)設(shè)置半個(gè)字符的樣式
來源:易賢網(wǎng) 閱讀:1425 次 日期:2014-07-04 20:46:22
溫馨提示:易賢網(wǎng)小編為您整理了“純CSS實(shí)現(xiàn)設(shè)置半個(gè)字符的樣式”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了純CSS實(shí)現(xiàn)設(shè)置半個(gè)字符的樣式,分別實(shí)現(xiàn)了水平和垂直一半、水平和垂直三分之一等效果,需要的朋友可以參考下。

在stackoverflow上看到的問題怎么給半個(gè)字符設(shè)置樣式,很多大神給出了答案。我就等就來學(xué)習(xí)圍觀吧。

一:基本解決方案:

html:

代碼如下:

<spanclass=”halfStyle”data-content=”X”>X</span>

<spanclass=”halfStyle”data-content=”Y”>Y</span>

<spanclass=”halfStyle”data-content=”Z”>Z</span>

<spanclass=”halfStyle”data-content=”A”>A</span>

css:

代碼如下:

.halfStyle{

position:relative;

display:inline-block;

font-size:80px;/*oranyfontsizewillwork*/

color:black;/*ortransparent,anycolor*/

overflow:hidden;

white-space:pre;/*topreservethespacesfromcollapsing*/

}

.halfStyle:before{

display:block;

z-index:1;

position:absolute;

top:0;

left:0;

width:50%;

content:attr(data-content);/*dynamiccontentforthepseudoelement*/

overflow:hidden;

color:#f00;

}

效果如圖:

名單

這種方法用于任何動(dòng)態(tài)文本或單個(gè)字符,并且都是自動(dòng)適用的。所有你需要做的就是在目標(biāo)文本上添加一個(gè)class,剩下的就解決了。

同時(shí),保留了原文的可訪問性,可以被盲人或視障人士使用的屏幕閱讀器識(shí)別。

單個(gè)字符的實(shí)現(xiàn):

純CSS。所有你需要做的就是把.halfStyleclass用在每個(gè)你想要渲染一半樣式字符的元素上。

對(duì)于每個(gè)包含字符的span元素,你可以添加一個(gè)data屬性,比如data-content=”X”,并且在偽元素上使用content:attr(data-content);這樣,.halfStyle:beforeclass將會(huì)是動(dòng)態(tài)的,你不需要為每個(gè)實(shí)例進(jìn)行硬編碼

以下其它效果自行測(cè)試了。。。

二:左右開弓,兩邊都設(shè)置樣式

更改CSS:

代碼如下:

.halfStyle{

position:relative;

display:inline-block;

font-size:80px;/*oranyfontsizewillwork*/

color:transparent;/*hidethebasecharacter*/

overflow:hidden;

white-space:pre;/*topreservethespacesfromcollapsing*/

}

.halfStyle:before{/*createstheleftpart*/

display:block;

z-index:1;

position:absolute;

top:0;

width:50%;

content:attr(data-content);/*dynamiccontentforthepseudoelement*/

overflow:hidden;

pointer-events:none;/*sothebasecharisselectablebymouse*/

color:#f00;/*fordemopurposes*/

text-shadow:2px-2px0px#af0;/*fordemopurposes*/

}

.halfStyle:after{/*createstherightpart*/

display:block;

direction:rtl;/*veryimportant,willmakethewidthtostartfromright*/

position:absolute;

z-index:2;

top:0;

left:50%;

width:50%;

content:attr(data-content);/*dynamiccontentforthepseudoelement*/

overflow:hidden;

pointer-events:none;/*sothebasecharisselectablebymouse*/

color:#000;/*fordemopurposes*/

text-shadow:2px2px0px#0af;/*fordemopurposes*/

}

三:設(shè)置水平一半的樣式

CSS:

代碼如下:

.halfStyle{

position:relative;

display:inline-block;

font-size:80px;/*oranyfontsizewillwork*/

color:transparent;/*hidethebasecharacter*/

overflow:hidden;

white-space:pre;/*topreservethespacesfromcollapsing*/

}

.halfStyle:before{/*createsthetoppart*/

display:block;

z-index:2;

position:absolute;

top:0;

height:50%;

content:attr(data-content);/*dynamiccontentforthepseudoelement*/

overflow:hidden;

pointer-events:none;/*sothebasecharisselectablebymouse*/

color:#f00;/*fordemopurposes*/

text-shadow:2px-2px0px#af0;/*fordemopurposes*/

}

.halfStyle:after{/*createsthebottompart*/

display:block;

position:absolute;

z-index:1;

top:0;

height:100%;

content:attr(data-content);/*dynamiccontentforthepseudoelement*/

overflow:hidden;

pointer-events:none;/*sothebasecharisselectablebymouse*/

color:#000;/*fordemopurposes*/

text-shadow:2px2px0px#0af;/*fordemopurposes*/

}

四:水平三分之一的樣式

css:

代碼如下:

.halfStyle{/*basecharandalsothebottom1/3*/

position:relative;

display:inline-block;

font-size:80px;/*oranyfontsizewillwork*/

color:transparent;

overflow:hidden;

white-space:pre;/*topreservethespacesfromcollapsing*/

color:#f0f;

text-shadow:2px2px0px#0af;/*fordemopurposes*/

}

.halfStyle:before{/*createsthetop1/3*/

display:block;

z-index:2;

position:absolute;

top:0;

height:33.33%;

content:attr(data-content);/*dynamiccontentforthepseudoelement*/

overflow:hidden;

pointer-events:none;/*sothebasecharisselectablebymouse*/

color:#f00;/*fordemopurposes*/

text-shadow:2px-2px0px#fa0;/*fordemopurposes*/

}

.halfStyle:after{/*createsthemiddle1/3*/

display:block;

position:absolute;

z-index:1;

top:0;

height:66.66%;

content:attr(data-content);/*dynamiccontentforthepseudoelement*/

overflow:hidden;

pointer-events:none;/*sothebasecharisselectablebymouse*/

color:#000;/*fordemopurposes*/

text-shadow:2px2px0px#af0;/*fordemopurposes*/

}

五:垂直三分之的樣式

css:

代碼如下:

.halfStyle{/*basecharandalsotheright1/3*/

position:relative;

display:inline-block;

font-size:80px;/*oranyfontsizewillwork*/

color:transparent;/*hidethebasecharacter*/

overflow:hidden;

white-space:pre;/*topreservethespacesfromcollapsing*/

color:#f0f;/*fordemopurposes*/

text-shadow:2px2px0px#0af;/*fordemopurposes*/

}

.halfStyle:before{/*createstheleft1/3*/

display:block;

z-index:2;

position:absolute;

top:0;

width:33.33%;

content:attr(data-content);/*dynamiccontentforthepseudoelement*/

overflow:hidden;

pointer-events:none;/*sothebasecharisselectablebymouse*/

color:#f00;/*fordemopurposes*/

text-shadow:2px-2px0px#af0;/*fordemopurposes*/

}

.halfStyle:after{/*createsthemiddle1/3*/

display:block;

z-index:1;

position:absolute;

top:0;

width:66.66%;

content:attr(data-content);/*dynamiccontentforthepseudoelement*/

overflow:hidden;

pointer-events:none;/*sothebasecharisselectablebymouse*/

color:#000;/*fordemopurposes*/

text-shadow:2px2px0px#af0;/*fordemopurposes*/

}

更多信息請(qǐng)查看IT技術(shù)專欄

更多信息請(qǐng)查看網(wǎng)頁制作
易賢網(wǎng)手機(jī)網(wǎng)站地址:純CSS實(shí)現(xiàn)設(shè)置半個(gè)字符的樣式
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
相關(guān)閱讀網(wǎng)頁制作
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)