London London is the capital of England. Paris Paris is the capital of France. Tokyo Tokyo is the capital of Japan. Using The class Attribute on Inline Elements The HTML class attribute can also be used on inline elements: Example My Important Heading This is some important text. Tip: The class attribute can be used on any HTML element. Note: The class name is case sensitive! Tip: You can learn much more about CSS in our CSS Tutorial. Select Elements With a Specific Class In CSS, to select elements with a specific class, write a period (.) character, followed by the name of the class: Example Use CSS to style all elements with the class name «city»: class=»city»>London London is the capital of England. class=»city»>Paris Paris is the capital of France. class=»city»>Tokyo Tokyo is the capital of Japan. London London is the capital of England. Paris Paris is the capital of France. Tokyo Tokyo is the capital of Japan. Multiple Classes HTML elements can have more than one class name, each class name must be separated by a space. Example Style elements with the class name «city», also style elements with the class name «main»: London In the example above, the first element belongs to both the «city» class and the «main» class. Different Tags Can Share Same Class Different tags, like and , can have the same class name and thereby share the same style: Example Using The class Attribute in JavaScript The class name can also be used by JavaScript to perform certain tasks for elements with the specified class name. JavaScript can access elements with a specified class name by using the getElementsByClassName() method: Example When a user clicks on a button, hide all elements with the class name «city»: Don’t worry if you don’t understand the code in the example above. You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial. HTML Exercises Test Yourself with Exercises! COLOR PICKER HOW TO SHARE CERTIFICATES Report Error If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail: Thank You For Helping Us! Your message has been sent to W3Schools. Top Tutorials Top References Top Examples Web Certificates W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2020 by Refsnes Data. All Rights Reserved. Powered by W3.CSS. Источник HTML class Attribute The HTML class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class. Using The class Attribute The class attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name. In the following example we have three elements with a class attribute with the value of «city». All of the three elements will be styled equally according to the .city style definition in the head section: Example London London is the capital of England. Paris Paris is the capital of France.
Tokyo Tokyo is the capital of Japan.
In the following example we have two elements with a class attribute with the value of «note». Both elements will be styled equally according to the .note style definition in the head section: Example My Important Heading This is some important text. Tip: The class attribute can be used on any HTML element. Note: The class name is case sensitive! Tip: You can learn much more about CSS in our CSS Tutorial. The Syntax For Class To create a class; write a period (.) character, followed by a class name. Then, define the CSS properties within curly braces <>: Example London is the capital of England. Paris is the capital of France. Tokyo is the capital of Japan. Multiple Classes HTML elements can belong to more than one class. To define multiple classes, separate the class names with a space, e.g. . The element will be styled according to all the classes specified. In the following example, the first element belongs to both the city class and also to the main class, and will get the CSS styles from both of the classes: Example London Different Elements Can Share Same Class Different HTML elements can point to the same class name. In the following example, both and points to the «city» class and will share the same style: Example Use of The class Attribute in JavaScript The class name can also be used by JavaScript to perform certain tasks for specific elements. JavaScript can access elements with a specific class name with the getElementsByClassName() method: Example Don’t worry if you don’t understand the code in the example above. You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial. Chapter Summary The HTML class attribute specifies one or more class names for an element Classes are used by CSS and JavaScript to select and access specific elements The class attribute can be used on any HTML element The class name is case sensitive Different HTML elements can point to the same class name JavaScript can access elements with a specific class name with the getElementsByClassName() method Источник HTML Class The class attribute allows CSS and javascript to access the HTML code. In the above example, element with class name logo is styled with .logo selector. Note: We use . before classname in CSS. Multiple Class in HTML An element can have more than one class. For example, Browser Output In the above example, element has two classname animal and dog . We can add more than one class to an HTML element and separate it with a space. Same class name on HTML elements We can also provide the same class name on multiple HTML elements. It makes it easier to apply the same style to multiple elements. Let’s us see an example, Browser output Here, we’ve used same class name animal on two elements. Class with Javascript in HTML The class attribute allows javascript to access the element via class selectors or getElementByClassName API. For example,
APPLE
Browser output In the above example, we have used document.getElementsByClassName(«fruits») to access the HTML element where the fruit is the name of the class in element. Then, the innerHTML property changes the content (the text inside the tag) from APPLE to ORANGE . Things to Note Class names are case-sensitive. Therefore, animal and Animal are two different class names. You cannot use a space in a class name. If you use a space in a class name, it will be treated as two separate class names. For example, if you have the following CSS: This will not apply styles to elements with the class my class . Instead, it will apply styles to elements with the class my and the class class (which are treated as two separate class names because of the space between them). To avoid this issue, you should use a different character (such as a hyphen or underscore) instead of a space in your class names. For example, change the name my class to my-class . Id vs Class An HTML id and a class are both attributes that can be added to an HTML element to give it an identifier. The main difference between an id and a class is that an id is unique within an HTML document, while a class can be used multiple times on a page. Moreover, we can only use one id in an HTML element while we can use multiple classes in an HTML element. Table of Contents Источник
Paris Paris is the capital of France. Tokyo Tokyo is the capital of Japan. London London is the capital of England. Paris Paris is the capital of France. Tokyo Tokyo is the capital of Japan. Using The class Attribute on Inline Elements The HTML class attribute can also be used on inline elements: Example My Important Heading This is some important text. Tip: The class attribute can be used on any HTML element. Note: The class name is case sensitive! Tip: You can learn much more about CSS in our CSS Tutorial. Select Elements With a Specific Class In CSS, to select elements with a specific class, write a period (.) character, followed by the name of the class: Example Use CSS to style all elements with the class name «city»: class=»city»>London London is the capital of England. class=»city»>Paris Paris is the capital of France. class=»city»>Tokyo Tokyo is the capital of Japan. London London is the capital of England. Paris Paris is the capital of France. Tokyo Tokyo is the capital of Japan. Multiple Classes HTML elements can have more than one class name, each class name must be separated by a space. Example Style elements with the class name «city», also style elements with the class name «main»: London In the example above, the first element belongs to both the «city» class and the «main» class. Different Tags Can Share Same Class Different tags, like and , can have the same class name and thereby share the same style: Example Using The class Attribute in JavaScript The class name can also be used by JavaScript to perform certain tasks for elements with the specified class name. JavaScript can access elements with a specified class name by using the getElementsByClassName() method: Example When a user clicks on a button, hide all elements with the class name «city»: Don’t worry if you don’t understand the code in the example above. You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial. HTML Exercises Test Yourself with Exercises! COLOR PICKER HOW TO SHARE CERTIFICATES Report Error If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail: Thank You For Helping Us! Your message has been sent to W3Schools. Top Tutorials Top References Top Examples Web Certificates W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2020 by Refsnes Data. All Rights Reserved. Powered by W3.CSS. Источник HTML class Attribute The HTML class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class. Using The class Attribute The class attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name. In the following example we have three elements with a class attribute with the value of «city». All of the three elements will be styled equally according to the .city style definition in the head section: Example London London is the capital of England. Paris Paris is the capital of France. Tokyo Tokyo is the capital of Japan. In the following example we have two elements with a class attribute with the value of «note». Both elements will be styled equally according to the .note style definition in the head section: Example My Important Heading This is some important text. Tip: The class attribute can be used on any HTML element. Note: The class name is case sensitive! Tip: You can learn much more about CSS in our CSS Tutorial. The Syntax For Class To create a class; write a period (.) character, followed by a class name. Then, define the CSS properties within curly braces <>: Example London is the capital of England. Paris is the capital of France. Tokyo is the capital of Japan. Multiple Classes HTML elements can belong to more than one class. To define multiple classes, separate the class names with a space, e.g. . The element will be styled according to all the classes specified. In the following example, the first element belongs to both the city class and also to the main class, and will get the CSS styles from both of the classes: Example London Different Elements Can Share Same Class Different HTML elements can point to the same class name. In the following example, both and points to the «city» class and will share the same style: Example Use of The class Attribute in JavaScript The class name can also be used by JavaScript to perform certain tasks for specific elements. JavaScript can access elements with a specific class name with the getElementsByClassName() method: Example Don’t worry if you don’t understand the code in the example above. You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial. Chapter Summary The HTML class attribute specifies one or more class names for an element Classes are used by CSS and JavaScript to select and access specific elements The class attribute can be used on any HTML element The class name is case sensitive Different HTML elements can point to the same class name JavaScript can access elements with a specific class name with the getElementsByClassName() method Источник HTML Class The class attribute allows CSS and javascript to access the HTML code. In the above example, element with class name logo is styled with .logo selector. Note: We use . before classname in CSS. Multiple Class in HTML An element can have more than one class. For example, Browser Output In the above example, element has two classname animal and dog . We can add more than one class to an HTML element and separate it with a space. Same class name on HTML elements We can also provide the same class name on multiple HTML elements. It makes it easier to apply the same style to multiple elements. Let’s us see an example, Browser output Here, we’ve used same class name animal on two elements. Class with Javascript in HTML The class attribute allows javascript to access the element via class selectors or getElementByClassName API. For example,
APPLE
Browser output In the above example, we have used document.getElementsByClassName(«fruits») to access the HTML element where the fruit is the name of the class in element. Then, the innerHTML property changes the content (the text inside the tag) from APPLE to ORANGE . Things to Note Class names are case-sensitive. Therefore, animal and Animal are two different class names. You cannot use a space in a class name. If you use a space in a class name, it will be treated as two separate class names. For example, if you have the following CSS: This will not apply styles to elements with the class my class . Instead, it will apply styles to elements with the class my and the class class (which are treated as two separate class names because of the space between them). To avoid this issue, you should use a different character (such as a hyphen or underscore) instead of a space in your class names. For example, change the name my class to my-class . Id vs Class An HTML id and a class are both attributes that can be added to an HTML element to give it an identifier. The main difference between an id and a class is that an id is unique within an HTML document, while a class can be used multiple times on a page. Moreover, we can only use one id in an HTML element while we can use multiple classes in an HTML element. Table of Contents Источник
The HTML class attribute is used to specify a class for an HTML element.
Multiple HTML elements can share the same class.
Using The class Attribute
The class attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name.
In the following example we have three elements with a class attribute with the value of «city». All of the three elements will be styled equally according to the .city style definition in the head section:
Example
London
London is the capital of England.
Paris
Paris is the capital of France.
Tokyo
Tokyo is the capital of Japan.
In the following example we have two elements with a class attribute with the value of «note». Both elements will be styled equally according to the .note style definition in the head section:
Example
My Important Heading
This is some important text.
Tip: The class attribute can be used on any HTML element.
Note: The class name is case sensitive!
Tip: You can learn much more about CSS in our CSS Tutorial.
The Syntax For Class
To create a class; write a period (.) character, followed by a class name. Then, define the CSS properties within curly braces <>:
Example
Create a class named «city»:
London is the capital of England.
Paris is the capital of France.
Tokyo is the capital of Japan.
Multiple Classes
HTML elements can belong to more than one class.
To define multiple classes, separate the class names with a space, e.g. . The element will be styled according to all the classes specified.
In the following example, the first element belongs to both the city class and also to the main class, and will get the CSS styles from both of the classes:
Example
London
Different Elements Can Share Same Class
Different HTML elements can point to the same class name.
In the following example, both and
point to the «city» class and will share the same style:
Example
Use of The class Attribute in JavaScript
The class name can also be used by JavaScript to perform certain tasks for specific elements.
JavaScript can access elements with a specific class name with the getElementsByClassName() method:
Example
Click on a button to hide all elements with the class name «city»:
Don’t worry if you don’t understand the code in the example above.
You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial.
Chapter Summary
The HTML class attribute specifies one or more class names for an element
Classes are used by CSS and JavaScript to select and access specific elements
The class attribute can be used on any HTML element
The class name is case sensitive
Different HTML elements can point to the same class name
JavaScript can access elements with a specific class name with the getElementsByClassName() method
The HTML class attribute is used to define equal styles for elements with the same class name.
So, all HTML elements with the same class attribute will get the same style.
Here we have three elements that point to the same class name:
Example
London
London is the capital of England.
Paris
Paris is the capital of France.
Tokyo
Tokyo is the capital of Japan.
London
London is the capital of England.
Paris
Paris is the capital of France.
Tokyo
Tokyo is the capital of Japan.
Using The class Attribute on Inline Elements
The HTML class attribute can also be used on inline elements:
Example
My Important Heading
This is some important text.
Tip: The class attribute can be used on any HTML element.
Note: The class name is case sensitive!
Tip: You can learn much more about CSS in our CSS Tutorial.
Select Elements With a Specific Class
In CSS, to select elements with a specific class, write a period (.) character, followed by the name of the class:
Example
Use CSS to style all elements with the class name «city»:
class=»city»>London
London is the capital of England.
class=»city»>Paris
Paris is the capital of France.
class=»city»>Tokyo
Tokyo is the capital of Japan.
London
London is the capital of England.
Paris
Paris is the capital of France.
Tokyo
Tokyo is the capital of Japan.
Multiple Classes
HTML elements can have more than one class name, each class name must be separated by a space.
Example
Style elements with the class name «city», also style elements with the class name «main»:
London
In the example above, the first element belongs to both the «city» class and the «main» class.
Different Tags Can Share Same Class
Different tags, like and
, can have the same class name and thereby share the same style:
Example
Using The class Attribute in JavaScript
The class name can also be used by JavaScript to perform certain tasks for elements with the specified class name.
JavaScript can access elements with a specified class name by using the getElementsByClassName() method:
Example
When a user clicks on a button, hide all elements with the class name «city»:
Don’t worry if you don’t understand the code in the example above.
You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial.
HTML Exercises
Test Yourself with Exercises!
COLOR PICKER
HOW TO
SHARE
CERTIFICATES
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Web Certificates
W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2020 by Refsnes Data. All Rights Reserved. Powered by W3.CSS.
The HTML class attribute is used to specify a class for an HTML element. Multiple HTML elements can share the same class.
Using The class Attribute
The class attribute is often used to point to a class name in a style sheet. It can also be used by a JavaScript to access and manipulate elements with the specific class name. In the following example we have three
elements with a class attribute with the value of «city». All of the three
elements will be styled equally according to the .city style definition in the head section:
Example
London
London is the capital of England.
Paris
Paris is the capital of France.
Tokyo
Tokyo is the capital of Japan.
In the following example we have two elements with a class attribute with the value of «note». Both elements will be styled equally according to the .note style definition in the head section:
Example
My Important Heading
This is some important text.
Tip: The class attribute can be used on any HTML element. Note: The class name is case sensitive! Tip: You can learn much more about CSS in our CSS Tutorial.
The Syntax For Class
To create a class; write a period (.) character, followed by a class name. Then, define the CSS properties within curly braces <>:
Example
London is the capital of England.
Paris is the capital of France.
Tokyo is the capital of Japan.
Multiple Classes
HTML elements can belong to more than one class. To define multiple classes, separate the class names with a space, e.g.
. The element will be styled according to all the classes specified. In the following example, the first element belongs to both the city class and also to the main class, and will get the CSS styles from both of the classes:
Example
London
Different Elements Can Share Same Class
Different HTML elements can point to the same class name. In the following example, both
and
points to the «city» class and will share the same style:
Example
Use of The class Attribute in JavaScript
The class name can also be used by JavaScript to perform certain tasks for specific elements. JavaScript can access elements with a specific class name with the getElementsByClassName() method:
Example
Don’t worry if you don’t understand the code in the example above. You will learn more about JavaScript in our HTML JavaScript chapter, or you can study our JavaScript Tutorial.
Chapter Summary
The HTML class attribute specifies one or more class names for an element
Classes are used by CSS and JavaScript to select and access specific elements
The class attribute can be used on any HTML element
The class name is case sensitive
Different HTML elements can point to the same class name
JavaScript can access elements with a specific class name with the getElementsByClassName() method
The class attribute allows CSS and javascript to access the HTML code. In the above example, element with class name logo is styled with .logo selector.
Note: We use . before classname in CSS.
Multiple Class in HTML
An element can have more than one class. For example,
Browser Output
In the above example, element has two classname animal and dog . We can add more than one class to an HTML element and separate it with a space.
Same class name on HTML elements
We can also provide the same class name on multiple HTML elements. It makes it easier to apply the same style to multiple elements. Let’s us see an example,
Browser output
Here, we’ve used same class name animal on two elements.
Class with Javascript in HTML
The class attribute allows javascript to access the element via class selectors or getElementByClassName API. For example,
APPLE
Browser output
In the above example, we have used document.getElementsByClassName(«fruits») to access the HTML element where the fruit is the name of the class in element. Then, the innerHTML property changes the content (the text inside the tag) from APPLE to ORANGE .
Things to Note
Class names are case-sensitive. Therefore, animal and Animal are two different class names.
You cannot use a space in a class name. If you use a space in a class name, it will be treated as two separate class names. For example, if you have the following CSS:
This will not apply styles to elements with the class my class . Instead, it will apply styles to elements with the class my and the class class (which are treated as two separate class names because of the space between them).
To avoid this issue, you should use a different character (such as a hyphen or underscore) instead of a space in your class names. For example, change the name my class to my-class .
Id vs Class
An HTML id and a class are both attributes that can be added to an HTML element to give it an identifier. The main difference between an id and a class is that an id is unique within an HTML document, while a class can be used multiple times on a page.
Moreover, we can only use one id in an HTML element while we can use multiple classes in an HTML element.