Hide table rows using pure CSS3 in Bootstrap datatable - html

I use Bootstrap with datatables and would like to hide specific table rows using pure CSS, without any JavaScript / jQuery.
I thought, I can do it like this, but it doesn't work, unfortunately:
input[name=hide_rows] + table tr.hide-this-row {
display: table-row;
}
input[name=hide_rows]:checked + table tr.hide-this-row {
display: none;
}
<div class="form-group">
<label for="hide_rows">Hide rows?</label>
<input type="checkbox" id="hide_rows" name="hide_rows" checked>
</div>
<div class="table-responsive">
<table class="table table-condensed table-hover" id="some-table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th>Head #1</th>
<th>Head #2</th>
<th>Head #3</th>
</tr>
</thead>
<tbody>
<tr class="show-this-row">
<td>Row #1</td>
<td>Row #1</td>
<td>Row #1</td>
</tr>
<tr class="hide-this-row">
<td>Row #2</td>
<td>Row #2</td>
<td>Row #2</td>
</tr>
<tr class="show-this-row">
<td>Row #3</td>
<td>Row #3</td>
<td>Row #3</td>
</tr>
#endforeach
</tbody>
</table>
</div>
Please note, that Bootstrap and the datatable is adding some divs there and at the end, it has some more HTML tags between.
That's the reason, why I don't would like to use this solution:
input[name=hide_rows] + div > div > div > table tr.hide-this-row {
display: table-row;
}
input[name=hide_rows]:checked + div > div > div > table tr.hide-this-row {
display: none;
}
<div class="table-responsive">
<label for="hide_rows">Hide rows?</label>
<input type="checkbox" id="hide_rows" name="hide_rows" checked>
<table class="table table-condensed table-hover" id="some-table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th>Head #1</th>
<th>Head #2</th>
<th>Head #3</th>
</tr>
</thead>
<tbody>
<tr class="show-this-row">
<td>Row #1</td>
<td>Row #1</td>
<td>Row #1</td>
</tr>
<tr class="hide-this-row">
<td>Row #2</td>
<td>Row #2</td>
<td>Row #2</td>
</tr>
<tr class="show-this-row">
<td>Row #3</td>
<td>Row #3</td>
<td>Row #3</td>
</tr>
#endforeach
</tbody>
</table>
</div>
I don't like this solution as you have to count and add that much divs as there are between and I also had to move the checkbox outside the <div class="form-group"> and inside the <div class="table-responsive">.
It would be much better, if there is an easier solution like the below code for the first shown HTML code:
input[name=hide_rows] + * table tr.hide-this-row {
display: table-row;
}
input[name=hide_rows]:checked + * table tr.hide-this-row {
display: none;
}
The last CSS code is not working, unfortunately.

You can achieve this with minor change in your html. If you move your table-responsive div inside the form-group below checkbox then you will be able to hide the rows.
Here is a code:
<div class="form-group">
<label for="hide_rows">Hide rows?</label>
<input type="checkbox" id="hide_rows" name="hide_rows" checked/>
<div class="table-responsive">
<table class="table table-condensed table-hover" id="some-table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th>Head #1</th>
<th>Head #2</th>
<th>Head #3</th>
</tr>
</thead>
<tbody>
<tr class="show-this-row">
<td>Row #1</td>
<td>Row #1</td>
<td>Row #1</td>
</tr>
<tr class="hide-this-row">
<td>Row #2</td>
<td>Row #2</td>
<td>Row #2</td>
</tr>
<tr class="show-this-row">
<td>Row #3</td>
<td>Row #3</td>
<td>Row #3</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
CSS:
input[name=hide_rows] + .table-responsive .hide-this-row {
display: table-row;
}
input[name=hide_rows]:checked + .table-responsive .hide-this-row {
display: none;
}
Snippet:
input[name=hide_rows] + .table-responsive .hide-this-row {
display: table-row;
}
input[name=hide_rows]:checked + .table-responsive .hide-this-row {
display: none;
}
<div class="form-group">
<label for="hide_rows">Hide rows?</label>
<input type="checkbox" id="hide_rows" name="hide_rows" checked/>
<div class="table-responsive">
<table class="table table-condensed table-hover" id="some-table" data-order='[[ 0, "desc" ]]'>
<thead>
<tr>
<th>Head #1</th>
<th>Head #2</th>
<th>Head #3</th>
</tr>
</thead>
<tbody>
<tr class="show-this-row">
<td>Row #1</td>
<td>Row #1</td>
<td>Row #1</td>
</tr>
<tr class="hide-this-row">
<td>Row #2</td>
<td>Row #2</td>
<td>Row #2</td>
</tr>
<tr class="show-this-row">
<td>Row #3</td>
<td>Row #3</td>
<td>Row #3</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
You can also check it here

Related

HTML tables in flex layout are getting overlapped after converting to PDF

