CSS Specificity Order - css

I have two css classes in a codebase and I'm unsure why one is being chosen over the other, I thought the specifity of my second one was stronger. Is anyone able to explain?
The CSS class that it's choosing is -
td, th, table { border-collapse: collapse; border: 1px solid #999; }
And the CSS that I want it to use is -
table.cancellation { border: none; }
I thought as the second one had a class selector it would have a stronger specificity, why am I wrong?

You're overwriting the style for the table, but not the cells.
In the first table, everything has a red border.
In the second table, the table has a blue border, but since border-collapse:collapse is set, the red of the td & td appear above it.
In the third table, border-collapse is set to separate and you can see that the table does truly have a blue border.
In the last table, the styles for td and th are also overwritten - giving a borderless table.
th,
td,
table {
border-collapse: collapse;
border: 1px solid red;
margin-bottom:1rem;// just for looks
}
table.table {
border-color: blue
}
table.separate{
border-collapse:separate;
}
table.none,
table.none th,
table.none td{
border:none
}
<table>
<tr>
<th>
Head
</th>
<td>
cell
</td>
</table>
<table class = "table">
<tr>
<th>
Head
</th>
<td>
cell
</td>
</table>
<table class = "table separate">
<tr>
<th>
Head
</th>
<td>
cell
</td>
</table>
<table class = "table none">
<tr>
<th>
Head
</th>
<td>
cell
</td>
</table>

CSS reads from top to bottom
if the class
td, th, table { border-collapse: collapse; border: 1px solid #999; }
is written after
table.cancellation { border: none; }
Then it will take the properties of the one that comes at the very last in your code!
If this isn't the case then you can use "border: none !important" as it has the highest rank among everything else
In case both of these methods don't work then I suggest you show me the bigger picture as in type in your related HTML and complete CSS of the div

Related

Change color of bootstrap 4 beta table row border?

I'm trying to change the border-top color of Bootstrap table.
HTML
<table class="table">
<thead>
<tr>
<td>Parent</td>
</tr>
</thead>
<tbody>
<tr>
<td>Mama</td>
</tr>
</tbody>
</table>
CSS I've tried
table > tr{
border-top: black;
}
table > tr > td{
border: 1px solid red !important;
}
FIDDLE
https://jsfiddle.net/o9b17p2d/43/
As seen in the fiddle.
I'd like to change the color of the line between Parent & Mama.
You have placed a wrong code.Try this
.table td, .table th{
border-color: black;
}
Try this in your css.
thead{
border-bottom: 2px solid #6c5ce7;
}
https://jsfiddle.net/o9b17p2d/45/
I hope it will help
You have used a wrong selector in table > tr > td { and table > tr {
because thead is direct children for table and no tr.
so, change like this:
table > thead > tr > td {
border-bottom: 1px solid red !important;
}
table > thead > tr > td {
border-bottom: 1px solid red !important;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
<table class="table">
<thead>
<tr>
<td>Parent</td>
</tr>
</thead>
<tbody>
<tr>
<td>Mama</td>
</tr>
</tbody>
</table>
So that you dont effect all other tables in your page/site.
The easiest way to do this - is to give the element an id, and then target it in CSS using the id.
#no-top-border-td {
border-top: none
}
<td id="no-top-border-td">Mama</td>
or you could add that as a style to the actual element
<td style="border-top: none" />
both of these have a high priority when the CSS is applied.
You can also use border-bottom property for row in thead.Add this code in css
.table thead tr td{
border-bottom:1px solid red;
}

:nth-child definition & when it meets tr and caption

with the code below I found I'm confused with the definition of :nth-child
The :nth-child(n) selector matches every element that is the nth child, regardless of type, of its parent.
tr:nth-child(2n) {
background-color: gray;
}
table {
margin-left: 20px;
margin-right: 20px;
border-spacing: 0px;
border: thin solid black;
caption-side: bottom;
border-collapse: collapse;
}
td,
th {
border: thin dotted gray;
padding: 5px;
}
caption {
font-style: italic;
padding-top: 8px;
}
<table>
<caption>content</caption>
<tr>
<th>table head</th>
</tr>
<tr>
<td>111111</td>
</tr>
<tr>
<td>222222</td>
</tr>
<tr>
<td>333333</td>
</tr>
<tr>
<td>444444</td>
</tr>
<tr>
<td>555555</td>
</tr>
<tr>
<td>666666</td>
</tr>
</table>
and the 111111, 333333, 555555 become gray. nothing changed after I delete the caption tag, but 222222, 4444444, 666666 become gray after I removed the tr tag of table title. Isn't :nth-child suppose to count every element of its parent?
The problem here is that your HTML is invalid. tr elements must be wrapped within either a thead, tbody or tfoot element, and most browsers will automatically fix this for you by sticking them in a tbody.
Your HTML on these browsers will actually end up looking like this:
<table>
<caption>...</caption>
<tbody>
<tr>...</tr>
...
</tbody>
</table>
And thus, deleting the <caption> element will have no impact on the positioning of your tr elements.
If you inspect your <table> element, this is what you'll see:

tableCss style with Nested tables

I am trying to set the first column of the outer table (tbStudentPreference) with special styles...
But the problem is that it applies not only to the outer table column but also to the table inside the outer table.
I want to apply my style only to the outer container table. Please help.
<style>
#tbStudentPreference td:first-child {
font-weight: bold;
vertical-align: top;
width: 100px;
}
#tbStudentPreference {
vertical-align: top;
padding: 3px;
}
</style>
<table id='tbStudentPreference'>
<tr>
<td>xxxxx
</td>
<td>.....
</td>
</tr>
<tr>
<td>xxxxx
</td>
<td>.....
</td>
</tr>
<tr>
<td colspan='2'>
<table>
<tr>
<td>Inside Table
</td>
<td>.....
</td>
</tr>
<tr>
<td>Inside Table
</td>
<td>.....
</td>
</tr>
</table>
</td>
</tr>
</table>
I am trying to set the first column of the outer table..
You need to negate the inner table.
Also, the browser automatically adds a tbody for you, so it is not enough to use a child combinator on tr directly. You need to override that with a child-combinator on tbody. You then negate the inner table by using the presence of colspan attribute.
So, you select td which is a first-child among those which are not having a colspan attribute, direct descendant of tr which itself is a direct descendant of tbody which itself is direct descendant of your table. Like this:
#tbStudentPreference > tbody > tr > td:not([colspan]):first-child {...
The negation pseudo-class: https://developer.mozilla.org/en-US/docs/Web/CSS/:not
Snippet:
#tbStudentPreference { border: 1px solid gray; border-collapse: collapse; }
#tbStudentPreference td { border: 1px solid gray; }
#tbStudentPreference > tr > td:not([colspan]):first-child {
font-weight: bold; color: red;
}
#tbStudentPreference > tbody > tr > td:not([colspan]):first-child {
font-weight: bold; color: red;
}
<table id='tbStudentPreference'>
<tr><td>xxxxx</td><td>.....</td></tr>
<tr><td>xxxxx</td><td>.....</td></tr>
<tr><td colspan='2'>
<table>
<tr><td>Inside Table</td><td>.....</td></tr>
<tr><td>Inside Table</td><td>.....</td></tr>
</table>
</td></tr>
</table>

border-bottom not appearing for all TDs,only appearing for the TDs of last TR

I have got a table here that Im trying to apply some CSS rules to it.I have got a CSS rule in place to set the border bottom and border top to be in red and green respectively. For some reason border-bottom is not appearing for all TDs but only for the TDs of last TR. Any reason why.
table{
border-collapse:collapse;
}
table td{
border-top:solid 4px green;
border-bottom:solid 2px red;
}
<table width="500px">
<tr>
<td>Computers</td>
<td>£900</td>
</tr>
<tr>
<td>Laptops</td>
<td>£600</td>
</tr>
<tr>
<td>Keyboards</td>
<td>£20</td>
</tr>
<tr>
<td>Hard Drives</td>
<td>£80</td>
</tr>
</table>
The borders are over lapping, the green is on top of the red. To rectify this set border-collapse: separate; instead of border-collapse: collapse;

How to make width of a dynamic table not increase?

I have a table which is created dynamically. Sometimes it can have two columns and sometimes 20.
My problem is if it has loads the width increases of the table.
How to I make it fixed?
<div class="rightXMLSubCategory">
<table id = "XMLtable">
<tr>
#foreach(var item in Model.Part.attributes){
foreach(var attr in item.attr_type){
<th>
#attr.name
</th>
}
}
</tr>
<tr>
#foreach(var item in Model.Part.attributes){
foreach(var attr in item.attr_type){
<td>
#foreach(var attrs in attr.attr_value){
#attrs
<br/>
}
</td>
}
}
</tr>
</table>
CSS:
.rightXMLSubCategory
{
text-align:left;
width: 710px;
padding-left: 230px;
}
#XMLtable
{
border-radius:4px;
border: 1px solid #004389;
}
#XMLtable th
{
border-left: 1px solid #0066B3;
border-right: 1px solid #0066B3;
border-bottom: 1px solid #0066B3;
padding:3px 3px 3px 3px;
color: white;
background-color: #004389;
}
#XMLtable td
{
border-left: 1px solid #0066B3;
border-right: 1px solid #0066B3;
border-bottom: 1px solid #0066B3;
padding:3px 3px 3px 3px;
color: white;
background-color: #0066B3;
}
The table populated:
<table id = "XMLtable">
<tr>
<th>
Product Type
</th>
<th>
Actuator Style
</th>
<th>
Button Color
</th>
<th>
Termination Type
</th>
<th>
Panel Thickness
</th>
<th>
Circuit Breaker Style
</th>
<th>
Current Rating
</th>
<th>
Operating Voltage, Max.
</th>
<th>
Series
</th>
<th>
Brand
</th>
</tr>
<tr>
<td>Circuit Breaker</td>
<td>Push to Reset</td>
<td>Red</td>
<td>6.35 [.250] Straight Quick Connect Tab</td>
<td>0.81 - 1.57</td>
<td>Fuseholder Type</td>
<td>9</td>
<td>32 VDC250 VAC</td>
<td>W28</td>
<td>Potter & Brumfield</td>
</tr>
</table>
First of all, maybe you should overthink how you generate your table, since you put everything in one row, and then split the single "rows" with <br />... but that's up to you.
To specify the width in css you can, as the other posters said, use:
.rightXMLSubCategory table {
width: 200px;
table-layout:fixed;
word-wrap:break-word;
overflow:hiden; /* Fallback for IE/Mac */
}
Clearly you have to insert the right width for this to work.
Here you have a small working example:
http://jsfiddle.net/ramsesoriginal/Guj5y/2/
You could also work with min-widthand max-width, but sadly Internet Explorer doesn't support them well..
EDIT:
I edited the above example and the jsfiddle as I saw what you ment. If there are so many columns that the table won't fit inside the given width, it will expand, ignoring the width and even ignoring eventual overlow:hidden;.
The solution lies in the table-layout:fixed; property, which defines that the table should be exactly as wide as you have defined it. since doing so would mess up your text (It would overlap all the way), you can add a word-wrap:break-word; to make it break the words to multiple lines.
table-layout:fixed; is pretty well supported, except for IE/Mac (http://caniuse.com/#search=table-layout), word-wrap:break-word; is less supported (even though http://caniuse.com/#search=word-wrap shows otherwise, the break-word is a bit tricky..), but you can leave it there since it won't hurt you and makes your site future-proof.
To your #XMLTable settings in CSS, add:
width: 100%;
EDIT:
If that doesn't work, then probably the minimum width of the content of your table is wider than you want it to be. You can solve this by breaking up the longest-width items somehow -- for instance, you may replace non-breaking spaces by regular spaces so that the HTML engine can wrap the text in that column.
How about setting the CSS width-attribute for the table, or am I missing something in your question?
#XMLtable
{
border-radius:4px;
border: 1px solid #004389;
width: 400px;
}

Resources