0
0

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 3 years have passed since last update.

idとclassの違い

Posted at

そもそもid、classとはなにか…

1ページで何度も同じ要素が出てくる際にid・classを用いてピンポイントで個別に要素を指定することができる。

idは#でclassは.で表す

実際の使用例


<h1 id="title>Hello World!</h1>
<p class="description">CSSの入門</p>

というHTMLに対して


# title {
  color: red;
}
.description {
  color: blue;
}

このように指定ができる。

一見、idとclassは同じ役割に見て取れるが、その役割は明確に異なっている。

それぞれの役割

  • **class:**同じclass名を1ページ内で何度でも使える
    (種別名を割り当てるイメージ)

  • **id:**同じid名は1ページ内で1度しか使えない。
    (固有の名前を割り当てるイメージ)

CSSの指定は:class
JavaScriptで操作する時には:id
というのが基本となる。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?