Home > Web Front-end > CSS Tutorial > How to Create a Placeholder Mixin in SCSS/CSS that Works With Colons and Semicolons?

How to Create a Placeholder Mixin in SCSS/CSS that Works With Colons and Semicolons?

DDD
Release: 2024-10-27 04:02:02
Original
731 people have browsed it

How to Create a Placeholder Mixin in SCSS/CSS that Works With Colons and Semicolons?

Placeholder Mixin in SCSS/CSS

You are seeking to create a mixin for placeholders in Sass. The mixin you designed encounters issues due to the inclusion of colons and semicolons in the provided CSS.

Fortunately, Sass provides a solution through the @content directive. By employing @content in your mixin, you can pass static CSS directly, as demonstrated below:

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

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

For more details, refer to the Sass Reference at:
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#mixin-content

Sass 3.4 and Beyond

In Sass 3.4 and later, you can utilize the @optional-at-root mixin to ensure functionality in both nested and unnested scenarios:

@mixin optional-at-root($sel) {
  @at-root #{if(not &, $sel, selector-append(&, $sel))} {
    @content;
  }
}

@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;
  }
}
Copy after login

Usage and Output

.foo {
  @include placeholder {
    color: green;
  }
}

@include placeholder {
  color: red;
}
Copy after login

This will output:

.foo::-webkit-input-placeholder {
  color: green;
}
.foo:-moz-placeholder {
  color: green;
}
.foo::-moz-placeholder {
  color: green;
}
.foo:-ms-input-placeholder {
  color: green;
}

::-webkit-input-placeholder {
  color: red;
}
:-moz-placeholder {
  color: red;
}
::-moz-placeholder {
  color: red;
}
:-ms-input-placeholder {
  color: red;
}
Copy after login

The above is the detailed content of How to Create a Placeholder Mixin in SCSS/CSS that Works With Colons and Semicolons?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template