1 Thing A Week 1 Thing
A Week

3 things I didn't know SCSS / SASS could do

Week 181.0 — 12th April '21

While I've spent over a decade working with LESS/SCSS/SASS, I never really pushed it beyond the basics. Don't get me wrong, the basics are enough of a reason to use the pre-processors. For me, it makes the code more readable and maintainable.

I've come across a variety of posts recently that have highlighted things that SASS can do that I hadn't realised.

1. Nesting properties

By nesting properties, you can save a few bytes, but more importantly it's another layer of organisation you can sprinkle throughout your CSS.

Nested properties in SCSS

2. Complex loops

Last week I talked about updating SkeletonCSS and introducing a map of colours. This has turned out to be very useful, so I'm not new to maps and loops themselves but I hadn't thought about pushing what they could do until this past week.

Thanks to the maps I've built up, it would actually be really easy to introduce dark mode by just flipping the map when creating CSS variables. You may look at my map and wonder why I started at -3 instead of 0. For me, it makes more sense to keep 0 as the primary shade of the colour and then the more negative you get, the darker the shade. The opposite is true too, the higher the number, the lighter the shade.

What I first started looking into reversing the map, this seemed like a world of pain and I thought I was going to have to switch it up but after a quick scribble on a piece of paper I found the formulas that make it work for me.

Reverse properties in a SASS map

3. Parent selectors

Now, I would argue a parent selector would selecting the parent of the current element... but that's not possible in CSS.

Thanks to SASS however, you can target the parent element which is another thing that aids organisation in your CSS.

Parent selector in SASS

I haven't used this in vein but I can see how it simplifies what you're writing and is another way to reduce the number of declarations you need. The best way to understand the example above, is that the nav item may be included multiple times and have a default style except when it's in a header. Instead of explicitly creating a new block for that scenario, you can now drive it from within the nested properties of the nav element itself.

Long overdue, but I'm going to read the docs for SASS in all it's glory and I found this great cheat sheet for SASS. I've always enjoyed working with CSS and HTML and SASS/SCSS is now firmly at the top of the list of programming languages I love to use.

More from 1 Thing A Week
« First update to SkeletonCSS Keeping kids entertained during the COVID-19 lockdown Some upcoming changes to the content on 1 Thing A Week »

Advertisement

Notable

Quick links and commentary on interesting articles, videos and more throughout the week.

  1. 181.4 – Thursday
    1. Paper Plane Lamp toxel.com
    2. Casio x NASA G-Shock watch   $140 coolmaterial.com
    3. Meet some of the last papyrus makes on Egypt Business Insider on youtube.com
  2. 181.7 – Sunday
    1. A collection of hand-lettered Marvel superhero logos kottke.org

Jump to notable items for Week #180, Week #182 , view the notable archive or my favourites.

About 1 Thing A Week

A website curated by @cchana bringing you 1 Thing A Week, delivered every Monday morning.

You can find a list of all articles in the archive, or read the most popular.

Find out more about 1 Thing A Week or read the privacy policy.

Market

Visit the market and support 1 Thing A Week.

You can find 1 Thing A Week on Twitter, Facebook, Instagram & Reddit