EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

SASS @at-root

By Mahantesh NagathanMahantesh Nagathan

Home » Software Development » Software Development Tutorials » CSS Tutorial » SASS @at-root

SASS @at-root

Definition of SASS at-root

The @at-root directive is a set of nested rules that can render the style block at the document’s root. Sometimes when we create styles for a component in js, we usually add all variant classes at the root level and then use it to modify all the elements in the component in js. This can get more complex using sass if we want to nest our styles inside a namespace.

Furthermore, we will have to interpolate the selector to process it. It is when @at-root begins to play. The @at-root will move and restart the selector out of its initial nesting scope, enabling us to add the variant selector to the parent.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax of SASS @at-root

The syntax for @at-root directive can be written as:

@at-root <selector> {
...
…
//code to be executed
}

As shown in the above syntax, it allows everything within the document to be emitted at the root rather than using regular nesting. The @at-root can be written as @at-root {… } To just have numerous rules of style within its scope to place them all at the root of the document.

For instance:

div{
font-size: 10px;
color: red;
@at-root .my_atroot_demo {
background-color: grey;
}
}

When you compile the above scss file, you will have the below CSS code:

div{
font-size: 10px;
color: red;
}
. my_atroot_demo {
background-color: grey;
}

Popular Course in this category
All in One Software Development Bundle (600+ Courses, 50+ projects)600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (3,144 ratings)
Course Price

View Course

Related Courses
CSS Training (9 Courses, 9+ Projects)Bootstrap Training (2 Courses, 6+ Projects)jQuery Training (8 Courses, 5 Projects)

How @at-root directive Works in SASS?

This is most commonly used for the SassScript parent selector and selector functions while doing some advanced nesting. It assists with selector functions while implementing enhanced nesting. The @at-root directive could be worked as a single selector inline or even as a block with several selectors.

For inline, it works as:

.parent_element {
...
…
@at-root .child {
//code for styling the elements
}
}

For a block with multiple selectors, it works as:

.parent_element {
...
…
@at-root {
.child_element1 {
//code for styling the elements
}
.child_element2 {
//code for styling the elements
}
}
}

Examples to Implement @at-root directive in SASS

Let’s create an example to make use of the @at-root directive in SASS. Here, we have created an HTML file called example1.html with the below code:

Example #1

Code:

<html>
<head>
<title> SASS @at-root Directive Example </title>
<link rel = "stylesheet" type = "text/css" href = "sass_atroot.css"/>
</head>
<body>
<h1> Hello World...Welocme to EDUCBA!!!!. </h1>
<h2> It is a leading global provider of skill based education. It is an online learning model along with amazing 2500+ courses prepared by top-notch professionals. </h2>
</body>
</html>

Now create a file called sass_atroot.scss with the below code:

sass_atroot.scss

h1 {
color: #A52A2A;
background-color: #DEB887;
@at-root {
h2{
font-size: 22px;
font-style: italic;
color: #1E90FF;
}
}
}

Now open the command prompt and run the below command to watch the file and communicates it to SASS and updates the CSS file every time SASS file changes.

sass –watch sass_atroot.scss: sass_atroot.css

Now, execute the file with the above command and it will create the sass_atroot.css file with the below code:

sass_atroot.css

h1 {
color: #A52A2A;
background-color: #DEB887;
}
h2 {
font-size: 22px;
font-style: italic;
color: #1E90FF;
}

Output:

  • Save the above-given html code in html file.
  • Now open the above HTML file in a browser, and you will see the below output as shown in the displayed image.

SASS @at-root-1.1

Example #2

Create an HTML file called example2.html with the below code:

Code:

<html>
<head>
<title> SASS @at-root Directive Example </title>
<link rel = "stylesheet" type = "text/css" href = "sass_atroot1.css"/>
</head>
<body>
<div class="myclass"> EDUCBA... </div>
</body>
</html>

Now create a file called sass_atroot1.scss with the below code:

