Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS Sass Basics Improve Your Workflow with Sass Use the Ampersand to Reference Parent Selectors

Vijayalaxmi vastrad
Vijayalaxmi vastrad
2,789 Points

how to apply both after and before pseudo elements to same element in scss

For example:

.circle:before { position: absolute; content: ""; /* more styles: width, height, etc */ }

.circle:after { position: absolute; content: ""; /* more styles: width, height, etc */ }

HOW TO WRITE THIS IN SCSS

2 Answers

Sam Glister
Sam Glister
1,704 Points

If this was me writing this in scss and they both have exactly the same rules I would potentially use a mixin which should achieve what you are wanting to do.

@mixin before-after-stlyes {
 &::before {
 position: absolute;
 content: "";
 width: 100px;
}
&::after {
 position: absolute;
 content: "";
 width: 100px;
}
}

Then apply this to whichever elements with the include function.

.circle {
 @include before-after-stlyes;
}

To make this SCSS even more DRY, I would add

@mixin styles {
  position: absolute;
  content: "";
  width: 100px;
}

so

@mixin before-after-styles {
  &::before {
  @include styles;
  }
  &::after {
  @include styles;
  }
}
Vijayalaxmi vastrad
Vijayalaxmi vastrad
2,789 Points

OHH THANK YOU SO MUCH, I AM BEGINNER TO SCSS, JUST STARTED LEARNING BASICS I DO NOT NO MIXINS CONCEPT.