centering table in html using css


Now that the CENTER tag and align="center" attribute are deprecated in the TABLE tag, how are you supposed to center your tables within your web page?

Use CSS to Center Your Table

You can add a single line to your CSS style sheet to center all tables horizontally:
table { margin: auto; }
Or you can add that same line to your table directly:
<table style="margin: auto;">
When you place a table inside a web page, you are placing it within a block-level element such as BODY, P,BLOCKQUOTE, or DIV. You can center the table within that element by using the margin: auto; style. This tells the browser to make the margins on all sides of the table equal. This puts the table in the center.

Internet Explorer 6 and Below Don’t Support This

If your site must support this old web browser, then you will need to continue to use align="center" or the CENTER tag to center your tables.


Method 1

To center a table, you need to set the margins, like this:
  table.center {
    margin-left:auto; 
    margin-right:auto;
  }
And then do this:
  <table class="center">
    ...
  </table>
At this point, Mozilla and Opera will center your table. Internet Explorer 5.5 and up, however, needs you to add this to your CSS as well:
  body {text-align:center;}

Method 2

If you want your table to be a certain percentage width, you can do this:
  table#table1 {
    width:70%; 
    margin-left:15%; 
    margin-right:15%;
  }
And then in your HTML/XHTML, you would do this:
  <table id="table1">
    ...
  </table>
Note that I was using an id to describe the table. You can only use an id once on a page. If you had many tables on a page that you wanted to be the same width and centered, you would do this in your CSS:
  table.center {
    width:70%; 
    margin-left:15%; 
    margin-right:15%;
  }
And this in your HTML:
  <table class="center">
    ...
  </table>
  <table class="center">
    ...
  </table>

Method 3

If you want your table to be of fixed width, define your CSS like this:
  div.container {
    width:98%; 
    margin:1%;
  }
  table#table1 {
    text-align:center; 
    margin-left:auto; 
    margin-right:auto; 
    width:100px;
  }
  tr,td {text-align:left;}
Set "width:100px" to whatever width you need.
"text-align: center" is there for Internet Explorer, which won't work without it. Unfortunately, "text-align: center" will center all the text inside your table cells, but we counter that by setting "tr" and "td" to align left.
In your HTML, you would then do this:
  <div class="container">
    <table id="table1">
      ...
    </table>
  </div>
Once again, I'm using an id. If you need to center several tables the same way, use a class instead of an id.

9 comments:

  1. nice post , very informative

    ReplyDelete
  2. Thank you a lot for providing individuals with such a spectacular possiblity to check tips from this blog. It is usually very terrific and as well , packed with a good time for me and my office co-workers to search the blog at the least 3 times in 7 days to read the latest stuff you have got. And indeed, I'm just at all times fulfilled with your magnificent advice you serve. Selected 4 tips in this post are clearly the most suitable we've ever had.slushy machine hire

    ReplyDelete
  3. "Dear Admin,
    I am Jamal Lloyd Johnson. Very informative post! I am thankful to you for providing this unique information.
    Jamal Lloyd Johnson
    Apollo Management:Mail: 1236 N. Sweetzer Ave, unit 17
    West Hollywood, CA 90069
    Jamal Lloyd Johnson
    Jamal Lloyd Johnson
    Jamal Lloyd Johnson
    Jamal Lloyd Johnson"

    ReplyDelete
  4. This amazing appears to be altogether recommended. Each smallish data are intended and also massive amount back ground awareness. I prefer this unique very much. Graphics Design

    ReplyDelete
  5. CSS is an important structure of the site, my site
    does feel average

    ReplyDelete
  6. Here you can generate or create Html table generator online for free without coding knowledge.

    ReplyDelete
  7. Much obliged for sharing the data, keep doing awesome... I truly delighted in investigating your site. great asset... click here digital marketing course

    ReplyDelete