纯CSS实现设置半个字符的样式
建站知识 2021-07-03 08:15www.168986.cn长沙网站建设
在stackoverflow上看到的问题怎么给半个字符设置样式,很多大神给出了答案。我就等就来学习围观吧。
一基本解决方案
html:
复制代码
代码如下:<span class=”halfStyle” data-content=”X”>X</span>
<span class=”halfStyle” data-content=”Y”>Y</span>
<span class=”halfStyle” data-content=”Z”>Z</span>
<span class=”halfStyle” data-content=”A”>A</span>
css:
复制代码
代码如下:.halfStyle {
position:relative;
display:inline-block;
font-size:80px; / or any font size will work /
color: black; / or transparent, any color /
overflow:hidden;
white-space: pre; / to preserve the spaces from collapsing /
}
.halfStyle:before {
display:block;
z-index:1;
position:absolute;
:0;
left:0;
width: 50%;
content: attr(data-content); / dynamic content for the pseudo element /
overflow:hidden;
color: #f00;
}
效果如图
这种方法用于任何动态文本或单个字符,并且都是自动适用的。所有你需要做的就是在目标文本上添加一个class,剩下的就解决了。
,保留了原文的可访问性,可以被盲人或视障人士使用的屏幕阅读器识别。
单个字符的实现
纯CSS。所有你需要做的就是把.halfStyle class用在每个你想要渲染一半样式字符的元素上。
对于每个包含字符的span元素,你可以添加一个data属性,比如data-content=”X”,并且在伪元素上使用content:attr(data-content);这样,.halfStyle:before class将会是动态的,你不需要为每个实例进行硬编码
以下其它效果自行测试了。。。
二左右开弓,两边都设置样式
更改CSS:
复制代码
代码如下:.halfStyle {
position:relative;
display:inline-block;
font-size:80px; / or any font size will work /
color: transparent; / hide the base character /
overflow:hidden;
white-space: pre; / to preserve the spaces from collapsing /
}
.halfStyle:before { / creates the left part /
display:block;
z-index:1;
position:absolute;
:0;
width: 50%;
content: attr(data-content); / dynamic content for the pseudo element /
overflow:hidden;
pointer-events: none; / so the base char is selectable by mouse /
color: #f00; / for demo purposes /
text-shadow: 2px -2px 0px #af0; / for demo purposes /
}
.halfStyle:after { / creates the right part /
display:block;
direction: rtl; / very important, will make the width to start from right /
position:absolute;
z-index:2;
:0;
left:50%;
width: 50%;
content: attr(data-content); / dynamic content for the pseudo element /
overflow:hidden;
pointer-events: none; / so the base char is selectable by mouse /
color: #000; / for demo purposes /
text-shadow: 2px 2px 0px #0af; / for demo purposes /
}
三设置水平一半的样式
CSS:
复制代码
代码如下:.halfStyle {
position:relative;
display:inline-block;
font-size:80px; / or any font size will work /
color: transparent; / hide the base character /
overflow:hidden;
white-space: pre; / to preserve the spaces from collapsing /
}
.halfStyle:before { / creates the part /
display:block;
z-index:2;
position:absolute;
:0;
height: 50%;
content: attr(data-content); / dynamic content for the pseudo element /
overflow:hidden;
pointer-events: none; / so the base char is selectable by mouse /
color: #f00; / for demo purposes /
text-shadow: 2px -2px 0px #af0; / for demo purposes /
}
.halfStyle:after { / creates the bottom part /
display:block;
position:absolute;
z-index:1;
:0;
height: 100%;
content: attr(data-content); / dynamic content for the pseudo element /
overflow:hidden;
pointer-events: none; / so the base char is selectable by mouse /
color: #000; / for demo purposes /
text-shadow: 2px 2px 0px #0af; / for demo purposes /
}
四水平三分之一的样式
css:
复制代码
代码如下:.halfStyle { / base char and also the bottom 1/3 /
position:relative;
display:inline-block;
font-size:80px; / or any font size will work /
color: transparent;
overflow:hidden;
white-space: pre; / to preserve the spaces from collapsing /
color: #f0f;
text-shadow: 2px 2px 0px #0af; / for demo purposes /
}
.halfStyle:before { / creates the 1/3 /
display:block;
z-index:2;
position:absolute;
:0;
height: 33.33%;
content: attr(data-content); / dynamic content for the pseudo element /
overflow:hidden;
pointer-events: none; / so the base char is selectable by mouse /
color: #f00; / for demo purposes /
text-shadow: 2px -2px 0px #fa0; / for demo purposes /
}
.halfStyle:after { / creates the middle 1/3 /
display:block;
position:absolute;
z-index:1;
:0;
height: 66.66%;
content: attr(data-content); / dynamic content for the pseudo element /
overflow:hidden;
pointer-events: none; / so the base char is selectable by mouse /
color: #000; / for demo purposes /
text-shadow: 2px 2px 0px #af0; / for demo purposes /
}
五垂直三分之的样式
css:
复制代码
代码如下:.halfStyle { / base char and also the right 1/3 /
position:relative;
display:inline-block;
font-size:80px; / or any font size will work /
color: transparent; / hide the base character /
overflow:hidden;
white-space: pre; / to preserve the spaces from collapsing /
color: #f0f; / for demo purposes /
text-shadow: 2px 2px 0px #0af; / for demo purposes /
}
.halfStyle:before { / creates the left 1/3 /
display:block;
z-index:2;
position:absolute;
:0;
width: 33.33%;
content: attr(data-content); / dynamic content for the pseudo element /
overflow:hidden;
pointer-events: none; / so the base char is selectable by mouse /
color: #f00; / for demo purposes /
text-shadow: 2px -2px 0px #af0; / for demo purposes /
}
.halfStyle:after { / creates the middle 1/3 /
display:block;
z-index:1;
position:absolute;
:0;
width: 66.66%;
content: attr(data-content); / dynamic content for the pseudo element /
overflow:hidden;
pointer-events: none; / so the base char is selectable by mouse /
color: #000; / for demo purposes /
text-shadow: 2px 2px 0px #af0; / for demo purposes /
}
长沙网站设计
- 如何自己建一个网站 自己想建个网站,怎么建
- 如何制作网站免费建站 创建网站免费注册
- html简单网页代码 html简单网页代码超链接
- dreamweaver网页制作 dreamweaver网页制作模板
- 上海网站建设 上海网站建设制作微信
- 如何制作网站和网页 如何制作一个网页
- html网页制作代码大全 端午节html网页制作代码大
- app开发公司 app开发公司前十名
- html网页制作 html网页制作文字居中
- app制作一个需要多少钱 请人制作一个app多少钱
- 成都网站制作 成都网站制作维护
- 百度建一个网站多少钱 百度做个公司网站要多少
- html+css网页制作成品 web网页制作成品css+javascrip
- html网页制作案例 html网页设计案例
- html+css网页制作成品 web网页制作成品css+javascrip
- 个人网站模板 个人网站模板HTML