Home >Web Front-end >CSS Tutorial >How to Center Text Vertically Within a Div?
How to Vertically Align Text in a Div
When working with a div, it's sometimes necessary to ensure that the text within it is aligned vertically in the middle. This can be achieved through various methods.
Using line-height
If the div has a fixed height, such as 50px, you can simply use the line-height CSS property.
This will center the text vertically within the div.
Using display properties
For multi-line text, you can wrap it in a span element and apply display properties and vertical-align.
Using the transform property
Another method involves using the transform property with the translateY() function. This is especially useful for older browsers that don't support display properties.
The above is the detailed content of How to Center Text Vertically Within a Div?. For more information, please follow other related articles on the PHP Chinese website!