CSS box-shadow on table rows - tr
- doesn\'t seem to be working consistently across browsers. On some browsers the shadow is displayed; on others, there is no s
Reasons behind it seem down to default CSS - the display: block
was the biggest factor.
CSS / HTML / Demo
tr {
background-color: rgb(165, 182, 229);
display: block;
margin-bottom: 5px;
-moz-box-shadow: 0px 2px 2px black;
-webkit-box-shadow: 0px 2px 2px black;
box-shadow: 0px 2px 2px black;
}
td,th {
padding: 5px;
text-align: left;
}
<table>
<tr>
<td> </td>
<th>One</th>
<th>Two</th>
</tr>
<tr>
<th>Title</th>
<td>Three</td>
<td>Four</td>
</tr>
<tr>
<th>Title2</th>
<td>Five</td>
<td>Six</td>
</tr>
<tr>
<th>Title3</th>
<td>Seven</td>
<td>Eight</td>
</tr>
<tr>
<th>Title4</th>
<td>Nine</td>
<td>Ten</td>
</tr>
</table>
I've got an effect quite similar to box-shadow
using filter
and drop-shadow
. It's a bit hacky and you'll need to find the best configuration of the shadow to match your scenario though.
My original class:
.project-row {
box-shadow: 0 0 15px 0 black;
}
My new class:
.project-row {
filter: drop-shadow(0 0 9px black);
}
https://codepen.io/nico_nj/pen/XWbaZPJ
in react, i have combined the answer as below. It worked fine in chrome, >firefox, ie11
.select_row{
color: #43B149;
font-weight: bolder !important;
background: #e4e5e6 !important;
box-shadow: 1px 0px 1px 0px #cad6ce !important;
-moz-box-shadow:1px 0px 1px 0px #cad6ce !important;
-webkit-box-shadow:1px 0px 1px 0px #cad6ce !important;
transform: scale(1);
-webkit-transform: scale(1);
-moz-transform: scale(1);
-ms-transform: scale(1);
td{box-shadow: 0px 3px 0px 0px #cad6ce !important;
-moz-box-shadow:0px 3px 0px 0px #cad6ce !important;
-webkit-box-shadow:0px 3px 0px 0px #cad6ce !important;
background: #e4e5e6 !important;
}
}
.table-forecast{
border-collapse: separate;
border-spacing: 0px;
}
Please star this bug if you want to see it get fixed:
https://code.google.com/p/chromium/issues/detail?id=94871
If you want the table cell widths to continue to adjust themselves automatically, you can apply the shadow to the individual cells instead:
td:first-child {
box-shadow:
inset 0px 11px 8px -10px blue,
inset 0px -11px 8px -10px blue,
inset 11px 0px 8px -10px blue;
}
td {
box-shadow:
inset 0px 11px 8px -10px blue,
inset 0px -11px 8px -10px blue;
}
td:last-child {
box-shadow:
inset 0px 11px 8px -10px blue,
inset 0px -11px 8px -10px blue,
inset -11px 0px 8px -10px blue;
}
Full example here. (jsfiddle)
(Inspired by https://stackoverflow.com/a/10150898/724752)
In each box shadow value:
Now, in v53 Chrome it fixed and box-shadow work fine for <tr></tr>
!
CSS / HTML / Demo
table {
border-spacing: 0 10px;
border-collapse: separate;
}
tbody {
display: table-row-group;
vertical-align: middle;
}
tr {
margin-bottom: 9px;
}
tr:hover {
box-shadow: 0 5px 8px 0 rgba(50, 50, 50, 0.35);
-webkit-box-shadow: 0 5px 8px 0 rgba(50, 50, 50, 0.35);
-moz-box-shadow: 0 5px 8px 0 rgba(50, 50, 50, 0.35);
}
<table class="table">
<caption>Optional table caption.</caption>
<thead>
<tr>
<th>#</th>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td>Larry</td>
<td>the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
As previously mentioned, box-shadow
property works only with elements that have display: block
or display:inline-block
property.
If you'll add display: block
to the table cell as a general styling rule, it will collapse, since automatic width/height proportions that cells had with display:table
won't be applied anymore. To simulate that behavior just assign min-width
attribute to each th
and td
.
Then apply box-shadow
to the row (on hover or without).
In summary, your code should look like this:
table { box-sizing: border-box; }
td, th { padding-left: 16px; min-width: 170px; text-align: left; }
tr { display: block; }
tr:hover { box-shadow: 0px 2px 18px 0px rgba(0,0,0,0.5); cursor: pointer; }
I've omitted vendor prefixes for simplicity.
Here is the full example:
table {
box-sizing: border-box;
border-bottom: 1px solid #e8e8e8;
}
td,
th {
padding-left: 16px;
min-width: 170px;
border: 1px solid #e8e8e8;
border-bottom: none;
font: 14px/40px;
text-align: left;
}
td {
color: #666;
}
tr {
display: block;
}
th {
color: #333;
}
tr:hover {
background-color: #fbfbfb;
box-shadow: 0px 2px 18px 0px rgba(0, 0, 0, 0.5);
cursor: pointer;
}
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>Phone number</th>
<th>Date</th>
<th>Name</th>
<th>Label</th>
</tr>
</thead>
<tbody>
<tr>
<td>0342443</td>
<td>10 August 2013</td>
<td>Kate</td>
<td>Loves cats</td>
</td>
<tr>
<td>0342442</td>
<td>9 August 2013</td>
<td>Mary</td>
<td>Boring</td>
</tr>
<tr>
<td>0342441</td>
<td>8 August 2013</td>
<td>Anna</td>
<td>Loves extreme stuff</td>
</tr>
</tbody>
</table>
You can also check out the fiddle here.