Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a clean way to get borders on a <tbody /> in pure CSS?

Tags:

css

I'd like to set a background and a rounded border on a <tbody/>, such as

tbody { border-radius: 15px; border: 1px solid black; background: #ccf; }

However, when I try this in Codepen, the border and background color display, but the <tbody/> still has square corners.

I'm able to work around this problem using a series of :last-child and :first-child selectors to apply the radius to individual tds on the corners, as for example

tbody tr:first-child td:first-child { border-top-left-radius: 15px; }

This version does what I want (at least, under firefox) but also feels extremely verbose and hacky, a problem that'll only get worse when I add the prefixed versions for compatibility (-moz-, -webkit- etc), and support for <th/> elements in addition to <td/>. Is there a succinct, pure-css way of getting this behavior?

like image 532
Dan Avatar asked Nov 03 '13 18:11

Dan


People also ask

What is border-style in CSS?

CSS Border Style The border-style property specifies what kind of border to display. The following values are allowed: dotted - Defines a dotted border

How do I put a border on the Tbody?

You can't put a border on the Tbody. You have to put the border-radius on the table element. This page should give you more information: codepen.io/anon/pen/xiaCc, found it on another question: stackoverflow.com/questions/19756736/…. It sets the borders of the bordering cells. (Not always desirable, but a work-around).

How many border-style values can the border-color property have?

The effect depends on the border-color value The border-style property can have from one to four values (for the top border, right border, bottom border, and the left border). A dotted border.

How do I change the color of a CSS border?

CSS Border Color. The border-color property is used to set the color of the four borders. The color can be set by: name - specify a color name, like "red". Hex - specify a hex value, like "#ff0000".


1 Answers

Assuming you have collapsed the borders in the table, simply set display:block on the tbody and apply the border-radius.

Codepen example

CSS

table {
    width: 100%;
    border-collapse: collapse;
    display: block;
    width: 600px;
}

tbody {
    background: #ccf;
    border: 1px solid black;
    border-radius: 15px;
    display: block;
}

th, td {
    width: 200px;
}

td, th {
    padding: 5px;
    text-align: center;
}
like image 175
Josh Crozier Avatar answered Sep 22 '22 11:09

Josh Crozier