27
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-06-19

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

/*ベースになる機能だけ*/
%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;
  }
}

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

27
25
0

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
  3. You can use dark theme
What you can do with signing up
27
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?