Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reusable Block with Nested Elements - SCSS [duplicate]

Tags:

css

sass

mixins

Say I have two unordered lists in one stylesheet. Both should be using the same styling, but both are nested in different parent elements:

#foo{
  position:absolute;
  ...
  ul{
    list-style-type:none;
    li{
      color:red;
      ...
    }
  }
}

#bar{
  position:relative;
  ...
  ul{
    list-style-type:none;
    li{
      color:red;
      ...
    }
  }
}

Is there a way to create something similar to a Rails' partial, where a single block of code can be reused / rendered inside different parent elements?

like image 910
dimitry_n Avatar asked Oct 29 '15 02:10

dimitry_n


People also ask

How do I reuse a class in SCSS?

You want to use @extend . btn; - @extend allows you to inherit all the properties of a selector without having to define it as a mixin.

What is nesting in SCSS?

Nesting is combining of different logic structures. Using SASS, we can combine multiple CSS rules within one another. If you are using multiple selectors, then you can use one selector inside another to create compound selectors.

What is the difference between SCSS and sass?

SASS (Syntactically Awesome Style Sheets) is a pre-processor scripting language that will be compiled or interpreted into CSS. SassScript is itself a scripting language whereas SCSS is the main syntax for the SASS which builds on top of the existing CSS syntax.

How do I compile SCSS to CSS in terminal?

Once Sass is installed, you can compile your Sass to CSS using the sass command. You'll need to tell Sass which file to build from, and where to output CSS to. For example, running sass input.scss output.css from your terminal would take a single Sass file, input.scss , and compile that file to output.css .


1 Answers

Solutions:

1-Mixins Link

@mixin ul-style ()
{
   ul{
    list-style-type:none;
    li{
      color:red;
    }
  }
}

#foo{
  position:absolute;
  @include ul-style();
}

#bar{
  position:relative;
  @include ul-style();
}

2-Inheritance Link

.ul-style
{
  ul
  {
    list-style-type:none;
    li{
      color:red;
    }  }

}

#foo{
  position:absolute;
  @extend .ul-style;

}

#bar{
  position:relative;
  @extend .ul-style;
}
like image 69
Alex Avatar answered Nov 11 '22 14:11

Alex