table row style overridden by td style css - css

EDIT3:
http://jsfiddle.net/ZgTHa/
Basically I have a table with an 1px solid grey border containing a radio button in each row. I apply an 1px red border on the row which is selected via the radio button. The color doesn't change. But if I set the red border to 2px, it changes.
I think this has to do with some priority issues, meaning that if both borders are 1px and both are solid, the td applies, if the td border is dotted, then the solid one applies for the chosen row. same situation if the selected row has a larger border width then the td.
I think this is just how it is in css (I might be wrong and missing something here) but I was wondering how one can work around this issue with relative ease (I could set a background image and put no borders and such, but that seems drastic)
edit (an example of what I'm trying to say):
http://www.w3schools.com/css/tryit.asp?filename=trycss_table_border-collapse
if you add a "red-border" class on one of the tr like so:
<tr class="red-border">
and specify the red-border class style like so:
.red-border {
1px solid red;
}
it doesn't apply. But if you add:
.red-border {
2px solid red;
}
it does apply. same goes if you set the td border to dotted:
table, td, th
{
border:1px dotted black;
}
and keep the red border as 1px solid red, it does apply.
ill just work around it with styling the tds with specific classes which get added on the click event. I'm just curious if this is how its intended to work or am I missing something?
edit2:
i have applied the styles like so:
.red-border {
background-color: #fbfafa !important;
color: #571c20;
.first {
border-left: 1px solid #571c20 !important;
border-top: 1px solid #571c20 !important;
border-bottom: 1px solid #571c20 !important;
}
.second {
border-top: 1px solid #571c20 !important;
border-bottom: 1px solid #571c20 !important;
}
.third {
border-top: 1px solid #571c20 !important;
border-bottom: 1px solid #571c20 !important;
}
.fourth {
border-top: 1px solid #571c20 !important;
border-bottom: 1px solid #571c20 !important;
border-right: 1px solid #571c20 !important;
}
}
it still doesn't apply sometimes. It applies well for the first row, on the second row the top border doesn't apply, same for the third row. On another table the right border doesn't apply.

Not all styles will work for tr elements, like border for instance
tr
You can however easily style the table element or the td elements.
If you want a border add it to the tds.
Example:
http://jsbin.com/avihuc/1/edit
td {
border:solid black 1px;
}
If you want higher "priority" (its actually called specifity)
use something like this:
table tr td {
}
wins over
tr td {}
As a rule of thumb for specifity,
ID selecotrs are worth 100 times more than elements selectors. Classes are worth 10 times more than element selectors.
!important is super specific and can be used as a last resort.

seems i have found the anwser:
.red-border {
border: 1px double red;
}
im guessing the double style resolves the conflict between the tr and td borders.

There is another way to accomplish what you want. Instead of setting style for tr you can set styles for tr's children elements — td's. Since you've got classes for the first td and the last one, you don't even have to use pseudo-classes for that. For example left border looks like that:
tr.red-border td.first {
border-left: 1px solid red;
}
Here is the complete example: http://jsfiddle.net/htn1cjoq/. I didn't change your html, only the css part. Hope it helps.

Related

How can I make this css border property one liner?

I have this working css but I like to make it into one line, is it possible?
border-radius: 0;
border-color: #ccc;
border-width: 0 0 2px 0;
border-style: none none solid none;
This short article covers the various bits of CSS shorthand you'll encounter in your day to day work.
https://www.w3.org/community/webed/wiki/CSS_shorthand_reference
Border
border allows you to set border width, style and, color.
UPDATE:
As #torazaburo pointed out it actually requires border: 0 none #ccc for it to be correct as well as adding border-radius: 0 as it's not part of the border shorthand.
#example {
border: 0 none #ccc;
border-bottom: 2px solid #ccc;
border-radius: 0;
}
If it's not an issue that the following could take non-bottom border width and style from other rules in the cascade then this should be fine:
#example {
border-bottom: 2px solid #ccc;
}
Produces the same CSS that you're wanting:
https://jsfiddle.net/betg5xue/5/
If you simply try to do
border-bottom: 2px solid #ccc;
it could possibly take non-bottom border width and style from other rules in the cascade. If that's not an issue, then the above would be fine.
The only reliable way that is identical to what you proposed involves three lines, there's no way around it:
#example {
border: 0 none #ccc;
border-bottom: 2px solid #ccc;
border-radius: 0;
}
Unfortunately, it is necessary to repeat the #ccc in the border-bottom property because if omitted it does not take the value from the cascade; instead it takes currentColor. It is necessary to specify border-radius as a separate property because it is not part of the border shorthand.

No left border to the right of certain class

Is there any way to specify with CSS to disable a left-border to the right of a cell with a certain class?
As you can see in this image the border to the right is double, it has the border of class "selected" plus the default gray border. I want the "selected" cell's right border to override the neighboring cell's left-border somehow.
FWIW, I think this is because of the table's CSS where I have commented 'border-collapse'. I need to do this otherwise I can't have rounded corner's on the table.
.ui-tabs-panel td { /* default table cell */
border-bottom: 1px solid #E9EBDF;
border-left: 1px solid #E9EBDF;
padding: 7px 7px;
font-family: Tahoma;
cursor: default;
background-color: #FAFDFF;
}
.ui-tabs-panel td.selected { /* when selected */
border-right: 1px solid #94DFEB !important;
border-left: 1px double #94DFEB !important;
background-color: #FFF !important;
color: #087DBD;
}
It seems, the current extra border belongs to the next <td> element, Try this to clear the border:
.ui-tabs-panel td.selected + td {
border-left: none;
}
You can select the next sibling element by Adjacent sibling combinator
element1 + element2 Selects every element2 element that are placed immediately after element1 element(s). They're siblings.

My tr border override the table border

I have the following CSS for my table :
table{
border: 1px solid black;
border-collapse: collapse;
}
tr{
border-bottom: 1px solid #a2a2a2;
}
I want my table to have a black border and inside, the line are supposed to be separated by a grey border. However the bottom border of the table is overridden by tr and is grey instead of black.
How can I manage to give the priority to the table border against the tr border?
Move your bottom border to the top for the tr, and set the first tr to have no border:
table{
border: 1px solid black;
border-collapse: collapse;
}
tr{
border-top: 1px solid #a2a2a2;
}
tr:first-child{
border:none;
}
jsFiddle to demonstrate it working (with slightly modified border colours and sizes to help them show up better in the demo)
Note that this solution involves slightly more CSS code than other valid solutions, but has the advantage of better browser compatibility. (:first-child is CSS2, whereas :last-child is CSS3)
[EDIT]
As per OP's comment below: To prevent the cell borders bleeding into the table border, we need to do the following:
Remove the border-collapse:collapse. This is what is making the borders combine together causing the bleeding effect.
Put the inner borders on the cells (td) rather than the rows (tr). This is necessary because without border-collapse, we can't have borders on the tr.
Add border-spacing:0 to the table. This closes up the gaps between the td elements, which would show up now because the border is on them rather than the tr.
Here's the finished code:
table{
border: 4px solid blue;
border-spacing:0;
}
td{
border-top: 4px solid red;
}
tr:first-child>td{
border:none;
}
And here's an updated version of the fiddle: http://jsfiddle.net/T5TGN/2/
use :not(:last-child)
table{
border: 1px solid black;
border-collapse: collapse;
}
tr:not(:last-child){
border-bottom: 1px solid #a2a2a2;
}
Please see this: http://jsfiddle.net/JSWorld/QmuA9/1/
Update the table css like the following
table, tr:last-child
{
border: 1px solid black;
border-collapse: collapse;
}
You can check the Demo
try adding padding-bottom to the tr:
tr{
border-bottom: 1px solid #a2a2a2;
padding-bottom: 1px;
}

Same style for two elements in table th and tr. How to avoid redundancy?

Right now I have the following:
table th.bordered {
border: 1px solid red;
}
table td.borderd {
border: 1px solid red;
}
How can I put this in a single rule??
Billy's is technically correct, but there's an even better way:
table .bordered {
border: 1px solid red;
}
Or even (if you want to use it on something other than tables):
.bordered {
border: 1px solid red;
}
Doing one of these two will help prevent being overly-specific, which will make maintaining it a lot easier.
Just want to add to Billy's answer that you could potentially shorten this to
table .bordered {
border: 1px solid red;
}
or even
.bordered {
border: 1px solid red;
}
if specificity is not an issue.
Performance would be slightly better as the browser doesn't have to check for parent elements after it has found an element with the class "bordered". But in most cases that would be negligible.
You might also want to be more semantic with your class name than just "bordered". Something like "highlightColumn" or "errorCell". Just in case you decide to use an orange background instead of a red border or want to use different borders for different purposes, for example.
Just combine them like so:
table th.bordered, table td.bordered {
border: 1px solid red;
}

CSS: grouping properties

.myclass {
border-top: solid 1px gray;
border-bottom: solid 1px gray;
background: #F2F2F2;
}
Is it possible to group properties that share a common definition, such as border-top and border-bottom in the example above.
Something like:
.myclass {
border-top , border-bottom: solid 1px gray; /* <-- grouped properties */
background: #F2F2F2;
}
TIA,
You can using LESS or SASS (I believe), but if you don't want to use those, you can instead group selectors that will have the same property:
.myclass,
.myOtherClass,
.myAnotherClass,
#anIdForGoodMeasure
{
border-top: solid 1px gray;
border-bottom: solid 1px gray;
background: #F2F2F2;
}
This will apply the style to all the elements.
Unfortunaly border doesnt have a shorthand version (Like say margin/padding for example), it has to be the same for all, or different.
However what you can do - is say you want to style one side uniquely, is specify all of the box, then underneath it, override it with an individual style. Heres a little fiddle of what I mean.
http://jsfiddle.net/XxWwn/
I think I see what you're trying to do here,
This is the only border shorthand I know, without using SASS/LESS.
.myclass {
border-color: red blue green coral;
border-width: 1px 2px 3px 4px;
border-style: solid;
}
This the same shorthand as margins and padding (TOP, RIGHT, BOTTOM, LEFT)

Resources