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).
Related
The css for tr causes the elements inside to not be centered anymore (you can see in the example they're too high up). How should I fix this?
tr {
height: 50px}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<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>
I believe your issue is because your vertical alignment is being set from bootstrap.
If you were to add this, for example, to your CSS:
td, th{
vertical-align:middle !important;
}
You'll find that your text has been vertically centered within the 50px rows. The issue only becomes apparent when your rows have a large enough space within them to allow text the room to be biased towards one side or the other.
Note: the !important declaration is only to fix specificity issues. You can always add classes and/or IDs to allow more specific rules to be set, effectively overriding the default styling coming from bootstrap.
Setting
tr {
height: 50px
vertical-align:middle}
Won't work, simply because of specificity and the fact that the TD/TH elements require the property in this context.
Here's a demo:
http://codepen.io/anon/pen/QEQgdo
Here's a stackoverflow snippet:
tr {
height: 50px}
td, th{
vertical-align:middle !important;}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<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>
Just do:
td {
height: 100px;
vertical-align:middle !important;
}
As you can see:
td {
height: 100px;
vertical-align:middle !important;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<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>
Try using line-height it should center your text without the need of !importent.
tr > td {
height: 50px;
line-height: 50px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<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>
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).
I have a responsive table where the end column shows money figures. Currently it looks like this:
But I want it to do this:
There may not be any data so the £ sign will be replace with N/A which should also be pulled to the right of the column.
The data is fetched from MySQL but here is a simplified snippet of my code:
<table id="mytable" class="table table-bordred table-striped">
<thead>
<th>Reference</th>
<th>Client</th>
<th>Description</th>
<th>Money</th>
</thead>
<tbody>
<tr>
<td>#1234</td>
<td>Josh Blease</td>
<td>Needs a new filter fixing</td>
<td class='money'>
<div class='text-right'>Budget: £123,456</div>
<div class='text-right'>Value: £200,000</div>
<div class='text-right'>Spent: N/A</div>
</td>
</tr>
</tbody>
Divs are useful but Ii don't think that be the best solution for this case maybe you need to use better the table properties
http://www.usabilidad.tv/tutoriales_html/colspan_y_rowspan.asp
<table id="mytable" class="table table-bordred table-striped">
<thead>
<th>Reference</th>
<th>Client</th>
<th>Description</th>
<th colspan="2">Money</th>
</thead>
<tbody>
<tr>
<td rowspan="4">#1234</td>
<td rowspan="4">Josh Blease</td>
<td rowspan="4">Needs a new filter fixing</td>
</tr>
<tr>
<td>Budget</td>
<td>£123,456</td>
</tr>
<tr>
<td>Value</td>
<td>£200,000</td>
</tr>
<tr>
<td>Spent</td>
<td>N/A</td>
</tr>
</tbody>
</table>
<table id="mytable" class="table table-bordred table-striped">
<thead>
<th>Reference</th>
<th>Client</th>
<th>Description</th>
<th>Money</th>
</thead>
<tbody>
<tr>
<td>#1234</td>
<td>Josh Blease</td>
<td>Needs a new filter fixing</td>
<td class='money'>
<div class='text-right'><span>Budget:</span> £123,456</div>
<div class='text-right'><span>Value:</span> £200,000</div>
<div class='text-right'><span>Spent:</span> N/A</div>
</td>
</tr>
</tbody>
and
#mytable tbody tr td .text-right span{
min-width:200px;
max-width:300px;
display: inline-block;
}
Spans need to be set as inline-block otherwise the width properties won't take hold as they are by default inline only elements.
Setting the min and max width will force the span container to not go below or above a certain point, meaning that your other text will be forced to stay to the left of the (example) 200px mark even if your initial text (as in spent) only ends up being (again, example) 80px
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>
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 {
}