Home > Article > Web Front-end > How to Vertically Center Text in a Div with Dynamic Height?
Problem:
Achieving vertical alignment of text within a div element of variable height, regardless of browser window dimensions.
Explanation:
To center the text vertically within the dynamically-sized div, we utilize the display property and the vertical-align attribute.
Solution:
Set the wrapper div to display as a table:
div { display: table; }
Style the text element as a table cell and vertically align it in the middle:
h1 { display: table-cell; vertical-align: middle; text-align: center; }
Additional Notes:
This solution has been thoroughly tested on multiple operating systems and browsers, including Windows XP, Windows 7, and Linux Ubuntu, with Internet Explorer, Opera, Firefox, Safari, and Google Chrome.
The above is the detailed content of How to Vertically Center Text in a Div with Dynamic Height?. For more information, please follow other related articles on the PHP Chinese website!