Targeting Specific Classes in CSS: A Definitive Guide


Specific Class Selector (tag.classname)

A Specific Class Selector in CSS combines both an HTML element type (tag) and a class name to target and apply styles to specific HTML elements that share the same class attribute. This selector is used when you want to target elements of a particular type that also have a specific class applied to them. It provides a way to style a subset of elements that meet both criteria.

Syntax: The syntax for a Specific Class Selector is as follows:

tag.classname {
  /* CSS styles go here */
}

Source Code :

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Specific Class Selector</title>
    <style>
      .color {
        color: goldenrod;
      }
      h1.color {
        text-decoration: underline;
        text-transform: uppercase;
      }
      .a {
        color: blue;
      }
      li.color {
        color: brown;
      }
    </style>
  </head>
  <body>
    <h1 class="color">Specific Class Selector</h1>
    <p>
      HTML elements or tags represent different parts of a web page's structure and content, such as <b class="color"> headings, paragraphs</b>, lists, images, links, and more. XML tags serve a similar purpose but can be customized to define the structure of data within an XML document according to
      a specific schema.
    </p>
    <h1>Specific Class Selector</h1>
    <ul>
      <li class="a">HTML</li>
      <li class="color">CSS</li>
      <li>JavaScript</li>
    </ul>
  </body>
</html>

Live Preview

.color { color: goldenrod; } This is a class selector .color targeting all HTML elements with the class name "color". It sets the text color of these elements to "goldenrod," which is a valid CSS color value.

h1.color { text-decoration: underline; text-transform: uppercase; } This is a Specific Class Selector h1.color targeting <h1> elements that also have the class name "color."

text-decoration: underline;: This rule underlines the text.text-transform: uppercase;: This rule transforms the text to uppercase.

.a { color: blue; } : This is a class selector .a targeting all HTML elements with the class name "a".It sets the text color of these elements to "blue."

li.color { color: brown; }:This is another Specific Class Selector li.color targeting

  • elements that also have the class name "color". It sets the text color of these specific <li> elements to "brown."

    Specific Class Selectors offer a flexible way to target and style specific elements within a group that share both a common class and a particular element type, allowing for more fine-grained control over your CSS styling.


  • CSS Tutorial


    Properties Reference


    Cascading Style Sheet


    Flexbox Tutorial


    CSS Grid Tutorial


    Transitions Properties


    CSS Properties with Examples


    CSS Selectors


    CSS Pseudo Elements


    CSS Attribute Selectors


    Input Pseudo Classes


    CSS Examples


    CSS Animation Projects