×

How to center a table in CSS

The table is used to store and arrange the data in tabular format. The data here can be of any form like text, image, links, etc. The table is made of rows and columns. We use the table in html for the tabular view of our data, but by default, the table alignment in html is towards the left. So now, if we want our table to be placed in the center, how can we do it using CSS.

We would think that the obvious and easy way might be using the align attribute on the table means <table align =" center"> or we might think that we can text-align: center on the table tag to align the table in the center.

But you're mistaken; this will not display your table in the center; instead, it will only align the text or content inside the table to the center, which we don't want.

We want to display the table in the center means the whole table with its content in the center of the web page. So, to achieve this using CSS, we can use the margin property.

Margin property in CSS can be used to set the left margin, right margin, top margin, and bottom margin.

So, for now, we want the table to be placed in the center, and for doing so, we will use margin-left and margin-right properties, and both of its values will be set to auto. If we set the margin-left and margin-right to auto, the browser on its own will adjust the table and margins and will show the table in the center.

Another way to simplify our work is to use only the margin property and set its value to auto, so now this will adjust all left, right, top, and bottom margins according to the browser and display the table in the center.

Not only this but also if we want to give some percentage width to our margin property that also can be done.

Let us now understand all this with the help of example so that it will get clearer.

Example: -

<html>    
<head>  
<style>    
table, th, td {    
border: 2px solid red;  
margin-left: auto;
margin-right: auto;
border-collapse: collapse;
width: 800px;
text-align: center;  
font-size: 24px;  
}    
</style>    
</head>  
<body>    
<table>    
<tr>  
<th>Employee First Name</th>  
<th>Employee SurName</th>  
<th>Employee Code</th>  
<th>Employee Designation</th>  
</tr>    
<tr>  
<td>Riya</td><td>Vartak</td><td>E02</td><td>Engineer</td>  
</tr>    
<tr>  
<td>Emlon</td><td>Richard</td><td>M78</td><td>Developer</td>  
</tr>    
<tr>  
<td>Sanju</td><td>Jones</td><td>P96</td><td>Tester</td>  
</tr> 
<tr>  
<td>Myra</td><td>Vadnere</td><td>R16</td><td>DBA</td>  
</tr> 
<tr>  
<td>Smith</td><td>Joy</td><td>Y53</td><td>QA Engineer</td>  
</tr>    
</table>    
</body>  
</html> 

Output: -

How to center a table in CSS?

So now let's understand the above example. Here in the above html code, we have created a table that stores employee details like employee first name, surname, employee code, and the employee's resignation. Now we want this table to be shown in the center of the browser, so for that purpose, we have to use margin-left and margin-right, and the value for them is auto so that the browser will be showing the table in the center also for text inside the table is appearing in the center for that purpose we have used text-align: center property. Here we also have the other properties on the table like border-collapse, which will merge or collapse the two borders into one single border if we had written border-collapse: separate then two borders would have appeared one whole outside and one for the content of each cell.

Also, we have used other properties like the table's width in pixel, font size, border size and color, and so on to make our table look more attractive.


Related Topics

How to create blinking text using CSS

The blinking effect is used in HTML and CSS to take the attraction towards a particular word or section. If you need such a blinking effect, we need to take...

2 minutes read.

Internal CSS

What is CSS? CSS stands for Cascading Style Sheet.CSS is used to apply styling the HTML elements.With CSS user can add or change color, font-size and font-family. It also helps user...

4 minutes read.

CSS Button Style

CSS button style: Using CSS properties, we can easily modify the default BUTTON and enhance its appearances using simple graphic commands. TYPES OF BUTTON STYLE: - Program to change button using CSS ...

35 minutes read.

CSS Justify Content

Justify Content: These properties are applied for the alignment of items of a flexible box container if any item does not cover every available space over the main-axis, i.e., horizontally....

4 minutes read.

What is Bootstrap CSS?

Bootstrap: - Bootstrap is a CSS framework that is free to use and is used for the front-end development of the website in a faster and easier way. Bootstrap was released as...

3 minutes read.

CSS Table

CSS table provides better look and feel. There are various properties of CSS table that are given below. border border-collapse padding width height text-align color background-color CSS Table Border You can set...

4 minutes read.

CSS Box-shadow

CSS Box-shadow This property is referred to include effects as shadow-like around an element’s frame. Syntax box-shadow: h-offset v-offset blur spread color |inset|inherit|none; Property values Following are the various property values which can be used...

3 minutes read.

CSS Color Keywords

Here, we will discuss the following keywords: transparent currentcolor Inherit Transparent keyword A color can generally be made transparent by using the transparent keyword. Often it is used to for all intents...

4 minutes read.

CSS Width

CSS Width The width property in CSS sets the content width of area of the element. These properties do not provide margin and border features. It is used to set the...

1 minute read.

CSS Line Height

CSS Line Height: This property in CSS can be used to describe the line box’s minimal height inside the element. It sets-up the distinction among two lines in our content. It...

3 minutes read.

CSS @keyframes rule

CSS @keyframes rule It describes the rule of animation that depicts the properties of CSS for the components at every state along with the timeline. We may make the properties of complex...

4 minutes read.

CSS Pagination

CSS Pagination The pagination in CSS is an advantageous approach for indexing of a website’s distinct pages over the homepage. When our site contains multiple pages, we must insert a few...

13 minutes read.

CSS Pseudo-elements

Pseudo-elements The pseudo-classes in CSS can be represented as any keyword which is connected to the selector that represents any selected element’s unique state. On the other hand, pseudo-elements can be...

6 minutes read.

Create a 3D text effect using HTML and CSS

A3D text effect is a beautiful and attractive part of a webpage. As a web page developer, it is crucial to know how to create a 3D text effect with...

3 minutes read.

How To Use CSS

You can use CSS(Cascading Style Sheet) in four ways. These are given below: The given above CSS type, in which various technique will be used. These are different from each other and...

2 minutes read.

CSS Pseudo-class

Pseudo-class The pseudo-class may be depicted as the keyword which can be mixed to any selector that specifies the elected element’s unique state. It can be included to a selector to...

6 minutes read.

Untitled Reusable Block

  ...

1 minute read.

What is CSS used for

What is CSS used for? CSS stands for Cascading Style Sheet. CSS is a stylesheet language for web pages. It specifies how a document created in a markup language looks and...

4 minutes read.

CSS Border

CSS Border: The border property of CSS allows us to define the style, color, width, and radius of the element’s border. It is applied to various elements to set-up the...

4 minutes read.

CSS Arrow

CSS Arrow The arrow of CSS is applied to insert an array with the tooltip. It can make a tooltip as any speech bubble. Also, this arrow can be illustrated in...

4 minutes read.