Can't center a table in HTML5/CSS3
up vote
0
down vote
favorite
I'm trying to center the rows in a table element on a page, and they won't center--and I've discovered that for some reason the tbody (and so, the table) width are expanding to the entire width of the page, so margin: 0, auto; will not then center the table.
Instead, the contents of the <tr>s end up all pushed to the left, no matter what I do. The only way I've been able to get the table centered is do something like set the table to width: 10px (obviously smaller than it actually is), but then it also squishes all of its descendants and removes my control over their width, which is not desirable.
How can I get this table centered? Happy to provide more detail as is necessary if the code isn't enough.
PHP/HTML:
<table id="items" class="inventory-table center" border="0" cellpadding="0" cellspacing="0" width="100%">
<?php for($i=0; $i<3; $i++) { ?>
<tr id="inventory-row-<?php echo $i ?>" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="<?php echo $i; ?>" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="<?php echo $i; ?>" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<?php } ?>
</table>
SASS:
/* inventory table css */
$triangle-size: 30px;
.inventory-table {
border-spacing: 10px;
margin: 0 auto;
}
.inventory-row {
display: inline-block;
td {
text-align: center;
}
}
.inventory-image img {
width: 200px;
height: auto;
}
td.purchase-number-td {
min-width: 30px;
}
.arrow-up {
width: 0;
height: 0;
border-left: $triangle-size solid transparent;
border-right: $triangle-size solid transparent;
border-bottom: $triangle-size solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: $triangle-size solid transparent;
border-right: $triangle-size solid transparent;
border-top: $triangle-size solid black;
}
.top-arrow {
margin-bottom: 30px;
}
.bottom-arrow {
margin-top: 30px;
}
Screenshot from Chrome dev tools:

html css html-table
add a comment |
up vote
0
down vote
favorite
I'm trying to center the rows in a table element on a page, and they won't center--and I've discovered that for some reason the tbody (and so, the table) width are expanding to the entire width of the page, so margin: 0, auto; will not then center the table.
Instead, the contents of the <tr>s end up all pushed to the left, no matter what I do. The only way I've been able to get the table centered is do something like set the table to width: 10px (obviously smaller than it actually is), but then it also squishes all of its descendants and removes my control over their width, which is not desirable.
How can I get this table centered? Happy to provide more detail as is necessary if the code isn't enough.
PHP/HTML:
<table id="items" class="inventory-table center" border="0" cellpadding="0" cellspacing="0" width="100%">
<?php for($i=0; $i<3; $i++) { ?>
<tr id="inventory-row-<?php echo $i ?>" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="<?php echo $i; ?>" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="<?php echo $i; ?>" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<?php } ?>
</table>
SASS:
/* inventory table css */
$triangle-size: 30px;
.inventory-table {
border-spacing: 10px;
margin: 0 auto;
}
.inventory-row {
display: inline-block;
td {
text-align: center;
}
}
.inventory-image img {
width: 200px;
height: auto;
}
td.purchase-number-td {
min-width: 30px;
}
.arrow-up {
width: 0;
height: 0;
border-left: $triangle-size solid transparent;
border-right: $triangle-size solid transparent;
border-bottom: $triangle-size solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: $triangle-size solid transparent;
border-right: $triangle-size solid transparent;
border-top: $triangle-size solid black;
}
.top-arrow {
margin-bottom: 30px;
}
.bottom-arrow {
margin-top: 30px;
}
Screenshot from Chrome dev tools:

html css html-table
the<table>is expanding because it hasstyle="width:100%;"as you were saying. So how do you want to center such an element? It has no space left on the sides. Please make demo to clarify the problem.
– Nico O
Mar 15 '14 at 22:51
@NicoO removing thewidth=100%changes nothing. The table element, when I hover over it in Chrome Inspector, extends all the way from the left of the page to the right, but the child elements are still left-aligned.
– jdotjdot
Mar 15 '14 at 22:55
Edit this demo to show your problem: jsfiddle.net/NicoO/ATvQ8
– Nico O
Mar 15 '14 at 23:00
@NicoO I've attached a Chrome dev tools screenshot to show the issue I'm having. As you can see,tbodytakes up the whole screen, so the elements below aren't centered. If you hover over thetr, it simply ends after the arrows.
– jdotjdot
Mar 15 '14 at 23:17
I added a center div and it works, check for the answer
– Chococroc
Mar 15 '14 at 23:42
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to center the rows in a table element on a page, and they won't center--and I've discovered that for some reason the tbody (and so, the table) width are expanding to the entire width of the page, so margin: 0, auto; will not then center the table.
Instead, the contents of the <tr>s end up all pushed to the left, no matter what I do. The only way I've been able to get the table centered is do something like set the table to width: 10px (obviously smaller than it actually is), but then it also squishes all of its descendants and removes my control over their width, which is not desirable.
How can I get this table centered? Happy to provide more detail as is necessary if the code isn't enough.
PHP/HTML:
<table id="items" class="inventory-table center" border="0" cellpadding="0" cellspacing="0" width="100%">
<?php for($i=0; $i<3; $i++) { ?>
<tr id="inventory-row-<?php echo $i ?>" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="<?php echo $i; ?>" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="<?php echo $i; ?>" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<?php } ?>
</table>
SASS:
/* inventory table css */
$triangle-size: 30px;
.inventory-table {
border-spacing: 10px;
margin: 0 auto;
}
.inventory-row {
display: inline-block;
td {
text-align: center;
}
}
.inventory-image img {
width: 200px;
height: auto;
}
td.purchase-number-td {
min-width: 30px;
}
.arrow-up {
width: 0;
height: 0;
border-left: $triangle-size solid transparent;
border-right: $triangle-size solid transparent;
border-bottom: $triangle-size solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: $triangle-size solid transparent;
border-right: $triangle-size solid transparent;
border-top: $triangle-size solid black;
}
.top-arrow {
margin-bottom: 30px;
}
.bottom-arrow {
margin-top: 30px;
}
Screenshot from Chrome dev tools:

html css html-table
I'm trying to center the rows in a table element on a page, and they won't center--and I've discovered that for some reason the tbody (and so, the table) width are expanding to the entire width of the page, so margin: 0, auto; will not then center the table.
Instead, the contents of the <tr>s end up all pushed to the left, no matter what I do. The only way I've been able to get the table centered is do something like set the table to width: 10px (obviously smaller than it actually is), but then it also squishes all of its descendants and removes my control over their width, which is not desirable.
How can I get this table centered? Happy to provide more detail as is necessary if the code isn't enough.
PHP/HTML:
<table id="items" class="inventory-table center" border="0" cellpadding="0" cellspacing="0" width="100%">
<?php for($i=0; $i<3; $i++) { ?>
<tr id="inventory-row-<?php echo $i ?>" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="<?php echo $i; ?>" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="<?php echo $i; ?>" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<?php } ?>
</table>
SASS:
/* inventory table css */
$triangle-size: 30px;
.inventory-table {
border-spacing: 10px;
margin: 0 auto;
}
.inventory-row {
display: inline-block;
td {
text-align: center;
}
}
.inventory-image img {
width: 200px;
height: auto;
}
td.purchase-number-td {
min-width: 30px;
}
.arrow-up {
width: 0;
height: 0;
border-left: $triangle-size solid transparent;
border-right: $triangle-size solid transparent;
border-bottom: $triangle-size solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: $triangle-size solid transparent;
border-right: $triangle-size solid transparent;
border-top: $triangle-size solid black;
}
.top-arrow {
margin-bottom: 30px;
}
.bottom-arrow {
margin-top: 30px;
}
Screenshot from Chrome dev tools:

