Vertical Text Alignment in a Responsive Div Element
When dealing with content within dynamically sized DIV containers, aligning text vertically can be challenging. This question explores how to center text vertically within a DIV element regardless of its height.
Problem Statement:
Consider the following HTML structure:
<body> <div>
How can the H1 tag be vertically centered within the DIV, regardless of the DIV's height?
Solution:
The most effective solution involves leveraging the display property. By setting the wrapper element as a table, we enable the use of vertical-align:middle to center the element:
<body> <div>Text
body {width: 100%; height: 100%;} /* For visualization purposes */ div { position:absolute; height:100%; width:100%; display: table; } h1 { display: table-cell; vertical-align: middle; text-align:center; }
Testing:
The solution has been tested on various platforms and browsers, including:
Windows XP Profissional versão 2002 Service Pack 3
Windows 7 Home Edition Service Pack 1
Linux Ubuntu 12.04
The above is the detailed content of How to Vertically Center Text in a Responsive Div Element?. For more information, please follow other related articles on the PHP Chinese website!