I have a class .card which includes a lot of absolute positioned elements.
.card {
height: 400px;
width: 200px;
.name {
position:absolute;
left: 34px;
}
}
Now I want to create easily many different sizes for .card
Like
.card.xs {
.createSizedCard(0.5);
}
.createSizedCard(#factor){
height: 400px * factor;
width: 200px * #factor;
// etc
}
So. This was obviously just an example, my actual CSS includes way more classes. The question is: can I use something like #inherited in my .createSizedCard mixin instead of have to declare my values a second time?
I know I could use variables, but I would like to avoid that.
Or alternatively: can I use a concrete value from another class? Like
height: .card[height] * #factor;
No, you cannot inherit the property values from another class and use it. But you can set the default properties (and their calculation logic) of the .card class to a ruleset or mixin like in below snippets and call it. The same can be done using a wrapper mixin also.
Here is the ruleset version where the height, width and factor are set within the caller's scope before calling the ruleset.
#card: {
height: #height * #factor;
width: #width * #factor;
};
.card{
#factor: 1;
#height: 400px;
#width: 200px;
#card();
&.xs{
#factor: 0.5;
#card();
}
&.lg{
#factor: 2;
#card();
}
}
Below is a mixin version of how it can be done. It is very similar to the ruleset version except that it can take an input parameter directly.
.card(#factor) {
height: #height * #factor;
width: #width * #factor;
};
.card{
#height: 400px;
#width: 200px;
.card(1);
&.xs{
.card(0.5);
}
&.lg{
.card(2);
}
}
The above when compiled would result in the below CSS:
.card {
height: 400px;
width: 200px;
}
.card.xs {
height: 200px;
width: 100px;
}
.card.lg {
height: 800px;
width: 400px;
}
Alternately, if the factor is constant and all card elements would need rules for all sizes then you could create a mixin like the below:
.generate-card(#height; #width) {
height: #height;
width: #width;
&.xs{
height: #height * 0.5;
width: #width * 0.5;
}
&.lg{
height: #height * 2;
width: #width * 2;
}
}
.card {
.generate-card(400px; 200px);
}
.card-2 {
.generate-card(450px; 150px);
}
Related
Let me start by showing you how I would do this in SCSS:
$submenu-padding-left: 1.5em;
transform: translateX(calc(-#{$submenu-padding-left} + .5em));
which would compile to:
transform: translateX(calc(-1.5em - .5em))
Basically SCSS allows me to concatenate a minus symbol - with a variable in order to convert it to a negative value.
Is it possible to achieve this with CSS Variables?
Yes you can do it. Simply multiply by -1:
:root {
--margin: 50px;
}
body {
margin: 0 100px;
border:1px solid;
}
.box-1 {
background: red;
height: 100px;
width: 200px;
margin-left: calc(-1 * var(--margin));
}
.box-2 {
background: green;
height: 100px;
width: 200px;
margin-left: calc(-1 * (-1 * var(--margin))); /* You can also nest calculation */
}
<div class="box-1">
</div>
<div class="box-2">
</div>
If you need negative value multiple times then you can define a new variable:
:root {
--margin: 50px;
--margin--: calc(var(--margin) * -1);
/*
while you may simply write like below
but I love to use as above
coz, we'll only need to change value
in one place if needed
*/
/* --margin--: -50px; */
}
.positive-margin {
margin: var(--margin);
}
.negative-margin {
margin-left: var(--margin--);
}
You can use --margin- or --negative-margin instead of --margin--. But I preferred this because of readability.
Let me start by showing you how I would do this in SCSS:
$submenu-padding-left: 1.5em;
transform: translateX(calc(-#{$submenu-padding-left} + .5em));
which would compile to:
transform: translateX(calc(-1.5em - .5em))
Basically SCSS allows me to concatenate a minus symbol - with a variable in order to convert it to a negative value.
Is it possible to achieve this with CSS Variables?
Yes you can do it. Simply multiply by -1:
:root {
--margin: 50px;
}
body {
margin: 0 100px;
border:1px solid;
}
.box-1 {
background: red;
height: 100px;
width: 200px;
margin-left: calc(-1 * var(--margin));
}
.box-2 {
background: green;
height: 100px;
width: 200px;
margin-left: calc(-1 * (-1 * var(--margin))); /* You can also nest calculation */
}
<div class="box-1">
</div>
<div class="box-2">
</div>
If you need negative value multiple times then you can define a new variable:
:root {
--margin: 50px;
--margin--: calc(var(--margin) * -1);
/*
while you may simply write like below
but I love to use as above
coz, we'll only need to change value
in one place if needed
*/
/* --margin--: -50px; */
}
.positive-margin {
margin: var(--margin);
}
.negative-margin {
margin-left: var(--margin--);
}
You can use --margin- or --negative-margin instead of --margin--. But I preferred this because of readability.
I want to get css code like this
.img-wrapper {
width: 100px;
height: 100px;
overflow: hidden;
}
.img-wrapper image {
width: 100px;
}
I want to use mixin in scss like this
#mixin fixed-img($width, $height) {
width: $width;
height: $height;
overflow: hidden;
...
}
.img-wrapper {
#include fixed-img(100px , 100px);
}
Can I get the the above css output by using only one mixin
use the parent selector & inside the mixin and define the rule for the nested img element
#mixin fixed-img($width, $height) {
width: $width;
height: $height;
overflow: hidden;
& img {
width: $width;
}
}
.img-wrapper {
#include fixed-img(100px , 100px);
}
Note that instead of
& img {
width: $width;
}
You may avoid to use the SASS variable and use inherit keyword (or also 100%)
& img {
width: inherit;
}
I'm using the Gridster plugin, where columns width are defined like this:
[data-sizex="12"] { width: ... }
[data-sizex="11"] { width: ... }
[data-sizex="10"] { width: ... }
....
I have 2 questions about this;
What kind of CSS classes are these? I have never done/seen anything like this, especially in CSS.
If I want to select all the columns from 1-12, how do I use the code? Typically I'm using something like [class*=".."] this. I don't think so I can get a format like this for the above scenario.
1) These are CSS attribute selectors, to be specific these are Attribute presence and value selectors
2) If you want to select all the columns you don't have to use attribute selectors, just apply the CSS to the element. Well, for gridster plugin can replace .grid with .gs_w { }, .gs_w[data-sizex="12"]{ } and so on in the demo jsfiddle.
.grid{
/* Applies to all */
background: #808080;
color: #fff;
border: 1px solid #eee;
padding: 5px 10px;
}
.grid[data-sizex="12"]{
width: 720px;
}
.grid[data-sizex="11"]{
width: 710px;
}
.grid[data-sizex="10"]{
width: 700px;
}
.grid[data-sizex="9"]{
width: 690px;
}
.grid[data-sizex="8"]{
width: 680px;
}
.grid[data-sizex="7"]{
width: 670px;
}
.grid[data-sizex="6"]{
width: 660px;
}
.grid[data-sizex="5"]{
width: 650px;
}
.grid[data-sizex="4"]{
width: 640px;
}
.grid[data-sizex="3"]{
width: 630px;
}
.grid[data-sizex="2"]{
width: 620px;
}
.grid[data-sizex="1"]{
width: 610px;
}
Demo Here
I have .aClass
.aClass {
width: 20px;
height: 20px;
}
In .anotherClass I would like to calculate a width value based on the value of the width attribute in .aClass.
.anotherClass {
width: .aClass.width
}
The above example does not work.
I couldnĀ“t find anything in the less docs. Is there any way to do this?
Declare variable at the top of code
#width: 10px
Then,
.aClass {
width: #width;
height: 20px;
}
.anotherClass {
width: #width;
}