QUESTION: Is it possible to use a SCSS list to generate a number of classes without repeating the attributes?
The point is to keep the file easy maintainable by have ONE list ($svgs) of names which then can be used for classes or variables (see example below, $svg). This list is much larger in reality.
Note that I can't use .box i[class*=".icon-"] or similar selectors, as there are other elements that would be affected.
Here's the current SCSS, which works, but creates bloated CSS. .
$svgs: cancel, danger, exit;
#each $svg in $svgs {
.box i.icon- {
&#{$svg} {
background-image: url($svg + '.svg');
height: 24px;
width: 24px;
background-repeat: no-repeat;
background-size: contain;
}
}
}
Result CSS is:
.box i.icon-cancel {
background-image: url("cancel.svg");
height: 24px;
width: 24px;
background-repeat: no-repeat;
background-size: contain;
}
.box i.icon-danger {
background-image: url("danger.svg");
height: 24px;
width: 24px;
background-repeat: no-repeat;
background-size: contain;
}
.box i.icon-exit {
background-image: url("exit.svg");
height: 24px;
width: 24px;
background-repeat: no-repeat;
background-size: contain;
}
Desired CSS:
.box i.icon-cancel, .box i.icon-danger, .box i.icon-exit {
height: 24px;
width: 24px;
background-repeat: no-repeat;
background-size: contain;
}
.box i.icon-cancel {
background-image: url("cancel.svg");
}
.box i.icon-danger {
background-image: url("danger.svg");
}
.box i.icon-exit {
background-image: url("exit.svg");
}
To get this result you can use #extend with a placeholder selector:
$svgs: cancel, danger, exit;
%svg-styles {
height: 24px;
width: 24px;
background-repeat: no-repeat;
background-size: contain;
}
#each $svg in $svgs {
.box i.icon- {
&#{$svg} {
#extend %svg-styles;
background-image: url($svg + '.svg');
}
}
}
Related
I'm using Scss and came upon something that puzzles me.
Background properties don't carry over into the media queries so I need to rewrite it all and this just seems very repetitive and not DrY at all.
Maybe I'm missing something or overlooking something here
Let's say I have two divs styled the same but different background images.
<div class="section section--dog"><h3>Dog</h3></div>
<div class="section section--cat"><h3>Dog</h3></div>
Now I add my style via SCSS as follow
.section {
padding: 3rem 2rem;
background-position: top center;
background-size:cover;
background-repeat: no-repeat;
&--dog {
background: url(./images/dog.jpg)
}
&--cat {
background: url(./images/cat.jpg)
}
}
Renders CSS:
.section {
padding: 3rem 2rem;
background-position: top center;
background-size:cover;
background-repeat: no-repeat;
}
.section--dog {
background: url(./images/dog.jpg)
}
.section--cat {
background: url(./images/cat.jpg)
}
The result is that the background properties don't carry over even though it's applied to the same div.
Then I tried the extend the background properties in SCSS:
.section {
padding: 3rem 2rem;
background-position: top center;
background-size:cover;
background-repeat: no-repeat;
}
.section--dog {
background: url(../images/dog.jpg);
#extend .section;
}
.section--cat {
background: url(../images/cat.jpg);
#extend .section;
}
Rendered CSS:
.section, .section--dog, .section--cat {
padding: 3rem 2rem;
background-position: top center;
background-size:cover;
background-repeat: no-repeat;
}
.section--dog {
background: url(../images/dog.jpg)
}
.section--cat {
background: url(../images/cat.jpg)
}
The result is that the background properties still don't carry over even though it's applied to each of the specific divs.
I then created a mixin to add in which solved part of the problem, but there was still an issue - The SCSS:
#mixin bg-properties {
background-position: top center;
background-size: cover;
background-repeat: no-repeat;
}
.section {
padding: 3rem 2rem;
}
.section--dog {
background: url(../images/dog.jpg);
#include bg-properties;
}
.section--cat {
background: url(../images/cat.jpg);
#include bg-properties;
}
Rendered CSS:
.section {
padding: 3rem 2rem;
}
.section--dog {
background: url("../images/dog.jpg");
background-position: top center;
background-size: cover;
background-repeat: no-repeat;
}
.section--cat {
background: url("../images/cat.jpg");
background-position: top center;
background-size: cover;
background-repeat: no-repeat;
}
This works fine until I add in the Media Query to use a different size image. Then the properties don't carry over again.
SCSS:
#mixin bg-properties {
background-position: top center;
background-size: cover;
background-repeat: no-repeat;
}
.section {
padding: 3rem 2rem;
}
.section--dog {
background: url(../images/mobile/dog.jpg);
#include bg-properties;
#media screen and (min-width:768px) {
background: url(../images/desktop/dog.jpg);
}
}
.section--cat {
background: url(../images/mobile/cat.jpg);
#include bg-properties;
#media screen and (min-width:768px) {
background: url(../images/desktop/cat.jpg);
}
}
Rendered CSS:
.section {
padding: 3rem 2rem;
}
.section--photography {
background: url("../images/mobile/cat.jpg");
background-position: top center;
background-size: cover;
background-repeat: no-repeat;
}
#media screen and (min-width: 768px) {
.section--photography {
background: url("../images/desktop/cat.jpg");
}
}
.section--design {
background: url("../images/mobile/dog.jpg");
background-position: top center;
background-size: cover;
background-repeat: no-repeat;
}
#media screen and (min-width: 768px) {
.section--design {
background: url("../images/desktop/dog.jpg");
}
}
So the above works fine for mobile but once you hit the media query it discards the background properties again. the only resolution was to add the mixin inside the media queries as well. All this is fine but doesn't feel very DRY.
Feedback would be greatly appreciated
I'm just creating .scss file for styled my website. In this .scss will be using in 4 differences web page with the not same class name. Here is my sample.
.my-login {
.my-login__aside {
width: 605px;
padding: 3rem 3.5rem;
background-repeat: no-repeat;
background-size: cover;
.my-login__logo {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
}
}
My question is "Can I duplicate my current .scss to other names without creating more statement ?".
I want the result like this when webpack compile my .scss file.
.my-login {
.my-login__aside {
width: 605px;
padding: 3rem 3.5rem;
background-repeat: no-repeat;
background-size: cover;
.my-login__logo {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
}
}
.my-register {
.my-register__aside {
width: 605px;
padding: 3rem 3.5rem;
background-repeat: no-repeat;
background-size: cover;
.my-register__logo {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
}
}
.my-verify {
.my-verify__aside {
width: 605px;
padding: 3rem 3.5rem;
background-repeat: no-repeat;
background-size: cover;
.my-verify__logo {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
}
}
.my-reset {
.my-reset__aside {
width: 605px;
padding: 3rem 3.5rem;
background-repeat: no-repeat;
background-size: cover;
.my-reset__logo {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
}
}
Finally, I can do it with a loop function.
$pages: login register verify reset;
#each $page in $pages {
.my-#{$page} {
.my-#{$page}__aside {
width: 605px;
padding: 3rem 3.5rem;
background-repeat: no-repeat;
background-size: cover;
.my-#{$page}__logo {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
}
}
}
How ampersand can be used in hover effect with parent element with ::after or ::before pseudo element or how this could be nested to make it work?
.showcase::after {
content: '';
height: 100vh;
width: 100%;
background-image: url('https://image.ibb.co/gzOBup/showcase.jpg');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
display: block;
filter: blur(10px);
-webkit-filter: blur(10px);
transition: $transAll;
:hover {
filter: blur(0px);
-webkit-filter: blur(0px);
}
}
You can not directly use the hover selector on a pseudo element.
In your case, you could use the #at-rootdirective to make it work.
Demo:
.showcase::after {
content: '';
height: 100vh;
width: 100%;
background-image: url('https://image.ibb.co/gzOBup/showcase.jpg');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
display: block;
filter: blur(10px);
-webkit-filter: blur(10px);
transition: $transAll;
#at-root .showcase:hover::after {
filter: blur(0px);
-webkit-filter: blur(0px);
}
}
Try this :
.showcase{
&::after {
content: '';
height: 100vh;
width: 100%;
background-image: url('https://image.ibb.co/gzOBup/showcase.jpg');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
display: block;
filter: blur(10px);
-webkit-filter: blur(10px);
transition: $transAll;
}
&:hover {
&::after {
filter: blur(0px);
-webkit-filter: blur(0px);
}
}
}
Images for mobile form factors are also getting downloaded in the browser when i use the desktop version. Is there any way i can load those images only in mobile form factor and not in desktop as i have different images for desktop.
Note:
I am building an adaptive design, not responsive. Hence it is fine
only those respective images of those form factors alone gets
downloaded.
Example code:
1. For mobile:
#media only screen and (max-device-width: 480px) {
div#wrapper {
background-image: url(media-queries-iphone5.jpg);
width: 400px;
}
div#header {
background-image: url(media-queries-iphone6.jpg);
height: 93px;
position: relative;
}
div#header h1 {
background-image: url(media-queries-iphone6s.jpg);
font-size: 140%;
}
#content {
background-image: url(media-queries-iphone5s.jpg);
float: none;
width: 100%;
}
#navigation {
background-image: url(media-queries-iphonese.jpg);
float:none;
width: auto;
}
}
2. For desktop:
#media only screen and (min-width: 768px) {
div#wrapper {
background-image: url(media-queries-samsung.jpg);
width: 400px;
}
div#header {
background-image: url(media-queries-dell.jpg);
height: 93px;
position: relative;
}
div#header h1 {
background-image: url(media-queries-hp.jpg);
font-size: 140%;
}
#content {
background-image: url(media-queries-macpro.jpg);
float: none;
width: 100%;
}
#navigation {
background-image: url(media-queries-mac.jpg);
float:none;
width: auto;
}
}
I have problem with Sass and this is connected with creating sprites and then reusing compiled class later.
This my styles.scss:
#import 'buttons/*.png';
#include all-buttons-sprites;
#import 'partial/buttons';
And this is buttons.scss:
.buttons {
#extend .buttons-blue-button;
background-repeat: no-repeat;
background-size: cover;
color: #ffffff;
cursor: pointer;
border: none;
font-size: 18px;
width: 242px;
height: 45px;
font: sky-text-med;
padding-bottom: 5px;
margin: 24px 4px 14px;
opacity: 0;
}
Compass not saying any errors but compile css is showing:
.buttons-sprite, .buttons-blue-button, .buttons, .buttons-blue-hover-button, .buttons-yellow-button, .buttons .yellow, .buttons-yellow-hover-button {
background: url('/welcome/assets/img/buttons-s5afcdf1a60.png') no-repeat; }
.buttons-blue-button, .buttons {
background-position: 0 0; }
.buttons {
background-repeat: no-repeat;
background-size: cover;
color: #ffffff;
cursor: pointer;
border: none;
font-size: 18px;
width: 242px;
height: 45px;
font: sky-text-med;
padding-bottom: 5px;
margin: 24px 4px 14px;
opacity: 0; }
But there is missing background with should be set by #extend .buttons-blue-button;
Why this isn't happening ?
.buttons {
#extend .buttons-blue-button;
...
}
You are telling the .button class to extend the .button-blue-button class.
.buttons-sprite, .buttons-blue-button, .buttons, .. {
background: url('/welcome/assets/img/buttons-s5afcdf1a60.png') no-repeat;
}
.buttons-blue-button, .buttons {
background-position: 0 0;
}
Compass is not making mistakes here, the background-image was set, the background-position was set too. Your expectations/assumptions & css-properties are Simply wrong.