Requesting a simple way to vertically align text in a container
See original GitHub issueRight 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:
- Created 6 years ago
- Comments:7
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
.Here it is with a red fill + blue rect to show
fontSize
:Android:
iOS:
I have to use both
dy
andy
. If I just doy={(height / 2) - (fontSize / 1.75)}
, then it works fine on Android, but looks like this on iOS:Anyway, was just wondering if there is a nicer way to do this.
textAlignVertical: 'center'
did the trick for me!