HTML Tutorial
An HTML element’s class can be specified using the HTML classes property.
The same class might be shared by several HTML components.
In a style sheet, the class attribute is frequently used to point to a class name. JavaScript can also use it to access and work with elements that have that particular class name.
Three <div> elements with the class attribute set to “city” are shown in the example below. The head section’s .city style definition will be applied uniformly to all three of the <div> elements:
Berlin
Berlin is the capital of Germany.
Rome
Rome is the capital of Italy.
Tokyo
Tokyo is the capital of Japan.
Two <span> elements with the class attribute set to “note” are shown in the example below. The head section’s .note style definition will apply an equal style to both <span> elements:
My Important Title
This is some important text.
The <div> element has no required attributes, but style, class and id are common.
Put a class name after the period (.) character to create a new class. Next, specify the CSS attributes enclosed in curly brackets {}:
Create a class named “city”:
Berlin
Berlin is the capital of Germany.
Rome
Rome is the capital of Italy.
Tokyo
Tokyo is the capital of Japan.
Elements in HTML are capable of being in several classes.
When defining multiple classes, use a space between the class names, like in <div class=”city main”>. The element will be styled in accordance with each of the listed classes.
The first <h2> element in the example below is a member of both the main class and the city class, and it will inherit the CSS styles from both:
Berlin
Rome
Tokyo
The class name of one HTML element can be referenced by another.
Both <h2> and <p> in the example below refer to the “city” class and will use the same style:
Berlin
Berlin is the capital of Germany
JavaScript can also use the class name to carry out particular actions for particular items.
JavaScript’s getElementsByClassName() function allows it to retrieve elements with a given class name:
Click on a button to hide all elements with the class name “city”:
HTML Classes: Organizing Your Web Content
In the world of web development, HTML classes play a crucial role in structuring and styling your web content. Classes allow you to group elements together and apply specific styles or behaviors to them, making your code more organized and maintainable.
An HTML class is an attribute that you can assign to one or more HTML elements. The class attribute is used to identify the element and associate it with a specific set of styles or functionality. This is particularly useful when you have multiple elements that share similar characteristics or need to be styled in a consistent manner.
To assign a class to an HTML element, you use the class attribute, like this:
This is a div with a class.
You can then use CSS to target the elements with that class and apply styles accordingly. For example:
.my-class {
background-color: #f1f1f1;
padding: 20px;
}
Additionally, you can assign multiple classes to a single element, separating them with a space:
This div has multiple classes.
This allows you to combine and apply different sets of styles to the same element, making your design more flexible and modular.
Another useful feature of HTML classes is the ability to select elements by their class using JavaScript. The `getElementsByClassName()` method allows you to retrieve a collection of elements with a specific class name, which can be helpful for manipulating or interacting with those elements programmatically.
By understanding and effectively utilizing HTML classes, you can create more organized and maintainable web content, making it easier to style and interact with your web pages.
CodingAsk.com is designed for learning and practice. Examples may be made simpler to aid understanding. Tutorials, references, and examples are regularly checked for mistakes, but we cannot guarantee complete accuracy. By using CodingAsk.com, you agree to our terms of use, cookie, and privacy policy.
Copyright 2010-2024 by Refsnes Data. All Rights Reserved.