Center Table in HTML

Center-align a HTML table

For your design, it is common practice to use divs rather than a table. This way, your layout will be more maintainable and changeable through proper styling. It does take some getting used to, but it will help you a ton in the long run and you will learn a lot about how styling works. However, I will provide you with a solution to the problem at hand.

In your stylesheets you have margins and padding set to 0 pixels. This overrides your align="center" attribute. I would recommend taking these settings out of your CSS as you don't normally want all of your elements to be affected in this manner. If you already know what's going on in the CSS, and you want to keep it that way, then you have to apply a style to your table to override the previous sets. You could either give the table a class or you can put the style inline with the HTML. Here are the two options:

  1. With a class:

    <table class="centerTable"></table>

In your style.css file you would have something like this:

.centerTable { margin: 0px auto; }

  1. Inline with your HTML:

    <table style="margin: 0px auto;"></table>

If you decide to wipe out the margins and padding being set to 0px, then you can keep align="center" on your <td> tags for whatever column you wish to align.

Center a table within a div

In your initial try, your table won't be centered since you're trying to center something that is taking 100% of the possible space. Technically, it is centered, you just can't see it's taking the entire space.

So imagine if you have a container of 100px. There's a block inside of this container that you want to center. But you're setting this block to have 100px in width. There's just no gap to see!

So this won't work:

{ 
width: 100%;
margin: 0 auto;
}

Instead, you should give the centering element a fixed width:

width: 400px; /* or whatever is needed */
margin: 0 auto;

That way it has some space around it.

Here, check this out:
https://jsfiddle.net/9gwcjvp3/

How to center table?

First of all, there are some other good answers you can read:
CSS tricks, Stackoverflow

Here you can do it by CSS 3:

HTML

 <div id="parent">
<form method="post" id="child">
<table align="center" cellpadding="2" cellspacing="2" border="0">
<tr>
<td>Username:</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td>Password:</td>
<td>
<input type="password" name="password">
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="btnLogin" style="width:173px; height:40px;" value="Login">
</td>
</tr>
</table>
</form>
</div>

and CSS:

#parent {
width: 500px;
height: 600px;
background-color: gold;
position: absolute;
/*it can be fixed too*/
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
/*this to solve "the content will not be cut when the window is smaller than the content": */
max-width: 100%;
max-height: 100%;
overflow: auto;
}

#child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

}

Fiddle

Center a Table inside a TD

You had the right idea with margin:auto 0; just take off the 0.

Working example: http://jsfiddle.net/cxnR8/

<table style="border:solid;width: 100%">
<tr>
<td>
<table style="margin:auto;border:solid; width:50%">
<tr>
<td >I must be in the center</td>
</tr>
</table>
</td>
</tr>
</table>​

But, more importantly, do you really need to use tables and in-line styling?

center table in HTML

Give width to table, and set margin auto horizontally.

table {
width:500px;
margin: 10px auto;
}

HTML align all column values to the center with one table parameter

To make David's (above) answer more visible:

To get data in all cells within a table to line up, use the following:

<table style="text-align: center;" border="1">

Working example:

<table border="1" style="text-align: center;"><TR><TD>1</TD><TD>2</TD><TD>3</TD><TD>4</TD></TR><TR><TD>1aaaaaa</TD><TD>2aaaaa</TD><TD>3aaaaa</TD><TD>4aaaa</TD></TR></table>

Center text in an HTML table across group of cells

Solution 1:
Use rowspan and colspan, together with valign and align (which are specific <table> centering attributes):

td {  border: 1px solid #eee;  padding: 1rem;}td[rowspan] {  background-color: rgba(255,0,0,.07);  border: 1px solid red;}
<table>   <tr>     <td rowspan="2" colspan="2" valign="center" align="center">     A     </td>     <td>x</td>   </tr>   <tr>    <td>x</td>  </tr>  <tr>    <td>x</td>    <td>x</td>    <td>x</td>  </tr>

Center child divs with tables inside a parent div

Try adding

display: flex;
justify-content: center;

to your parent containers

.parent_container {
width: 100%;
text-align: center;
}

.table_container {
float: left;
width: 30rem;
margin-bottom: 3rem;
}

.table_container2 {
float: left;
width: 30rem;
margin-bottom: 3rem;
}

.container {
width: 100%;
margin-top: 30px;
display: flex;
justify-content: center;
}

.container::after {
content: "";
clear: both;
display: table;
}

table {
margin: 2rem auto;
border-radius: 10px;
}

tr {
padding: 15px;
text-align: center;
}

td {
color: black;
text-align: center;
vertical-align: middle;
height: 60px;
background-color: #e1edf9;
width: 272px;
border-top: 1px solid white;
}

td: first-of-type {
border-top: none;
}

.sub_text {
font-size: 12px;
font-style: italic;
color: #0071ce;
font-weight: 100;
}

.wrapper {
text-align: center;
margin-top: 20px;
}

@media screen and (min-width: 1400px) {
.table_container {
float: left;
width: 15rem;
}
.table_container:first-of-type {
width: 30rem;
}
}

@media screen and (min-width: 1400px) {
.table_container2 {
float: left;
width: 15rem;
}
.table_container2:first-of-type {
width: 30rem;
}
}

@media only screen and (min-width: 1000) and (max-device-width: 1400px) {
.table_container2 {
float: left;
width: 14rem;
}
.table_container2:first-of-type {
width: 27rem;
}
}

@media only screen and (min-width: 1000) and (max-device-width: 1400px) {
.table_container {
float: left;
width: 14rem;
}
.table_container:first-of-type {
width: 27rem;
}
}
<div class="parent_container">
<div class="wrapper">
<a type="button" id="modalButton" class="btn btn-primary" data-target="#myModal" onclick="showmodal ();">
Compare
</a>
</div>
<div class="container">
<div class="table_container">
<table>
<tr>
<td></td>
</tr>
</table>
</div>
<div class="table_container">
<table>
<tr>
<td></td>
</tr>
</table>
</div>
<div class="table_container">
<table>
<tr>
<td></td>
</tr>
</table>
</div>
</div>
<div class="container container2">
<div class="table_container2">
<table>
<tr>
<td></td>
</tr>
</table>
</div>
<div class="table_container2">
<table>
<tr>
<td></td>
</tr>
</table>
</div>
<div class="table_container2">
<table>
<tr>
<td></td>
</tr>
</table>
</div>
</div>
</div>

How to center the contents of an HTML table?

Here is an example with CSS and inline style attributes:

td {    height: 50px;     width: 50px;}
#cssTable td { text-align: center; vertical-align: middle;}
<table border="1">    <tr>        <td style="text-align: center; vertical-align: middle;">Text</td>        <td style="text-align: center; vertical-align: middle;">Text</td>    </tr></table>
<table border="1" id="cssTable"> <tr> <td>Text</td> <td>Text</td> </tr></table>


Related Topics



Leave a reply



Submit