DEV Community

Jibran Kalia
Jibran Kalia

Posted on • Originally published at jibrankalia.com on

SASS Ampersands

One of the many reasons I love working at Scalefactor is that I get to work extensively both in the front-end and the back-end. That being said at the moment, the back-end is my stronger suit. That is why you might see a lot more front-end related posts here as I try to level up in that area. Going back to SASS. Here is something small I learned today. Borrowing the explanation below from CSS-Tricks.

Here is a simple nesting example that works in SASS:

.parent {
  .child {}
}

Which is compiled to in css:

.parent .child {}

What about when we use an &

.first-parent {
  &.second-parent {}
}

Which is compiled to in css:

.first-parent.second-parent {}

The difference between the two is that in the second example we are selecting an element that has both classes: first-parent and second parent.

I like to think of & as copying the parent selector and pasting it next to the child selector. This gets interesting when used along with BEM CSS Methodology— Block Element Modifier. More on that later.

There are a lot more uses of & in sass. This article in CSS-Tricks does an amazing job explaining everything.

Thank you for reading!

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay