HTML 클래스 속성


예시

HTML 문서에서 class 속성 사용:

<html>
<head>
<style>
h1.intro {
  color: blue;
}

p.important {
  color: green;
}
</style>
</head>
<body>

<h1 class="intro">Header 1</h1>
<p>A paragraph.</p>
<p class="important">Note that this is an important paragraph. :)</p>

</body>
</html>

아래에서 더 많은 "직접 사용해 보기" 예를 살펴보세요.


정의 및 사용

속성 은 class요소에 대해 하나 이상의 클래스 이름을 지정합니다.

속성은 주로 스타일 시트 의 class클래스를 가리키는 데 사용됩니다. 그러나 HTML DOM을 통해 JavaScript에서 지정된 클래스를 사용하여 HTML 요소를 변경할 수도 있습니다.


브라우저 지원

Attribute
class Yes Yes Yes Yes Yes

통사론

<element class="classname">

속성 값

Value Description
classname Specifies one or more class names for an element. To specify multiple classes, separate the class names with a space, e.g. <span class="left important">. This allows you to combine several CSS classes for one HTML element.

Naming rules:

  • Must begin with a letter A-Z or a-z
  • Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), and underscores ("_")

더 많은 예

예시

하나의 HTML 요소에 여러 클래스 추가:

<!DOCTYPE html>
<html>
<head>
<style>
h1.intro {
  color: blue;
  text-align: center;
}

.important {
  background-color: yellow;
}
</style>
</head>
<body>

<h1 class="intro important">Header 1</h1>
<p>A paragraph.</p>

</body>
</html>

예시

JavaScript를 사용하여 class="example"(인덱스 0)이 있는 첫 번째 요소에 노란색 배경색을 추가합니다.

var x = document.getElementsByClassName("example");
x[0].style.backgroundColor = "yellow";

예시

JavaScript를 사용하여 id="myDIV"인 요소에 "mystyle" 클래스 추가:

document.getElementById("myDIV").classList.add("mystyle");

관련 페이지

HTML 튜토리얼: HTML 속성

CSS 튜토리얼: CSS 구문

CSS 참조: CSS .class 선택기

HTML DOM 참조: HTML DOM getElementsByClassName() 메서드

HTML DOM 참조: HTML DOM className 속성

HTML DOM 참조: HTML DOM classList 속성

HTML DOM 참조: HTML DOM 스타일 개체