Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Vertically Center <text> Element

I must be missing something and would appreciate if anyone knows how to do this.  I've shared my index and styles files below.  My goal is simply to center the text element vertically on the screen without having to eyeball it.  I set the x and y positions to 50%, set the text-anchor to middle, and get what I would expect - text with it's baseline on the vertical center of the screen.  I recognize that I can adjust the position further using something like "50%+somePixelAmount", but what is the correct adjustment amount?  From my tests, using half the font size doesn't seem sufficient.

 

Thanks in advance for the tips!

 

index.gui

<svg id="background">
  <text id="time" />
</svg>

 

Here is my styles.css:

 

#background {
  viewport-fill: fb-white;
}

 

#time {
  font-size: 80;
  font-family: System-Bold;
  text-length: 32;
  text-anchor: middle;
  x: 50%;
  y: 50%;
  fill: fb-black;
}

Best Answer
0 Votes
3 REPLIES 3

In case it wasn't clear, this is for a clock face.

Best Answer
0 Votes

Hello @pappyvanstinkle ,

 

Interesting question; I ran into this early on while doing much the same thing. Normally you can set where the baseline of text is or you can offset the y position by em's or ex's (basically the height of an 'M' or and 'x' in your font). Asking for this level of control in a device this small is probably asking too much and neither of these solutions will work. Frankly, I'm a bit amazed how much they actually did implement.

 

My solution was to plot a temporary grid, then tweak the text position until I got the aesthetic I was looking for. 

A font pitch vs font height is extremely useful when doing this kind of formatting. An example follows; many examples exist on line.

 

Regards,

Reign

 

Point Pixel Em Percent Keyword Default sans-serif

6pt8px0.5em50% Sample
7pt9px0.55em55% Sample
7.5pt10px0.625em62.5%x-smallSample
8pt11px0.7em70% Sample
9pt12px0.75em75% Sample
10pt13px0.8em80%smallSample
10.5pt14px0.875em87.5% Sample
11pt15px0.95em95% Sample
12pt16px1em100%mediumSample
13pt17px1.05em105% Sample
13.5pt18px1.125em112.5%largeSample
14pt19px1.2em120% Sample
14.5pt20px1.25em125% Sample
15pt21px1.3em130% Sample
16pt22px1.4em140% Sample
17pt23px1.45em145% Sample
18pt24px1.5em150%x-largeSample
20pt26px1.6em160% Sample
22pt29px1.8em180% Sample
24pt32px2em200%xx-largeSample
26pt35px2.2em220% Sample
27pt36px2.25em225% Sample
28pt37px2.3em230% Sample
29pt38px2.35em235% Sample
30pt40px2.45em245% Sample
32pt42px2.55em255% Sample
34pt45px2.75em275% Sample
36pt48px3em300% Sample

 

Best Answer
0 Votes

Thanks for sharing the info Reign, that's helpful to know how you've approached it.  Perhaps it's as you said and it's a matter of the device capabilities.  I'd have thought doing things like masking and animating gradients would be more costly than aligning text, but then again vertical text alignment is always an adventure.

Best Answer
0 Votes