How to create a SASS mixin for placeholders that handles static CSS without errors?

Barbara Streisand
Release: 2024-10-26 14:12:31
Original
308 people have browsed it

How to create a SASS mixin for placeholders that handles static CSS without errors?

Placeholder Mixin in SCSS/CSS

Question:
How to create a mixin for placeholders in SASS that allows the passing of static CSS without syntax errors?

Answer:
To achieve seamless passing of static CSS in a placeholder mixin, we can utilize the @content directive.

Updated Mixin:

<code class="scss">@mixin placeholder {
  ::-webkit-input-placeholder {@content}
  :-moz-placeholder           {@content}
  ::-moz-placeholder          {@content}
  :-ms-input-placeholder      {@content}  
}</code>
Copy after login

Usage:

<code class="scss">@include placeholder {
    font-style:italic;
    color: white;
    font-weight:100;
}</code>
Copy after login

Advanced Mixin (Sass 3.4 and above):

For enhanced flexibility, you can use the following mixin to support both nested and unnested usage:

<code class="scss">@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;
  }
}</code>
Copy after login

This mixin provides a more robust placeholder approach that works across various CSS selectors and nested elements.

The above is the detailed content of How to create a SASS mixin for placeholders that handles static CSS without errors?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!