- HTML class Attribute
- Using The class Attribute
- Example
- London
- Paris
- Tokyo
- Example
- My Important Heading
- The Syntax For Class
- Example
- Multiple 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 Источник Class selectors The CSS class selector matches elements based on the contents of their class attribute. Syntax .class_name style properties > Note that this is equivalent to the following attribute selector : [class~=class_name] style properties > Examples CSS .red color : #f33; > .yellow-bg background : #ffa; > .fancy font-weight : bold; text-shadow : 4px 4px 3px #77f; > HTML p class = " red" > This paragraph has red text.p > p class = " red yellow-bg" > This paragraph has red text and a yellow background. p > p class = " red fancy" > This paragraph has red text and "fancy" styling.p > p > This is just a regular paragraph.p > Result Specifications Browser compatibility BCD tables only load in the browser See also Found a content problem with this page? This page was last modified on Jul 22, 2023 by MDN contributors. Your blueprint for a better internet. MDN Support Our communities Developers Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation. Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license. Источник CSS Selectors A CSS selector selects the HTML element(s) you want to style. CSS Selectors CSS selectors are used to «find» (or select) the HTML elements you want to style. We can divide CSS selectors into five categories: Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state) Pseudo-elements selectors (select and style a part of an element) Attribute selectors (select elements based on an attribute or attribute value) This page will explain the most basic CSS selectors. The CSS element Selector The element selector selects HTML elements based on the element name. Example Here, all elements on the page will be center-aligned, with a red text color: The CSS id Selector The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element. Example The CSS rule below will be applied to the HTML element with Note: An id name cannot start with a number! The CSS class Selector The class selector selects HTML elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the class name. Example In this example all HTML elements with will be red and center-aligned: You can also specify that only specific HTML elements should be affected by a class. Example In this example only elements with will be red and center-aligned: HTML elements can also refer to more than one class. Example In this example the element will be styled according to and to This paragraph refers to two classes. Note: A class name cannot start with a number! The CSS Universal Selector The universal selector (*) selects all HTML elements on the page. Example The CSS rule below will affect every HTML element on the page: The CSS Grouping Selector The grouping selector selects all the HTML elements with the same style definitions. Look at the following CSS code (the h1, h2, and p elements have the same style definitions): h2 text-align: center; color: red; > p text-align: center; color: red; > It will be better to group the selectors, to minimize the code. To group selectors, separate each selector with a comma. Example In this example we have grouped the selectors from the code above: All CSS Simple Selectors Selector Example Example description #id #firstname Selects the element with > .class .intro Selects all elements with > element.class p.intro Selects only elements with > * * Selects all elements element p Selects all elements element,element. div, p Selects all elements and all elements Источник
- 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 Источник Class selectors The CSS class selector matches elements based on the contents of their class attribute. Syntax .class_name style properties > Note that this is equivalent to the following attribute selector : [class~=class_name] style properties > Examples CSS .red color : #f33; > .yellow-bg background : #ffa; > .fancy font-weight : bold; text-shadow : 4px 4px 3px #77f; > HTML p class = " red" > This paragraph has red text.p > p class = " red yellow-bg" > This paragraph has red text and a yellow background. p > p class = " red fancy" > This paragraph has red text and "fancy" styling.p > p > This is just a regular paragraph.p > Result Specifications Browser compatibility BCD tables only load in the browser See also Found a content problem with this page? This page was last modified on Jul 22, 2023 by MDN contributors. Your blueprint for a better internet. MDN Support Our communities Developers Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation. Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license. Источник CSS Selectors A CSS selector selects the HTML element(s) you want to style. CSS Selectors CSS selectors are used to «find» (or select) the HTML elements you want to style. We can divide CSS selectors into five categories: Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state) Pseudo-elements selectors (select and style a part of an element) Attribute selectors (select elements based on an attribute or attribute value) This page will explain the most basic CSS selectors. The CSS element Selector The element selector selects HTML elements based on the element name. Example Here, all elements on the page will be center-aligned, with a red text color: The CSS id Selector The id selector uses the id attribute of an HTML element to select a specific element. The id of an element is unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element. Example The CSS rule below will be applied to the HTML element with Note: An id name cannot start with a number! The CSS class Selector The class selector selects HTML elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the class name. Example In this example all HTML elements with will be red and center-aligned: You can also specify that only specific HTML elements should be affected by a class. Example In this example only elements with will be red and center-aligned: HTML elements can also refer to more than one class. Example In this example the element will be styled according to and to This paragraph refers to two classes. Note: A class name cannot start with a number! The CSS Universal Selector The universal selector (*) selects all HTML elements on the page. Example The CSS rule below will affect every HTML element on the page: The CSS Grouping Selector The grouping selector selects all the HTML elements with the same style definitions. Look at the following CSS code (the h1, h2, and p elements have the same style definitions): h2 text-align: center; color: red; > p text-align: center; color: red; > It will be better to group the selectors, to minimize the code. To group selectors, separate each selector with a comma. Example In this example we have grouped the selectors from the code above: All CSS Simple Selectors Selector Example Example description #id #firstname Selects the element with > .class .intro Selects all elements with > element.class p.intro Selects only elements with > * * Selects all elements element p Selects all elements element,element. div, p Selects all elements and all elements Источник
- Different Elements Can Share Same Class
- Example
- Use of The class Attribute in JavaScript
- Example
- Chapter Summary
- Class selectors
- Syntax
- Examples
- CSS
- HTML
- Result
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- CSS Selectors
- CSS Selectors
- The CSS element Selector
- Example
- The CSS id Selector
- Example
- The CSS class Selector
- Example
- Example
- Example
- The CSS Universal Selector
- Example
- The CSS Grouping Selector
- Example
- All CSS Simple Selectors
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
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 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
Class selectors
The CSS class selector matches elements based on the contents of their class attribute.
Syntax
.class_name style properties >
Note that this is equivalent to the following attribute selector :
[class~=class_name] style properties >
Examples
CSS
.red color: #f33; > .yellow-bg background: #ffa; > .fancy font-weight: bold; text-shadow: 4px 4px 3px #77f; >
HTML
p class="red">This paragraph has red text.p> p class="red yellow-bg"> This paragraph has red text and a yellow background. p> p class="red fancy">This paragraph has red text and "fancy" styling.p> p>This is just a regular paragraph.p>
Result
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Jul 22, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
CSS Selectors
A CSS selector selects the HTML element(s) you want to style.
CSS Selectors
CSS selectors are used to «find» (or select) the HTML elements you want to style.
We can divide CSS selectors into five categories:
- Simple selectors (select elements based on name, id, class)
- Combinator selectors (select elements based on a specific relationship between them)
- Pseudo-class selectors (select elements based on a certain state)
- Pseudo-elements selectors (select and style a part of an element)
- Attribute selectors (select elements based on an attribute or attribute value)
This page will explain the most basic CSS selectors.
The CSS element Selector
The element selector selects HTML elements based on the element name.
Example
Here, all
elements on the page will be center-aligned, with a red text color:
The CSS id Selector
The id selector uses the id attribute of an HTML element to select a specific element.
The id of an element is unique within a page, so the id selector is used to select one unique element!
To select an element with a specific id, write a hash (#) character, followed by the id of the element.
Example
The CSS rule below will be applied to the HTML element with
Note: An id name cannot start with a number!
The CSS class Selector
The class selector selects HTML elements with a specific class attribute.
To select elements with a specific class, write a period (.) character, followed by the class name.
Example
In this example all HTML elements with will be red and center-aligned:
You can also specify that only specific HTML elements should be affected by a class.
Example
In this example only
elements with will be red and center-aligned:
HTML elements can also refer to more than one class.
Example
In this example the
element will be styled according to and to
This paragraph refers to two classes.
Note: A class name cannot start with a number!
The CSS Universal Selector
The universal selector (*) selects all HTML elements on the page.
Example
The CSS rule below will affect every HTML element on the page:
The CSS Grouping Selector
The grouping selector selects all the HTML elements with the same style definitions.
Look at the following CSS code (the h1, h2, and p elements have the same style definitions):
h2 text-align: center;
color: red;
>
p text-align: center;
color: red;
>
It will be better to group the selectors, to minimize the code.
To group selectors, separate each selector with a comma.
Example
In this example we have grouped the selectors from the code above:
All CSS Simple Selectors
Selector | Example | Example description |
---|---|---|
#id | #firstname | Selects the element with > |
.class | .intro | Selects all elements with > |
element.class | p.intro | Selects only elements with > |
* | * | Selects all elements |
element | p | Selects all elements |
element,element. | div, p | Selects all elements and all elements |