Referring to a CSS attribute inside the same class [duplicate] - html

This question already has answers here:
Sass - Manipulate inherited property?
(4 answers)
Closed 6 years ago.
I want to darken each button when you hover over them and I have no idea how to refer to the original color value in the :hover attribute.
//buttons.scss
//Define the colors
$tumblr-color: #35455C;
$twitter-color: #5AB6FC;
//Put these colors into classes
.btn-tumblr
{
color: $tumblr-color;
}
.btn-twitter
{
color: $twitter-color;
}
//Add the "darkens when hovered over" attribute
.btn-tumblr,
.btn-twitter
{
&:hover
{
color: darken(/*ATTRIBUTE OF THE ORIGINAL COLOR HERE*/, 20%);
}
}

There doesn't seem to be a way yet as of this writing, but another way to approach organizing your code to achieve the same end result (keeping things DRY) would be:
//buttons.scss
//Define the colors
$tumblr-color: #35455C;
$twitter-color: #5AB6FC;
$colors: $tumblr-color, $twitter-color;
//Mixins
#mixin thedarkening($color) {
color: darken($color, 20%);
}
#for $i from 1 through length($colors) {
.btn-#{$i} {
color: nth($colors, $i);
&:hover {
#include thedarkening(nth($colors, $i));
}
}
}
Demo
Caveat: requires you to change your classes to .btn-1, .btn-2, so on.

You can put the hover style inside each class, like this:
//Define the colors
$tumblr-color: #35455C;
$twitter-color: #5AB6FC;
.btn-tumblr {
color: $tumblr-color;
&:hover {
color: darken($tumblr-color, 20%);
}
}
.btn-twitter {
color: $twitter-color;
&:hover {
color: darken($twitter-color, 20%);
}
}

You can mimic the darken effect using opacity.
// Mimic the darken effect on hover
.btn-tumblr,
.btn-twitter {
opacity: .7;
&:hover {
opacity: 1;
}
}

Related

Generate a dynamic SCSS variable by appending another variable

I'm trying to assign different colors to similar items using an SCSS #for loop. Can I append the $i variable used in the #for loop to $color-?
<div>
<h1>Hello</h1>
<h1>World</h1>
<h1>Goodbye</h1>
</div>
$color-1: red;
$color-2: blue;
$color-3: yellow;
#for $i from 1 to 3 {
div>h1:nth-child(#{$i}) {
color: $color-{$i};
}
}
I don't know about dynamic variable names, but the standard way to achieve what you want is SCSS lists, over which you can iterate.
$colors-list: red blue yellow;
#each $current-color in $colors-list {
$i: index($colors-list, $current-color);
div>h1:nth-child(#{$i}) {
color: $current-color;
}
}
which compiles to
div > h1:nth-child(1) {
color: red;
}
div > h1:nth-child(2) {
color: blue;
}
div > h1:nth-child(3) {
color: yellow;
}

Using hex-code in scss class name to generate css color class of same hex-code

I am trying to define global colors, and I wrote a scss compiler to compile all color to its respective class names, But when I try to use them in my html div, the color is not applying to it.
scss snippet:
$blue-1: #001233;
$blue-2: #002132;
$blue-3: #004237;
$blue-4: #003027;
$blue-5: #CCCCCC;
$blue-6: #FFFFFF;
$allcolors:$blue-1 $blue-2 $blue-3 $blue-4 $blue-5 $blue-6;
#each $color in $allcolors {
.color-#{nth($color, 1)} {
color: nth($color, 1);
}
}
I am calling this in my html div as
<div class="color-#CCCCCC">TEST</div>
I don't see my style applied & when I tried compiling it, I can see my css style compiled as shown below
.color-#001233 {
color: #001233;
}
.color-#002132 {
color: #002132;
}
.color-#004237 {
color: #004237;
}
.color-#003027 {
color: #003027;
}
.color-#CCCCCC {
color: #CCCCCC;
}
.color-#FFFFFF {
color: #FFFFFF;
}
any help would be appreciated.
You need to escape the # in your CSS file since it's a special character used for ID selector.
.color-\#CCCCCC {
color: #CCCCCC;
}
<div class="color-#CCCCCC">TEST</div>
you can adjust your SCSS accordingly to add \
$blue-1: #001233;
$blue-2: #002132;
$blue-3: #004237;
$blue-4: #003027;
$blue-5: #CCCCCC;
$blue-6: #FFFFFF;
$allcolors:$blue-1 $blue-2 $blue-3 $blue-4 $blue-5 $blue-6;
#each $color in $allcolors {
.color-#{unquote("\\" + $color)} {
color: $color;
}
}

How to style paper-dropdown-menu (how to use mixins?)

I want to style paper-dropdown-menu, specifically for example the paper-input components of the element. I guess I need to learn/understand how mixins work.
I probably need to combine the mixins of paper-dropdown-menu with the mixins/custom properties of paper-input-container, am I right about this?
https://elements.polymer-project.org/elements/paper-dropdown-menu
https://elements.polymer-project.org/elements/paper-input?active=paper-input-container
I don't even know where to start. The doc suggets something in curly braces as a default in a mixin, so the first step is probably something like:
paper-dropdown-menu {
--paper-dropdown-menu: {
something here?
}
}
I barely understand this stuff myself, but I think after much struggle I might be able to shed enough insight to get you started.
You can define variables at the ":root" level that you want to use over and over again. (Yes you actually type ":root".) Consider:
<style is="custom-style">
:root{
--main-color: rgb(244,67,54);
--dark-color: rgba(0, 0, 0, 0.258824);
--light-color: rgb(153, 153, 153);
--app-header-background-front-layer-background-image: url(../../img/ConcertChoirSATour.jpg);
--app-header-background-front-layer-height: 400px;
}
</style>
Maybe you're using the Polymer appheader element, and you see in the docs that you can set the backgrounds with:
app-header {
--app-header-background-front-layer: {
/*something or other*/
};
--app-header-background-rear-layer: {
/*something or other*/
};
}
Here's where you use the variables you assigned in your :root level using the var() keyword:
app-header {
--app-header-background-front-layer: {
background-image: var(--app-header-background-front-layer-background-image);
};
--app-header-background-rear-layer: {
/* The header is blue when condensed */
background-color: var(--main-color);
};
}
Here's some sample code:
<style is="custom-style">
:root{
--main-color: rgb(244,67,54);
--dark-color: rgba(0, 0, 0, 0.258824);
--light-color: rgb(153, 153, 153);
--app-header-background-front-layer-background-image: url(../../img/ConcertChoirSATour.jpg);
--app-header-background-front-layer-height: 400px;
}
app-header {
--app-header-background-front-layer: {
background-image: var(--app-header-background-front-layer-background-image);
};
--app-header-background-rear-layer: {
/* The header is blue when condensed */
background-color: var(--main-color);
};
}
paper-icon-button {
--paper-icon-button-ink-color: white;
}
paper-dropdown-menu {
--paper-input-container-focus:{
color: var(--main-color);
};
--paper-input-container: {
color: var(--dark-color);
};
--paper-input-container-input: {
color: var(--light-color);
};
--paper-input-container-label: {
color: var(--main-color);
};
}
</style>
You can find some examples here:
https://www.polymer-project.org/1.0/docs/devguide/styling.html

Sass variables with same names but different outcomes based on class?

I've tried to wrap my brain round this, I assume that I need an if statement somewhere along the way.
But I'd like to be able to do this with sass. But this will just take the green colour and ignore the default colour regardless of the class.
SASS
// Default Colours --------------------------------------------------------------
$textColor: #FFF;
.green {
// Base Colours --------------------------------------------------------------
$textColor: green;
}
body {
text: $textColor
}
HTML
<p>jamie</P> //Output is #FFF
<p class="green">jamie</P> //Output is green
Here is a little mixin you could use.
$base-color: green;
#mixin change-var($var: $base_color, $selector: x, $property: color) {
#if $selector == x {
$var: blue;
} #else if $selector == y {
$var: green
} #else {
$var: $var;
}
#{$property}: $var;
}
usage:
.x {
#include change-var($base-color, x, color)
}
.y {
#include change-var($base-color, y, background-color)
}
output:
.x {
color: blue;
}
.y {
background-color: green;
}
Try this
$textColor: #fff;
body {
color: $textColor;
}
.green {
$textColor: green;
color: $textColor;
}

Can i nest the disabled attribute using LESS?

I have the following code to target buttons that are in a disable state:
.btn-default[disabled] {
background-color: lighten(#btn-default-bg, 30%)
}
Is it possible to use nested rules to target the disabled attribute? something similar to &:Hover
You can indeed, like this:
.btn-default {
&[disabled] {
background-color: lighten(#btn-default-bg, 30%)
}
}
http://jsbin.com/aKuLire/1/edit
You can even do further nesting:
input {
&[type="submit"] {
&[disabled] {
background: #blue;
}
}
}