Margin for table rows css

How to Add Space Between Rows in the Table

Today’s task is to create space between two rows in a table. The space between two rows in a can be added by using the CSS border-spacing and border-collapse properties. The border-spacing property is used to set the spaces between cells of a table , and the border-collapse property specifies whether the border of the table is collapsed or not. The border-spacing property can be used only when the border-collapse property is set to «separate».

Let’s see an example and show how to do that step by step.

Create HTML

body> div> h2>W3docsh2> h3>Row spacing in a tableh3> table> tr> th>Employee IDth> th>Nameth> th>Genderth> th>Ageth> tr> tr> td >td">10001td> td>Tomtd> td>Mtd> td>30td> tr> tr> td >td">10002td> td>Sallytd> td>Ftd> td>28td> tr> tr> td >td">10003td> td>Emmatd> td>Ftd> td>24td> tr> table> div> body>

Add CSS

  • Use the border-collapse property with its «separate» value for the table.
  • Use the border-spacing property to set the distance between the borders of neighbouring table cells.
  • For the first row, set the background color and the color of the text by using the background-color and color properties.
  • Set the width and padding of the rows.
  • Use the text-align property with the «center» value which aligns the text to the center.
  • You can create a border around the cells of the table by using the border property and use the border-width, border-style and border-color properties.
  • You can set the color of the element of the document by using the color property. Also, you can choose any color from our color picker.
table < border-collapse: separate; border-spacing: 0 15px; > th < background-color: #4287f5; color: white; > th, td < width: 150px; text-align: center; border: 1px solid black; padding: 5px; > h2 < color: #4287f5; >

Here is the result of our code.

Example of adding space between horizontal rows:

html> html> head> title>Title of the document title> style> table < border-collapse: separate; border-spacing: 0 15px; > th < background-color: #4287f5; color: white; > th, td < width: 150px; text-align: center; border: 1px solid black; padding: 5px; > h2 < color: #4287f5; > style> head> body> div> h2>W3docs h2> h3>Row spacing in a table h3> table> tr> th>Employee ID th> th>Name th> th>Gender th> th>Age th> tr> tr> td class="td">10001 td> td>Tom td> td>M td> td>30 td> tr> tr> td class="td">10002 td> td>Sally td> td>F td> td>28 td> tr> tr> td class="td">10003 td> td>Emma td> td>F td> td>24 td> tr> table> div> body> html>

Result

Row spacing in a table

Читайте также:  Python variable equals or
Employee ID Name Gender Age
10001 Tom M 30
10002 Sally F 28
10003 Emma F 24

Example of adding space between vertical columns:

html> html> head> title>Title of the document title> style> table < border-collapse: separate; border-spacing: 20px 0; > th < background-color: #4287f5; color: white; > th, td < width: 150px; text-align: center; border: 1px solid black; padding: 5px; > h2 < color: #4287f5; > style> head> body> div> h2>W3docs h2> h3>Row spacing in a table h3> table> tr> th>Employee ID th> th>Name th> th>Gender th> th>Age th> tr> tr> td class="td">10001 td> td>Tom td> td>M td> td>30 td> tr> tr> td class="td">10002 td> td>Sally td> td>F td> td>28 td> tr> tr> td class="td">10003 td> td>Emma td> td>F td> td>24 td> tr> table> div> body> html>

In our first example, for the border-spacing property, we use a «0 15px» value which means that space is between the horizontal rows. In the second example, we use a «20px 0 » value which means that space is between the vertical rows.

A problem?!

Let’s give some background to our table to see what we’re talking about, so:

table < border-collapse: separate; border-spacing: 20px 0; background: khaki; /* add this line */ >

row-spacing-in-tables

What if we want inner borders to be removed between the columns in this example? Now we have it in outer space of Employee ID and Age columns.

Ok, let’s fix this together!

Remove the border-collapse: separate and border-spacing: 20px 0 from the table CSS.

Now, we will add the border-spacing: 20px 0 on each td of our table, instead of the whole table.

We should also add a display property of the block to have it work the way we want it to.

So, our changes would be like this:

table < background: khaki; > table tbody < display: block; border-spacing: 20px 0; >

The result will be the same as before. Now, its’ time for us to delete the left and right outer border space. It can be done quickly by just adding the negative margin to the left and right of each td element so that it will remove that space for us.

table < background: khaki; > table tbody < margin: 0 -20px; /* add this line, -20px margin to left and right, 20px is based on the border-spacing amount which is 20 px in this example */ display: block; border-spacing: 20px 0; >

And here we go! This is precisely what we wanted! As you see, the left and right outer space have gone for good!

Читайте также:  Java android import context

