CSS - hide if has child with class - css

I'm trying to make a print stylesheet which hides (does not print) elements which do not have a child element with a set class.
I thought this might work but sadly it doesn't.
<style type="text/css" media="print">
table:not( > thead > tr > th > .Collapse) {
display:none;
}
</style>
I don't want to use any javascript if possible though.
Can this be done?
Here's the html for the element in question... the second table should be hidden when printed...
<table>
<thead>
<tr>
<th>
<span class="Collapse">Lorem ipsum...</span>
</th>
</tr>
</thead>
<tbody style="display: none; ">
<tr>
<td>Blah Blah...</td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th>
<span class="Expand">Lorem ipsum...</span>
</th>
</tr>
</thead>
<tbody style="display: none; ">
<tr>
<td>Blah Blah...</td>
</tr>
</tbody>
</table>

To target the span:
table > thead > tr > th > .Expand {
display:none;
}
Update
Targeting a css class and effecting it's parent is not currently possible. The easiest way to hide the table is to add a class to it, then you can target that class easily.
Example
<table>
<thead>
<tr>
<th>
<span class="Collapse">Lorem ipsum...</span>
</th>
</tr>
</thead>
<tbody style="display: none; ">
<tr>
<td>Blah Blah...</td>
</tr>
</tbody>
</table>
<table class="hide-for-print">
<thead>
<tr>
<th>
<span class="Expand">Lorem ipsum...</span>
</th>
</tr>
</thead>
<tbody style="display: none; ">
<tr>
<td>Blah Blah...</td>
</tr>
</tbody>
</table>

I would suggest instead, putting the classes on the tables, and then selecting the spans inside them.
<table class="Collapse">
<thead>
<tr>
<th>
<span>Lorem ipsum...</span>
</th>
</tr>
</thead>
<tbody style="display: none; ">
<tr>
<td>Blah Blah...</td>
</tr>
</tbody>
</table>
<table class="Expand">
<thead>
<tr>
<th>
<span>Lorem ipsum...</span>
</th>
</tr>
</thead>
<tbody style="display: none; ">
<tr>
<td>Blah Blah...</td>
</tr>
</tbody>
</table>
.Collapse {
display:none;
}
.Collapse thead tr th span {
}
.Expand thead tr th span {
}

Related

CSS to hide all theads other than first table [duplicate]

This question already has answers here:
What is the difference between :first-child and :first-of-type?
(4 answers)
Closed 2 months ago.
I'm trying to hide all <thead></thead> except first table by using the following CSS rule:
#media screen {
.container>table:not(:first-child)>thead {
display: none !important;
}
}
<div class="container">
<div>Table Caption</div>
<table>
<thead></thead>
<tbody></tbody>
</table>
<div>Table Caption</div>
<table>
<thead></thead>
<tbody></tbody>
</table>
<div>Table Caption</div>
<table>
<thead></thead>
<tbody></tbody>
</table>
</div>
But this rule hides all <thead></thead>. Any help?
You can use :not & :first-of-type Pseudo-classes on the table and the select it's child thead like below example. This will apply the CSS to all tables' thead except the first.
#media screen {
.container>:not(table:first-of-type)>thead {
display: none;
}
}
<div class="container">
<div>Table Caption</div>
<table>
<thead>
<tr>
<td>xyz-head</td>
</tr>
</thead>
<tbody>
<tr>
<td>xyz-body</td>
</tr>
</tbody>
</table>
<div>Table Caption</div>
<table>
<thead>
<tr>
<td>zxc-head</td>
</tr>
</thead>
<tbody>
<tr>
<td>zxc-body</td>
</tr>
</tbody>
</table>
<div>Table Caption</div>
<table>
<thead>
<tr>
<td>esd-head</td>
</tr>
</thead>
<tbody>
<tr>
<td>esd-body</td>
</tr>
</tbody>
</table>
</div>
The first table is not the first child in the container.
You need first-of-type
.container>table:not(:first-of-type)>thead {
display: none;
}
<div class="container">
<div>Table Caption</div>
<table>
<thead>
<tr>
<th>thead1</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div>Table Caption</div>
<table>
<thead>
<tr>
<th>thead2</th>
</tr>
</thead>
<tbody></tbody>
</table>
<div>Table Caption</div>
<table>
<thead>
<tr>
<th>thead3</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
Note - I have put in fuller mark up (you need tr and th elements in the thead to see the content selected correctly).

How to hide all child elements except first child element using CSS

I have a table which is looping dynamically. Now I need to hide all <tr> with the class .dynamic-tr except 1st table's <tr class="dynamic-tr"> by using CSS.
<table class="dynamic-table">
<tr class="dynamic-tr">
<th>Resource Name</th>
<th>Allocation</th>
</tr>
<tr>
<td>John</td>
<td>100%</td>
</tr>
</table>
<table class="dynamic-table">
<tr class="dynamic-tr">
<th>Resource Name</th>
<th>Allocation</th>
</tr>
<tr>
<td>Alex</td>
<td>50%</td>
</tr>
</table>
<table class="dynamic-table">
<tr class="dynamic-tr">
<th>Resource Name</th>
<th>Allocation</th>
</tr>
<tr>
<td>Bryce</td>
<td>100%</td>
</tr>
</table>
I have tried with this CSS but its not working. Can somebody please suggest.
.dynamic-table .dynamic-tr {
display: none;
}
.dynamic-table:first-of-type .dynamic-tr {
display: block;
}
Use :not to achieve what you need.
table.dynamic-table:not(:first-child) .dynamic-tr {
display:none;
}
<table class="dynamic-table">
<tr class="dynamic-tr">
<th>Resource Name</th>
<th>Allocation</th>
</tr>
<tr>
<td>John</td>
<td>100%</td>
</tr>
</table>
<table class="dynamic-table">
<tr class="dynamic-tr">
<th>Resource Name</th>
<th>Allocation</th>
</tr>
<tr>
<td>Alex</td>
<td>50%</td>
</tr>
</table>
<table class="dynamic-table">
<tr class="dynamic-tr">
<th>Resource Name</th>
<th>Allocation</th>
</tr>
<tr>
<td>Bryce</td>
<td>100%</td>
</tr>
</table>
Does this satisfy your requirements ?
body > table > tbody > tr.dynamic-tr { display: none; }
body > table:first-of-type > tbody > tr.dynamic-tr {display: inline;}

Align text in middle when there is a image in a row Bootstrap table

I have a bootstrap table, but I can't seem to get the text to align in the middle because the image expands the row.
I have tried adding vertical-align: middle to .table in my CSS, but it doesn't seem to do anything.
Example
https://jsfiddle.net/DTcHh/#&togetherjs=hy7S1lFDR3
<div class="container">
<h2>Hover Rows</h2>
<p>The .table-hover class enables a hover state on table rows:</p>
<table class="table table-hover">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Image</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>Doe</td>
<td>john#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin- server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
<tr>
<td>Mary</td>
<td>Moe</td>
<td>mary#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin- server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
<tr>
<td>July</td>
<td>Dooley</td>
<td>july#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin-server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
</tbody>
</table>
</div>
Answer
Twitter Bootstrap have a CSS class called text-center, you can simply use this one, as shown below.
Try this:
CSS
.mid-align:before {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
margin-right: -0.25em;
}
HTML
<div class="container">
<h2>Hover Rows</h2>
<p>The .table-hover class enables a hover state on table rows:</p>
<table class="table table-hover text-center">
<thead>
<tr>
<th class="text-center">Firstname</th>
<th class="text-center">Lastname</th>
<th class="text-center">Email</th>
<th class="text-center">Image</th>
</tr>
</thead>
<tbody>
<tr>
<td class="mid-align">John</td>
<td class="mid-align">Doe</td>
<td class="mid-align">john#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin-server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
<tr>
<td class="mid-align">Mary</td>
<td class="mid-align">Moe</td>
<td class="mid-align">mary#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin-server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
<tr>
<td class="mid-align">July</td>
<td class="mid-align">Dooley</td>
<td class="mid-align">july#example.com</td>
<td><img src="http://fullsoftwarepro.com/wp-content/uploads/2015/07/Radmin-server-and-viewer-3.5-Crack-License-key-Download.jpg" width=50></td>
</tr>
</tbody>
</table>
</div>
As for specificity, declaring a style attribute inside a tag overwrites the style written in your *yourStyle*.css file, so in most cases it is not the best practice to declare the style inside the HTML element, but it is surely acceptable (just added a small note).

How do I target a first-child that is visible (after children that are set to display:none), with only CSS

I need to target the first <td> element in a table row that is visible--meaning it does not have inline styling of display:none.
Here's the gotchyas:
They are not always hidden
Sometimes more than one is hidden.
I can't edit the HTML or use javascript/jQuery--This needs to be a pure CSS solution (hopefully)
Here is a quick sample of what the code looks like:
<table>
<thead>
<tr>
<th style="display:none">Header1</th>
<th>Header2</th>
<th>Header3</th>
<th>Header4</th>
</tr>
</thead>
<tbody>
<tr>
<td style="display:none">Body1</td>
<td>Body2</td>
<td>Body3</td>
<td>Body4</td>
</tr>
</tbody>
</table>
I tried messing with something like this to no avail:
table > tbody > tr > td:first-of-type:not([style*="display:none"])
Here's a fiddle to mess with
Thanks in advance.
If your hidden elements are always at the beginning of the row, you can use the direct sibling selector for this +.
td:first-child,
td[style*="none"] + td {
color: red;
}
<table>
<thead>
<tr>
<th style="display:none">Header1</th>
<th>Header2</th>
<th>Header3</th>
<th>Header4</th>
</tr>
</thead>
<tbody>
<tr>
<td style="display:none">Body1</td>
<td>Body2</td>
<td>Body3</td>
<td>Body4</td>
</tr>
<tr>
<td>First</td>
<td>Second</td>
<td>Third</td>
</tr>
<tr>
<td style="display:none">and</td>
<td>here's</td>
<td>an</td>
<td style="display:none">edge</td>
<td>case</td>
</tr>
</tbody>
</table>
This will style anything that isn't "display:none", but undo that when it gets to a subsequent "display:none".
table > tbody > tr > td:not([style*="display:none"]) {
color: red;
}
table > tbody > tr > td:not([style*="display:none"]) ~ td:not([style*="display:none"]) {
color: inherit;
}
<table>
<thead>
<tr>
<th style="display:none">Header1</th>
<th>Header2</th>
<th>Header3</th>
<th>Header4</th>
</tr>
</thead>
<tbody>
<tr>
<td style="display:none">Body1</td>
<td>Body2</td>
<td>Body3</td>
<td>Body4</td>
</tr>
<tr>
<td>First</td>
<td>Second</td>
<td>Third</td>
</tr>
</tbody>
</table>

Vertical align for tbody

I have a table separated by thead and tbody. The text I have in tbody is vertically centered. I want to have it displayed at the top of the tbody.
<thead>
<tr>
<td>.... </td>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5">
<h1>Hi</h1>
<p> Hello!
</p>
</td>
</tr>
</tbody>
I tried doing something like:
tbody { vertical-align:top;}
But that doesnt work
The problem is not with valign as it works correctly, the problem is the element h1 has a margin by default, you should set it to 0:
h1 {
margin: 0;
}
<table border="1">
<thead>
<tr>
<td>.... </td>
<td>.... </td>
</tr>
</thead>
<tbody valign="top">
<tr>
<td>
<h1>Hi</h1>
<p> Hello!</p>
</td>
<td>
A
</td>
</tr>
</tbody>
</table>

Resources