Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Giulia Chiola
Giulia Chiola

Posted on • Originally published atgiuliachiola.dev

     

Sass placeholder and its limits

From the official📚 Sass documentation

Sass has a special kind of selector known as a “placeholder”. It looks and acts a lot like a class selector, but it starts with a % and it's not included in the CSS output. In fact, any complex selector (the ones between the commas) that even contains a placeholder selector isn't included in the CSS, nor is any style rule whose selectors all contain placeholders.

We can write a placeholder for reusable pieces of code prefixed by the% keyword:

// colors.scss%colors-per-viewport{background:red;@media(min-width:768px){background:blue;}}
Enter fullscreen modeExit fullscreen mode

and call the placeholder using the syntax@extend %[placeholder-name]

// component.scss.colors{@extend%colors-per-viewport;}
Enter fullscreen modeExit fullscreen mode

CSS output:

.colors{background:red;}@media(min-width:768px){.colors{background:blue;}}
Enter fullscreen modeExit fullscreen mode

As seen above, we could also declare a code snippet with mediaquery inside.

The small matter

Unfortunately, we cannot call a placeholder declarationinside a mediaquery 😩

For instance, if we try to declare two placeholders and call them inside a media query

%colors-mobile{background:yellow;}%colors-tablet{background:green;}
Enter fullscreen modeExit fullscreen mode
.colors-viewport{@extend%colors-mobile;// ok!@media(min-width:768px){@extend%colors-tablet;// nope!}}
Enter fullscreen modeExit fullscreen mode

The code above will throw an error 😭

You may not @extend an outer selector from within @media.You may only @extend selectors within the same directive.
Enter fullscreen modeExit fullscreen mode

So, if we really need to reuse a code snipped inside a mediaquery, we can use amixin declaration.

I know, it is not the correct use of themixin function... but it's a desperate measure! 😅

@mixincolors-mobile{background:yellow;}@mixincolors-tablet{background:green;}
Enter fullscreen modeExit fullscreen mode
.colors-viewport{@includecolors-mobile;// ok!@media(min-width:768px){@includecolors-tablet;// yasss!}}
Enter fullscreen modeExit fullscreen mode

📚 More info

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
aileenr profile image
Aileen Rae
A web developer with a particular fondness for VueJS. She/her.
  • Location
    Edinburgh, UK
  • Education
    BSci Chemical Physics, University of Glasgow
  • Pronouns
    She/her
  • Work
    Senior Software Engineer at Dayshape
  • Joined

I just learned about SASS placeholders today and came here from Google. I'm really glad I found this because I use nested media queries in SASSall the time. Thank you ✨

CollapseExpand
 
giulia_chiola profile image
Giulia Chiola
Front-end Developer. 👾Media engineer. ⚡Facing life one bug at a time. 🔥 Easily bribed with chocolate. 🍫 she/her.
  • Location
    Val di Susa, Italia
  • Education
    Media Engineer, Politecnico di Torino
  • Work
    Front-end web developer
  • Joined

I'm glad that it has been helpful 😊 Thank you!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Front-end Developer. 👾Media engineer. ⚡Facing life one bug at a time. 🔥 Easily bribed with chocolate. 🍫 she/her.
  • Location
    Val di Susa, Italia
  • Education
    Media Engineer, Politecnico di Torino
  • Work
    Front-end web developer
  • Joined

More fromGiulia Chiola

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp