css box shadow on thead that has sticky position - html

I'm styling a table with CSS, I want my th has box shadow when its position is sticky. I've tried but I want to get rid the shadow that's in the right (so I'm expecting the shadow only appear in the bottom of the th element)
this is my table CSS
table {
text-align: left;
position: relative;
width: 100%;
border-collapse: collapse;
border-spacing: 0;
}
th {
font-family: $font2;
font-size: 13px;
font-weight: 700;
padding: 12px 16px;
padding-right: 32px;
color: #555;
background: white;
position: -webkit-sticky;
position: sticky;
border-bottom: 5px solid #eee;
top: 0; /* Don't forget this, required for the stickiness */
box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.4);
}

You can do it with javascript, check the below snippet
const header = document.querySelectorAll('.fixed-top');
const rowObserver = new IntersectionObserver(entries => {
if (entries[0].isIntersecting) {
header.forEach(th => th.classList.add('noShadow'));
} else {
header.forEach(th => th.classList.remove('noShadow'));
}
});
rowObserver.observe(document.querySelector('.observed-row'));
div {
display: flex;
max-height: 20rem;
overflow: auto;
}
table {
border-spacing: 0;
}
th {
text-align: left;
}
th,
td {
border-bottom: 1px solid #c6c6c6;
height: 5rem;
white-space: nowrap;
padding-right: 2rem;
}
.fixed-top {
background-color: white;
position: sticky;
box-shadow: 0 10px 6px -5px rgba(0, 0, 0, 0.4);
top: 0;
z-index: 2;
}
.noShadow {
box-shadow: none !important;
}
<div>
<table>
<thead>
<tr>
<th class="fixed-top">Column 1</th>
<th class="fixed-top">Column 2</th>
<th class="fixed-top">Column 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Row One</td>
<td class="observed-row">1</td>
</tr>
<tr>
<td>2</td>
<td>Row Two</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>Row Three</td>
<td>3</td>
</tr>
<tr>
<td>4</td>
<td>Row Four</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>Row Five</td>
<td>5</td>
</tr>
<tr>
<td>6</td>
<td>Row Six</td>
<td>6</td>
</tr>
</tbody>
</table>
</div>

You can understand by the syntax below taken from MDN:
th{
/* offset-x | offset-y | blur-radius | spread-radius | color */
box-shadow: 0px 5px 2px -2px rgba(0, 0, 0, 0.2);
}
You can decrease the spread-radius until the shadow doesn't extend to lefft or right sides.. It will make your shadow a little smaller though
Edit: I think I understand your problem now.. A similar Problem
Summary: It's not possible to use box-shadow like you mention. Just use pseudo-elements to simulate a shadow to the bottom.
I assume you want shadow on each of the th boxes, so putting shadow on tr:has(th) is not possible

Related

How to remove space between column headers and their below row in a HTML table?

I styled a HTML table like this
I want to remove space between table column headers and their below row. I tried various tricks but nothing worked. Please note that I am also using Eric Meyer's Reset CSS v2.0.
I want to remove this red marked space without disturbing others.
My code:
#tbstud {
width:700px;
margin:50px auto;
border-collapse:separate;
border-spacing:2px;
}
.column_heading {
background-color:#d9e5f0;
-moz-border-radius:5px 5px 0 0;
-webkit-border-radius:5px 5px 0 0;
border-radius:5px 5px 0 0;
color:#000;
font-weight:bold;
height:20px;
line-height:20px;
padding:10px;
text-align:center;
}
.customer_row td {
padding:15px;
background-color:#f5f7f9;
}
.customer_row td:first-child {
border-left:3px solid #2585fe;
border-radius:5px 0 0 5px
}
.customer_row td:last-child {
border-radius:0 5px 5px 0
}
#tbstud .customer_row:nth-child(2) td:last-child {
border-radius:0 0 5px 0
}
<link rel="stylesheet" type="text/css" href="https://meyerweb.com/eric/tools/css/reset/reset.css" />
<table id="tbstud">
<tr>
<th>Sr. No.</th>
<th class="column_heading">Roll No.</th>
<th class="column_heading">Name</th>
<th class="column_heading">Class</th>
<th class="column_heading">Address</th>
</tr>
<tr class="customer_row">
<td>1</td>
<td>101</td>
<td>Sam</td>
<td>MSc</td>
<td>Delhi</td>
</tr>
<tr class="customer_row">
<td>2</td>
<td>102</td>
<td>Amit</td>
<td>BCA</td>
<td>Mumbai</td>
</tr>
<tr class="customer_row">
<td>3</td>
<td>103</td>
<td>Rahul</td>
<td>BCA</td>
<td>Delhi</td>
</tr>
<tr class="customer_row">
<td>4</td>
<td>104</td>
<td>Neha</td>
<td>BA</td>
<td>Pune</td>
</tr>
<tr class="customer_row">
<td>5</td>
<td>105</td>
<td>Pooja</td>
<td>B.Tech.</td>
<td>Chandigarh</td>
</tr>
</table>
I still need gap between each row and each cell. But just need to remove this gap between very first row and below its row.
Please remove border-spacing and use table border property. You cannot restrict border-spacing for tbody only instead what we can do is remove the border-spacing property and use border property for the table tr td
#tbstud {
width:700px;
margin:50px auto;
border-collapse:separate;
/* border-spacing:2px; */
}
/* Check Here*/
table tr td {
border: 3px solid white;
border-top: 0;
}
table tr th {
border: 2px solid white;
border-top: 0;
border-bottom: 0;
}
.column_heading {
background-color:#d9e5f0;
-moz-border-radius:5px 5px 0 0;
-webkit-border-radius:5px 5px 0 0;
border-radius:5px 5px 0 0;
color:#000;
font-weight:bold;
height:20px;
line-height:20px;
padding:10px;
text-align:center;
}
.customer_row td {
padding:15px;
background-color:#f5f7f9;
}
.customer_row td:first-child {
border-left:3px solid #2585fe;
border-radius:5px 0 0 5px
}
.customer_row td:last-child {
border-radius:0 5px 5px 0
}
#tbstud .customer_row:nth-child(2) td:last-child {
border-radius:0 0 5px 0
}
<link rel="stylesheet" type="text/css" href="https://meyerweb.com/eric/tools/css/reset/reset.css" />
<table id="tbstud">
<tr>
<th>Sr. No.</th>
<th class="column_heading">Roll No.</th>
<th class="column_heading">Name</th>
<th class="column_heading">Class</th>
<th class="column_heading">Address</th>
</tr>
<tr class="customer_row">
<td>1</td>
<td>101</td>
<td>Sam</td>
<td>MSc</td>
<td>Delhi</td>
</tr>
<tr class="customer_row">
<td>2</td>
<td>102</td>
<td>Amit</td>
<td>BCA</td>
<td>Mumbai</td>
</tr>
<tr class="customer_row">
<td>3</td>
<td>103</td>
<td>Rahul</td>
<td>BCA</td>
<td>Delhi</td>
</tr>
<tr class="customer_row">
<td>4</td>
<td>104</td>
<td>Neha</td>
<td>BA</td>
<td>Pune</td>
</tr>
<tr class="customer_row">
<td>5</td>
<td>105</td>
<td>Pooja</td>
<td>B.Tech.</td>
<td>Chandigarh</td>
</tr>
</table>
Maybe you can use this kind of hack:
<link rel="stylesheet" type="text/css" href="https://meyerweb.com/eric/tools/css/reset/reset.css" />
<table id="tbstud">
<thead>
<tr>
<th>Sr. No.</th>
<th>
<div class="column_heading">Roll No.</div>
</th>
<th>
<div class="column_heading">Name</div>
</th>
<th>
<div class="column_heading">Class</div>
</th>
<th>
<div class="column_heading">Address</div>
</th>
</tr>
</thead>
<tbody>
<tr class="customer_row">
<td>1</td>
<td>101</td>
<td>Sam</td>
<td>MSc</td>
<td>Delhi</td>
</tr>
<tr class="customer_row">
<td>2</td>
<td>102</td>
<td>Amit</td>
<td>BCA</td>
<td>Mumbai</td>
</tr>
<tr class="customer_row">
<td>3</td>
<td>103</td>
<td>Rahul</td>
<td>BCA</td>
<td>Delhi</td>
</tr>
<tr class="customer_row">
<td>4</td>
<td>104</td>
<td>Neha</td>
<td>BA</td>
<td>Pune</td>
</tr>
<tr class="customer_row">
<td>5</td>
<td>105</td>
<td>Pooja</td>
<td>B.Tech.</td>
<td>Chandigarh</td>
</tr>
</tbody>
</table>
#tbstud {
width: 700px;
margin: 50px auto;
border-collapse: separate;
border-spacing: 2px;
}
.column_heading {
background-color: #d9e5f0;
-moz-border-radius: 5px 5px 0 0;
-webkit-border-radius: 5px 5px 0 0;
border-radius: 5px 5px 0 0;
color: #000;
font-weight: bold;
height: 20px;
line-height: 20px;
padding: 10px;
text-align: center;
margin-bottom: -2px;
}
.customer_row td {
padding: 15px;
background-color: #f5f7f9;
}
.customer_row td:first-child {
border-left: 3px solid #2585fe;
border-radius: 5px 0 0 5px;
}
.customer_row td:last-child {
border-radius: 0 5px 5px 0;
}
#tbstud .customer_row:nth-child(2) td:last-child {
border-radius: 0 0 5px 0;
}
Working codepen: https://codepen.io/leo-melin/pen/rNaWxOq
So you just put the contents of tags inside divs that have margin-bottom: -2px css in addition to normal heading style.
Also added <thead> and <tbody> tags
removed border-spacing
no gap only after header
black color as example to be visible
#tbstud tr th{
border: 1px solid black;
border-bottom: 0px;
}
.customer_row td{
border: 1px solid black;
}
#tbstud tr:last-child td{
border-bottom: 2px solid black;
}
.customer_row:nth-child(2) td{
border-top: 0px;
}
If I understood what you mean then the solution would be to give the .column_heading a position: relative;, then add a .column_heading:after to add a line and the bottom of the <th> tag.
Try this
.column_heading:after {
content: '';
position: absolute;
left: 0;
bottom: -3px;
width: 100%;
height: 3px;
background-color: #d9e5f0;
}
Don't forget to give the .column_heading a position: relative;
Hope this help =]
Remove border-spacing property on #tbstud
and add this css
.column_heading{
border-left: 1px solid #fff;
}
.customer_row td {
border-left: 1px solid #fff;
border-bottom: 1px solid #fff;
}

How to add margin on table row

I want add space between row on table like image below:
If possible please show your code to me.
The border-spacing property will work for this particular case.
table {
border-collapse:separate;
border-spacing: 0 1em;
}
https://developer.mozilla.org/en-US/docs/Web/CSS/border-spacing
Or you can use the hacky approach. That give the appearance of margins between table rows i
tr{
border: 5px solid white;
}
You can use border-spacing. Here is an simple example.
table, th, td {
background: #ffffff;
padding: 5px;
}
table {
background: #999999;
border-spacing: 15px;
}
<h2>Border Spacing</h2>
<p>Border spacing specifies the space between the cells.</p>
<table style="width:100%">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr>
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
<p>Try to change the border-spacing to 5px.</p>
https://www.w3schools.com/html/html_tables.asp
https://www.w3schools.com/html/tryit.asp?filename=tryhtml_table_cellspacing
The border-spacing property sets the distance between the borders of adjacent cells.
Note: This property works only when border-collapse is separate.
table {
border-collapse: separate;
border-spacing: 15px;
}
You cannot give margin to the table row. you can either give border-colapse and border spacing to the table or give border to table row and change its color to table background color. Plz refer below link.
Thanks
http://jsfiddle.net/x1hphsvb/10966/
table{
border-collapse: separate;
border-spacing: 0 20px;
background-color: #e3e7ee
}
table tr td{
padding:20px !important;
background-color:white;
}
/* this is the second option */
tr{
/* border:2px solid #e3e7ee !important */
}
<table class="table ">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary#example.com</td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july#example.com</td>
</tr>
</tbody>
</table>
Try to use this for the design
or visit for more code demo
#import "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css";
.caseTableWrap {
margin-bottom: 50px;
background: #f5f5f5;
padding: 20px; }
.caseTable {
border-collapse: separate;
border-spacing: 0 20px; }
.caseTable tr {
-webkit-box-shadow: 0px 10px 10px rgba(0, 0, 0, 0.05);
box-shadow: 0px 10px 10px rgba(0, 0, 0, 0.05);
-webkit-transition: all 0.5s;
-o-transition: all 0.5s;
transition: all 0.5s; }
.caseTable tr + tr {
cursor: pointer; }
.caseTable tr + tr:hover {
-webkit-transform: translateY(-2px);
-ms-transform: translateY(-2px);
transform: translateY(-2px);
-webkit-box-shadow: 0px 10px 10px rgba(0, 0, 0, 0.2);
box-shadow: 0px 10px 10px rgba(0, 0, 0, 0.2); }
.caseTable tr .caseTableData:last-child, .caseTable tr th:last-child, .caseTable tr td:last-child {
max-width: 220px; }
.caseTable tr th {
border: none;
font-size: 18px;
font-weight: 500; }
.caseTable tr th:first-child {
border-radius: 8px 0 0 8px; }
.caseTable tr th:last-child {
border-radius: 0 8px 8px 0; }
.caseTable tr td {
border: none;
position: relative; }
.caseTable tr td:first-child {
border-radius: 8px 0 0 8px; }
.caseTable tr td:last-child {
border-radius: 0 8px 8px 0; }
.caseTable tr .caseTableData, .caseTable tr th, .caseTable tr td {
background: #fff;
padding: 20px;
position: relative; }
.caseTable tr .caseTableData p, .caseTable tr th p, .caseTable tr td p {
color: #484848;
font-size: 16px;
font-weight: 400; }
<div class="caseTableWrap">
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="caseTableInner">
<h3 class="secTitle">
<div class="text">
Recent Case
</div>
</h3>
<div class="caseTableWrap table-responsive">
<table class="table caseTable">
<tr>
<th>Case ID</th>
<th>Created Date</th>
<th>Expiry Date</th>
<th>Status</th>
</tr>
<tr>
<td>75814</td>
<td>01 January 2020</td>
<td>30 January 2020</td>
<td>Delivered</td>
</tr>
<tr>
<td>75814</td>
<td>01 January 2020</td>
<td>30 January 2020</td>
<td>Delivered</td>
</tr>
<tr>
<td>75814</td>
<td>01 January 2020</td>
<td>30 January 2020</td>
<td>Delivered</td>
</tr>
<tr>
<td>75814</td>
<td>01 January 2020</td>
<td>30 January 2020</td>
<td>Delivered</td>
</tr>
<tr>
<td>75814</td>
<td>01 January 2020</td>
<td>30 January 2020</td>
<td>Delivered</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
Best option:
*{box-sizing: border-box}
table {
border-collapse: separate;
}
tr > td {
display: inline-block;
margin-top: 1rem;
border-right: 1px solid black;
border-bottom: 1px solid black;
border-top: 1px solid black;
}
tr > td:first-of-type {
border-left: 1px solid black;
}
you can add top and bottom borders with the same color as the background
table{
width: 100%;
border-collapse: collapse;
}
tr{border-bottom: 4px solid white;border-radius: 10px;}
th{ background-color: white;}
td{ background-color: grey;}
<table>
<thead>
<tr>
<th>Name</th>
<th>id</th>
</tr>
</thead>
<tbody>
<tr>
<td>BDR</td>
<td>1</td>
</tr>
<tr>
<td>Bader</td>
<td>2</td>
</tr>
</tbody>
</table>

How to make a custom looking table with HTML and CSS?

I was wondering how could I create a custom looking Table like that?
body {
background-color: white;
}
table {
border: 1px solid black;
border-radius: 15px;
background: lightgrey;
color: black;
padding-left: 15px;
padding-right: 15px;
}
.t_head {
background-color: dodgerblue;
}
td {
padding-left: 10px;
padding-right: 10px;
}
#t_body {
background-color: grey;
}
<div id="test_div">
<table>
<thead>
<tr>
<div class="t_head">
<th>Test Header</th>
<th>Test Header</th>
<th>Test Header</th>
</div>
</tr>
</thead>
<tbody>
<div id="t_body">
<tr>
<th>Test data</th>
<th>Test data</th>
<th>Test data</th>
</tr>
<tr>
<th>Test data</th>
<th>Test data</th>
<th>Test data</th>
</tr>
</div>
</tbody>
</table>
</div>
I tried to add some styling for thead/tr element but the results are pretty much the same. Also, the borders in thead is something I can't add. I am very new to html and css and my searching attempts weren't very successful. I would appreciate any help!
Firstly, fix your html - remove the divs from the table as they are invalid, then you can put the blue colour on the cells in the header and the grey on the cells in the body.
I have put a whole border on the table and hidden the overflow for the rounded edges, then on the header I have just added a bottom and left border to fill in the lines - removing the left from the first cell (as that is on the outline of the whole table).
The border-spacing just removes any space between the cells so the borders are next to each other (like using border-collapse)
body {
background-color: white;
}
table {
overflow: hidden;
color: black;
border: 1px solid #000000;
border-radius: 15px;
border-spacing: 0;
}
thead th {
border-bottom: 1px solid black;
border-left: 1px solid black;
padding: 5px 10px;
background-color: dodgerblue;
border-collapse: collapse;
}
thead th:first-child {
border-left: none;
}
td {
padding: 5px 10px;
background: lightgrey;
}
<div id="test_div">
<table>
<thead>
<tr>
<th>Test Header</th>
<th>Test Header</th>
<th>Test Header</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test data</td>
<td>Test data</td>
<td>Test data</td>
</tr>
<tr>
<td>Test data</td>
<td>Test data</td>
<td>Test data</td>
</tr>
</tbody>
</table>
</div>
You could do this with divs and flex, it's easier
https://codepen.io/anon/pen/YJyBmy
.d-flex {
display: flex;
}
#table {
border: 4px solid black;
border-radius: 20px;
width: 500px;
}
#thead>div,
#tbody>div {
border-left: 4px solid black;
padding: 15px;
width: 33%;
}
#thead>div:first-child,
#tbody>div:first-child {
border: 0;
}
#thead {
background: dodgerblue;
border-radius: 15px 15px 0 0;
border-bottom: 4px solid black;
}
#tbody {
background: grey;
border-radius: 0 0 15px 15px;
}
<div id="table">
<div id="thead" class="d-flex">
<div>test</div>
<div>test</div>
<div>test</div>
</div>
<div id="tbody" class="d-flex">
<div>test</div>
</div>
</div>

properties of :hover table rows don't revert to their pre-:hover state

I'm trying to highlight rows of a table on hover with a 2px border. To prevent cells from shifting they're given a 4px padding if not selected and not hovered, 2px border and 2px padding if hovered, and a 4px border if selected.
The code can be found in this JS Fiddle or pasted below.
td {
padding: 6px 10px;
text-align: center;
border-bottom: 1px solid #E1E1E1;
font-size: 1.3rem; }
table {
border-collapse: collapse;
}
tr {
padding: 4px;
}
tr.selected {
border: 4px solid black; }
tr.selected td:first-child {
padding-left : 8px; }
tr.selected td:last-child {
padding-right : 8px; }
tr.notselected {
border: 0px solid black;
padding: 4px;}
tr.notselected:hover td:first-child {
padding-left : 9px; }
tr.notselected:hover td:last-child {
padding-right : 9px; }
tr.notselected:hover {
border: 2px solid black;
padding: 2px; }
<table>
<tr>
<td></td>
<td colspan="2">Header 1</td>
<td colspan="2">Header 2</td>
</tr>
<tr>
<td></td>
<td>Subheader 1.1</td>
<td>Subheader 1.2</td>
<td>Subheader 2.1</td>
<td>Subheader 2.2</td>
</tr>
<tr class="selected">
<td>Row 1</td>
<td >10.1</td>
<td >10.6</td>
<td >9.1</td>
<td >9.4</td>
</tr>
<tr class="notselected">
<td>Row 2</td>
<td>12.9</td>
<td>11.3</td>
<td>10.1</td>
<td>10.5</td>
</tr>
<tr class="notselected">
<td>Row 3</td>
<td></td>
<td></td>
<td>8.7</td>
<td>8.8</td>
</tr>
<tr class="notselected">
</table>
Instead of remaining static, the borders and contents of the rows below the hovered row shift down, remain shifted down after the row is un-hovered, but shift back up after a second hover on hover off.
I would use CSS outlines instead of the borders/padding combination you have to prevent the rows shifting like in this fork of your Fiddle.
outline: 2px solid black;

Add box-shadow to tbody in CSS?

I am trying to recreate this material design table I made in a Photoshop mockup:
I've got everything I want except for a drop shadow. When I apply a drop shadow to the tbody element, it shows in Firefox, but not Chrome or Edge.If I apply a "display: block" style to it as well, it will show. However, this removes the default "display: table-row-group" style, effectively ruining the structure of the table. How can I add a drop shadow without affecting the rest of the layout?
* {
font-family: Roboto, Arial;
}
body {
background: #f8f8f8;
}
table {
border-collapse: collapse;
width: 100%;
}
th {
font-weight: normal;
opacity: .7;
text-align: left;
padding-left: 20px;
padding-bottom: 10px;
}
td {
padding: 20px;
}
tbody tr:nth-child(even) {
background: rgba(128,128,128,.05);
}
tbody {
background: #fff;
/* This doesn't work unless "display: block" is applied, which ruins the structure */
box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12);
-moz-box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12);
-webkit-box-shadow: 0 2px 5px 0 rgba(0,0,0,0.16),0 2px 10px 0 rgba(0,0,0,0.12);
}
.grey {
opacity: .5;
}
.green {
color: #4caf50;
}
<table>
<thead>
<tr>
<th>Name</th>
<th>Date</th>
<th>Grade</th>
</tr>
</thead>
<tbody>
<tr>
<td>You</td>
<td>Today, 4:12 PM</td>
<td class="green">92%</td>
</tr>
<tr>
<td>John Smith</td>
<td>Today, 3:28 PM</td>
<td class="grey">Hidden</td>
</tr>
<tr>
<td>Bobby Newport</td>
<td>Yesterday</td>
<td class="grey">Hidden</td>
</tr>
<tr>
<td>Jim Halpert</td>
<td>2 days ago</td>
<td class="grey">Hidden</td>
</tr>
<tr>
<td>Detlow Coffin</td>
<td>2 days ago</td>
<td class="grey">Hidden</td>
</tr>
</tbody>
</table>
I spent a few hours on this issue and have finally a working solution that doesn't break the table layout. But now that I think about it flexbox would probably be much simpler...
table {
position: relative;
}
table:after {
content: '';
position: absolute;
box-shadow: 0 2px 4px 0 rgba(37, 37, 37, 0.2); // your box-shadow
left: 0;
right: 0;
top: 36px; // height of your table header
bottom: 0;
z-index: -1;
}
Here is the fiddle: https://jsfiddle.net/jitowix/o2gx7sc4/
Add a property of
display:table;
or
display:inherit;
to tbody and see what happens.
You can also try :before and :after.