純css實現(xiàn)設置半個字符的樣式
來源:易賢網 閱讀:907 次 日期:2016-06-23 09:12:43
溫馨提示:易賢網小編為您整理了“純css實現(xiàn)設置半個字符的樣式”,方便廣大網友查閱!

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

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

一:基本解決方案:

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;

}

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

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

單個字符的實現(xiàn):

純css。所有你需要做的就是把.halfstyleclass用在每個你想要渲染一半樣式字符的元素上。

對于每個包含字符的span元素,你可以添加一個data屬性,比如data-content=”x”,并且在偽元素上使用content:attr(data-content);這樣,.halfstyle:beforeclass將會是動態(tài)的,你不需要為每個實例進行硬編碼

以下其它效果自行測試了。。

二:左右開弓,兩邊都設置樣式

更改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*/

}

三:設置水平一半的樣式

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*/

}

更多信息請查看網頁制作
下一篇:html元素:isindex

2025國考·省考課程試聽報名

  • 報班類型
  • 姓名
  • 手機號
  • 驗證碼
關于我們 | 聯(lián)系我們 | 人才招聘 | 網站聲明 | 網站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 新媒體/短視頻平臺 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網安備53010202001879號 人力資源服務許可證:(云)人服證字(2023)第0102001523號
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關注公眾號:hfpxwx
咨詢QQ:1093837350(9:00—18:00)版權所有:易賢網