How do i center tables in CSS?

https://www.codecademy.com/practice/projects/independent-project-html-documentation

In the project Build your own cheat sheet they gave us an example: https://s3.amazonaws.com/codecademy-content/PRO/independent-practice-projects/html-css-cheat-sheet/example/index.html

Were the table is align in the center i tried to do it using

align-text: center;

but this only align the text no the table, i checked the source code and couldnt find the solution to this problem.

At the end i find the way using google and applying this :

table {
margin-left:auto;
margin-right:auto;
}

The best way to do it is usually exactly how you did it, although you can write that a little cleaner as

table {
   margin: 0 auto;
}

Which sets the top & bottom margins to 0, and centers the table.

1 Like

Have you tried using only the align code instead of using text-align? Or, alternatively, you can do this;

Table {
padding-right:###
}

or

Table {
padding-left:###
}

According to what fits your need. Hope this helps!

1 Like

Thanks for your help, just one thing can you tell me how they did it in the example cheat sheet that they gave us?

This only works if we give the table a width property that is less than the viewport it is rendered in.

Not a good suggestion. Padding should be used very sparingly and in closed controlled situations. Centering a table is not one of them.

Also, Table is not the correct selector. table is.