因此,我需要制作一个圆圈,每个圆圈中都有一个字母,而我希望将这些字母居中。但是,当我使用大小不同的字母时,它们居中的方式有所不同,在这种情况下,是小写q 和大写A (仅举例来说,可能是任何字母):
span {
align-items: center;
display: flex;
font-family: sans-serif;
justify-content: center;
border-radius: 50%;
height: 30px;
width: 30px;
border: 1px solid black;
float: left;
}
<span>q</span>
<span>A</span>
您可以看到小写字母q低于大写字母A,这是因为字母的大小不同并且q没有填充大写字母。这会导致字母q看起来无法正确居中。
这是一张用来模糊我的意思的图像,左边是我所得到的,右边是我想要的:
q应该稍微高一些,以使其看起来正确居中。 不用更改大小不同的每个字母的CSS就能做到吗?
答案 0 :(得分:1)
一个JavaScript解决方案。我将小写字母分为三种类型:标准小写字母,行下方和大写字母。我使用ASCII代码来定位它们,并添加CSS类并相应地更改margin-top
。您可以相应地添加更多例外(字母j可能需要下降一个像素。)
$('span').each(function(){
var letter = $(this).text();
var letterCode = letter.charCodeAt(0);
//check if lowercase
if (letter == letter.toLowerCase()){
$(this).addClass('lowercase');
}
if (letterCode == 106 || letterCode == 112 || letterCode == 113 || letterCode == 121 || letterCode == 103 ){ //jpqyg
$(this).addClass('below-line');
}
//All these other letters : bdfiklt
if(letterCode == 98 || letterCode == 100 || letterCode == 102 || letterCode == 105 || letterCode == 107 || letterCode == 108 || letterCode == 116){
$(this).addClass('tall-letters');
}
});
div {
align-items: center;
display: flex;
font-family: sans-serif;
justify-content: center;
border-radius: 50%;
height: 30px;
width: 30px;
border: 1px solid black;
float: left;
}
span{
margin-top: 0;
}
.lowercase{
margin-top: -3px;
}
.below-line{
margin-top: -6px;
}
.tall-letters{
margin-top: -1px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div><span>A</span></div>
<div><span>a</span></div>
<div><span>B</span></div>
<div><span>b</span></div>
<div><span>C</span></div>
<div><span>c</span></div>
<div><span>D</span></div>
<div><span>d</span></div>
<div><span>E</span></div>
<div><span>e</span></div>
<div><span>F</span></div>
<div><span>f</span></div>
<div><span>G</span></div>
<div><span>g</span></div>
<div><span>H</span></div>
<div><span>h</span></div>
<div><span>I</span></div>
<div><span>i</span></div>
<div><span>J</span></div>
<div><span>j</span></div>
<div><span>K</span></div>
<div><span>k</span></div>
<div><span>L</span></div>
<div><span>l</span></div>
<div><span>M</span></div>
<div><span>m</span></div>
<div><span>N</span></div>
<div><span>n</span></div>
<div><span>O</span></div>
<div><span>o</span></div>
<div><span>P</span></div>
<div><span>p</span></div>
<div><span>Q</span></div>
<div><span>q</span></div>
<div><span>R</span></div>
<div><span>r</span></div>
<div><span>S</span></div>
<div><span>s</span></div>
<div><span>T</span></div>
<div><span>t</span></div>
<div><span>U</span></div>
<div><span>u</span></div>
<div><span>T</span></div>
<div><span>t</span></div>
<div><span>U</span></div>
<div><span>u</span></div>
<div><span>V</span></div>
<div><span>v</span></div>
<div><span>W</span></div>
<div><span>w</span></div>
<div><span>X</span></div>
<div><span>x</span></div>
<div><span>Y</span></div>
<div><span>y</span></div>
<div><span>Z</span></div>
<div><span>z</span></div>