PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 17, 2022

[FIXED] How can I center a single unicode character vertically in a container?

 November 17, 2022     css, html, typography, vertical-alignment     No comments   

Issue

When I'm making web pages I like to use unicode characters for icons when images or SVGs really aren't necessary. It's easy for me and it makes the page lighter. This works pretty well, usually, except that I always end up having to fiddle with the offset to get it centered correctly.

Yes, yes, I know about this:

.someDiv {
   width: 10px;
   height: 10px;
   line-height: 10px;
   text-align: center;
}

But the catch is that most glyphs are not themselves vertically centered. This means that I end up, for example, with this vertically off-center ● in a square container with that CSS:

example of off-center circle glyph inside its container

Is there a CSS way to center the glyph and not the font's mean line? (Or whatever is technically being centered.)


Solution

I was able to achieve something that works reasonably well by ditching CSS and thinking a little more outside the box -

First I downloaded a Noto font with the symbols I want, since Noto fonts are very permissively licensed. https://www.google.com/get/noto/

Then I installed fontforge http://fontforge.github.io/

I added this Python script to my %appdata%/Roaming/fontforge/python/ directory (seeing as I'm on Windows) https://github.com/gumblex/stamico/blob/master/centerglyph.py

I opened the Noto TTF with fontforge. Using select-all then the Tools > Metrics > Center in Glyph option that the script adds, then some adjusting with Tools > Metrics > Y Offset I was able to vertically center all the glyphs in the font. (In my case, for some reason, choosing the Center in Glyph/Center in Height options resulted in glyphs that were positioned higher than the center line, so I had to further adjust them.)

I added a @font-face to my CSS and specified the font style for glyphs that I want to be vertically centered.

@font-face {
    font-family: "symbol-font";
    src: url(ux/NotoSansSymbols2-Aligned.ttf);
}

Image of properly vertically-centered bullet character



Answered By - btd
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing