Forum Moderators: open
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Centered</title>
</head>
<body>
<table cellpadding="0" cellspacing="0" border="0" width="100%" height="100%">
<tr>
<td width="100%" align="center">This text is centered on the screen</td>
</tr>
</table>
</body>
</html> Note that the above requires quirks mode in the browser and doesn't validate so you should not add a doctype. This method is perfect for a quick-and-dirty one-page implementation.
The second method is to use CSS:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<title>Centered</title>
<style type="text/css">
#outer {
text-align:center;
position:absolute;
top:50%;
left:0;
width:100%;
height:1px;
background:blue;
}
#inner {
position: absolute;
top: -25px;
left: 50%;
margin-left:-125px;
width: 250px;
height: 50px;
border:1px solid black;
background:yellow;
}
</style>
</head>
<body>
<div id="outer">
<div id="inner">
This text box is centered on the screen
</div>
</div>
</body>
</html> This method centers a box (not an arbitrary piece of text) both horizontally and vertically. The basic idea is to have an outer container placed at 50% from the top with a 1px nominal height, and an inner container with a fixed height in pixels, a negative top position representing half of the declared height, and a negative left margin representing half of the declared width.
The CSS version is better when you have a more complex layout dependent on standards mode, but you may still need to use a table within the inner box - unfortunately as the difference between the two methods shows, vertical centering within the viewport is one of the weak points with CSS designs.