In the era of table layout, there is no need to think too much about vertical centering. In cells, the default is vertical centering. One line of text is vertically centered, and three lines of text will also be vertically centered. Perform CSS web page layout so that the form is changed. The text is positioned at the top of the container by default.
As shown below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>chinaz.com</title> <style type="text/css"> #MrJin { width:500px; height:200px; border:1px solid #03c; text-align:center; } </style> </head> <body> <div id="MrJin"><a href="http://www.52CSS.com/">CSS Web Design</a></div> </body> </html> |
In this case, how to vertically center the text? Divided into three situations:
1. If it is a single line of text, you can use line spacing to solve the problem.
We add the definition of line spacing to it to get the effect of vertically centering a single line of text.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>chinaz.com</title> <style type="text/css"> #MrJin { width:500px; height:200px; border:1px solid #03c; text-align:center; line-height:200px; } </style> </head> <body> <div id="MrJin"><a href="http://www.52CSS.com/">CSS Web Design </a></div> </body> </html> |
2. If it is multi-line text, the parent container does not have a fixed height.
We can use padding to solve the problem.
Set the padding of the container to the same fixed value, and the height of the container increases as the content increases.
Use this to achieve vertical centering of multi-line text.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>www.52CSS.com</title> <style type="text/css"> #MrJin { width:500px; padding:50px 0; border:1px solid #03c; text-align:center; } </style> </head> <body> <div id="MrJin"><p><a href="http://www.52CSS.com/">CSS Web Design </p><p>We are committed to powering Chinese websites! </a></p></div> </body> </html> |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>chinaz.com</title> <style type="text/css"> #MrJin { width:500px; padding:50px 0; border:1px solid #03c; text-align:center; } </style> </head> <body> <div id="MrJin"><a href="http://www.52CSS.com/"> <p>CSS Web Design</p> <p>China Webmaster Site</p> <p>We are committed to powering Chinese websites! </p> </a></div> </body> </html> |