LoginSignup
26

More than 5 years have passed since last update.

posted at

updated at

Sass(SCSS)のプレースホルダーを使ってDRYなCSSを書く

擬似セレクタを使って、アイコンを左もしくは右に設置するようなボタンを書くときに便利だったのでメモ。

/*ベースになる機能だけ*/
%base_button {
  background: #3B8B00;
  color: #ffffff;
  text-align: center;
  text-decoration: none;
  border-color: #c9c7d6;
  border-style: solid;
  border-width: 0px 2px 2px 0px;
  font-size: 14px;
  line-height: 21px;
  padding: 4px 10px 4px 10px;
  @mixin hover_and_active {
    background: #6BC42A;
    border: solid #c9c7d6;
  }
  &:hover {
    @include hover_and_active;
    border-width: 0px 2px 2px 0px;
    cursor: pointer;
  }
  &:active {
    @include hover_and_active;
    border-width: 2px 0px 0px 2px;
    position: relative;
    top: 2px;
    left: 2px;
  }
}
/*ボタンの左に要素を追加する*/
.left_icon_button {
  @extend %base_button;
  &:after {
    content: "<";
    position: relative;
    right: -4px;
  }
}

/*ボタンの右に要素を追加する*/
.right_icon_button {
  @extend %base_button;
  &:after {
    content: ">";
    position: relative;
    right: -4px;
  }
}

ちなみに要素の上書きもできるので、似たようなパーツはうまくまとめるとかなり記述量を減らせると思いました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
26