What is Class and ID for in CSS ? CSS - Basic Selectors - Tags, Class, ID, Important.

In CSS (Cascading Style Sheets), “class” and “id” are two common ways to select and style HTML elements. They are part of the selector syntax in CSS and are used to apply styles to specific elements on a web page.


  • A class is a way to select one or more HTML elements with a common characteristic or property and apply the same styles to all of them. You can assign a class to multiple HTML elements by using the class attribute. To define a class in CSS, you use a period (.) followed by the class name. For example: HTML:

<p class="my-paragraph">This is a paragraph.</p>
<div class="my-paragraph">This is another paragraph.</div>


.my-paragraph {
  color: blue;
  font-size: 16px;

In this example, both the <p> and <div> elements with the class “my-paragraph” will have the specified styles applied to them.


  • An ID is used to select a single unique HTML element on a web page. Each HTML element should have a unique ID, and you can select that element using the # symbol followed by the ID name. For example: HTML:

<div id="unique-element">This is a unique element.</div>


#unique-element {
  background-color: yellow;
  border: 2px solid red;

In this case, the styles are applied only to the element with the unique ID “unique-element.” IDs should be unique within the HTML document, and you should not use the same ID for multiple elements.

In web development and Cascading Style Sheets (CSS), “Basic Selectors” refer to the fundamental methods used to target and style HTML elements. These selectors are essential for applying styles to specific elements on a web page. The three basic selectors you mentioned - Class, Type, and Id - are some of the most commonly used basic selectors in CSS.

Type Selector:

  • The Type Selector is the simplest of all selectors. It targets HTML elements based on their tag names. For example, if you want to apply styles to all <p> (paragraph) elements on a web page, you can use the type selector as follows:

p {
  color: blue;

Class Selector:

  • The Class Selector targets elements with a specific class attribute. Classes are used to group multiple HTML elements and apply the same styles to them. To select elements with a particular class, you prefix the class name with a period (dot). For example, to style all elements with the class “highlight,” you can use the class selector like this:

.highlight {
  background-color: yellow;

In your HTML, you would mark elements with this class as follows:

Copy code
<p class="highlight">This is a highlighted paragraph.</p>
<div class="highlight">This is a highlighted div.</div>

ID Selector:

  • The ID Selector is used to select a single element with a unique ID attribute. To select elements by ID, you prefix the ID name with a hash (#). For example, to style an element with the ID “header,” you can use the ID selector like this:

#header {
  font-size: 24px;
  font-weight: bold;

In your HTML, you would mark the specific element with this ID as follows:

Copy code
<header id="header">This is the header.</header>

It’s important to note that while Type and Class

Fundamental 3 things of CSS - Cascade, Specificity, Inheritance
How to Add Comments in CSS ?