search
HomeWeb Front-endCSS TutorialHow to center a table with CSS?

如何用 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>

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>

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>

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!

Statement
This article is reproduced at:tutorialspoint. If there is any infringement, please contact admin@php.cn delete
Demystifying Screen Readers: Accessible Forms & Best PracticesDemystifying Screen Readers: Accessible Forms & Best PracticesMar 08, 2025 am 09:45 AM

This is the 3rd post in a small series we did on form accessibility. If you missed the second post, check out "Managing User Focus with :focus-visible". In

Create a JavaScript Contact Form With the Smart Forms FrameworkCreate a JavaScript Contact Form With the Smart Forms FrameworkMar 07, 2025 am 11:33 AM

This tutorial demonstrates creating professional-looking JavaScript forms using the Smart Forms framework (note: no longer available). While the framework itself is unavailable, the principles and techniques remain relevant for other form builders.

Adding Box Shadows to WordPress Blocks and ElementsAdding Box Shadows to WordPress Blocks and ElementsMar 09, 2025 pm 12:53 PM

The CSS box-shadow and outline properties gained theme.json support in WordPress 6.1. Let's look at a few examples of how it works in real themes, and what options we have to apply these styles to WordPress blocks and elements.

Working With GraphQL CachingWorking With GraphQL CachingMar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Making Your First Custom Svelte TransitionMaking Your First Custom Svelte TransitionMar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)Mar 04, 2025 am 10:22 AM

This article explores the top PHP form builder scripts available on Envato Market, comparing their features, flexibility, and design. Before diving into specific options, let's understand what a PHP form builder is and why you'd use one. A PHP form

Show, Don't TellShow, Don't TellMar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

What the Heck Are npm Commands?What the Heck Are npm Commands?Mar 15, 2025 am 11:36 AM

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft