Border style do not work with sticky position element
The problem occurs because of the use of border-collapse: collapse
. When browsers collapse the borders, the top and bottom border on the <th>
must be getting applied to surrounding elements—the top border to the <table>
and the bottom border to the following <tr>
.
If you use border-collapse: separate
and fashion your borders to sit on one side, the borders will truly attach to the <th>
, stay fixed as expected, and appear collapsed.
Here are example styles that can be applied to your HTML snippet.
#wrapper {
width: 400px;
height: 200px;
overflow: auto;
}
table {
width: 100%;
text-align: center;
border-collapse: separate; /* Don't collapse */
border-spacing: 0;
}
table th {
/* Apply both top and bottom borders to the <th> */
border-top: 2px solid;
border-bottom: 2px solid;
border-right: 2px solid;
}
table td {
/* For cells, apply the border to one of each side only (right but not left, bottom but not top) */
border-bottom: 2px solid;
border-right: 2px solid;
}
table th:first-child,
table td:first-child {
/* Apply a left border on the first <td> or <th> in a row */
border-left: 2px solid;
}
table thead th {
position: sticky;
top: 0;
background-color: #edecec;
}
Why border is not visible with position: sticky when background-color exists?
seems like to force a reflow will partially help :
table { border-collapse: collapse;}
thead { position: sticky; top: 0;}
th { border-right: 5px solid red; transform:scale(0.999);}
<table> <thead> <tr> <th>First</th> <th>Second</th> <th>Third</th> </tr> </thead> </table>
Table headers position:sticky and border issue
You can add
.table {
border-collapse: separate;
}
But unfortunately it looks bad, a better solution will be adding a workaround using a pseudo-element.
th:after,
th:before {
content: '';
position: absolute;
left: 0;
width: 100%;
}
th:before {
top: -1px;
border-top: 1px solid red;
}
th:after {
bottom: -1px;
border-bottom: 2px solid red;
}
.table-sticky-container {
height: 200px;
overflow-y: scroll;
border-top: 1px solid green;
border-bottom: 1px solid green;
}
.table-sticky th {
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 2;
}
th:after,
th:before {
content: '';
position: absolute;
left: 0;
width: 100%;
}
th:before {
top: -1px;
border-top: 1px solid red;
}
th:after {
bottom: -1px;
border-bottom: 2px solid red;
}
<link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css'>
<div class="table-sticky-container">
<table class="table table-sticky">
<thead class="thead-light">
<tr>
<th scope="col">Name</th>
<th scope="col">Title</th>
<th scope="col">ID</th>
<th scope="col">Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
<tr>
<td>Zoe Washburne</td>
<td>First Officer</td>
<td>8908980980</td>
<td>zwashburne</td>
</tr>
<tr>
<td>Kaylee Frye</td>
<td>Engineer</td>
<td>6678687678</td>
<td>kfrye</td>
</tr>
<tr>
<td>Malcolm Reynolds</td>
<td>Captain</td>
<td>9035749867</td>
<td>mreynolds</td>
</tr>
<tr>
<td>Zoe Washburne</td>
<td>First Officer</td>
<td>8908980980</td>
<td>zwashburne</td>
</tr>
<tr>
<td>Kaylee Frye</td>
<td>Engineer</td>
<td>6678687678</td>
<td>kfrye</td>
</tr>
</tbody>
</table>
</div>
Why does position=sticky not work for an element next to AgGrid?
You can try adding a z-index: 1;
to your style to make the element come to to the front, see the snippet below:
const eGridDiv = document.getElementById("myGrid");
new agGrid.Grid(eGridDiv, {});
div.sticky {
position: -webkit-sticky;
position: sticky;
top: 0;
z-index: 1;
background-color: #666;
padding: 40px;
font-size: 25px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Ag Grid App</title>
<!-- Include the JS for AG Grid -->
<script src="https://unpkg.com/ag-grid-community/dist/ag-grid-community.min.noStyle.js"></script>
<!-- Include the core CSS, this is needed by the grid -->
<link
rel="stylesheet"
href="https://unpkg.com/ag-grid-community/dist/styles/ag-grid.css"
/>
<!-- Include the theme CSS, only need to import the theme you are going to use -->
<link rel="stylesheet" href="https://unpkg.com/ag-grid-community/dist/styles/ag-theme alpine.css"
/>
</head>
<body>
<div class="sticky">
<button>Deselect Rows</button>
</div>
<div id="myGrid" class="ag-theme-alpine" style="height: 1500px"></div>
</body>
</html>
Related Topics
Why Media Queries Has Less Priority Than No Media Queries Css
What Is the Purpose of the "Role" Attribute in Html
Why Is My Grid Element'S Height Not Being Calculated Correctly
Word Wrap Options in a Select List
Allow 2 Decimal Places in ≪Input Type="Number"≫
Fixed Header, Footer With Scrollable Content
Unwanted Margin in Inline-Block List Items
Regular Expression For Extracting Tag Attributes
Position: Absolute and Parent Height
Pure CSS Multi-Level Drop-Down Menu
Why Anchor Tag Does Not Take Height and Width of Its Containing Element
How to Make ≪Div≫ Fill ≪Td≫ Height
Canvas Drawings, Like Lines, Are Blurry
How to Remove the Gutter (Spacing) Between Columns in Bootstrap
What Do ≪ and ≫ Stand For
How to Get the HTML Source of a Page from a HTML Link in Android
Best Way to Synchronize Local HTML5 Db (Websql Storage, SQLite) with a Server (2 Way Sync)