A Comprehensive Guide on How to Center a Table with CSS

how to Center A Table with CSS

A table is an invaluable tool for presenting plenty of information in a structured manner. From sales figures to website traffic, stock market trends, and even students’ grades, tables serve as a reliable means of organization.

Incorporating a table into a web page using HTML can significantly enhance the visual appeal. While centering text and images can be accomplished through methods like the text-align class or CSS, achieving the same effect for a table necessitates a distinct approach. 

Let’s see a detailed guide on how to center a table seamlessly within a web page.

How to center a table in HTML

The utilization of tables in web design has an intriguing history. Before the widespread adoption of CSS, tables were not merely confined to displaying tabular data or lists conventionally. Instead, they were frequently employed to control entire page layouts.

In those days, HTML tables served a dual role, defining both the structure and visual appearance of web pages. You could directly specify the table’s positioning in HTML with a simple line of code.

<table align=”center”>
<!– Table content here –>

However, the practice of centering tables in this manner is now considered outdated, as it has been deprecated in HTML5. Contemporary web standards prioritize the separation of structure (HTML) and style (CSS), and the aforementioned technique contravenes this fundamental principle.

HTML should no longer dictate the visual presentation of elements; this responsibility now squarely rests on CSS. So, what is the correct way to center a table using CSS? Let’s delve into the art of centering tables using CSS.

How to center a table with CSS

CSS assumes a pivotal role in determining the appearance and positioning of every element on a web page, including tables and their components like <th>, <tr>, and <td>.

Modern browsers will automatically render the table in the center when both the left and right margins are set to equal values. This can be effortlessly accomplished by setting both margins to “auto,” as demonstrated below:

table {  
margin-right: auto;  
margin-left: auto;

Centering a table is not accomplished in the same way as centering text, which can be achieved using text-align: center. This is because the table element is considered a block-level element, as opposed to an inline element. It is designed to center inline content, such as text within the table, rather than the table itself.

How to center a table in old browsers

However, for older versions of Internet Explorer, a peculiar bug treats block-level elements as inline elements. Consequently, to center a table for compatibility with certain versions of IE, you must explicitly apply text-align: center to the parent element of the table, typically the body element.

body {  
text-align: center;

How to center a table in both modern and older browsers

To ensure compatibility across different browsers, you can experiment with both margin-left: auto; margin-right: auto, and text-align: center and observe their behavior.

Let’s explore how to center a table in both modern and older browsers to achieve optimal results.

<!– Table content here –>  

Styles will be applied to either the <div>, <table>, or sometimes both. The style sheet sections that we’ll manipulate to set the margins are as follows:

For modern browsers-

.center1 {  
margin-right: auto;  
margin-left: auto;

For older and newer browsers combined:

.centertbl {  
text-align: center;
.centertbl table {
margin-left: auto;  
margin-right: auto;  
text-align: left;

How It Works- center a table in a div

The first part is applied to the <div> containing the <table>, effectively centering the table in Internet Explorer 5 and Netscape 4. The second part is targeted at the <table> within the <div>.

By adjusting the margin settings, you can successfully center a table in browsers that are well-equipped to handle CSS, while ensuring that the inline text defaults to left alignment for older browser support, thereby maintaining consistent presentation.

Centering with margins

One of the most commonly employed techniques to center a table involves setting both top and bottom margins to 0, while the left and right margins are set to auto. Here’s a frequently used approach:

table {
 margin: 0 auto; 

Alternatively, you can achieve the same result like this:

table {
   margin-left: auto;  
margin-right: auto;

If you desire a table with an exact width, you can set it as you normally would, and the automatic margin will distribute the remaining space evenly.

table {  
width: 500px;  
margin: 0 auto;

Another option is to use percentages to define the width, providing greater flexibility.

table {
  width: 50%;  
margin: 0 auto; /* Equivalent to margin: 0 25%; */

Cell alignment: text-align vs. vertical-align

When it comes to centering text in CSS, there are two aspects to consider: 

  1. Horizontal alignment, and 
  2. vertical alignment within a cell. 

Horizontal alignment (center, left, or right) is controlled by the text-align property, while vertical alignment (middle, top, or bottom) is governed by the vertical-align property.

To align text as desired within TH or TD elements, apply the following properties:

td {  
text-align: center;  
vertical-align: top; /* Adjust as needed */

Text justification refers to adding spaces between words until they fit the available line width, excluding the final line, which is not justified.

Table styling tips

Before going to the next part, let’s provide a quick reference list of tips to enhance your table styling with CSS:

  1. Align <td> and <th> text using text-align to improve readability and visual coherence.
  2. Use <tfoot>, <tbody>, and <thead> to structure your table logically, enabling precise CSS application for better styling.
  3. Employ table-layout to simplify column width management; setting the heading width will automatically adjust column widths accordingly.
  4. Enhance readability by employing alternating colors for rows, facilitating quick data comprehension.
  5. Keep your tables simple, using percentages to maintain consistent sizing and minimize the need for frequent adjustments.
  6. Implement border-collapse for a cleaner and neater table appearance.
Ninja tables Subscribe To Get
Subscribe to Our Newsletter

Get Exclusive Tips, Updates, and Inspirations in Your Inbox!

Subscribe Form (Blog)

The easiest way to center a table

So, that was the way you can center your website tables with CSS. But if you are not that good with HTML and CSS, don’t worry, Ninja Tables got your back.

Ninja Tables is WordPress’s best table builder plugin that enables you to create almost any kind of table for your website. And the best thing about these tables is the easiest customization option. You can center your table in one click.

Fully responsive tables are automatically aligned at the center of your web pages. You have all the options to customize the alignment and every other aspect of your tables. 

How to center your table with Ninja Tables

It’s a feature-rich table builder plugin that comes with drag and drop and advanced table builder options. There are free table templates and 100+ customization options with 3 most popular CSS libraries.

Now, table building for your website is as easy as pie.

Mohiuddin Omran17-Apr-24Misleading Statistics: Learn, Spot, Avoid, and Get Rid of them publishTips
Jahan Liza03-Apr-24A Guide to Comparison Tables for Products, Services, and FeaturespublishTips
Nusrat Fariha19-Mar-24Ninja Tables Features That Simplify Your LifepublishNinja Tables
Jahan Liza06-Mar-247 Techniques to Speed up WooCommerce CheckoutpublishTips, WooCommerce
Editorial Panel01-Mar-245 Best WordPress Pricing Table Plugins in 2024publishWordPress Plugins
Jahan Liza27-Feb-24Ninja Tables Turns 6!publishNinja Tables, Review
Jahan Liza28-Feb-24Color Palettes for Data VisualizationpublishTips
Nusrat Fariha09-Feb-24Best Chart Plugins in WordPress: Improve UX with Interactive ChartspublishTips, WordPress Plugins
Mohiuddin Omran26-Jan-24How to Make a Graph in Google SheetspublishTips
Jahan Liza15-Jan-24Export-Import Tables in WordPresspublishNinja Tables, Tips
Mohiuddin Omran10-Jan-24Best jQuery Table Plugins in 2024publishTips, WordPress Plugins
Jahan Liza28-Dec-23Mapping Tomorrow: Emerging Data Trends for 2024publishTips
Mohiuddin Omran22-Dec-23FluentSnippets Review: Facts, Features, and AlternativespublishReview, WordPress Plugins
Nusrat Fariha09-Jan-24Links in Data Tables: Dynamic Data VisualizationpublishNinja Tables
Mohiuddin Omran04-Dec-23Amazon Affiliate Program: Everything You Need to KnowpublishTips
Nusrat Fariha20-Dec-23Ninja Tables in 2023: Year In ReviewpublishNinja Tables, Review
Jahan Liza23-Nov-23Conditional Formatting in WordPress TablespublishTips, WordPress Plugins
Jahan Liza07-Nov-23Guiding Data Table Examples for Great Website VisualizationpublishTips
Tanzil Ebad Chowdhury27-Oct-239 WordPress Data Visualization Plugins You Can't Overlook!publishWordPress Plugins
Jahan Liza20-Oct-23Introducing FluentBooking: WordPress Scheduling PluginpublishReview, WordPress Plugins
Jahan Liza18-Oct-23Get in The Spirit of Holiday Sales with 13 Halloween Marketing IdeaspublishTips
Mohiuddin Omran12-Oct-23Bootstrap Table: Beginners Guide with Usable ExamplespublishTips
Mohiuddin Omran27-Sep-23A Comprehensive Guide on How to Center a Table with CSSpublishTips
Jahan Liza07-Sep-23How to Generate Leads, Increase User Engagement, and Drive TrafficpublishTips
Nusrat Fariha24-Aug-23FOMO Marketing Ideas: Create Urgency and Drive ConversionspublishTips
Tanzil Ebad Chowdhury23-Aug-237+ Dynamic Data Types: Toolkit for Configuring Table ColumnspublishTips, WordPress Plugins
Jahan Liza21-Aug-23How to Create Stackable Tables with Ninja TablespublishNinja Tables, WordPress Plugins
Jahan Liza31-Jul-23How to Add Media to a Data TablepublishNinja Tables, WordPress Plugins
Tanzil Ebad Chowdhury27-Jul-232 Easy Ways to Merge Cells in WordPress Tables (+Pro Tips)publishNinja Tables, Tips
Nusrat Fariha20-Jul-23Simplifying Data Presentations & Common MistakespublishNinja Tables, Tips
Mohiuddin Omran18-Jul-23How to Make a Table in Google Sheets: In-depth TutorialpublishTips
Tanzil Ebad Chowdhury12-Jul-237 Types of Comparison Charts for Effective Data VisualizationpublishTips
Jahan Liza14-Jul-23Closer Look at Charts vs Tables: The Right Data VisualizationpublishComparison, Ninja Tables
Mohiuddin Omran03-Jul-23How to Write a Google Review for a Company + ExamplespublishReview, Tips
Nusrat Fariha22-Jun-23Effective Summer Marketing Tips to Boost Your SalespublishTips
Jahan Liza19-Jun-23How To Design a Table the Right Way: Mastering Data Table DesignpublishNinja Tables, Tips
Tanzil Ebad Chowdhury15-Jun-23Creating Responsive Tables in WordPress | No-Code SolutionpublishNinja Tables
Md Shahjahan08-Jun-23Introducing Ninja Tables 5.0.0: Smoother, Lighter, and More PowerfulpublishNinja Tables
Nusrat Fariha02-Jun-23How To Customize Default WooCommerce ButtonpublishNinja Tables, WooCommerce
Jahan Liza07-Jun-23Limitations of WooCommerce and Best AlternativespublishNinja Tables, WooCommerce
Tanzil Ebad Chowdhury25-May-23TablePress vs Ninja Tables: Which Is Better?publishComparison
Jahan Liza22-May-23What Makes a Good Website: Checklist for Business OwnerspublishTips
Mohiuddin Omran11-May-23TablePress Review: Is It Worth Upgrading to Pro Or Not?publishComparison, WordPress Plugins
Tanzil Ebad Chowdhury08-May-23Good Data Visualization: Master the Art of Visual StorytellingpublishTips
Nusrat Fariha29-Apr-23Color Customizing WordPress TablespublishNinja Tables, WordPress Plugins
Mohiuddin Omran17-Apr-23WooCommerce Inventory Management: Why, How, WhatpublishTips, WooCommerce
Nusrat Fariha10-Apr-23Table Data Visualization: Efficiency and GuidelinespublishWordPress Plugins
Mohiuddin Omran30-Mar-23WordPress WooCommerce Tutorial: Step-by-Step GuidepublishWooCommerce
Mohiuddin Omran30-Mar-23History, Pros, Cons, and Brief Overview of WooCommercepublishWooCommerce
Nusrat Fariha23-Mar-23Optimize WooCommerce for Higher Conversion RatespublishWooCommerce
Mohiuddin Omran08-Mar-23Data Visualization Basics, Skills & TechniquespublishTips
Nusrat Fariha07-Mar-23Reviews of Ninja Tables Google Sheets IntegrationpublishWordPress Plugins
Mohiuddin Omran23-Feb-23Making a Product Comparison Table in WordPresspublishNinja Tables, WordPress Plugins
Mohiuddin Omran17-Feb-23Ninja Tables Is Turning 5!publishNinja Tables, Review
Nusrat Fariha10-Feb-23Easily Add Restaurant Reservation System to WordPresspublishTips, WordPress Plugins
Mohiuddin Omran07-Feb-23Valentine’s Day Marketing Ideas, Strategies & ExamplespublishNinja Tables, Tips
Nusrat Fariha06-Feb-23Create Charts in WordPress With a Free Charts PluginpublishNinja Tables
Nusrat Fariha02-Feb-23How To Create WP Posts TablepublishNinja Tables
Mohiuddin Omran27-Jan-23Valentine’s Ideas for Your Business to Celebrate LovepublishTips
Nusrat Fariha24-Jan-23Ninja Tables Free vs PropublishNinja Tables, WordPress Plugins
Mohiuddin Omran10-Jan-23Ways to Create a Responsive Table in WordPresspublishNinja Tables, Tips
Nusrat Fariha11-Jan-23Best WooCommerce Product Table PluginspublishWooCommerce, WordPress Plugins
Nusrat Fariha03-Jan-23How To Integrate WooCommerce With Ninja TablespublishNinja Tables, WooCommerce
Mohiuddin Omran14-Dec-22Ninja Tables 2022 Year in Review: What's New, What's next?publishNinja Tables, Review
Nusrat Fariha09-Dec-22Designing Data Tables for ChristmaspublishTips, WooCommerce
Mohiuddin Omran07-Dec-22Bad Data Visualization Examples: Fix It or Risk ItpublishTips
Mohiuddin Omran04-Jan-23Easiest Way to Make Beautiful Restaurant Menu for WebsitepublishNinja Tables, Tips
Mohiuddin Omran10-Nov-22Use SEO-Optimized Tables to Get Rich Snippets in SERPpublishTips
Nusrat Fariha09-Nov-22Ninja Tables Advanced Features ExplainedpublishNinja Tables
Mohiuddin Omran03-Nov-22wpDataTables Review: Pros, Cons, Pricing, and SubstitutepublishReview, WordPress Plugins
Mohiuddin Omran31-Oct-22How to Design A Converting Pricing Table to Engage CustomerspublishNinja Tables, Tips
Nusrat Fariha27-Oct-22Marketing Tips to Increase Sales for WordPress UserspublishTips
Mohiuddin Omran24-Oct-22How To Create League Points Table in WordPress with Ninja TablespublishNinja Tables, Tips
Nusrat Fariha19-Oct-22Online Pricing Tables Without CodingpublishNinja Tables
Md Shahjahan13-Oct-22Ninja Tables 4.3.1: Bug Fixes And A Lot of ImprovementspublishNinja Tables
Nusrat Fariha06-Oct-22WooCommerce Halloween Sales 2023publishNinja Tables, WooCommerce
Mohiuddin Omran04-Oct-22Comparison Table Best Practices to Boost Customer ExperiencepublishNinja Tables, Tips
Mohiuddin Omran27-Sep-22Pricing Tables: Strategies, Examples, and Best PracticespublishNinja Tables, Tips
Mohiuddin Omran15-Sep-22Paymattic: The Ultimate Payment and Fundraising PluginpublishReview, WordPress Plugins
Mohiuddin Omran09-Sep-22Best WooCommerce Product Table AlternativespublishComparison, WooCommerce
Mohiuddin Omran02-Sep-22wpDataTables Alternatives: Best Way to Visualize Table DatapublishComparison
Mohiuddin Omran29-Aug-22How To Create WooCommerce Product Table EffortlesslypublishNinja Tables, WooCommerce
Nusrat Fariha26-Aug-22Barn2 Plugins Vs Ninja Tables | Posts Table & WooCommerce Product TablespublishComparison, WooCommerce
Nusrat Fariha23-Aug-22Barn2 Plugins Bundle Offer or Standalone Ninja Tables?publishComparison, WooCommerce
Nusrat Fariha19-Aug-22Ninja Tables vs wpDataTables | Best Table Plugin in WordPresspublishWordPress Plugins
Mohiuddin Omran18-Aug-22Diving into the Best Table Plugins With Google Sheets IntegrationpublishWordPress Plugins
Nusrat Fariha15-Aug-22A Beginner’s Guide to Ninja Tables Drag and Drop BuilderpublishNinja Tables
Mohiuddin Omran12-Aug-22Best TablePress Alternatives to Make Data Tables More EfficientpublishComparison
Mohiuddin Omran10-Aug-22Image Gallery Plugin for WordPress: Create Awesome GalleriespublishNinja Tables, Tips
Mohiuddin Omran06-Aug-22How to Create an Image Gallery in WordPresspublishNinja Tables, Tips
Nusrat Fariha23-Sep-22The Right Tools To Boost WooCommerce SalespublishWooCommerce, WordPress Plugins
Mohiuddin Omran29-Jul-22How to Create Responsive Tables Without Compromising UXpublishNinja Tables
Nusrat Fariha02-Aug-22WordPress Tables With Online Form EntriespublishNinja Tables
Mohiuddin Omran27-Jul-22Product Comparison Table: Best WP Plugin to Create Amazon Affiliate TablespublishWordPress Plugins
Mohiuddin Omran22-Jul-226 Strategies to Optimize WooCommerce Product PagepublishTips, WooCommerce
Mohiuddin Omran18-Jul-22Create Online Tables Easily with Drag and Drop Table BuilderpublishNinja Tables, Tips
Nusrat Fariha25-Jul-22Best Form Plugins in WordPresspublishWordPress Plugins
Nusrat Fariha05-Jul-22Ninja Tables & WP Table Builder ComparedpublishComparison, Ninja Tables
Nusrat Fariha04-Jul-22Why Use Table Plugins | Benefits & ImportancepublishNinja Tables
Nusrat Fariha23-Jun-22Best Drag and Drop Table Plugin In WordPresspublishNinja Tables
Nusrat Fariha23-Jun-22Best Uses of Ninja Tables For WooCommercepublishNinja Tables, WooCommerce
Nusrat Fariha23-Jun-22Best Uses Of WooCommerce Table PluginspublishNinja Tables, WooCommerce
Nusrat Fariha22-Jun-22Google Sheets Integration In WordPress TablepublishNinja Tables
Nusrat Fariha22-Jun-22Best Ways To Create WordPress TablespublishNinja Tables

Closing thoughts on how to center a table

Now you know how to center tables using CSS effectively. As discussed, the recommended approach involves setting both left and right margins to auto. This method is suitable for almost all modern browsers that seamlessly support CSS.

In cases where this method doesn’t work, just opt for the text-align approach by wrapping the table in an outer <div> and applying text-align to it. Additionally, for fine-grained control over text alignment within cells, leverage the text-align and vertical-align properties.

Alternatively, just opt for Ninja Tables to automate all these things. Just drag and drop elements to create amazing customizable tables.

Ninja Tables Logo

Ninja Tables– Easiest Table Plugin in WordPress

Hopefully, this article helped you with your query about how to center a table with CSS. For today, that’s all your answer with an advanced tip. See you with new tips and tricks. Comment below to express your opinion.

Follow us on Facebook, Linkedin, and Twitter for the latest updates. Watch our YouTube playlist for tips and tricks about your website.

Get In touch with Ninja Tables

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *