这篇文章主要讲解了“怎么用CSS生成26个字母”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用CSS生成26个字母”吧!
一、借助CSS border实现案例
实现效果如下(为实时渲染效果):
如何使用?
引用CSS文件,例如:
<link rel="stylesheet" href="./css-letters1.css">
或者直接CSS代码到你的项目中:
.letter {
color: #2486ff;
border-style: solid;
border-width: .5em;
display: inline-block;
position: relative;
}
.letter:after {
border-style: solid;
border-width: .5em;
content: '';
position: absolute;
}
.letter[data-char="A"] {
border-bottom: none;
border-radius: 1em 1em 0 0;
height: 2.05em;
margin-top: -.05em;
width: 1em;
}
.letter[data-char="A"]:after {
border-bottom: none;
border-left: none;
border-right: none;
left: 0;
right: 0;
top: .75em;
}
.letter[data-char="B"] {
border-radius: 0 1em 1em 0;
height: .5em;
width: 1em;
}
.letter[data-char="B"]:after {
border-radius: 0 1em 1em 0;
bottom: 100%;
height: .5em;
left: -.5em;
width: .9em;
}
.letter[data-char="C"] {
border-right: none;
border-radius: 1em 0 0 1em;
height: 1.5em;
width: 1.5em;
}
.letter[data-char="C"]:after {
border-bottom: none;
border-left: none;
border-top: none;
height: .5em;
right: 0;
top: 0;
width: .5em;
}
...
HTML部分如下:
<span class="letter" data-char="A"></span>
<span class="letter" data-char="B"></span>
<span class="letter" data-char="C"></span>
<span class="letter" data-char="D"></span>
<span class="letter" data-char="E"></span>
<span class="letter" data-char="F"></span>
<span class="letter" data-char="G"></span>
<span class="letter" data-char="H"></span>
<span class="letter" data-char="I"></span>
<span class="letter" data-char="J"></span>
<span class="letter" data-char="K"></span>
<span class="letter" data-char="L"></span>
<span class="letter" data-char="M"></span>
<span class="letter" data-char="N"></span>
<span class="letter" data-char="O"></span>
<span class="letter" data-char="P"></span>
<span class="letter" data-char="Q"></span>
<span class="letter" data-char="R"></span>
<span class="letter" data-char="S"></span>
<span class="letter" data-char="T"></span>
<span class="letter" data-char="U"></span>
<span class="letter" data-char="V"></span>
<span class="letter" data-char="W"></span>
<span class="letter" data-char="X"></span>
<span class="letter" data-char="Y"></span>
<span class="letter" data-char="Z"></span>
二、border加圆角与另一种风格字体
还是先看效果,实时渲染:
如何使用?
引用CSS文件,例如:
<link rel="stylesheet" href="./css-letters2.css">
或者直接复制CSS代码到你的项目中,由于篇幅限制,我这里仅显示前几个字母的CSS样式
.letter-a {
position: relative;
width: 30px;
height: 40px;
background: white;
border-radius: 10px 10px 0 0;
border-style: solid;
border-color: currentColor currentColor transparent currentColor;
border-width: 10px 10px 0 10px;
}
.letter-a::before {
content: "";
position: absolute;
top: 10px;
height: 10px;
width: 30px;
background: currentColor;
}
.letter-b {
position: relative;
width: 30px;
height: 30px;
border-width: 10px 10px 10px 10px;
border-style: solid;
border-color: transparent transparent transparent currentColor;
background: transparent;
}
.letter-b::before {
content: "";
position: absolute;
left: -10px;
top: -10px;
height: 10px;
width: 30px;
background: transparent;
border-radius: 0 12.5px 12.5px 0;
border: 10px solid currentColor;
}
.letter-b::after {
content: "";
position: absolute;
left: -10px;
bottom: -10px;
height: 10px;
width: 30px;
background: transparent;
border-radius: 0 12.5px 12.5px 0;
border: 10px solid currentColor;
}
...
HTML部分代码使用示意:
<span class="letter-a"></span>
<span class="letter-b"></span>
<span class="letter-c"></span>
<span class="letter-d"></span>
<span class="letter-e"></span>
<span class="letter-f"></span>
<span class="letter-g"></span>
<span class="letter-h"></span>
<span class="letter-i"></span>
<span class="letter-j"></span>
<span class="letter-k"></span>
<span class="letter-l"></span>
<span class="letter-m"></span>
<span class="letter-n"></span>
<span class="letter-o"></span>
<span class="letter-p"></span>
<span class="letter-q"></span>
<span class="letter-r"></span>
<span class="letter-s"></span>
<span class="letter-t"></span>
<span class="letter-u"></span>
<span class="letter-v"></span>
<span class="letter-w"></span>
<span class="letter-x"></span>
<span class="letter-y"></span>
<span class="letter-z"></span>
每个字母都可以独立使用。
不过这里的实现有个不好的是,这里的字母都是使用px单位实现的,因此,想要自如控制字母的大小不太方便。需要借助transform进行缩放控制才行。
三、活用transform的css-sans字体生成
使用CSS生成的 无衬线26个英文字母。
实时效果如下:
//zxx: 接缝处有些间隙是因为对字体进行缩放导致,实际1:1呈现时候不会有这个现象。
hover对应代码可以看到对应字体部件,非常使用CSS图形绘制的学习,如下截图示意:
如何使用?
复制页面上呈现的对应的CSS代码,然后HTML部分如下:
<div class="A"></div>
<div class="B"></div>
<div class="C"></div>
<div class="D"></div>
<div class="E"></div>
<div class="F"></div>
<div class="G"></div>
<div class="H"></div>
<div class="I"></div>
<div class="J"></div>
<div class="K"></div>
<div class="L"></div>
<div class="M"></div>
<div class="N"></div>
<div class="O"></div>
<div class="P"></div>
<div class="Q"></div>
<div class="R"></div>
<div class="S"></div>
<div class="T"></div>
<div class="U"></div>
<div class="V"></div>
<div class="W"></div>
<div class="X"></div>
<div class="Y"></div>
<div class="Z"></div>
要显示哪个字母,就复制对应HTML到页面上就好了。
感谢各位的阅读,以上就是“怎么用CSS生成26个字母”的内容了,经过本文的学习后,相信大家对怎么用CSS生成26个字母这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是编程网,小编将为大家推送更多相关知识点的文章,欢迎关注!