Javatpoint Logo
Javatpoint Logo

CSS Class

CSS Class

The selector .class is used to select the elements that belong to the specific class attribute. For selecting an element using a particular class, we can use the (.) character followed by the name of the corresponding class. The class name is widely used to set the CSS property to the specific class.

Classes in CSS allow you to group different components and apply the same styles to them all at once.

They aid in separating style from structure, enabling us to design reusable styles that can be applied to multiple elements throughout a webpage.

Note: It ensures that a class name should not be started with a number.

Syntax:

Defining Styles for a Class:

The class selector (.class) in CSS is used to specify styles for a class. A period (.) and the class name come before the class selector.

Example:

We may use the .highlight selector in our CSS rules to define the styling for the "highlight" class. For illustration:

This CSS code will bold the text and set the background color of any element with the "highlight" class to yellow.

Applying a Class to an Element:

The class attribute must be added, and its value must be set to the desired class name to apply a class to an HTML element.

For instance, if we have a class called "highlight," we can use the following syntax to apply it to an element:

In this instance, the class name "highlight" will be connected to the paragraph element. You may more efficiently manage the appearance of items by utilizing classes to assign them particular styles.

Applying Multiple Classes:

By providing multiple class names in the 'class' property of an element in HTML, you can apply multiple classes to that element. As a result, you can blend and apply styles from different classes to a single element.

Put spaces between the class names in the class attribute value to apply several classes simultaneously.

Example:

The styles specified in each class will be applied in the order they appear in the HTML when multiple classes are applied to one element. This enables a cascading effect whereby styles from several classes can support or override one another.

Example: Suppose we have the following CSS rules:

An element will have a red text color from class1 and a yellow background color from class2 if both classes are applied. A more individualized and aesthetically pleasing style is possible thanks to the combination of these classes.

Note: The class styles applied later in the class attribute will take precedence if conflicting styles are defined in multiple classes. Using classes in a particular order lets you quickly override or adjust styles.

Specificity and Class Selectors:

When multiple selectors are directed at the same element, specificity, a core CSS feature, determines which styles are applied first. It is essential for settling conflicts and ensuring the appropriate styles are used correctly.

A CSS rule's specificity is determined by the kind of selectors it uses. Compared to element selectors and ID selectors, class selectors have a modest amount of specificity.

The number and type of selectors used in a rule can be used to calculate the specificity values. A selector's specificity value increases as it becomes more focused. An overview of the specificity hierarchy is provided below:

  • Inline styles: The most exact styles are those that are inline. The style attribute is used to apply them to specific HTML components directly.
  • ID selectors: Compared to class selectors, ID selectors are more particular. They target elements according to the value of the id attribute.
  • Class Selectors: Class selectors have a fair amount of specificity. Based on the class attribute value, they target particular items.
  • Element selectors: These have the least specificity. They target elements according to the names of their HTML tags.

When two styles with different selectors are defined for the same element, the style with the highest level of specificity takes priority.

Example:

HTML:

CSS:

In this instance, the.myClass class selection is less particular than the #myId ID selector. Because the style specified by the ID selector takes precedence, the text color of the <p> element will be blue.

Combined Class Selectors:

CSS's class selectors can be combined with other selectors to target elements more precisely and apply styles to certain elements inside bigger structures.

When combining class selectors, a space is used to separate each selector. This area denotes that components with both classes applied should follow the style.

Example:

The parent element selector.parent-element and the class selector.class are joined with a space in the example above. This combination's target is elements with the class name "class" that are offspring of elements with the class name "parent-element." Only those particular items will be subject to the rules set styles.

Overriding Styles with Classes:

The styles other selections define can be modified or overridden using CSS classes. The following are the key points:

  • When different selectors specify conflicting styles for the same element, a class selector's style definition may take precedence if it is more explicit or applied later in the CSS.
  • You can selectively override particular characteristics while maintaining the integrity of the other styles by applying a class to an element with competing styles.
  • You can override styles established by previously applied classes in HTML or CSS by applying a class later. This allows you to change the styles of specific elements without changing default or global styles.
  • When using CSS frameworks or third-party libraries that offer predefined classes for styling UI components, it can be advantageous to override styles with classes. By adding new classes or modifying existing ones, you can use these classes to adjust styles.

Example:

HTML:

CSS:

In this illustration, the text color is set to blue by the .default class, but the.myClass class, which is added afterward, overrides it and sets the text color to red.

Reusing Classes:

You can apply the same styles to multiple elements by using CSS's strong reusing classes idea.

The following are the key ideas to comprehend:

  • The use of classes encourages consistent coding practices across all your web pages. You can specify a class once and apply it to numerous elements instead of creating separate styles for related items.
  • Apply the class name to the desired elements using the class attribute to reuse a class.
  • Reusing classes makes styling easier and guarantees a unified look. All elements that have that class applied will immediately update when the styles of a class are modified.
  • You can establish consistency in look by using the same class name on many elements. All elements will share a set of styles established for a class with that class.

Example:

The class "highlight" has been given to this illustration's header and paragraph components. Because of this, they both use the.highlight CSS selector's styles.

Note: Reusing classes will speed up your styling process, minimize code duplication, and guarantee unified design across all aspects.

Updating a class's styles automatically updates all components with that class applied throughout your web pages, simplifying maintenance.

CSS Frameworks and Classes:

The predefined classes offered by CSS frameworks can be used consistently and effectively to style elements.

The following are the salient features of CSS classes and frameworks:

  • CSS frameworks like Bootstrap, Foundation, or Tailwind CSS provide a set of CSS rules, components, and stylesheets to speed up web development and achieve uniform designs.
  • Frameworks heavily rely on classes to offer a modular and adaptable styling approach. They specify precise class names that you can use to apply predetermined styles to HTML components.
  • You can quickly create visually appealing designs using preset classes from CSS frameworks without creating many new CSS codes.
  • Classes for popular UI elements like buttons, forms, navigation menus, and grids are usually included in CSS frameworks. These classes allow you to apply uniform styling and guarantee a unified appearance and feel throughout your website.
  • Framework classes frequently use a naming scheme to explain their function, making it simpler to comprehend and use them. For instance, the Bootstrap class name "btn btn-primary" designates a button component with primary styling.
  • Additionally, CSS frameworks offer utilities and auxiliary classes that provide extra functionality and styling choices. These classes can be used for text alignment, responsive design, layout modifications, etc.

Example (using Bootstrap):

The button element in this illustration is given the "btn" and "btn-primary" classes from the Bootstrap framework. As a result, the button now has the default styling that Bootstrap defines for primary buttons.

Now, Let's Understand the Whole Concept Using Examples:

Example 1:

Example:


Next TopicCSS Center Image





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA