How do I assign variables in one statement? - css

I am using CSS Less for my project, I have a problem to write the following CSS into Less.
Here color is same for .btn and .header but how can I simplify using Less?
variable
#whiteColor: #fff;
CSS
.btn{
border: none;
color: #ffffff;
}
.header{
color:#fff;
}
I have written in the following way. Is there any way to write it in only one statement?
.btn{
border: none;
color: #whiteColor;
}
.header{
background:#whiteColor;
}

Assuming that the property to which the value needs to be assigned is the same (color), you can do it using a single statement by grouping selectors. Below is a sample snippet:
#whiteColor: #fff;
.btn{
border: none;
}
.btn,
.header{ /* selector grouping */
color: #whiteColor;
}
Note that you don't even need Less for the above if the color is not going to change often. I would assume this is the case because of how closely the variable name is coupled to the color and in that case you can use pure CSS like in the below snippet.
.btn {
border: none;
}
.btn,
.header { /* selector grouping */
color: #fff;
}
/* Just for demo */
body {
background: black;
}
<div class='btn'>Some button</div>
<div class='header'>Some header</div>
If the properties are different (one has white as its color while the other has it as background), then it cannot be simplified into a single line even while using Less.
You can use a parametric mixin and property name interpolation like in the below snippets but these only complicate the situation than simplify it.
/* If color is same but property is different */
#whiteColor: #fff;
.apply-white-color(#property){
&{
#{property}: #whiteColor;
}
}
.btn{
border: none;
.apply-white-color(color); /* the property to which white color should applied is passed as parameter */
}
.header{
.apply-white-color(background);
}
/* If property is same but color is different */
#whiteColor: #fff;
#redColor: #f00;
.apply-color(#value){
&{
color: #value;
}
}
.btn{
border: none;
.apply-color(#whiteColor); /* the color which should applied is passed as parameter */
}
.header{
.apply-color(#redColor);
}

Related

How would I use Sass to give something a default but optionally overridable value?

I'd like to use Sass to create a reusable HTML snippet with default values, but then make those values optionally customizable. Let's say I'm making a button and I set up some base SCSS to make the button red by default like so:
%button-shared {
$button-color: red;
td {
border-color: $button-color;
color: $button-color;
background-color: white;
text-decoration: none;
width: 50px;
height: 15px;
border-radius: 6px;
}
}
I use a variable because I want it set in two different places. In this example, I want a white button with a colored outline that matches colored text like so:
Now let's say I give my two buttons in HTML different classes, button-one and button-two. Now I'd like to assign that SCSS to my two buttons, so I extend %button-shared to those two HTML classes. Except, I'd also like button-one to be a different color from the default red:
.button-one {
$button-color: green;
#extend %cta-shared;
}
.button-two {
#extend %cta-shared;
}
Unfortunately, this implementation ends up with them both being red. How can I setup my SCSS to allow for easy overriding of of a variable?
Use mix-ins: https://sass-lang.com/documentation/at-rules/mixin
#mixin button-shared($button-color: red) {
td {
background-color: white;
border-color: $button-color;
color: $button-color;
text-decoration: none;
width: 50px;
height: 15px;
border-radius: 6px;
}
}
.button-one {
#include button-shared(green);
}
.button-two {
#include button-shared();
}

why variables in css is not working although I had read a lot about it?

I have variables in my css but it isn't recognized.I have tried setting variable like the code below but when I run it in chrome it doesn't work.
li {
border: 2px solid red;
}
:special {
background: yellow;
--col: blue;
}
.completed {
text-decoration: line-through lime;
color: var(--col);
}
but when I set to blue directly it works like that
li {
border: 2px solid red;
}
:special {
background: yellow;
--col: blue;
}
.completed {
text-decoration: line-through lime;
color: blue;
}
I don't understand what is the problem.
:special isn't a recognised selector which is most likely causing your problem here. People tend to add global custom CSS property declarations to the :root {} selector. Or simply scope them to the parent selector that you're using.
:root {
--col: blue;
}
.class-name {
color: var(--col);
}
:special is an invalid selector. Rules in invalid selectors are ignored as per the specification, so your variable is never defined.

Property for specific selector

1. Summary
I have list of selectors, to which properties should always apply.
For some selectors must be additionally added another properties.
I can't find, how I can do it without duplicates.
2. MCVE
2.1. Expected CSS
.KiraFirst,
.KiraSecond,
.KiraThird {
color: red;
}
.KiraSecond {
background-color: yellow;
}
In example, I use class .KiraSecond 2 times. Can I get expected behavior without this duplicate?
2.2. Stylus
Live demo on stylus-lang.com
.KiraFirst
.KiraSecond
.KiraThird
color red
.KiraSecond
background-color yellow
This is compiled to expected CSS, but I still use .KiraSecond 2 times.
I don't understand, how I can not use duplicate. For example, syntax as this not compile to expected CSS:
.KiraFirst
.KiraSecond
background-color yellow
.KiraThird
color red
Result:
.KiraFirst,
.KiraSecond {
background-color: #ff0;
}
.KiraThird {
color: #f00;
}
3. Not helped
Stylus official documentation include Selectors section
Stack Overflow Stylus questions
Stylus GitHub issues
Maybe you can use basic class for all elements? For example - .Kira and if you need to specify something for other elements you can add extra class .KiraSecond or use .Kira:nth-child(2)
in your example it can be something like this
.Kira {
color: red;
}
.KiraSecond {
background-color: yellow;
}
or
.Kira {
color: red;
}
.Kira:nth-child(2) {
background-color: yellow;
}
You (can't)? and you shouldn't.
Duplicating that selector in that case is not a bad practice.
you are not duplicating the same property/value for many classes
you can clearly override specific property
you can clearly modify/change behaviour for specific class
e.g:
.class1, .class2, .class3 {
color: red;
background: yellow;
border: 1px solid;
}
.class1:hover {
color: blue;
}
.class2 {
border: 2px dotted;
}
.class3 {
color: pink;
}
What would be a bad practice in that case (with no selector duplication)
.class1 {
color: blue;
background: yellow;
border: 1px solid;
}
.class2 {
color: red;
background: yellow;
border: 2px dotted;
}
.class3 {
color: pink;
background: yellow;
border: 1px solid;
}

CSS Scopes Equation without Combine

I need to equation without combine multiple scopes, for example with native:
Native CSS:
.test
{
color: black;
}
.demo
{
color: black;
}
And combined version:
.test,
.demo
{
color: black;
}
But i need following:
#.test: .demo; // Something like that equation, all test classes equal demo class
.demo
{
color: black;
}
If it isnt possible with CSS, as a last resort i can use LESS
In LESS, You can use the extend function to achieve this.
.demo
{
color: black;
}
.test:extend(.demo){}; /* extends the properties of the .demo */
/* .test:extend(.demo all){}; /* the all keyword would extend nested sub classes of .demo also */
Demo
in addition to Harrys answer, you can do this with SASS like this:
.demo
{
color: black;
}
.test {
#extend .demo;
}
[ SAMPLE ]

CSS - Extending class properties

I'm pretty new to CSS and have been finding my way around so far.
I am creating these button like links with shadows and stuff. Now there are several such buttons required on the site - everything about the buttons is same - except few properties change like width and font size.
Now instead of copying the same code - over and over for each button - is there a way of extending the button and adding just the properties that change.
Example of two buttons - css
.ask-button-display {
background: #8BAF3B;
border-radius: 4px;
display: block;
letter-spacing: 0.5px;
position: relative;
border-color: #293829;
border-width: 2px 1px;
border-style: solid;
text-align:center;
color: #FFF;
width:350px;
font-size: 20px;
font-weight: bold;
padding:10px;
}
.ask-button-submit {
background: #8BAF3B;
border-radius: 4px;
display: block;
letter-spacing: 0.5px;
position: relative;
border-color: #293829;
border-width: 2px 1px;
border-style: solid;
text-align:center;
color: #FFF;
font-weight: bold;
width:75px;
font-size: 12px;
padding: 1px;
}
And this is how I'm currently using it in my html
Ask a Question Submit
So I'm wondering if there is a cleaner way to do this - like
.button {
/* PUT ALL THE COMMON PROPERTIES HERE */
}
AND THEN SOMEHOW EXTEND IT LIKE
.button #display {
/* THE DIFFERENT PROPERTIES OF Display BUTTON */
}
.button #ask {
/* THE DIFFERENT PROPERTIES OF Ask BUTTON */
}
But I'm not sure how to do this.
Thanks for your inputs
You can add multiple classes to one element, so have one .button class which covers everything, then a .button-submit class, which adds things in.
For example:
.button {
padding: 10px;
margin: 10px;
background-color: red;
}
.button-submit {
background-color: green;
}​
See a live jsFiddle here
In your case, the following should work:
.button {
background: #8BAF3B;
border-radius: 4px;
display: block;
letter-spacing: 0.5px;
position: relative;
border-color: #293829;
border-width: 2px 1px;
border-style: solid;
text-align:center;
color: #FFF;
width:350px;
font-size: 20px;
font-weight: bold;
padding:10px;
}
.button-submit {
width:75px;
font-size: 12px;
padding: 1px;
}​
See a live jsFiddle here
You might want to try this:
.button {
padding: 10px;
margin: 10px;
background-color: red;
}
.button.submit {
background-color: green;
}
.button.submit:hover {
background-color: #ffff00;
}
This way you avoid repeating the word and will able to use the classes in the elements like this:
Ask a Question
Submit
See the example in JSFiddle (http://goo.gl/6HwroM)
Rather than repeat the common "Add a button class to the element" answer I'm going to show you something new in the weird and whacky world of new age CSS, or better known as SCSS!
This reuse of code in stylesheets can be achieved with something called a 'Mixin'. What this allows us to do is reuse certain styles by using the '#include' attribute.
Let me give you an example.
#mixin button($button-color) {
background: #fff;
margin: 10px;
color: $color;
}
and then whenever we have a button we say
#unique-button {
#include button(#333);
...(additional styles)
}
Read more here: http://sass-lang.com/tutorial.html.
Spread the word!!!
You can do this since you can apply more than one class to an element. Create your master class and and other smaller classes and then just apply them as needed. For example:
Ask a Question Submit
This would apply the button and submit classes you would create while allowing you to also apply those classes separately.
Modify your code example along the lines of:
.master_button {
/* PUT ALL THE COMMON PROPERTIES HERE */
}
AND THEN SOMEHOW EXTEND IT LIKE
.button_display {
/* THE DIFFERENT PROPERTIES OF Display BUTTON */
}
.button_ask {
/* THE DIFFERENT PROPERTIES OF Ask BUTTON */
}
And apply like:
Ask a Question
Submit
.ask-button-display,
.ask-button-submit {
/* COMMON RULES */
}
.ask-button-display {
}
.ask-button-submit {
}
You may want to look into Sass. With Sass you can basically create variables in your css file and then re-use them over and over. http://sass-lang.com/
The following example was taken from Sass official website:
$blue: #3bbfce;
$margin: 16px;
.content-navigation {
border-color: $blue;
color:
darken($blue, 9%);
}
.border {
padding: $margin / 2;
margin: $margin / 2;
border-color: $blue;
}
Add a button class to both links for the common parts
.button {
background: #8BAF3B;
border-radius: 4px;
display: block;
letter-spacing: 0.5px;
position: relative;
border-color: #293829;
border-width: 2px 1px;
border-style: solid;
text-align:center;
color: #FFF;
}
Keep in your other classes the rules that aren't common.
And your HTML will be
Ask a Question
Submit
Without changing/ adding new classes you can add styles to all elements with a class name starting with "ask-button"... (whatever the elements with the class are; button, anchor, div etc.) let's say your buttons are divs then:
div[class^="ask-button"] {
// common css properties
}
You can also list all classes that will have common properties like this:
.ask-button-display,
.ask-button-submit {
// common css properties here
}
And then you add the separate styling for each button:
.ask-button-display{
// properties only for this button
}
.ask-button-submit {
// properties only for this button
}

Resources