K o m p x . c o m or Compmiscellanea.com

HTML centering

Windows : Internet Explorer 3.0+, Firefox 1.0+, Google Chrome, Opera 3.51 - 6.xx and 9.0+, Safari 3.1+, SeaMonkey 1.0+ [ 1 ].

Linux : Firefox 1.0+, Chromium, Opera 5.0 - 6.xx and 9.0+, SeaMonkey 1.0+ [ 2 ].

Centering the whole content of a web page in the viewable area of a browser by pure HTML - no CSS. A box to keep the content of the page is HTML centered horizontally and vertically - [ Open demo page ]

HTML. Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>HTML centering</title>

<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">

</head>

<body bgcolor="#ffffff">

<table width="100%" height="100%" bgcolor="#a3ddc4">

<tr>

<td align="center">

<table width="800" height="500" bgcolor="#ff6f6f">

<tr>

<td>&nbsp;</td>

</tr>

</table>

</td>

</tr>

</table>

</body>

</html>

The outer HTML table makes the whole web browser viewable area, except margins, a HTML table and the whole area of this HTML table - a HTML table cell.

The cell of the outer HTML table inherits the default value for valign attribute from its parent table row. And this row in its turn inherits the default value for valign attribute from the outer HTML table tbody - even if tbody tag is not used. And that value is middle. So a block of content inside the cell of the outer HTML table is centered vertically in web browser viewable are.

Align="center" of the outer HTML table cell makes a block of content inside it centered horizontally in web browser viewable are.

The inner table, the one inside of the outer HTML table cell makes up a box of a given size. Or there may be no set height or no set width or both. Then the size of the box is to adjust to accommodate the content, whatever its dimensions are. If the width or height or both of the box results to be larger than web browser viewable area, then it still ends up to be centered.

Since HTML centering is the oldest method to center content horizontally and vertically, it works well not just in older web browsers, but in ones that can be described as downright ancient. Like adding one more HTML tag ( <center></center> ) around the outer table makes it funcion as deep as Internet Explorer 3 : [ Open demo page ]

Download Internet Explorer 3: a pack, containing 3.0, 4.01, 5.01, 5.5, 6.0 versions of Internet Explorer

HTML. Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>HTML centering for Internet Explorer 3</title>

<meta http-equiv="Content-Type" content="text/html; charset=Windows-1252">

</head>

<body bgcolor="#ffffff">

<!-- For Internet Explorer 3 --><center><!-- For Internet Explorer 3 -->

<table width="100%" height="100%" bgcolor="#a3ddc4">

<tr>

<td align="center">

<table width="800" height="500" bgcolor="#ff6f6f">

<tr>

<td>&nbsp;</td>

</tr>

</table>

</td>

</tr>

</table>

<!-- For Internet Explorer 3 --><center><!-- For Internet Explorer 3 -->

</body>

</html>

HTML horizontal and vertical centering of a box containing web page content has become obsolescent as a result of semantic markup spread. But it is still reliable. Not only in the major and modern web browsers, but in many alternative and older ones as well.

There is a minor flaw in it though. The height attribute of the <table><table/> tag is used there. It is supported for years by the majority of web browsers, but is incompatible with the standards promoted by World Wide Web Consortium (W3C). So the code does not pass W3C validation. Back in 1990s - early 2000s, it was complicated or impossible to succeed in making a code both valid and working in the majority of web browsers, so the flaw was not looked at as a real problem.

[ 1 ]

As well as Netscape 2.02 - 4.80 and Offbyone. There is some shift of page content to the top left corner of the web browser viewable area in Netscape 2.02 - 4.80, since these web browsers reserve the place for scrollbars.

[ 2 ]

As well as Netscape 2.02 - 4.80. There is some shift of page content to the top left corner of the web browser viewable area in Netscape 2.02 - 4.80, since these web browsers reserve the place for scrollbars.