SASS | @forward rule

The @forward rule processes the Sass stylesheets and makes their functions, mixins, and variables available in the cases when the stylesheet is loaded using @use rule. Organizing Sass libraries across many files are made possible, along with providing users to process a single entry point file.

Syntax:

filter_none

edit
close

play_arrow

link
brightness_4
code

@forward "<url>"

chevron_right


The above code loads the module available at the given URL. It makes the public members of the loaded module available to users of the module as though they were defined directly in the module. Those members aren’t available in the module if needed, @use rule is also likely to be used. It only processes the module once. If both @forward and @use are written for the same module in the same file, always write @forward first. Through this, if the users want to configure the forwarded module, that configuration will be applied to @forward before @use processes it without any configuration.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// gfg/_list.css
@mixin geeks 
  font-family: times new roman
  font-size: 4px
  padding: 2px
  
  
// geeksforgeeks.scss
@forward "gfg/list"
  
  
// style.scss
@use "geeksforgeeks"
gfg1 
  @include geeksforgeeks.geeks

chevron_right


This would result in the following CSS output:

gfg1 {
  font-family: times new roman
  font-size: 4px
  padding: 2px
}

Adding Prefix:
Sometimes the names may not be correct outside of the module that they’re defined in, for this reason, @forward has the option of adding an extra prefix to all the members that it points to.
Syntax:



filter_none

edit
close

play_arrow

link
brightness_4
code

@forward "<url>" as <prefix>-*

chevron_right


This adds the given prefix to the beginning of each mixin, function, and variable name pointed by the module.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// gfg/_list.css
@mixin forgeeks
  font-family: times new roman
  font-size: 4px
  padding: 2px
  
  
// geeks.scss
@forward "gfg/list" as geeks*
  
  
// style.scss
@use "geeksforgeeks"
gfg1 
  @include geeks.geeksforgeeks

chevron_right


This would result in the following CSS output:

gfg1 {
  font-family: times new roman
  font-size: 4px
  padding: 2px
}

Configuring Modules:
Processing a module with configuration is also possible using @forward rule. @forward rule’s configuration can also use !default flag in its configuration. This allows the module to change the defaults of the upstream stylesheet while also allowing downstream stylesheets to override them.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// _gfg.sass
$green: #000 !default
$border-radius: 2px !default
$box-shadow: 0 2px 1px rgba($green, 1) !default
  
  
geeks
  border-radius: $border-radius
  box-shadow: $box-shadow
  
// _geeksforgeeks.sass
@forward 'gfg' with ($green: #222 !default,
               $border-radius: 4px !default)
  
  
// style.sass
@use 'geeksforgeeks' with ($green: #333)

chevron_right


This would result in the following CSS output:

geeks {
  border-radius: 4px;
  box-shadow: 0 2px 1px rgba(51, 51, 51, 1);
}



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.