sass_atroot1.scss

.myclass {
background-color: grey;
height: 100px;
margin: 50px;
width: 100px;
@at-root {
@keyframes fade {
from { transform: scale(1.5); }
to { transform: scale(1.0); }
}
}
&:hover {
animation: fade 5s infinite;
}
}

Execute the file with the above command shown in the previous example and it will create the sass_atroot1.css file with the below code:

sass_atroot1.css

.myclass {
background-color: grey;
height: 100px;
margin: 50px;
width: 100px;
}
@keyframes fade {
from {
transform: scale(1.5);
}
to {
transform: scale(1);
}
}
.myclass:hover {
animation: fade 5s infinite;
}

Output

  • Save the above-given html code in html file.
  • Now open the above HTML file in a browser, and you will see the below output as shown in the displayed image.

Output-1.2

When you hover an element, it moves from scale 1.5 to scale 1. The animation effect will last for 5 seconds. This effect will then start over again, and go on forever (infinite).

Example #3

Create an HTML file called example3.html with the below code:

Code:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title> SASS @at-root Directive Example </title>
<link rel="stylesheet" type="text/css" href="sass_atroot2.css"/>
</head>
<body>
<h1> Hello World...</h1>
<div class="styledemo">
<h2> Welcome to EDUCBA... !!! </h2>
<p>Educba is online teaching providing server which helps various stream people or candidates to upgrade their knowledge respective to their domain.</p>
</div>
</body>
</html>

Now create a file called sass_atroot2.scss with the below code:

sass_atroot2.scss

h1 {
color: #ff8000;
@at-root {
h2{
color: #A52A2A;
background-color: #DEB887;
}
p{
font-size: 25px;
font-style: italic;
color: #666600;
}
}
}

Execute the file with the above command shown in the previous example and it will create the sass_atroot2.css file with the below code:

sass_atroot2.css

h1 {
color: #ff8000;
}
h2 {
color: #A52A2A;
background-color: #DEB887;
}
p {
font-size: 25px;
font-style: italic;
color: #666600;
}

Output

  • Save the above-given html code in html file.
  • Now open the above HTML file in a browser, and you will see the below output as shown in the displayed image.

Output-1.3

Conclusion

In this article, we have covered the concept of a @at-root directive in SASS in detail along with some examples which make the work easier. The obvious uses for @at-root are pretty limited but the control that this directive provides is beneficial in specific edge cases.

Nesting is probably most often used to create more specific selectors. Using the @at-root directive, allows you to keep styles grouped without creating more specificity than you need. For any selector, we could use this @at-rule directive to compile them in the stylesheet as root selector, but not as a nested selector.

Recommended Articles

This is a guide to SASS @at-root. Here we also discuss the definition and how @at-root directive works in sass? along with different examples and code implementation. You may also have a look at the following articles to learn more –

  1. SASS if else
  2. Sass Variables
  3. SASS Comments
  4. SASS @media

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
CSS Tutorial
  • sass
    • How to Install SASS
    • SASS Interview Questions
    • What is Sass
    • SASS Comments
    • Sass Variables
    • SASS Import
    • SASS if else
    • SASS Nesting
    • SASS @each
    • SASS @at-root
    • SASS @extend
    • SASS @media
    • SASS @for
    • SASS Map
    • SASS Selectors
    • SASS Color Functions
    • SASS Mixins
  • CSS
    • Introduction To CSS
    • What is CSS?
    • Uses Of CSS
    • Advantages of CSS
    • Career In CSS
    • CSS Commands
    • Is Css Case Sensitive
    • CSS object-fit
    • Types of CSS Selectors
    • CSS Radio Button
    • CSS Attribute Selector
    • CSS first child of class
    • CSS Selector nth Child
    • CSS Parent Selector
    • CSS Child Selector
    • CSS Not Selector
    • CSS Descendant Selector
    • CSS Inline Style
    • Checkbox CSS
    • CSS Appearance
    • CSS Font Properties
    • CSS font-variant
    • CSS Pagination
    • CSS Table Styling
    • CSS Table Cell Padding
    • CSS Padding Color
    • CSS Text Formatting
    • CSS for Text-Shadow
    • CSS text-stroke
    • CSS text-indent
    • CSS Rotate Text
    • CSS Text Color
    • CSS Center Div
    • CSS Arrow
    • CSS Arrow Down
    • CSS offset
    • CSS Cursor
    • CSS Layout
    • CSS Grid Layout
    • Button in CSS
    • CSS Button Border
    • text-align in CSS
    • CSS Horizontal Align
    • CSS Position
    • CSS Box Sizing
    • CSS box-shadow
    • CSS Text Underline
    • CSS Text Outline
    • CSS Blinking Text
    • Text Decoration CSS
    • CSS Vertical Align
    • CSS Word Wrap
    • CSS Padding
    • CSS Font Color
    • CSS Color Generator
    • CSS Margin Right
    • CSS Margin Color
    • CSS Color Codes
    • CSS Color Transparent
    • CSS Color Chart
    • CSS Link Color
    • CSS z-index
    • CSS Curved Border
    • CSS Border Left
    • CSS left
    • CSS Gradient Generator
    • Radial Gradient in CSS
    • CSS Shape Generator
    • CSS Triangle Generator
    • CSS background-color
    • CSS Background Image
    • CSS background-clip
    • CSS background-blend-mode
    • CSS Drop Shadow
    • CSS line height
    • CSS line break
    • Sticky Footer CSS
    • CSS Header Design
    • CSS Border Style
    • CSS Border Generator
    • Sticky Sidebar CSS
    • CSS Transparent Border
    • CSS Border Radius
    • CSS translate
    • CSS transform
    • CSS 3D Transforms
    • CSS Text Transform
    • CSS Transition Effects
    • CSS Transition Property
    • CSS Animation Transition
    • Negative Margin CSS
    • CSS Navigation Bar
    • CSS Overflow
    • CSS overflow-wrap
    • CSS Lists
    • CSS list-style
    • CSS Order
    • CSS Box Model
    • CSS Inner Border
    • CSS Icon
    • Menu Icon CSS
    • CSS Multiple Borders
    • Opacity in CSS
    • CSS Float Right
    • CSS Clear Float
    • CSS clip
    • CSS disabled
    • CSS Border Padding
    • Border Images in CSS
    • CSS Visibility
    • CSS Validator
    • CSS Clearfix
    • CSS Counter
    • CSS Letter Spacing
    • CSS root
    • CSS zoom
    • CSS calc()
    • CSS.supports()
    • CSS Loader
    • Media Query CSS
    • CSS @keyframes
    • CSS @bottom
    • CSS page-break-after Property
    • CSS page-break
    • CSS Position Fixed
    • CSS skew()
    • CSS Row
    • CSS Masking
    • CSS Scrollbar
    • CSS Overlay
    • CSS Important
    • CSS Cursor Hand
    • CSS Inherit
    • CSS Position Relative
    • CSS Compressor
    • CSS tricks
    • CSS Outline Property
    • CSS Flexbox Properties
    • CSS flex-direction
    • CSS content property
    • CSS Typography
    • CSS Formatter
    • CSS nowrap
    • CSS Column
    • GridView CSS
    • CSS Viewport
    • CSS Minify
    • CSS Combinators
    • CSS in React
    • CSS Matrix 
    • CSS Pseudo Elements
    • CSS Pseudo Classes
    • CSS Pointer Events
    • CSS Resize
    • CSS Inheritance
    • CSS Interview Questions
    • Cheat Sheet CSS
  • CSS3
    • What is CSS3?
    • CSS3 Interview Questions
    • Cheat sheet CSS3

Related Courses

CSS Training Course

Bootstrap Training Course

JQuery Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More