Trying to convert html file to pdf using weasyprint, but due to bug in weasyprint, I can't use flex layout as it is overlapping the two tables in the first row. Is there any other workaround to get two tables in the first row side by side without disturbing other elements like <p> and two tables after that?
Instead of using flexboxes, need to use other alternative to achieve same align as mentioned in sample code!
<html>
<head>
<style>
/* Custom CSS */
table {
border-collapse: collapse;
width: 45%;
}
th,
td {
border: 1px solid black;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
/* td:first-child {
background-color: grey;
} */
.first-column {
background-color: rgb(187, 185, 185);
}
.mainWrapper {
display: flex;
flex-direction: column;
}
.rowTable {
display: flex;
flex-direction: row;
}
/* End Custom CSS */
</style>
</head>
<body>
<div class="mainWrapper">
<div class="rowTable">
<table style="margin-right: 20px;">
<tr>
<td class="first-column">table 111 Header 1</td>
<td>Header 2</td>
</tr>
<tr>
<td class="first-column">Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td class="first-column">Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
<table>
<tr>
<td class="first-column">table 222 Header 1</td>
<td>Header 2</td>
</tr>
<tr>
<td class="first-column">Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td class="first-column">Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
</div>
<div>
<p> some paragraph </p>
<div>
<div>
<table style="margin-bottom:20px;">
<tr>
<th>table 333 Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
<table>
<tr>
<th>table 444 Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
</div>
</div>
</body>
</html>
Actual output:( in pdf)
You could use display: inline-table; on the tables (without display:flex on their parent element). I can't check that in weasyprint, but you can try:
table {
border-collapse: collapse;
width: 45%;
}
.rowTable table {
display: inline-table;
}
:not(.rowTable) table {
margin: 0 auto;
}
th,
td {
border: 1px solid black;
padding: 8px;
}
th {
background-color: #f2f2f2;
}
.first-column {
background-color: rgb(187, 185, 185);
}
<div class="mainWrapper">
<div class="rowTable">
<table style="margin-right: 20px;">
<tr>
<td class="first-column">table 111 Header 1</td>
<td>Header 2</td>
</tr>
<tr>
<td class="first-column">Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td class="first-column">Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
<table>
<tr>
<td class="first-column">table 222 Header 1</td>
<td>Header 2</td>
</tr>
<tr>
<td class="first-column">Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td class="first-column">Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
</div>
<div>
<p> some paragraph </p>
<div>
<div>
<table style="margin-bottom:20px;">
<tr>
<th>table 333 Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
<table>
<tr>
<th>table 444 Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1, Cell 1</td>
<td>Row 1, Cell 2</td>
</tr>
<tr>
<td>Row 2, Cell 1</td>
<td>Row 2, Cell 2</td>
</tr>
</table>
</div>
</div>

How can I apply style to each last rows before specific class in a table?

I need to style the last row in each group (that is Row 3, Row 5 and Row 8). My css sample only apply style to Row 8.
Code:
tr.row:last-child td {
background-color: red;
}
<table>
<tr class="group">
<td>Group 1</td>
</tr>
<tr class="row">
<td>Row 1</td>
</tr>
<tr class="row">
<td>Row 2</td>
</tr>
<tr class="row">
<td>Row 3</td>
</tr>
<tr class="group">
<td>Group 2</td>
</tr>
<tr class="row">
<td>Row 4</td>
</tr>
<tr class="row">
<td>Row 5</td>
</tr>
<tr class="group">
<td>Group 3</td>
</tr>
<tr class="row">
<td>Row 6</td>
</tr>
<tr class="row">
<td>Row 7</td>
</tr>
<tr class="row">
<td>Row 8</td>
</tr>
</table>

How do I target a table that is buried under some divs?

I have a table that I'm trying to target the 5th row using CSS. It is buried under 4 divs.
What is the best way to target the table? It uses a class that is duplicated many times throughout the website on many other table but I only wish to target this table which I have added a class of .q4 to:
Basically I'm trying to change the bottom border of a row within the table which is nested in a div within a div, within a div, within a div. The full html is below:
<div class="mobile-league-table q4">
<div class="sportspress">
<div class"sp-template sp-template-league-table">
<h4 class="sp-table-caption">UAE Division 1</h4>
<div class="sp-table-wrapper">
<div class="sp-scrollable-table-wrapper">
<table class="sp-league-table sp-data-table sp-scrollable-table" data-sp-rows="10">
<thead>
<tr>
<th class="data-rank">Pos</th>
<th class="data-name">Team</th>
<th class="data-p">P</th>
<th class="data-w">W</th>
<th class="data-l">L</th>
<th class="data-d">D</th>
<th class="data-pf">PF</th>
<th class="data-pa">PA</th>
<th class="data-pd">PD</th>
<th class="data-bp">BP</th>
<th class="data-pts">Pts</th>
<th class="data-form">Form</th>
</tr>
</thead>
<tbody>
<tr class="odd sp-row-no-0">…</tr>
<tr class="even sp-row-no-1">…</tr>
<tr class="odd sp-row-no-2">…</tr>
<tr class="even sp-row-no-3">…</tr>
<tr class="odd sp-row-no-4">…</tr>
<tr class="even sp-row-no-5">…</tr>
<tr class="odd sp-row-no-6">…</tr>
<tr class="even sp-row-no-7">…</tr>
<tr class="odd sp-row-no-8">…</tr>
<tr class="even sp-row-no-9">…</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
Thanks
Since the table inside the q4 div is the only one we can target it quite simply
.q4 table tbody tr:nth-child(your number here) td {
border-bottom: 1px solid black;
}
td {
padding:.25em;
}
.q4 table tbody tr:nth-child(3) td {
border-bottom: 1px solid black;
}
<div class="mobile-league-table q4">
<div class="sportspress">
<div class"sp-template sp-template-league-table">
<h4 class="sp-table-caption">UAE Division 1</h4>
<div class="sp-table-wrapper">
<div class="sp-scrollable-table-wrapper">
<table
class="sp-league-table sp-data-table sp-scrollable-table"
data-sp-rows="10"
>
<thead>
<tr>
<th class="data-rank">Pos</th>
<th class="data-name">Team</th>
<th class="data-p">P</th>
<th class="data-w">W</th>
<th class="data-l">L</th>
<th class="data-d">D</th>
<th class="data-pf">PF</th>
<th class="data-pa">PA</th>
<th class="data-pd">PD</th>
<th class="data-bp">BP</th>
<th class="data-pts">Pts</th>
<th class="data-form">Form</th>
</tr>
</thead>
<tbody>
<tr>
<td>Item 1</td>
<td>Item 2</td>
<td>Item 3</td>
<td>Item 4</td>
<td>Item 5</td>
</tr>
<tr>
<td>Item 1</td>
<td>Item 2</td>
<td>Item 3</td>
<td>Item 4</td>
<td>Item 5</td>
</tr>
<tr>
<td>Item 1</td>
<td>Item 2</td>
<td>Item 3</td>
<td>Item 4</td>
<td>Item 5</td>
</tr>
<tr>
<td>Item 1</td>
<td>Item 2</td>
<td>Item 3</td>
<td>Item 4</td>
<td>Item 5</td>
</tr>
<tr>
<td>Item 1</td>
<td>Item 2</td>
<td>Item 3</td>
<td>Item 4</td>
<td>Item 5</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
Wordpress? Yikes.
Your best bet is to do some DOM manipulations after the HTML loads. I suggest using jquery. But if you can't import jquery into your project. You can still of course achieve this with javascript.
var x = document.getElementsByClassName('.sp-row-no-4');
x[0].innerHTML = "Hello World!";
getElementsByClassName() returns an array even if only one element is found via that class name. Hope that helps.

highlight a column on table element - CSS

I have a table constructed in the following format:
<thead>
<th></th>
<th></th>
<th></th>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
I'm trying to highlight a column if the header column is hovered. So let's say if I hovered the second header <th> from, say blue, then the second <td> from every <tr>, which makes it a column, will be highlighted in yellow. How can I achieve this? I've tried many different ways but its not highlighting the <tr>, only the header. I would like to keep it in a table structure. Can anybody help with this?
you can do something like this:
<table>
<thead>
<th>head 1</th>
<th>head 2</th>
<th>head 3</th>
</thead>
<tbody>
<tr>
<td>row 1 cell 1</td>
<td>row 1 cell 2</td>
<td>row 1 cell 3</td>
</tr>
<tr>
<td>row 2 cell 1</td>
<td>row 2 cell 2</td>
<td>row 2 cell 3</td>
</tr>
<tr>
<td>row 3 cell 1</td>
<td>row 3 cell 2</td>
<td>row 3 cell 3</td>
</tr>
</tbody>
</table>
and css
table {
overflow: hidden;
}
td, th {
position: relative;
}
th:hover {background-color:blue;}
th:hover::after {
content: "";
position: absolute;
background-color: grey;
left: 0;
top: -5000px;
height: 10000px;
width: 100%;
z-index: -1;
}

border-collapse using HTML attribute

Is there an HTML attribute available equivalent to the CSS property border-collapse: collapse;?
I am trying to achieve the same 1px border with HTML only, and not using css.
table{
border-collapse: collapse;
}
<table border="1px" cellpadding="3">
<tr>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
</table>
You could try cellspacing.
<table border="1px" cellspacing="0" cellpadding="3">
<tr>
<td>Row 1</td>
<td>Row 1</td>
<td>Row 1</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
</table>
Note: cellspacing doesn't overlap borders of adjacent cells, which CSS property does.
You can use cellspacing or cellpadding but it's deprecated in HTML5
i don't understand why you want to stop using CSS because that is what css do
you can use boostrap the css is included inside.
for more info go to w3chools
<div class="container">
<h2>Basic Table</h2>
<p>The .table class adds basic styling (light padding and only horizontal dividers) to a table:</p>
<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>
</div>