자리 표시 자 Mixin SCSS / CSS


122

sass에서 자리 표시 자에 대한 믹스 인을 만들려고합니다.

이것이 제가 만든 믹스 인입니다.

@mixin placeholder ($css) {
  ::-webkit-input-placeholder {$css}
  :-moz-placeholder           {$css}
  ::-moz-placeholder          {$css}
  :-ms-input-placeholder      {$css}  
}

이것은 내가 mixin을 포함하는 방법입니다.

@include placeholder(font-style:italic; color: white; font-weight:100;);

분명히 이것은 믹스 인으로 전달되는 모든 콜론과 세미콜론 때문에 작동하지 않을 것입니다. 그러나 ... 저는 정말로 정적 CSS를 입력하고 위의 함수와 똑같이 전달하고 싶습니다.

이게 가능해?

답변:


253

@content지시문을 찾고 있습니다 .

@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;
}

SASS Reference에는 http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#mixin-content 에서 자세한 정보가 있습니다 .


Sass 3.4부터이 믹스 인은 중첩 및 비 중첩 모두 작동하도록 다음과 같이 작성할 수 있습니다.

@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;
  }
}

용법:

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

@include placeholder {
  color: red;
}

산출:

.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;
}

1
완벽합니다, 정확히 제가 필요로 한 것입니다.
— Shannon Hochkins

4
이것이 왜 되돌려 졌는지 확실하지 않지만 대답은 정확하지 않습니다. 각 개별 공급 업체 접두사 시작 부분에 '@'가 필요합니다. Dave Hein의 답변을 더 살펴 보거나 더 나은 방법으로이 코드를 실행하면 작동하지 않는 것을 볼 수 있습니다.
— Sk446

1
@RickM 수정 한 내용이 컴파일되지 않았기 때문에 되돌 렸습니다 (오류 : 기본 수준 규칙에는 부모 선택자 참조 문자 '&'가 포함될 수 없습니다.). 이것은 OP가 찾고있는 정확한 출력을 생성하지만, "정답"이라고 주장하는 대답은 그렇지 않습니다.
— cimmanon

흥미 롭군요 ... CLI를 통해 컴파일하고 있습니까? 저에게는 정반대가 발생하고 다른 응답으로 판단하면 버전 관리 충돌이 있어야합니다.
— Sk446

4
예, CLI : Compass를 통한 Sass 3.3.0.rc.3. CodePen 및 Sassmeister로 두 번 확인했습니다 . 은 &이 같은 선택을받을 수있게하려면 필요가 input::-webkit-input-placeholder믹스 인으로하지만, 루트 수준에서 그것을 사용에서 당신을 방지 할 수 있습니다. sassmeister.com/gist/9469073 . 이제 LibSass를 사용한다면 그것은 다른 이야기입니다.
— cimmanon

168

cimmanon 과 Kurt Mueller가 제공 한 접근 방식 은 거의 효과가 있었지만 부모 참조가 필요하다는 것을 알았습니다 (즉, 각 공급 업체 접두사에 '&'접두사를 추가해야합니다). 이렇게 :

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

나는 다음과 같이 mixin을 사용합니다.

input {
    @include placeholder {
        font-family: $base-font-family;
        color: red;
    }
}

상위 참조가 제자리에 있으면 올바른 CSS가 생성됩니다. 예 :

input::-webkit-input-placeholder {
    font-family: Constantia, "Lucida Bright", Lucidabright, "Lucida Serif", Lucida, "DejaVu Serif", "Liberation Serif", Georgia, serif;
    color: red;
}

부모 참조 (&)가 없으면 공급 업체 접두사 앞에 공백이 삽입되고 CSS 프로세서는 선언을 무시합니다. 다음과 같이 보입니다.

input::-webkit-input-placeholder {
    font-family: Constantia, "Lucida Bright", Lucidabright, "Lucida Serif", Lucida, "DejaVu Serif", "Liberation Serif", Georgia, serif;
    color: red;
}

9
대단히 감사합니다. 당신은 ... 허용 대답 / 솔루션 작업을하기 위해 나에게 시행 착오의 시간을 저장
— tmuecksch

선택기가 이제 약간 과잉 자격이 부여되었다는 점에 주목할 가치가 있습니다 (진짜로 입력 또는 선택 요소에서 작동하는 것을 원하지 않는 경우). 부모 선택기를 추가하면 중첩없이 믹스 인을 사용할 수 없습니다 (OP가 찾고 있던 것임).
— cimmanon 2014 년

1
는 &완전히 필요하다. 이것을 반영하기 위해 인기있는 답변을 편집했습니다.
— AlexKempton 2014 년

1
+1. 일부 브라우저 공식 지원 ::placeholder내가 상단에이를 추가하는 것이 좋습니다 것 지금 속성을 :&::placeholder {@content}
— 매트 브라운

11

이것은 속기 구문입니다.

=placeholder
  &::-webkit-input-placeholder
    @content
  &:-moz-placeholder
    @content
  &::-moz-placeholder
    @content
  &:-ms-input-placeholder
    @content

그것을 사용하십시오

input
  +placeholder
    color: red

4
솔직히이되지 또한 일반 구문처럼 깔끔한 읽기 어렵습니다 생각
— 섀넌 Hochkins

댓글로 썼는데 너무 큽니다. 차라리이 구문을 사용하고 선택한 답변이 너무 쉽게 작동하도록 만들 수 없습니다. 다른 사람에게 유용 할 수 있다고 생각했습니다.
— igrossiter

2
이것은 가장 이해하기 쉽고 구현하기 쉽습니다.
— arslion 2015-06-26

3

왜 이런 건 아니야?

목록, 반복 및 보간 조합을 사용합니다.

@mixin placeholder ($rules) {

  @each $rule in $rules {
    ::-webkit-input-placeholder,
    :-moz-placeholder,
    ::-moz-placeholder,
    :-ms-input-placeholder {
      #{nth($rule, 1)}: #{nth($rule, 2)};
    }  
  }
}

$rules: (('border', '1px solid red'),
         ('color', 'green'));

@include placeholder( $rules );

1
조금 복잡하고 콘텐츠 지시문을 찾고 있었지만 감사합니다!
— Shannon Hochkins

3

'Unclosed block : CssSyntaxError'오류가 sass 컴파일러에서 발생하지 않도록하려면 ';' @content의 끝까지.

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

0

NoDirection이 작성한 것과 똑같은 sass mixin 자리 표시자를 사용합니다. 여기 sass mixins 컬렉션에서 찾을 수 있으며 매우 만족합니다. 거기에 텍스트 더 유지 mixin 옵션을 설명합니다.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.