Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

align = "center" is not working with table

I have this table I want to align in the middle of the webpage. But it is stuck on the left hand side of the page. This is my code:

<table class="tableOne" align="center">
    <tr >
        <img src='Images/Header.png' />
    </tr>
</table>

What is wrong with the code? I just need the table in the middle of the page.

like image 317
Christopher Quibell Avatar asked Oct 26 '13 21:10

Christopher Quibell


People also ask

Why does Align Center doesn't work?

This is because text-align:center only affects inline elements, and <aside> is not an inline element by default. It is a block-level element. To align it, we will have to use something other than text-align , or turn it into an inline element.

How do I center align data in a table?

To center align text in table cells, use the CSS property text-align. The <table> tag align attribute was used before, but HTML5 deprecated the attribute. Do not use it. So, use CSS to align text in table cells.

How do I center align text in a table?

Select the text that you want to center, and then click Paragraph on the Format menu. On the Indents and Spacing tab, change the setting in the Alignment box to Centered, and then click OK.


1 Answers

Don't use align="center" it's not a standard property. You could opt use CSS instead.

To center the table within its container using margin:auto.

<style>
  .tableOne {
    margin:auto;
  }
</style>

To center the text within the table cell, use text-align:center.

<style>
  .tableOne td {
    text-align:center;
  }
</style>

Also you didn't add a <td> inside the <tr>. You should always make sure the contents of a table cell is within a <td> so your HTML is valid.

<table class="tableOne">
    <tr>
        <td>
            <img src='Images/Header.png' />
        </td>
    </tr>
</table>

As a side note, if you're only using the table to center your image you should get rid of the table. This could be replaced by a simple div.

HTML

<div class="center-me">
    <img src='Images/Header.png' />
</div>

CSS

.center-me {
    text-align:center;
}
like image 56
Daniel Imms Avatar answered Oct 01 '22 04:10

Daniel Imms