Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent double border in tr > td element

Tags:

css

border

I would like to know if is possible to prevent double borders in a tr > td element. If I use border:1px solid #DDD then the first element will have all borders and then the second one too but because the first has a border-right and the second has a border-left then the borders are double and the same happens for the second tr where first has border-bottom and second has border-top. Any tips? I see this post but won't work for me because is for DIV and I'm using tables.

like image 508
ReynierPM Avatar asked Sep 14 '12 16:09

ReynierPM


People also ask

How do I get rid of the double border in a table?

You can remove the space between the different borders by using the CSS border-collapse property. You can apply this property against the HTML table element. When you apply this against the table element, you'll notice that the table border simply disappears, or "collapses".

How do you avoid double border in CSS?

Add a border-left and border-top to the parent. Add border-right and border-bottom to each of the children.

How do I get rid of the double border in HTML?

To avoid having double borders like in the example above, set the CSS border-collapse property to collapse .

Can TR element have border?

Not directly: adding a border to a tr isn't allowed. But you can target the first and last cell, give those a left/right border respectively. Then on each cell put a top border and a bottom border on each td element.


3 Answers

Start with:

border-collapse:collapse;

and then tune as necessary. Using the :first-child and :last-child pseudo selectors can be used to modify default styling on one end.

like image 180
Matt Whipple Avatar answered Oct 06 '22 19:10

Matt Whipple


Instead of putting borders on the cells, set a background color on the table itself to the color you want the borders to be, then space the cells by 1px:

HTML:

​<table>
    <tr>
        <td>One</td>
        <td>Two</td>
        <td>Three</td>
    </tr>
    <tr>
        <td>Four</td>
        <td>Five</td>
        <td>Six</td>
    </tr>
</table>

CSS:

table {
    background: #ccc;
    border-spacing: 1px;
}
td {
    background: #fff;
    padding: 5px;
}

That will give you this:

cellspacing example

Note that you have to set a background color on the cells themselves, too, otherwise the background color of the table will show through.

like image 31
daGUY Avatar answered Oct 06 '22 20:10

daGUY


You're looking for border-collapse

The border-collapse CSS property selects a table's border model. This has a big influence on the look and style of the table cells.

Values are as such.

border-collapse:  collapse | separate | inherit
like image 4
Ryan Kinal Avatar answered Oct 06 '22 20:10

Ryan Kinal