html css html-table
html css html-table
edited 16 hours ago
Brian Tompsett - 汤莱恩
4,153133699
4,153133699
asked Mar 15 '14 at 22:35
jdotjdot
8,91374385
8,91374385
the<table>is expanding because it hasstyle="width:100%;"as you were saying. So how do you want to center such an element? It has no space left on the sides. Please make demo to clarify the problem.
– Nico O
Mar 15 '14 at 22:51
@NicoO removing thewidth=100%changes nothing. The table element, when I hover over it in Chrome Inspector, extends all the way from the left of the page to the right, but the child elements are still left-aligned.
– jdotjdot
Mar 15 '14 at 22:55
Edit this demo to show your problem: jsfiddle.net/NicoO/ATvQ8
– Nico O
Mar 15 '14 at 23:00
@NicoO I've attached a Chrome dev tools screenshot to show the issue I'm having. As you can see,tbodytakes up the whole screen, so the elements below aren't centered. If you hover over thetr, it simply ends after the arrows.
– jdotjdot
Mar 15 '14 at 23:17
I added a center div and it works, check for the answer
– Chococroc
Mar 15 '14 at 23:42
add a comment |
the<table>is expanding because it hasstyle="width:100%;"as you were saying. So how do you want to center such an element? It has no space left on the sides. Please make demo to clarify the problem.
– Nico O
Mar 15 '14 at 22:51
@NicoO removing thewidth=100%changes nothing. The table element, when I hover over it in Chrome Inspector, extends all the way from the left of the page to the right, but the child elements are still left-aligned.
– jdotjdot
Mar 15 '14 at 22:55
Edit this demo to show your problem: jsfiddle.net/NicoO/ATvQ8
– Nico O
Mar 15 '14 at 23:00
@NicoO I've attached a Chrome dev tools screenshot to show the issue I'm having. As you can see,tbodytakes up the whole screen, so the elements below aren't centered. If you hover over thetr, it simply ends after the arrows.
– jdotjdot
Mar 15 '14 at 23:17
I added a center div and it works, check for the answer
– Chococroc
Mar 15 '14 at 23:42
the
<table> is expanding because it has style="width:100%;" as you were saying. So how do you want to center such an element? It has no space left on the sides. Please make demo to clarify the problem.– Nico O
Mar 15 '14 at 22:51
the
<table> is expanding because it has style="width:100%;" as you were saying. So how do you want to center such an element? It has no space left on the sides. Please make demo to clarify the problem.– Nico O
Mar 15 '14 at 22:51
@NicoO removing the
width=100% changes nothing. The table element, when I hover over it in Chrome Inspector, extends all the way from the left of the page to the right, but the child elements are still left-aligned.– jdotjdot
Mar 15 '14 at 22:55
@NicoO removing the
width=100% changes nothing. The table element, when I hover over it in Chrome Inspector, extends all the way from the left of the page to the right, but the child elements are still left-aligned.– jdotjdot
Mar 15 '14 at 22:55
Edit this demo to show your problem: jsfiddle.net/NicoO/ATvQ8
– Nico O
Mar 15 '14 at 23:00
Edit this demo to show your problem: jsfiddle.net/NicoO/ATvQ8
– Nico O
Mar 15 '14 at 23:00
@NicoO I've attached a Chrome dev tools screenshot to show the issue I'm having. As you can see,
tbody takes up the whole screen, so the elements below aren't centered. If you hover over the tr, it simply ends after the arrows.– jdotjdot
Mar 15 '14 at 23:17
@NicoO I've attached a Chrome dev tools screenshot to show the issue I'm having. As you can see,
tbody takes up the whole screen, so the elements below aren't centered. If you hover over the tr, it simply ends after the arrows.– jdotjdot
Mar 15 '14 at 23:17
I added a center div and it works, check for the answer
– Chococroc
Mar 15 '14 at 23:42
I added a center div and it works, check for the answer
– Chococroc
Mar 15 '14 at 23:42
add a comment |
3 Answers
3
active
oldest
votes
up vote
0
down vote
accepted
Well, what about adding a table_conteiner which you do can center?
I've added a div called table_container, and I've centred it using margin: 0 auto;. As the table is inside, is centred in your markup. I've also removed inline-block from the tr, as it made not possible to the td expand to fill all the row. Note that I modified the values of the PHP and SASS from the file to make it pure HTML and CSS, as it's easier to everybody to test a pure markup problem.
/* inventory table css */
.inventory-table {
border-spacing: 10px;
margin: 0 auto;
}
.inventory-row {
/*display: inline-block;*/
}
.inventory-row td {
text-align: center;
}
.inventory-image img {
width: 200px;
height: auto;
}
td.purchase-number-td {
min-width: 30px;
}
.arrow-up {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom: 30px solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-top: 30px solid black;
}
.top-arrow {
margin-bottom: 30px;
}
.bottom-arrow {
margin-top: 30px;
}
.table_container {
width: 500px;
}
.table_container {
margin: 0 auto;
border: 1px solid black;
width: 700px
}
</style>
<div class="table_container">
<table id="items" class="inventory-table center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
</table>
</div>
Culprit turned out to be.inventory-row {display: inline-block; }. Thanks!
– jdotjdot
Mar 18 '14 at 6:02
add a comment |
up vote
0
down vote
Adjust the table margin, Add:
margin: 0 auto;
I actually had this before and added it now, and it still is not centered. No change.
– jdotjdot
Mar 15 '14 at 22:53
I cant understand what exactly u want to be centered
– user1972155
Mar 15 '14 at 23:00
I think you've understood what I'm looking to center. The advice you gave should work but isn't working--that's the problem I've been having. I want the table itself as a whole to be centered, but addingmargin: 0 auto;doesn't have an effect.
– jdotjdot
Mar 15 '14 at 23:03
ok screenshot now talks,, try using width:200px; or <table align='center'>.. but ur problem is the table width having full width
– user1972155
Mar 17 '14 at 14:59
add a comment |
up vote
0
down vote
fiddle
HTML
<table>
<th colspan='3'>header</th>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</table>
CSS
table {
border: 1px solid red;
width: 100%; /* for small screens */
max-width: 20em; /* define width while keeping it flexible */
margin-right: auto;
margin-left: auto;
}
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Well, what about adding a table_conteiner which you do can center?
I've added a div called table_container, and I've centred it using margin: 0 auto;. As the table is inside, is centred in your markup. I've also removed inline-block from the tr, as it made not possible to the td expand to fill all the row. Note that I modified the values of the PHP and SASS from the file to make it pure HTML and CSS, as it's easier to everybody to test a pure markup problem.
/* inventory table css */
.inventory-table {
border-spacing: 10px;
margin: 0 auto;
}
.inventory-row {
/*display: inline-block;*/
}
.inventory-row td {
text-align: center;
}
.inventory-image img {
width: 200px;
height: auto;
}
td.purchase-number-td {
min-width: 30px;
}
.arrow-up {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom: 30px solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-top: 30px solid black;
}
.top-arrow {
margin-bottom: 30px;
}
.bottom-arrow {
margin-top: 30px;
}
.table_container {
width: 500px;
}
.table_container {
margin: 0 auto;
border: 1px solid black;
width: 700px
}
</style>
<div class="table_container">
<table id="items" class="inventory-table center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
</table>
</div>
Culprit turned out to be.inventory-row {display: inline-block; }. Thanks!
– jdotjdot
Mar 18 '14 at 6:02
add a comment |
up vote
0
down vote
accepted
Well, what about adding a table_conteiner which you do can center?
I've added a div called table_container, and I've centred it using margin: 0 auto;. As the table is inside, is centred in your markup. I've also removed inline-block from the tr, as it made not possible to the td expand to fill all the row. Note that I modified the values of the PHP and SASS from the file to make it pure HTML and CSS, as it's easier to everybody to test a pure markup problem.
/* inventory table css */
.inventory-table {
border-spacing: 10px;
margin: 0 auto;
}
.inventory-row {
/*display: inline-block;*/
}
.inventory-row td {
text-align: center;
}
.inventory-image img {
width: 200px;
height: auto;
}
td.purchase-number-td {
min-width: 30px;
}
.arrow-up {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom: 30px solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-top: 30px solid black;
}
.top-arrow {
margin-bottom: 30px;
}
.bottom-arrow {
margin-top: 30px;
}
.table_container {
width: 500px;
}
.table_container {
margin: 0 auto;
border: 1px solid black;
width: 700px
}
</style>
<div class="table_container">
<table id="items" class="inventory-table center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
</table>
</div>
Culprit turned out to be.inventory-row {display: inline-block; }. Thanks!
– jdotjdot
Mar 18 '14 at 6:02
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Well, what about adding a table_conteiner which you do can center?
I've added a div called table_container, and I've centred it using margin: 0 auto;. As the table is inside, is centred in your markup. I've also removed inline-block from the tr, as it made not possible to the td expand to fill all the row. Note that I modified the values of the PHP and SASS from the file to make it pure HTML and CSS, as it's easier to everybody to test a pure markup problem.
/* inventory table css */
.inventory-table {
border-spacing: 10px;
margin: 0 auto;
}
.inventory-row {
/*display: inline-block;*/
}
.inventory-row td {
text-align: center;
}
.inventory-image img {
width: 200px;
height: auto;
}
td.purchase-number-td {
min-width: 30px;
}
.arrow-up {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom: 30px solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-top: 30px solid black;
}
.top-arrow {
margin-bottom: 30px;
}
.bottom-arrow {
margin-top: 30px;
}
.table_container {
width: 500px;
}
.table_container {
margin: 0 auto;
border: 1px solid black;
width: 700px
}
</style>
<div class="table_container">
<table id="items" class="inventory-table center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
</table>
</div>
Well, what about adding a table_conteiner which you do can center?
I've added a div called table_container, and I've centred it using margin: 0 auto;. As the table is inside, is centred in your markup. I've also removed inline-block from the tr, as it made not possible to the td expand to fill all the row. Note that I modified the values of the PHP and SASS from the file to make it pure HTML and CSS, as it's easier to everybody to test a pure markup problem.
/* inventory table css */
.inventory-table {
border-spacing: 10px;
margin: 0 auto;
}
.inventory-row {
/*display: inline-block;*/
}
.inventory-row td {
text-align: center;
}
.inventory-image img {
width: 200px;
height: auto;
}
td.purchase-number-td {
min-width: 30px;
}
.arrow-up {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-bottom: 30px solid black;
}
.arrow-down {
width: 0;
height: 0;
border-left: 30px solid transparent;
border-right: 30px solid transparent;
border-top: 30px solid black;
}
.top-arrow {
margin-bottom: 30px;
}
.bottom-arrow {
margin-top: 30px;
}
.table_container {
width: 500px;
}
.table_container {
margin: 0 auto;
border: 1px solid black;
width: 700px
}
</style>
<div class="table_container">
<table id="items" class="inventory-table center" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
<tr id="inventory-row-1" class="inventory-row">
<td class="inventory-image">
<img src="<my image>" />
</td>
<td class="inventory-name">
Classic frame
</td>
<td class="inventory-price">
$<span class="price">139</span>
</td>
<td class="purchase-number-td">
<span class="purchase-number"></span>
</td>
<td>
<div data-inventoryrownumber="1" class="arrow-up top-arrow purchase-arrow"></div>
<div data-inventoryrownumber="1" class="arrow-down bottom-arrow purchase-arrow"></div>
</td>
</tr>
</table>
</div>
answered Mar 15 '14 at 23:42
Chococroc
8,90532548
8,90532548
Culprit turned out to be.inventory-row {display: inline-block; }. Thanks!
– jdotjdot
Mar 18 '14 at 6:02
add a comment |
Culprit turned out to be.inventory-row {display: inline-block; }. Thanks!
– jdotjdot
Mar 18 '14 at 6:02
Culprit turned out to be
.inventory-row {display: inline-block; }. Thanks!– jdotjdot
Mar 18 '14 at 6:02
Culprit turned out to be
.inventory-row {display: inline-block; }. Thanks!– jdotjdot
Mar 18 '14 at 6:02
add a comment |
up vote
0
down vote
Adjust the table margin, Add:
margin: 0 auto;
I actually had this before and added it now, and it still is not centered. No change.
– jdotjdot
Mar 15 '14 at 22:53
I cant understand what exactly u want to be centered
– user1972155
Mar 15 '14 at 23:00
I think you've understood what I'm looking to center. The advice you gave should work but isn't working--that's the problem I've been having. I want the table itself as a whole to be centered, but addingmargin: 0 auto;doesn't have an effect.
– jdotjdot
Mar 15 '14 at 23:03
ok screenshot now talks,, try using width:200px; or <table align='center'>.. but ur problem is the table width having full width
– user1972155
Mar 17 '14 at 14:59
add a comment |
up vote
0
down vote
Adjust the table margin, Add:
margin: 0 auto;
I actually had this before and added it now, and it still is not centered. No change.
– jdotjdot
Mar 15 '14 at 22:53
I cant understand what exactly u want to be centered
– user1972155
Mar 15 '14 at 23:00
I think you've understood what I'm looking to center. The advice you gave should work but isn't working--that's the problem I've been having. I want the table itself as a whole to be centered, but addingmargin: 0 auto;doesn't have an effect.
– jdotjdot
Mar 15 '14 at 23:03
ok screenshot now talks,, try using width:200px; or <table align='center'>.. but ur problem is the table width having full width
– user1972155
Mar 17 '14 at 14:59
add a comment |
up vote
0
down vote
up vote
0
down vote
Adjust the table margin, Add:
margin: 0 auto;
Adjust the table margin, Add:
margin: 0 auto;
answered Mar 15 '14 at 22:39
user1972155
283
283
I actually had this before and added it now, and it still is not centered. No change.
– jdotjdot
Mar 15 '14 at 22:53
I cant understand what exactly u want to be centered
– user1972155
Mar 15 '14 at 23:00
I think you've understood what I'm looking to center. The advice you gave should work but isn't working--that's the problem I've been having. I want the table itself as a whole to be centered, but addingmargin: 0 auto;doesn't have an effect.
– jdotjdot
Mar 15 '14 at 23:03
ok screenshot now talks,, try using width:200px; or <table align='center'>.. but ur problem is the table width having full width
– user1972155
Mar 17 '14 at 14:59
add a comment |
I actually had this before and added it now, and it still is not centered. No change.
– jdotjdot
Mar 15 '14 at 22:53
I cant understand what exactly u want to be centered
– user1972155
Mar 15 '14 at 23:00
I think you've understood what I'm looking to center. The advice you gave should work but isn't working--that's the problem I've been having. I want the table itself as a whole to be centered, but addingmargin: 0 auto;doesn't have an effect.
– jdotjdot
Mar 15 '14 at 23:03
ok screenshot now talks,, try using width:200px; or <table align='center'>.. but ur problem is the table width having full width
– user1972155
Mar 17 '14 at 14:59
I actually had this before and added it now, and it still is not centered. No change.
– jdotjdot
Mar 15 '14 at 22:53
I actually had this before and added it now, and it still is not centered. No change.
– jdotjdot
Mar 15 '14 at 22:53
I cant understand what exactly u want to be centered
– user1972155
Mar 15 '14 at 23:00
I cant understand what exactly u want to be centered
– user1972155
Mar 15 '14 at 23:00
I think you've understood what I'm looking to center. The advice you gave should work but isn't working--that's the problem I've been having. I want the table itself as a whole to be centered, but adding
margin: 0 auto; doesn't have an effect.– jdotjdot
Mar 15 '14 at 23:03
I think you've understood what I'm looking to center. The advice you gave should work but isn't working--that's the problem I've been having. I want the table itself as a whole to be centered, but adding
margin: 0 auto; doesn't have an effect.– jdotjdot
Mar 15 '14 at 23:03
ok screenshot now talks,, try using width:200px; or <table align='center'>.. but ur problem is the table width having full width
– user1972155
Mar 17 '14 at 14:59
ok screenshot now talks,, try using width:200px; or <table align='center'>.. but ur problem is the table width having full width
– user1972155
Mar 17 '14 at 14:59
add a comment |
up vote
0
down vote
fiddle
HTML
<table>
<th colspan='3'>header</th>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</table>
CSS
table {
border: 1px solid red;
width: 100%; /* for small screens */
max-width: 20em; /* define width while keeping it flexible */
margin-right: auto;
margin-left: auto;
}
add a comment |
up vote
0
down vote
fiddle
HTML
<table>
<th colspan='3'>header</th>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</table>
CSS
table {
border: 1px solid red;
width: 100%; /* for small screens */
max-width: 20em; /* define width while keeping it flexible */
margin-right: auto;
margin-left: auto;
}
add a comment |
up vote
0
down vote
up vote
0
down vote
fiddle
HTML
<table>
<th colspan='3'>header</th>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</table>
CSS
table {
border: 1px solid red;
width: 100%; /* for small screens */
max-width: 20em; /* define width while keeping it flexible */
margin-right: auto;
margin-left: auto;
}
fiddle
HTML
<table>
<th colspan='3'>header</th>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
<tr>
<td>data</td>
<td>data</td>
<td>data</td>
</tr>
</table>
CSS
table {
border: 1px solid red;
width: 100%; /* for small screens */
max-width: 20em; /* define width while keeping it flexible */
margin-right: auto;
margin-left: auto;
}
answered Mar 15 '14 at 23:56
sheriffderek
5,43752854
5,43752854
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f22430639%2fcant-center-a-table-in-html5-css3%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
the
<table>is expanding because it hasstyle="width:100%;"as you were saying. So how do you want to center such an element? It has no space left on the sides. Please make demo to clarify the problem.– Nico O
Mar 15 '14 at 22:51
@NicoO removing the
width=100%changes nothing. The table element, when I hover over it in Chrome Inspector, extends all the way from the left of the page to the right, but the child elements are still left-aligned.– jdotjdot
Mar 15 '14 at 22:55
Edit this demo to show your problem: jsfiddle.net/NicoO/ATvQ8
– Nico O
Mar 15 '14 at 23:00
@NicoO I've attached a Chrome dev tools screenshot to show the issue I'm having. As you can see,
tbodytakes up the whole screen, so the elements below aren't centered. If you hover over thetr, it simply ends after the arrows.– jdotjdot
Mar 15 '14 at 23:17
I added a center div and it works, check for the answer
– Chococroc
Mar 15 '14 at 23:42