center aligning a table - html

how do you center align a table in the middle of the page using css. This is what I have http://jsfiddle.net/m5uEr/
<div style="text-align: center; border-style:solid;">
<table style="border-style:solid;">
<tr>
<td>test</td>
</tr>
</table>
</div>

table.center {
margin-left:auto;
margin-right:auto;
}
<table style="border-style:solid;" class="center">
<tr>
<td>test</td>
</tr>
</table>
Your updated fiddle
Google found it for me with this text: "center align table css"

Set a width on your table and set
margin:auto

css wasn't working so I did a <table align="center">
this is on IE8

Did you put margin-left:auto, margin-right:auto in the properties of the div?
That is easy to do.
Those properties should belong in table
<div style="text-align: center; border-style:solid;">
<table style="border-style:solid; margin-left:auto; margin-right:auto; ">
<tr>
<td>tdddest</td>
</tr>
</table>
</div>​

Related

HTML/CSS Table in front of image

I'm trying to get this table in front of this image and it just doesn't happen, I know the table isn't that good its just an example!
HTML
<section>
<div class="image-wrapper">
<img src="img/animesea.png" alt="animeb-image" class="picimage">
<table>
<tr>
<td>a</td>
</tr>
</table>
</div>
</section>
Thanks
I would place the image as a background-image like #SVK suggested.
Here is another option. Wrap the img tag and the table tag in separate divs and apply position: absolute on both.
jsFiddle
You could also use z-index and positioning. jsFiddle. It's not clear exactly what you want to achieve, but based on what you've asked I would apply the image as a background like #SVK said.
You can use css style so background image will be adjusted all the time with table size.
There is code :
.tableBckImg
{
background:url(http://www.placehold.it/300x300);
background-repeat:no-repeat;
background-size:100% 100%;
}
<table cellspacing="0" cellpadding="0" border="0" class="tableBckImg">
<tr>
<td width="50" align="center">1</td>
<td width="50" align="center">2</td>
<td width="50" align="center">3</td>
</tr>
<tr>
<td width="50" align="center">4</td>
<td width="50" align="center">5</td>
<td width="50" align="center">6</td>
</tr>
</table>
You can change number of rows and columns and see how background image expand or shrink, here, in fiddle example
In this example, background image is 300x300 px.
Check it out.You will get some idea.
<!DOCTYPE>
<html>
<style>
.image-wrapper
{
width:650px;
height:488px;
background: url("http://stuffpoint.com/sea-and-beach/image/56782-sea-and-beach-anime-sea-pic.jpg") no-repeat;
background-size:contain;
padding:5px;
margin:auto;
}
table
{
width:650px;
height:150px;
border:2px solid yellow;
color:white;
background-color:rgba(0,0,0,0.5);
text-align:center;
padding:2px;
}
th
{
border:1px solid yellow;
color:white;
font-size:20px;
}
td
{
border:1px solid yellow;
color:white;
}
</style>
<body>
<section>
<div class="image-wrapper">
<table>
<th colspan='3'>Person name and age</th>
<tr>
<td>First name</td>
<td>Last name</td>
<td>Age</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>54</td>
</tr>
<tr>
<td>John</td>
<td>smith</td>
<td>74</td>
</tr>
</table>
</div>
</section>
</body>
</html>

Align table vertically and horizontally

I am trying to align a table. The table needs to be centered horizontally, and to vertically maintain the same margin from the top.
When I use the following CSS, it would center the table horizontally, but ignore the top:50px.
Then I used position:absolute; on CSS, then it would read left:50px; but would not center horizontally.
How can I achieve this? I am trying to stay away from DIV, but if it is the only way, I will use it.
<body>
<table border="1" id="mainTable">
<tr>
<td width="150px" height="100"> </td>
<td width="700px"> </td>
<td width="150px"> </td>
</tr>
<tr>
<td height="800"> </td>
<td></td>
<td> </td>
</tr>
<tr>
<td height="100"> </td>
<td> </td>
<td> </td>
</tr>
</table>
</body>
#mainTable{
width:1000px;
top:50px;
margin-left:auto;
margin-right:auto;
}
you just add text-align:center in td tag,
#mainTable tr td{
text-align:center;
}
Check this Fiddle Demo
Just solved. I used position:relative; instead of position:absolute; And it worked.
Thank you everybody.
#mainTable tr td{vertical-align:middle;}

DIV in <td> float right

I got a table with a couple <td>:
<table>
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px;">
<div>
Third
</div>
</td>
</tr>
</table>
What I want to do is to place the "Third" <td> (with the div) to the right side of the table and the "First" and "Second" <td> should stay left.
Style with float:right; didn't work for me...
You need to make your table's width 100%, then control the widths of your first 2 columns, and finally right-align your third column.
http://jsfiddle.net/25Mqa/1/
<table>
<tr>
<td class="first">First</td>
<td class="second">Second</td>
<td class="third">Third</td>
</tr>
</table>
CSS:
table { width:100%; }
.first, .second { width:50px; }
.third { text-align:right; }
The problem is that the width of a <table> is determined by its content, by default. If you want the <table> to span 100% width (of its containing block) like block-level elements do, you can either add table-layout: fixed; and then specify your width - or just give it a width, depending on what you're after, e.g.
table {
width: 100%;
}
http://jsfiddle.net/QEaAd/2/
try add style="text-align:right;"
<table>
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px; text-align:right;">
<div>
Third
</div>
</td>
</tr>
</table>
Only if you have 2 divs one near other:
<div id="fr">div1</div>
<div id="fr">div2</div>
you can float them right:
<style>
#fr{float:right;}
</style>
<table style="width: 100%;">
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px; display: block; float: right;">
<div>
Third
</div>
</td>
</tr>
</table>
http://jsfiddle.net/pbesD/
I believe this does what you want, however from what I understand, floating table elements will cause problems in versions of Internet Explorer <8
I dont know what you are trying to do with tables and divs? But I normally use this for emailers.
I use the align attribute for td's. This helps a lot in making sure your layout looks the way you want. And it works in all browsers :)
FIDDLE
HTML:
<table width="100%">
<tr>
<td>First</td>
<td>Second</td>
<td style="padding:20px;" align="right">
<div>
Third
</div>
</td>
</tr>
</table>
In Bootstrap 5.2 you can add .text-start and .text-end to your text class to align elements inside a table.

center table in HTML

How can I center the table within a div using html?
I have placed my content within a div tag and set the text-align attribute to center like the following.
text-align: center;
This has not worked.
Below is my html.
<html>
<body>
<div style="text-align:center">
<p>
text test
</p>
<table border="1">
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</div>
</body>
</html>
Give width to table, and set margin auto horizontally.
table {
width:500px;
margin: 10px auto;
}
Try the below
<table style="margin:0px auto; width:500px">
<div style="text-align:center">
<table style="display: inline-table;">
</table>
</div>
Edit the table tag like below
<table border="1" align="center">
and then check.
<html>
<body>
<div style="text-align:center">
<p>
text test
</p>
<table border="1" style="margin: 0 auto;">
<tr>
<td>100</td>
<td>200</td>
<td>300</td>
</tr>
<tr>
<td>400</td>
<td>500</td>
<td>600</td>
</tr>
</table>
</div>
</body>
</html>
Instead of <div style="text-align:center"> you could write <div style="width:600px;margin:0 auto">
Which gives you a div with the width of 600 pixels and centers the div in the parent element.
Add margin: 0px auto to your table tag
you can put the style in the table
<table border="1" style="margin:0 auto;">
However I do suggest that you use a style sheet and not inline styles

multiple tables on same line

I have three tables that are all contained in a div. I am trying to get them to all show inline in the div, but they each show on their own line. I set them all to display: inline at the table level, but this didn't do the trick. Any ideas of how to get multiple tables to show on the same line?
<div id="container">
<table id="previous-arrow" class="scroll">
<tr>
<td><span>Previous</span></td>
</tr>
</table>
<table id="tour-carousel">
<tr id="carousel-row">
<td>data here</td>
</tr>
</table>
<table id="next-arrow" class="scroll">
<tr>
<td><span>Next</span></td>
</tr>
</table>
</div>
You want to make them display inline, but still as tables (otherwise their content will create anonymous table blocks which cause wrapping).
display: inline-table;
You can float them:
HTML
<div class="container">
<table><tr><td>Table 1</td></tr></table>
<table><tr><td>Table 2</td></tr></table>
<table><tr><td>Table 3</td></tr></table>
</div>
CSS
table {
float:left;
width:33%;
}
Fiddle: http://jsfiddle.net/kboucher/6HuyW/
Try to float them left in the css.
table {
float:left;
}
And make sure the width's of them side-by-side isn't greater than that of there container.
USE BELOW CODE
<div id="container">
<table id="previous-arrow" class="scroll" **style="float:left"**>
<tr>
<td><span>Previous</span></td>
</tr>
</table>
<table id="tour-carousel" **style="float:left"**>
<tr id="carousel-row">
<td>data here</td>
</tr>
</table>
<table id="next-arrow" class="scroll" **style="float:left"**>
<tr>
<td><span>Next</span></td>
</tr>
</table>
</div>