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
The Lost CSS Tricks of Cohost.orgThe Lost CSS Tricks of Cohost.orgApr 25, 2025 am 09:51 AM

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

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

Video Face Swap

Video Face Swap

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

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!