HTML : Apply cellpadding and cellspacing to table with and without using CSS

What is Cellpadding?

Cellpadding – Sets the amount of space between the contents of the cell and the cell wall

What is Cellspacing?

Cellspacing – Controls the space between table cells

Old school way of setting cellpadding and c…


This content originally appeared on DEV Community and was authored by Rajesh Kumar Yadav

What is Cellpadding?

Cellpadding - Sets the amount of space between the contents of the cell and the cell wall

What is Cellspacing?

Cellspacing - Controls the space between table cells

Old school way of setting cellpadding and cellspacing is like this:

<table cellspacing="1" cellpadding="1">

But there are various way to achieve the same.

Method 1

For controlling "cellpadding" in CSS, you can simply use padding on table cells. E.g. for 10px of "cellpadding":

td { 
    padding: 10px;
}

For "cellspacing", you can apply the border-spacing CSS property to your table. E.g. for 10px of "cellspacing":

table { 
    border-spacing: 10px;
    border-collapse: separate;
}

This property will even allow separate horizontal and vertical spacing, something you couldn't do with old-school "cellspacing".

Method 2

The default behavior of the browser is equivalent to:

table {border-collapse: collapse;}
td    {padding: 0px;}

Alt Text

Set Cellpadding

To set this we need set the padding to td

table {border-collapse: collapse;}
td    {padding: 6px;}

So that it will look like as -
Alt Text

Set Cellspacing

To set this we need to set border-spacing to table

table {border-spacing: 2px;}
td    {padding: 0px;}

So that it will look like as -
Alt Text

Setting Cellspacing and Cellspacing both

table {border-spacing: 8px 2px;}
td    {padding: 6px;}

So that it will look like as -
Alt Text

table {border-spacing: 2px;}
td    {padding: 6px;}

So that it will look like as -
Alt Text

Summary

table
{
    border-collapse: collapse; /* 'cellspacing' equivalent */
}

table td, table th
{
    padding: 0; /* 'cellpadding' equivalent */
}

Buy Me A Coffee

With all that being said, I highly recommend you keep learning!

Thank you for reading this article. Please feel free to connect with me on LinkedIn and Twitter.


This content originally appeared on DEV Community and was authored by Rajesh Kumar Yadav


Print Share Comment Cite Upload Translate Updates
APA

Rajesh Kumar Yadav | Sciencx (2021-05-29T10:26:14+00:00) HTML : Apply cellpadding and cellspacing to table with and without using CSS. Retrieved from https://www.scien.cx/2021/05/29/html-apply-cellpadding-and-cellspacing-to-table-with-and-without-using-css/

MLA
" » HTML : Apply cellpadding and cellspacing to table with and without using CSS." Rajesh Kumar Yadav | Sciencx - Saturday May 29, 2021, https://www.scien.cx/2021/05/29/html-apply-cellpadding-and-cellspacing-to-table-with-and-without-using-css/
HARVARD
Rajesh Kumar Yadav | Sciencx Saturday May 29, 2021 » HTML : Apply cellpadding and cellspacing to table with and without using CSS., viewed ,<https://www.scien.cx/2021/05/29/html-apply-cellpadding-and-cellspacing-to-table-with-and-without-using-css/>
VANCOUVER
Rajesh Kumar Yadav | Sciencx - » HTML : Apply cellpadding and cellspacing to table with and without using CSS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/29/html-apply-cellpadding-and-cellspacing-to-table-with-and-without-using-css/
CHICAGO
" » HTML : Apply cellpadding and cellspacing to table with and without using CSS." Rajesh Kumar Yadav | Sciencx - Accessed . https://www.scien.cx/2021/05/29/html-apply-cellpadding-and-cellspacing-to-table-with-and-without-using-css/
IEEE
" » HTML : Apply cellpadding and cellspacing to table with and without using CSS." Rajesh Kumar Yadav | Sciencx [Online]. Available: https://www.scien.cx/2021/05/29/html-apply-cellpadding-and-cellspacing-to-table-with-and-without-using-css/. [Accessed: ]
rf:citation
» HTML : Apply cellpadding and cellspacing to table with and without using CSS | Rajesh Kumar Yadav | Sciencx | https://www.scien.cx/2021/05/29/html-apply-cellpadding-and-cellspacing-to-table-with-and-without-using-css/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.