How to vertically align all text in CSS?

Chron Bag

The issue seems to be that certain letters like g, y, q, etc. that have a tail that slopes downwards, do not allow for vertical centering. Here's an image to showcase the problem a.

The characters in the green box are basically perfect, as they have no downward tail. Those in the red box demonstrate the problem.

I would like for all characters to be perfectly vertically centered. In the image, characters with a downward tail are not vertically centered. Is this possible to rectify?

Here is the fiddle that demonstrates the problem in full.

.avatar {
    border-radius: 50%;
    display: inline-block;
    text-align: center;
    width: 125px;
    height: 125px;
    font-size: 60px;
    background-color: rgb(81, 75, 93);
    font-family: "Segoe UI";
    margin-bottom: 10px;
}

.character {
    position: relative;
    top: 50%;
    transform: translateY(-50%);
    line-height: 100%;
    color: #fff;
}
<div class="avatar">
  <div class="character">W</div>
</div>

<div class="avatar">
  <div class="character">y</div>
</div>

Temani Afif

Here is my solution using JS. The idea is to transform the element into an image in order to get its data as pixel then loop through them to find the top and bottom of each character and apply a translation to fix the alignment. This will work with dynamic font properties.

The code is not optimized but it highlight the main idea:

var elems = document.querySelectorAll(".avatar");

var fixes = [];


for (var i = 0; i < elems.length; i++) {
  var current = elems[i];
  domtoimage.toPixelData(current)
    .then(function(im) {
      /* Search for the top limit */
      var t = 0;
      for (var y = 0; y < current.scrollHeight; ++y) {
        for (var x = 0; x < current.scrollWidth; ++x) {
          var j = (4 * y * current.scrollHeight) + (4 * x);
          if (im[j] == 255 && im[j + 1] == 255 && im[j + 2] == 255) {
            t = y;
            break;
          }
        }
      }
      /* Search the bottom limit*/
      var b = 0;
      for (var y = (current.scrollHeight - 1); y >= 0; --y) {
        for (var x = (current.scrollWidth - 1); x >= 0; --x) {
          var j = (4 * y * current.scrollHeight) + (4 * x);
          if (im[j] == 255 && im[j + 1] == 255 && im[j + 2] == 255) {
            b = current.scrollHeight - y;
            break;
          }
        }
      }
      /* get the difference and apply a translation*/
      var diff = (b - t)/2;
      fixes.push(diff);
      /* we apply the translation when all are calculated*/
      if(fixes.length == elems.length) {
        for (var k = 0; k < elems.length; k++) {
          elems[k].querySelector('.character').style.transform = "translateY(" + fixes[k] + "px)";
        }
      }
    });
}
.avatar {
  border-radius: 50%;
  display: inline-flex;
  vertical-align:top;
  justify-content: center;
  align-items: center;
  width: 125px;
  height: 125px;
  font-size: 60px;
  background: 
    linear-gradient(red,red) center/100% 1px no-repeat,
    rgb(81, 75, 93);
  font-family: "Segoe UI";
  margin-bottom: 10px;
}

.character {
  color: #fff;
}
<script type="text/javascript" src="https://css-challenges.com/wp-content/themes/ronneby_child/js/dom-to-image.js"></script>
<div class="avatar">
  <div class="character">W</div>
</div>

<div class="avatar">
  <div class="character">y</div>
</div>

<div class="avatar">
  <div class="character" style="font-size:35px">a</div>
</div>

<div class="avatar">
  <div class="character" style="font-size:25px">2</div>
</div>
<div class="avatar">
  <div class="character">o</div>
</div>
<div class="avatar">
  <div class="character">|</div>
</div>
<div class="avatar">
  <div class="character">@</div>
</div>
<div class="avatar">
  <div class="character">Â</div>
</div>

<div class="avatar">
  <div class="character" style="font-family:arial">Q</div>
</div>
<div class="avatar">
  <div class="character">~</div>
</div>
<div class="avatar">
  <div class="character">8</div>
</div>

<div class="avatar">
  <div class="character">ä</div>
</div>
<div class="avatar">
  <div class="character">ç</div>
</div>

<div class="avatar">
  <div class="character">$</div>
</div>

<div class="avatar">
  <div class="character">></div>
</div>
<div class="avatar">
  <div class="character">%</div>
</div>

UPDATE

Here is a first optimization of the code:

var elems = document.querySelectorAll(".avatar");
var k = 0;

for (var i = 0; i < elems.length; i++) {
  domtoimage.toPixelData(elems[i])
    .then(function(im) {
     var l = im.length;
      /* Search for the top limit */
      var t = 0;
      for (var j = 0; j < l; j+=4) {
          if (im[j+1] == 255) { /* Since we know the colors, we can only test the G composant */
            t = Math.ceil((j/4)/125);
            break;
          }
      }
      /* Search the bottom limit*/
      var b = 0;
      for (var j = l - 1; j >= 0; j-=4) {
          if (im[j+1] == 255) {
            b = 125 - Math.ceil((j/4)/125);
            break;
          }
      }
      /* get the difference and apply a translation*/
      elems[k].querySelector('.character').style.transform = "translateY(" + (b - t)/2 + "px)";
      k++;
    });
}
.avatar {
  border-radius: 50%;
  display: inline-flex;
  vertical-align:top;
  justify-content: center;
  align-items: center;
  width: 125px;
  height: 125px;
  font-size: 60px;
  background: 
    linear-gradient(red,red) center/100% 1px no-repeat,
    rgb(81, 75, 93);
  font-family: "Segoe UI";
  margin-bottom: 10px;
}

.character {
  color: #fff;
}
<script type="text/javascript" src="https://css-challenges.com/wp-content/themes/ronneby_child/js/dom-to-image.js"></script>
<div class="avatar">
  <div class="character">W</div>
</div>

<div class="avatar">
  <div class="character">y</div>
</div>

<div class="avatar">
  <div class="character" style="font-size:35px">a</div>
</div>

<div class="avatar">
  <div class="character" style="font-size:25px">2</div>
</div>
<div class="avatar">
  <div class="character">o</div>
</div>
<div class="avatar">
  <div class="character">|</div>
</div>
<div class="avatar">
  <div class="character">@</div>
</div>
<div class="avatar">
  <div class="character">Â</div>
</div>

<div class="avatar">
  <div class="character" style="font-family:arial">Q</div>
</div>
<div class="avatar">
  <div class="character">~</div>
</div>
<div class="avatar">
  <div class="character">8</div>
</div>

<div class="avatar">
  <div class="character">ä</div>
</div>
<div class="avatar">
  <div class="character">ç</div>
</div>

<div class="avatar">
  <div class="character">$</div>
</div>

<div class="avatar">
  <div class="character">></div>
</div>
<div class="avatar">
  <div class="character">%</div>
</div>

I am using dom-to-image plugin for this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to align text vertically equal top CSS

From Dev

CSS - how to align text and an image vertically?

From Dev

How to align text vertically?

From Dev

Text Align Middle Vertically CSS

From Dev

Align an unwrapped text vertically in css

From Dev

CSS how to vertically align text within a pseudo element

From Dev

CSS3: How to align text vertically within a div?

From Dev

How to align text in middle (vertically)

From Dev

How to align text vertically in JavaFX?

From Dev

CSS: Right align a vertically rotated text

From Dev

css align text in input vertically to top

From Dev

css text align vertically in responsive site

From Dev

css align text in input vertically to top

From Dev

Align text vertically inside div with CSS

From Dev

CSS vertically align text / buttons in the middle

From Dev

How to vertically align text in a definition list?

From Java

How do I vertically align text in a div?

From Java

How to vertically align text with icon font?

From Java

How to align View vertically to the text inside TextView?

From Dev

How to align vertically span text (floated) to bottom

From Dev

How to Vertically Align Text Using UILabel and NSAttributedString

From Dev

How to vertically align bootstrap buttons with text in a row?

From Dev

How to vertically align text in the middle of two divs

From Dev

How to align text horizontally & vertically in UITextView?

From Dev

How to Vertically "center" align the multi line text

From Dev

How to vertically align text in a TextView with Compound Drawable

From Dev

How to vertically align text beside an image in the footer?

From Dev

How to vertically align text over an image

From Dev

How to vertically align bootstrap buttons with text in a row?

Related Related

  1. 1

    How to align text vertically equal top CSS

  2. 2

    CSS - how to align text and an image vertically?

  3. 3

    How to align text vertically?

  4. 4

    Text Align Middle Vertically CSS

  5. 5

    Align an unwrapped text vertically in css

  6. 6

    CSS how to vertically align text within a pseudo element

  7. 7

    CSS3: How to align text vertically within a div?

  8. 8

    How to align text in middle (vertically)

  9. 9

    How to align text vertically in JavaFX?

  10. 10

    CSS: Right align a vertically rotated text

  11. 11

    css align text in input vertically to top

  12. 12

    css text align vertically in responsive site

  13. 13

    css align text in input vertically to top

  14. 14

    Align text vertically inside div with CSS

  15. 15

    CSS vertically align text / buttons in the middle

  16. 16

    How to vertically align text in a definition list?

  17. 17

    How do I vertically align text in a div?

  18. 18

    How to vertically align text with icon font?

  19. 19

    How to align View vertically to the text inside TextView?

  20. 20

    How to align vertically span text (floated) to bottom

  21. 21

    How to Vertically Align Text Using UILabel and NSAttributedString

  22. 22

    How to vertically align bootstrap buttons with text in a row?

  23. 23

    How to vertically align text in the middle of two divs

  24. 24

    How to align text horizontally & vertically in UITextView?

  25. 25

    How to Vertically "center" align the multi line text

  26. 26

    How to vertically align text in a TextView with Compound Drawable

  27. 27

    How to vertically align text beside an image in the footer?

  28. 28

    How to vertically align text over an image

  29. 29

    How to vertically align bootstrap buttons with text in a row?

HotTag

Archive