Table of Contents
Use margin-left and margin-right properties
Example
illustrate
Use grid attribute
Using Flexbox properties
in conclusion
Home Web Front-end CSS Tutorial How to center a table with CSS?

How to center a table with CSS?

Sep 13, 2023 am 08:21 AM

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

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles