SASS | Property Declaration

Property Declarations in SASS are used to define how the elements matching a selector need to be styled. Along with that SASS adds some extra features to property declarations in order to make them easier to write and to automate them.
Firstly, a declaration value can be any expression which can be evaluated and included in the result.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

.rectangle
  $side: 80px
  width: $side / 2
  length: $side
  border-radius: $side / 4

chevron_right


This will give the following CSS output:

.circle {
  width: 40px;
  length: 80px;
  border-radius: 20px;
}

Interpolation
A property declaration’s name may consist of interpolation. This allows SASS to dynamically generate properties according to our requirement. User can also interpolate the entire property name.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

@mixin create($property, $value, $lhs) 
  @each $create in $lhs 
    -#{$create}-#{$property}: $value
  #{$property}: $value
  
.gfg 
  @include create(font, times new roman, moz webkit)

chevron_right


This will give the following CSS output:

.gfg {
  -moz-font: times new roman;
  -webkit-font: times new roman;
  font: times new roman;
}

Nesting
Various CSS properties start with the same prefix. For example- margin-bottom, margin-top, margin-left, margin-right. SASS with the help of nesting. SASS makes it easier and less redundant. The outer property names are automatically added to the inner ones, separated with the help of “-“.
Example:



filter_none

edit
close

play_arrow

link
brightness_4
code

.fonts 
  transition-duration: 4s
  font
    size: 4px
    family: times new roman
    color: green
    
  
  &:hover  
    font
      size: 36px
      color: black

chevron_right


This will give the following CSS output:

.fonts {
  transition-duration: 4s;
  font-size: 4px;
  font-family: times new roman;
  font-color: green;
}
.fonts:hover {
  font-size: 36px;
  font-color: black;
}

Some of these CSS properties have shorthand versions that use the namespace as the property name. For these, you can write both the shorthand value and the more explicit nested versions.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

.gfg
  margin: auto
    left: 5px
    right: 5px
    bottom: 10px
    top: 2px

chevron_right


This will give the following CSS output:

.gfg {
  margin: auto;
  margin-left: 5px;
  margin-right: 5px;
  margin-bottom: 10px;
  margin-top: 2px;
}

Hidden Declarations
In cases when you need to implement a property only in some conditions i.e only when certain properties follow, you can use the hidden declarations of SASS.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

$times_new_roman: true
$arial: false
  
.gfg 
  font-size: 5px
  color: if($times_new_roman, green, null)
  color: if($arial, black, null)

chevron_right


This will give the following CSS output:

.gfg {
  font-size: 5px;
  color: green;
}

full-stack-img




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.