Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Align the table on the center of the page

Tags:

html

css

I just want to put the table in the middle of the page. Any help would be great.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Randy's first html web page !</title>
<style type="text/css">
body
{background-image:url('Koala.gif');} 
h1
{
text-align:center;
}
h2
{
text-align:center;
}
p
{
text-align:center;
font-size:200%;
color:#00fff0;
}
div
{
background-color:#efffff;
}
</style>
</head>
<h1> Hello Professor</h1>
<h2> By: Randy White</h2>
<P> I haven't done anything like this before.</P> 
<P>Seems to be ok</P>
<P><img src="Hydrangeas.jpg" width="150" height="100" alt="Hydrangeas.jpg"></P> 
<table border="1">
<tr>
<th>Month</th>
<th>Day</th>
<th>Year</th>
</tr>
<tr>
<td>December</td>
<td>1</td>
<td>2010</td>
</tr>
</table>
<a href="http://www.google.com">Visit Google!</a>
</body>
</html>
like image 571
user770022 Avatar asked Dec 14 '10 00:12

user770022


People also ask

How do you align a table to the center of the page?

To center this table, you would need to add ;margin-left:auto;margin-right:auto; to the end of the style attribute in the <table> tag. The table tag would look like the following. Changing the style attribute in the <table> tag, as shown above, results in the table being centered on the web page, as shown below.

How do I center align a table in CSS?

We can use the shorthand property margin and set it to auto for aligning the table at the center rather than using the margin-left and margin-right property. Instead of aligning the table to center, the text-align: center; property only centers the table content, such as the text inside the table.

How do you center a table in the middle?

One of the most common ways to center a table is to set both the bottom and top margins to 0, and the left and right margins to auto. If you're after a table that's an exact width, you may do this as you usually would and the automatic margin will divide the space left over.


1 Answers

Try:

table {
    width: 50%;
    margin-left: auto;
    margin-right: auto;
}

or:

table {
    width: 200px;
    margin-left: auto;
    margin-right: auto;
}

BTW, this sets this for all tables. You might want to have the properties on a more specific selector (like table.someclass).

like image 160
Kos Avatar answered Sep 28 '22 01:09

Kos