How to retrieve the opacity value within a mixin - css

So I have the following problem, I want to be able to select a row in a table, and when I do that row gets an active background color. So far no problems. However, if the user should specify the background color to be transparent or set the opacity in rgba()-string to 0 then I want to display the underlaying row-color.
Now if I use a mixing I could style my css in different ways. But how can I get the opacity from the rgba-string to determine whether the string should be transparent or not? Does regex work within a mixin?
I.e.: I would something like (I know, the regex part is js but somthing similar for css/mixin):
#mixin active-background-color($activeBackgroundColor){
#if ($activeBackgroundColor.replace(/^.*,(.+)\)/,'$1') == 0) {
background-color: none;
} #else {
background-color: $activeBackgroundColor
}
}
So the solution turned out to be the following (should anyone be interested):
#mixin active-background-color($color) {
$alphaVal: alpha($color);
#if ($alphaVal== 0){
background-color: inherit;
} #else {
background-color: $color;
}
}
Link to the solution I used: The Sass Way

Sounds like you should simply define the selection on the tr
and add the user picked color to the td:
tr { background-color: whitesmoke; } // default row color
tr:hover { background-color: #ccc; } // default row selected color
tr:hover td { background-color: tomato } // user defined selected color
// if the user selects a transparent selection color
// the default row selected color will show
table { width:100%; border-collapse: collapse;}
tr { cursor: pointer; }
td { font:1rem sans-serif; padding:.2rem; }
tr { background-color: whitesmoke; }
tr:hover { background-color: #ccc; }
tr:hover td { background-color: tomato }
tr:last-of-type:hover td { background-color: transparent }
<table>
<tr><td>One</td><td></td><td></td><td></td></tr>
<tr><td>Two</td><td></td><td></td><td></td></tr>
<tr><td>Three</td><td></td><td></td><td></td></tr>
</table>

Related

What is the proper way of nesting related CSS lines?

I am trying to nest these lines of code together, I have tried the following but it does not work, is there any other way to have both the button and a tag changed by hovering the button through a single piece of code ?
.about-right button:hover {
background: #91c8ff;
& .a {
color: #151515;
}
}
Here is the orginal code:
.about-right button:hover {
background: #91c8ff;
}
.about-right button:hover a {
color: #151515;
}

Exclude elements from group in SCSS

Imagine a set of rules like the ones shown below:
span, div { color: red; }
span { background: white; }
div { background: black; }
Is it possible to wrap them under 1 SCSS rule? Something in the form of:
span, div {
& { color: red; }
&:not(div) { background: white;}
&:not(span) { background: black; }
}
Unfortunately an approach like this could very easily get quite large. So I'm hoping for an SCSS implementation of the code shown at the top but without the use of :not(<every other selector>).
Preferably something looking like (invalid code):
span, div {
& { color: red; }
&(span) { background: white;}
&(span) { background: black; }
}
I don't think that it is possible to do what you want this way (but I may be wrong).
The code below achieve the result you are looking for but uses a map, a #mixin and #extend instead of a single selector. Maybe it's a bit too complex for want you want to achieve but I hope it can help:
#mixin setSelectors($elements) {
%commonProperties {
#content;
}
#each $selector, $properties in $elements {
#{$selector} {
#extend %commonProperties;
#each $property, $value in $properties {
#{$property}: #{$value};
}
}
}
}
#include setSelectors((
span: (background: white),
div: (background: black)
)) {
color: red; // Common properties
}
Will return:
div, span { color: red; }
span { background: white; }
div { background: black; }
The first argument is a map containing all your selectors and their specific properties. The #content of the #mixin contains shared properties.
If you need to add a selector that doesn't have any specific property, you can add it to the map with null as key. Such as:
#include setSelectors((
span: (background: white),
div: (background: black),
i: null
)) {
color: red;
}
However, this solution doesn't allow nested selectors so I believe that separating the selectors is the best way to go.

How to create a proper class structure?

I'm using LESS as css compiler.
Everything works fine, but now I need to create a specific class structure and I'm a bit stuck.
I'd like to have this structure:
.default .{color} {.icon-after/.icon-before} {.icon}
this is the code that I've done:
.default {
&.disabled {
background: lighten(#grayBackground, 5%);
color: lighten(#darkText, 35%);
cursor: default;
border: #grayBorder;
text-shadow: #grayTextShadow;
}
&.gray {
background: #grayBackground;
color: #darkText;
border: #grayBorder;
text-shadow: #grayTextShadow;
&:hover {
background: darken(#grayBackground, 5%);
}
}
&.green {
background: #greenBackground;
border: #greenBorder;
color: #lightText;
text-shadow: #greenTextShadow;
&:hover {
background: darken(#greenBackground, 10%);
}
}
&.yellow {
background: #yellowBackground;
border: #yellowBorder;
color: #lightText;
text-shadow: #yellowTextShadow;
&:hover {
background: darken(#yellowBackground, 10%);
}
}
&.blue {
background: #blueBackground;
border: #blueBorder;
color: #lightText;
text-shadow: #blueTextShadow;
&:hover {
background: darken(#blueBackground, 10%);
}
}
&.black {
background: #blackBackground;
border: #blackBorder;
color: #lightText;
text-shadow: #blackTextShadow;
&:hover {
background: darken(#blackBackground, 10%);
}
}
&.red {
background: #redBackground;
border: #redBorder;
color: #lightText;
text-shadow: #redTextShadow;
&:hover {
background: darken(#redBackground, 10%);
}
}
&.icon-before{
.IconDefaultStyleBefore
}
&.icon-after{
.IconDefaultStyleAfter()
}
}
obviously this doesn't work, as the result is something like this:
.default .{color / .icon-after / .icon-before}
Any suggestions on how can I obtain my structure?
Thanks a lot
EDIT
I'd like to add the classes to the buttons in this order:
.default( gives the default style )
{.colours} (so that the background, the border and all colour related properties are setted)
{.icon-after or .icon-before} so that I can choose if adding the icon before or after with the proper margin
{.icon-name} (for example a questionmark or a tick etc)
so, for example, adding this classes:
.default .blue .icon-before .tick
I will have:
default blue button with the tick icon before the text
Hope is now more clear than before.
The required structure can be achieved as shown in the below example. The code can be simplified a lot by using loops (guarded mixins).
Explanation:
#colors - An array list variable which has the list of colors required for the element.
#bckground - Another array list variable which holds the required background color for each color class declared in the #colors list.
e(extract(#colors, #index)) and extract(#bckground, #index) - Extract functions are used to fetch the color name and background color value corresponding to the index of each array iteration (similar to colors[i]). e() function is used to extract the color values without the quotes.
&.#{color} - Selector interpolation to form the selector value. & is the parent selector and #{color} is the name of the color from the #colors list variable.
length(#colors) - The no. of color items present in the #colors array list variable. This is passed to the loop function to tell the Less Compiler as to how many times the loop should be executed.
#colors: "red","green","black","blue","gray";
#bckground: #AAA, #0F0, #00F, #000, #F00;
.loop-colors(#index) when (#index > 0){ // loop to generate rules for each color
.loop-colors(#index - 1);// call for the next iteration
#color: e(extract(#colors, #index));
#bgColor: extract(#bckground, #index);
&.#{color}{
background: #bgColor; //set background
/* all other props */
&:hover {
background: darken(#bgColor, 5%);
}
&.icon-before{
.IconDefaultStyleBefore;
}
&.icon-after{
.IconDefaultStyleAfter();
}
}
}
.default{
.loop-colors(length(#colors));
}
Note: As seven-phases-max mentioned in his comment, we are essentially generating a selector structure like .default.red.icon-before. Such a selector would essentially mean the same element has all the three classes and so even if it is specified like .default.icon-before.red it wouldn't make any difference but I assume that you are trying to make a more readable structure (like a default red button with an icon-before).
.default{
[...]
&.gray, &.black, [...every color...] {
.icon-before{
[...]
}
}
}
EDIT: or if you need a different .icon-before for every color you have to insert it one by one:
.default{
[...]
&.gray{
[...]
.icon-before{
[...]
}
}
}

CSS - Two different hover colors on a table

I've got a table that I'm using for a check list. For a collection.
Basically what I'm looking for is a way to have the table hover color GREEN if it's an item I own, and red, if its an item I don't
I'm very new to CSS and I'd appreciate any help if possible.
Here is my code (Messy)
**http://jsfiddle.net/6TYBb/1/**
Simplest way: http://jsfiddle.net/9gmrG/
Sub Class out the td so you have owned and not owned. Then trigger them on hover.
tr:hover{
background-color: #ccc;
}
tr:hover td.owned{
background-color: green;
}
tr:hover td.notowned{
background-color: red;
}
You can use two CSS classes to style the rows.
tr.own:hover { background: green; }
tr.not:hover { background: red; }
http://jsfiddle.net/6TYBb/215/
How is this table generated? You could add different classes to the table rows to indicate which hover style you wish to use. Example:
http://jsfiddle.net/BkmaW/
tr:hover {
color: red;
}
tr.true:hover {
color: green
}
edit: removed "!important", had added it without reason.

How to override a tr:nth-child(2n)?

A site-wide style sheet has a directive of the form
#outerdiv tr:nth-child(2n) {
background-color: #cccccc;
}
which I would like to override for the tables contained inside div #innerdiv, which in turn is contained inside div #outerdiv.
The only thing I've found works is:
#innerdiv tr:nth-child(2n) {
background-color: #ffffff;
}
Which seems to me excessively specific, and probably very fragile.
Is there some other way to just rescind the site-wide directive? Alternatively, is there a way to specify a background color for all the table rows within #innerdiv.
FWIW, the following does not work:
#innerdiv tr {
background-color: #ffffff; !important;
}
try this
#innerdiv tr {
background-color: #ffffff !important;
}
Depending on how your tables are nested, you can use the following code should the first table is a direct child of #outerdiv:
table tr {
background-color: #fff
}
#outerdiv > table tr:nth-child(2n) {
background-color: #cccc;
}

Resources