The Wayback Machine - https://web.archive.org/web/20240607145402/https://www.geeksforgeeks.org/how-to-reduce-space-between-two-columns-in-html-table/
Open In App

How to Reduce Space Between Two Columns in HTML Table ?

Last Updated : 30 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In HTML, we can create tables with multiple columns to display data in an organized manner. Sometimes, we may want to reduce the space between two adjacent columns in a table. We can achieve this by applying CSS properties to the table elements. In this article, we will see the different approaches to reducing the space between two columns in an HTML table.

Using cellpadding attribute: 

The cell padding attribute specifies the space between the content of a cell and its border. By setting a lower value for this attribute, we can reduce the space between two adjacent columns. However, this approach may not be very effective in some cases as it affects the padding of all cells in the table.

Syntax:

<table cellpadding="value">
<!-- table content -->
</table>

Example 1: This example uses the Cellpadding attribute to reduce space between two columns in html table.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Using cellpadding attribute</title>
</head>
 
<body>
    <h1 style="color:green;">GeeksforGeeks</h1>
    <table cellpadding="5">
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
        </tr>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
        </tr>
    </table>
</body>
 
</html>


Output:

Using cellspacing attribute:

The cellspacing attribute specifies the space between two adjacent cells in a table. By setting a lower value for this attribute, we can reduce the space between two adjacent columns. However, this approach may not be very effective in some cases as it affects the spacing of all cells in the table.

Syntax:

<table cellspacing="value">
<!-- table content -->
</table>

Example 2: This example uses the Cellspacing attribute to reduce space between two columns in html table.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Using cellspacing attribute</title>
</head>
 
<body>
    <h1 style="color:green">GeeksforGeeks</h1>
    <table border="1" cellspacing="10">
        <tr>
            <td>Cell 1</td>
            <td>Cell 2</td>
        </tr>
        <tr>
            <td>Cell 3</td>
            <td>Cell 4</td>
        </tr>
    </table>
</body>
 
</html>


Output:

Using CSS properties:

We can use CSS properties to set the padding and spacing for individual cells or columns in a table. By setting a lower value for the padding or spacing property, we can reduce the space between two adjacent columns.

Syntax:

<style>
table {
/* CSS properties for the table */
}
td {
/* CSS properties for the table cells */
}
</style>
<table>
<!-- table content -->
</table>

Example 3: This example uses the CSS properties to reduce space between two columns in html table.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>Using CSS properties</title>
    <style>
        table {
            border-spacing: 0;
        }
 
        td {
            padding: 5px;
        }
 
        td:first-child {
            padding-right: 10px;
        }
 
        td:last-child {
            padding-left: 10px;
        }
    </style>
</head>
 
<body>
    <h1 style="color:green">GeeksforGeeks</h1>
 
    <table>
        <tr>
            <td>Column 1</td>
            <td>Column 2</td>
        </tr>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
        </tr>
    </table>
</body>
 
</html>


Output:



Previous Article
Next Article

Similar Reads

Space between two rows in a table using CSS?
The space between two rows in a table can be done using CSS border-spacing and border-collapse property. The border-spacing property is used to set the spaces between cells of a table and border-collapse property is used to specify whether the border of table is collapse or not. The border-spacing attribute can only be used if border-collapse attri
1 min read
How to Add Space Around Table Border in HTML ?
Crafting visually appealing tables often involves adjusting the spacing between the content and the border. This guide dives into effective methods using HTML and CSS to achieve the desired spacing around your table's border, enhancing both readability and aesthetics. Below are the approaches to add space around the table border in HTML: Table of C
2 min read
Bootstrap 5 Columns Offsetting Columns
Bootstrap 5 Offsetting columns can offset grid columns in two ways, offset classes and margin utilities. Offset classes are used to match the size of columns and margin utilities are used for quick layouts. Bootstrap 5 Offsetting columns: Offset classes: This class is used to set the offset of the grid. Size is the among small (sm), medium (md), an
2 min read
What are the space-x and space-y Classes in Tailwind CSS ?
In Tailwind CSS, space-x and space-y classes are used to add spacing between child elements along the horizontal (x-axis) and vertical (y-axis) directions, respectively. These classes are beneficial in flex container scenarios for managing the spacing between flex items. Syntax:&lt;div class="flex space-x-4"&gt; &lt;!-- Child elements with horizont
2 min read
How to create table with 100% width, with vertical scroll inside table body in HTML ?
This article explains how to create a table with a 100% width using the `width` property. To add a vertical scroll bar inside the table body, use the `overflow-y` property. By changing the `display` property of `tbody` to `block`, we can display the block level element. However, when changing the `display` property of `tbody`, we must also change t
2 min read
Which table property specifies the width that should appear between table cells in CSS ?
In this article, we will see how to specify the width that should appear between table cells. CSS provides the border-spacing property that can be used to set the distance between the borders of neighboring cells in the Table. This property works only when the border-collapse property is set to no-collapse separate. Syntax: border-spacing: length|i
2 min read
Angular PrimeNG Table Dynamic Columns
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Table Dynamic Columns in Angular PrimeNG. Angular PrimeNG Table Dynamic Columns is used to set and render the col
5 min read
How to fix the width of columns in the table ?
We have already seen the article, How to fix the height of rows in the table. The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the &lt;td&gt; tag. If the width is not specified, the width of the column changes according to the change in the content. The specifications of width f
3 min read
Angular PrimeNG Table Styling Certain Rows and Columns
Angular PrimeNG is an open-source framework with a rich set of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. This article will show us how to use Style Certain Rows and Columns in Angular PrimeNG. The Table component shows some data to the user in tabular for
5 min read
How to Count Number of Rows and Columns in a Table Using jQuery?
Given an HTML document containing a table, the task is to count the number of rows and columns in that table using JQuery. There are a few methods available in jQuery to count the number of rows and columns in an HTML table. Table of Content By iterating through the rows and columnsBy using the length propertyBy iterating through the rows and colum
3 min read