1
2

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.

!importantと継承

Last updated at Posted at 2015-07-02

基本的に、CSSでは「最後の手段」とすべき!importantですが、かけ方によっては効かないこともありえます。意図的に!importantをかける際にも注意が必要です。

事象

ある処理を組んでいる過程で、「bodyにあるクラスをかけると、ページ全体でマウスカーソルが砂時計になる」という機能を実装しようと考えました。そこで、

失敗例
body.some-class{
  cursor: wait !important;
}

というようにCSSをかけました…が、(明示的にcursorをかけたものを除いて)一部の要素ではマウスカーソルが別な形となったままでした。

原因

さすがに動きとして気になったので、Chromeのデベロッパーツールで追いかけてみると、bodyから継承したcursorは、ばっちり上書きされていました。…input要素などの、デフォルトスタイルに。

どうやら、!importantが付いたプロパティでも、継承された時には通常のプロパティとなってしまうようでした。

対策

ひとまず、全要素に対して!importantをかけることにして解消しました(例はSCSSです)。

改善例
body.some-class{
  cursor: wait !important;
  *{
    cursor: wait !important;
  }
}

その他

CSSでcursor:waitとしても、カーソルの見た目が変わるだけで、ふつうに操作は可能なままです。操作自体を無効にするには、別途の手段が必要です。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?