row-spacing-in-tables-2

Now you can remove the background color as well and have your beautiful table!

Hope you enjoyed it, have a good time!

Источник

How to add a margin to a table row in Css?

Margins in CSS can be added to various HTML elements to provide spacing between elements on a web page. However, adding margins to table rows (tr) can be a bit more challenging since table cells (td) have default padding. In this tutorial, we will explore different methods to add margins to table rows in CSS.

Method 1: Add Margins to td Elements

  1. First, you need to target the td elements within the tr element. You can do this using the tr td selector.
  1. Next, you can add margins to the td elements using the margin property. You can specify the margin value for each side (top, right, bottom, left) individually, or you can use the shorthand margin property to set them all at once.
tr td  margin: 10px; /* sets margin for all sides */ /* OR */ margin-top: 10px; margin-right: 20px; margin-bottom: 30px; margin-left: 40px; >
  1. You can also use negative margin values to create overlapping elements or adjust the spacing between elements.
tr td  margin-top: -10px; /* overlaps the element above */ margin-bottom: 20px; /* adds extra space below */ >
table> tr> td>Cell 1td> td>Cell 2td> tr> tr> td>Cell 3td> td>Cell 4td> tr> table>

This will add a margin of 10 pixels to all sides of each td element within the tr elements in the table.

Method 2: Wrap td Elements in div and Apply Margins

tr> td> div class="table-cell-wrapper">Contentdiv> td> td> div class="table-cell-wrapper">Contentdiv> td> tr>
.table-cell-wrapper  margin: 10px; >

This will apply a margin of 10px to all sides of the .

Here’s an example with different margin values:

table> tr> td> div class="table-cell-wrapper">Contentdiv> td> td> div class="table-cell-wrapper">Contentdiv> td> tr> tr> td> div class="table-cell-wrapper">Contentdiv> td> td> div class="table-cell-wrapper">Contentdiv> td> tr> table> style> .table-cell-wrapper  margin: 10px; > .table-cell-wrapper:nth-child(odd)  margin: 20px; > .table-cell-wrapper:nth-child(even)  margin: 30px; > style>

In this example, the first in each row will have a margin of 20px , the second will have a margin of 30px , and all other s will have a margin of 10px .

You can adjust the margin values and use different CSS selectors to target specific s within the table rows.

Method 3: Add Negative Margins to tr Element

tr  margin-top: -10px; margin-bottom: -10px; >
  1. You can adjust the values of the margin-top and margin-bottom properties to achieve the desired margin size.
  2. Here’s another example code snippet that demonstrates how to add a margin to a specific table row using the Add Negative Margins to tr Element method:
#my-table tr:nth-child(2)  margin-top: -20px; margin-bottom: -20px; >

In this example, the #my-table selector selects the table that contains the row, and the tr:nth-child(2) selector selects the second row in the table.

  1. You can also combine the Add Negative Margins to tr Element method with other CSS layout techniques, such as using padding on the table cells to create spacing between the cells and the row margins.
tr  margin-top: -10px; margin-bottom: -10px; > td  padding: 10px; >

In this example, the td element has a padding of 10px , which creates a space between the table cells and the row margins.

That’s it! You now know how to add a margin to a table row using the Add Negative Margins to tr Element method.

Источник

How to add space between CSS table rows?

I have a grid that is using CSS display: table; . What I would like to do is to have a gap between the rows. Is there a way I can do that when using display: table-row; . I already tried padding and margin but these don’t work for me as I want to set the margin background color.

3 Answers 3

For Individual Cells

You can achieve this with a transparent border :

border: 5px solid transparent; 

To do this for just the top and bottom of the cells, you can use:

border-bottom: 5px solid transparent; border-top: 5px solid transparent; 
 
Row 1 Cell 1
Row 1 Cell 2
Row 2 Cell 1
Row 2 Cell 2

For Every Cell

If you want to add this to every cell in your table, you can use the border-spacing CSS property on the element set to display: table :

 
Row 1 Cell 1
Row 1 Cell 2
Row 2 Cell 1
Row 2 Cell 2

@SamanthaJ I’ve added the background on the For Every Cell example. As the border is transparent, the For Individual Cells example will inherit the background of the cell itself, so unless you were to specify a non-transparent border colour it would make the entire surrounding area a solid colour.

Your solution looks good but what I was really wanting was spacing between rows only and not rows and columns. Do you have any idea if this is possible. Thanks.

@SamanthaJ in that case you need only specify border-top and/or border-bottom . The border property targets all four sides.

Thanks. Can you update your example and add in some background color to the table, cells and rows if that would help to make more clear. Glad to hear there’s a solution

Источник

Оцените статью