How to center a table with CSS?

王林
Release: 2023-09-13 08:21:02
forward
2023 people have browsed it

如何用 CSS 使表格居中?

The

tag is used to create traditional components in HTML called tables. When designing web pages, knowing how to improve the overall visualization of your design is essential. Center-aligning the table is one of the important aspects. This tutorial will teach us how to center a table using CSS.

Use margin-left and margin-right properties

This is a popular way to center align table elements in HTML and CSS. The CSS margin-left and margin-right properties are used to set the left and right margins of elements respectively. Margins create space outside of an element's borders, effectively pushing the element away from other elements on the page.

The value of the attribute can be set using a length value (for example, px, em, cm), a percentage, or the predefined values ​​auto, inherit, or initial.

The Chinese translation of

Example

is:

Example

<!DOCTYPE html>
<html lang="en">
   <head>
      <style>
         .table-container,
         th,
         td {
            border: 2px solid rgb(96, 100, 218);
         }
         .table-container {
            width: 70vw;
            margin-left: auto;
            margin-right: auto;
         }
      </style>
   </head>
   <body>
      <table class="table-container">
         <th>Name</th>
         <th>id</th>
         <th>Salary</th>
         <tr>
            <td>Suranjan</td>
            <td>12475142</td>
            <td>32000</td>
         </tr>
      </table>
   </body>
</html>
Copy after login

illustrate

  • The code is an HTML file that creates a table with three columns: Name, id, and Salary. The table's "table container" class width is 70% of the viewport width and is centered on the page. Header cells (name, id, and salary) and table cells have 2-pixel solid borders with color rgb(96, 100, 218).

  • The table has a row of data, which contains the name "Suranjan", the id is 12475142, and the salary is 32000.

Use grid attribute

Another common method is to use the grid attribute to center-align the table. Grids are two-dimensional elements of HTML and CSS that we can use to create rows and columns. If we first declare the table as a grid element, we can then center align the table using the grid's place-items property. The place-items property actually aligns the grid horizontally and vertically to the center.

The Chinese translation of

Example

is:

Example

<!DOCTYPE html>
<html lang="en">
   <head>
      <style>
         .table-container,
         th,
         td {
            border: 2px solid rgb(96, 100, 218);
         }
         .table-container {
            width: 70vw;
         }
         body{
            display: grid;
            place-items: center;
         }
      </style>
   </head>
   <body>
      <table class="table-container">
         <th>Name</th>
         <th>country</th>
         <th>Occupation</th>
         <tr>
            <td>Tom Holland</td>
            <td>USA</td>
            <td>Software Developer</td>
         </tr>
      </table>
   </body>
</html>
Copy after login

illustrate

  • This is basic HTML code to create a table with three columns - Name, Country, and Occupation. The table has a row of data that contains values ​​for name (Tom Holland), country (United States), and occupation (Software Developer).

  • The CSS style defined in the head section of HTML sets the table's borders, table cells (th, td) and the table itself (class="table-container") to a 2px solid RGB color (96, 100, 218). The width of the table is set to 70vw (70% of the viewport width). The body section is set to appear as a grid and center the items on the page.

Using Flexbox properties

Another very popular method is to use the CSS flexbox property to center align the table. Flexbox is a responsive element for HTML and CSS. Flexbox has certain properties like alien-items, justify-content, etc. that we can use to center the table. Programmers often find this method the most convenient way to center a table.

The Chinese translation of

Example

is:

Example

<!DOCTYPE html>
<html lang="en">
   <head>
      <style>
         .table-container,
         th,
         td {
            border: 2px solid rgb(96, 100, 218);
         }
         .table-container {
            width: 70vw;
         }
         body{
            display: flex;
            flex-direction: row;
            align-items: center;
            justify-content: center;
         }
      </style>
   </head>
   <body>
      <table class="table-container">
         <th>Name</th>
         <th>class</th>
         <th>Subject</th>
         <tr>
            <td>Suranjan</td>
            <td>12</td>
            <td>Mathematics</td>
         </tr>
      </table>
   </body>
</html>
Copy after login

illustrate

  • This code is an HTML file that creates a table with three columns: Name (name), id (number) and Salary (salary). The table has a class named "table-container" with a width of 70% of the viewport width. The header cells (Name, id, and Salary) and table cells all have 2px solid borders with the color rgb(96, 100, 218). The table has one row of data, including the name "Suranjan", the number 12475142, and the salary 32,000.

  • The main body of the HTML document has the CSS style display: flex, making the main body a flexible container. CSS style flex-direction: row sets the flex container's item direction to row. The CSS styles align-items and justify-content center items vertically and horizontally respectively.

in conclusion

In this article, we learned how to center a table with the help of CSS. In this tutorial, we saw the use of margin properties, grid, flexbox, and more. Of all the methods discussed, Flexbox should be used. This is because flexboxes are more convenient and responsive to UI elements.

The above is the detailed content of How to center a table with CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template