in Technology by
How to center a table in CSS?

1 Answer

0 votes
by

The use of tables in the designing of a website is an exciting task. By default, the alignment of a table is towards the left, but by using the margin property in CSS, we can align it at the center.

If we set the value of margin-left and margin-right to auto, then the browsers show the table centered. We can use the shorthand property margin and set it to auto for aligning the table at the center rather than using the margin-left and margin-right property.

Instead of aligning the table to center, the text-align: center; property only centers the table content, such as the text inside the table.

we are using the text-align: center; property to center the content inside the table and margin-left: auto; and margin-right: auto; to center the table.

  1. <!DOCTYPE>  
  2. <html>    
  3. <head>  
  4. <style>    
  5. table, th, td {    
  6. border: 1px solid black;  
  7. margin-left: auto;  
  8. margin-right: auto;  
  9. border-collapse: collapse;    
  10. width: 500px;  
  11. text-align: center;  
  12. font-size: 20px;  
  13. }    
  14. </style>    
  15. </head>  
  16. <body>    
  17. <table>    
  18. <tr>  
  19. <th>First_Name</th>  
  20. <th>Last_Name</th>  
  21. <th>Subject</th>  
  22. <th>Marks</th>  
  23. </tr>    
  24. <tr>  
  25. <td>James</td><td>Gosling</td><td>Maths</td><td>92</td>  
  26. </tr>    
  27. <tr>  
  28. <td>Alan</td><td>Rickman</td><td>Maths</td><td>89</td>  
  29. </tr>    
  30. <tr>  
  31. <td>Sam</td><td>Mendes</td><td>Maths</td><td>82</td>  
  32. </tr>    
  33. </table>    
  34. </body>  
  35. </html>     

Related questions

0 votes
0 votes
    I have a problem with table-layout:fixed and width of inner columns if their content wider than column. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    How to install a Custom Jenkins Plugin or a Version of Plugin Not available in Jenkins Update Center?...
asked Jul 17, 2021 in Technology by JackTerrance
0 votes
    Describe how Israel is emerging as a new center of power. Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    I have a barChart : that looks like this The bars align to the left of the category, for instance ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I want my browser to open my local images and to place them at the center of my browser. The ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 20, 2022 in Education by JackTerrance
0 votes
    How to add your app to notification center? I want to create an custom link that will appear in ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    How to add your app to notification center? I want to create an custom link that will appear in ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    What will be the output of the following Python code? print('*', "abcde".center(6), '*', sep='') a) * abcde * b) *abcde * c) * abcde* d) * abcde *...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What do you mean by data center in Cassandra?...
asked Apr 15, 2021 in Technology by JackTerrance
+1 vote
    You can view the latest data center map and Pay as You Go subscription information in (the) ________?...
asked Oct 20, 2020 in Technology by JackTerrance
+1 vote
    You can view the latest data center map and Pay as You Go subscription information in (the) ________?...
asked Oct 20, 2020 in Technology by JackTerrance
0 votes
    What are new center of power Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    why do you think India is now considered as am important center of power? Select the correct answer from above options...
asked Aug 3, 2022 in Education by JackTerrance
0 votes
    This gear was inserted from the Catalog. It relocates the player to the center of the map when it' ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 22, 2022 in Education by JackTerrance
...