Why doesn’t this EXTREMELY simple webpage show the div with the id “mainContent” in the centre of the page.
Right now the div is on the left. But I would like the div to be centred in the webpage(body element).
Whats going wrong?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> </title>
<style type="text/css">
<!--
body {
text-align: center;
background-color: red;
}
#mainContent {
width: 800px;
background-color: blue;
}
#content {
width: 600px;
float: left;
background-color: green;
overflow: hidden;
}
#sidebar {
width: 200px;
float: left;
background-color: yellow;
overflow: hidden;
}
-->
</style>
</head>
<body>
<div id="mainContent">
<!-- Left Column/Bar -->
<div id="content">
<p>content</p>
</div>
<!-- Right Column/Bar -->
<div id="sidebar">
<p>sidebar</p>
</div>
</div>
</body>
</html>
You just need to add
margin: 0 auto;which gives it 0 margin on top and bottom, and automatic margins left and right.