question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Requesting a simple way to vertically align text in a container

See original GitHub issue

Right now I can’t figure out how to do this without manually setting dy. I’m using textAnchor="middle" to center the text horizontally, but I have to set y={HEIGHT / 2}, which vertically centers the top of the Text element. So then I have to manually figure out the height and set a dy value to textHeight / -2, so that the Text is vertically centered.

It would be nice if I could change the “origin” of the text to be in the center of the Text element. Or if there could just be a “vertical alignment” option. Or if I could just force a “line height” for the Text, so that I can be confident in the dy setting. Because right now I have to set two different dy values for iOS and Android, because they seem to render differently.

Thanks!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7

github_iconTop GitHub Comments

35reactions
ndbroadbentcommented, Apr 16, 2017

I’ve just figured out how to vertically center text, in a way that works on both iOS and Android. Apparently the magic number is: 0.25 * fontSize.

const fontSize = 64;
const width = 200;
const height = 200;

<Text
  fill="white"
  fontFamily="Helvetica"
  fontWeight="bold"
  fontSize={fontSize}
  textAnchor="middle"
  x={width / 2}
  y={(height / 2) - (fontSize / 2)}
  height={fontSize}
  dy={fontSize * -0.25}
>
  hello
</Text>

Here it is with a red fill + blue rect to show fontSize:

Android:

screenshot 2017-04-16 14 52 25

iOS:

screenshot 2017-04-16 14 52 45

I have to use both dy and y. If I just do y={(height / 2) - (fontSize / 1.75)}, then it works fine on Android, but looks like this on iOS:

screenshot 2017-04-16 15 05 20

Anyway, was just wondering if there is a nicer way to do this.

27reactions
sunilkumarccommented, Jun 15, 2018

textAlignVertical: 'center' did the trick for me!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I vertically center text with CSS? - Stack Overflow
The CSS just sizes the <div> , vertically center aligns the <span> by setting the <div> 's line-height equal to its height, and...
Read more >
How to Vertically Center Text with CSS - W3Docs
To position text vertically positioned with CSS you can use vertical-align, line-height, transform properties, flexbox. Learn more methods. Try examples.
Read more >
vertical-align - CSS: Cascading Style Sheets - MDN Web Docs
The vertical-align property can be used in two contexts: To vertically align an inline element's box inside its containing line box.
Read more >
How to vertically center text with CSS ? - GeeksforGeeks
In this article, we will learn how to align the text vertically using CSS & Although CSS2 doesn't support Vertical aligning.
Read more >
Requesting a simple way to vertically align text in a container
I'm using textAnchor="middle" to center the text horizontally, but I have to set y={HEIGHT / 2} , which vertically centers the top of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found