"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Create a Placeholder Mixin in SCSS/CSS Using @content and @at-root?

How to Create a Placeholder Mixin in SCSS/CSS Using @content and @at-root?

Published on 2024-11-19
Browse:248

How to Create a Placeholder Mixin in SCSS/CSS Using @content and @at-root?

Placeholder Mixin in SCSS/CSS

You're facing an issue creating a placeholder mixin in Sass due to the presence of colons and semicolons in the CSS properties passed into the mixin.

To overcome this challenge, utilize the @content directive in your mixin:

@mixin placeholder {
  ::-webkit-input-placeholder {@content}
  :-moz-placeholder           {@content}
  ::-moz-placeholder          {@content}
  :-ms-input-placeholder      {@content}  
}

You can now include the mixin as follows:

@include placeholder {
    font-style: italic;
    color: white;
    font-weight: 100;
}

Additionally, Sass 3.4 introduces the @at-root directive, enabling you to write your mixin in a way that works in both nested and unnested contexts:

@mixin placeholder {
  @include optional-at-root('::-webkit-input-placeholder') {
    @content;
  }

  @include optional-at-root(':-moz-placeholder') {
    @content;
  }

  @include optional-at-root('::-moz-placeholder') {
    @content;
  }

  @include optional-at-root(':-ms-input-placeholder') {
    @content;
  }
}

By using @at-root in conjunction with @content, you ensure your mixin works correctly in all scenarios.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3