Center individual letters visually regardless of whether they are capital or lowercase

℡╲_俬逩灬. 提交于 2019-12-13 03:35:59

问题


So I need to make circles that each have a single letter in them and I'm looking to center the letters. There is however a difference in how they get centered when I use letters with different sizes, in this case the lowercase q and the capital A (just an example, it could be any letter):

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>

You can see the lowecase q being lower than the uppercase A, which is because the letters have different sizes and the q doesn't fill the upper part. This causes the letter q to not look centered correctly.

Here's an image to demontrate what I mean, left being what I got, right being what I want:

The q should be a bit higher in order to make it look centered correctly. Is it possible to do this without changing the CSS for every individual letter with a different size?


回答1:


A javascript solution. I've broken down the lowercase letters in to three different types: standard lowercase, below the line, and tall letters. I'm targeting them using ASCII codes and adding CSS classes and changing margin-top accordingly. You could add in some more exceptions accordingly (the letter j might need to come down a pixel.)

Here is a pen.

$('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>


来源:https://stackoverflow.com/questions/51190682/center-individual-letters-visually-regardless-of-whether-they-are-capital-or-low

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!