CSS Selectors

Used to target and apply styles to specific HTML elements on a web page.
CSS selectors allow you to define rules that determine which elements should receive a particular style.
THere are 4 selectors use din css
  1. Universal Selector (*)
  2. Type Selector
  3. Class Selector (.classname)
  4. ID Selector (#idname)

Example

/* Target all paragraphs inside a div with class "content" */
div.content p {
  margin: 10px;
  color: green;
}

Universal Selector (*)

Targets all elements on the page.
Rarely used due to its broad scope.
Syntax: * { margin: 0; padding: 0; }

Example

* {
  margin: 0;
  padding: 0;
  font-size: 12px;
}

Type Selector

Targets elements of a specific HTML tag.
Syntax: p { font-size: 16px; }

Example

p {
  font-size: 16px;
}

Class Selector (.classname)

Targets elements with a specific class attribute.
Class names start with a dot (.) followed by the class name.
Syntax: .highlight { background-color: yellow; }

Example

.highlight {
  background-color: yellow;
}

ID Selector (#idname)

Targets a single element with a specific ID attribute.
ID names start with a hash (#) followed by the ID name.
Should be unique within the page.
Syntax: #header { color: blue; }

Example

#header {
  color: blue;
}

Quick Recap

Topics Covered

Element

Description

Example

Universal Selector (*)

Targets all elements on the page.

Run

Type Selector

Targets elements of a specific HTML tag.

Run

Class Selector (.classname)

Targets elements with a specific class attribute.

Run

ID Selector (#idname)

Targets a single element with a specific ID attribute.

Run


Practice With Examples in Compilers

The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compile or Try on own Now

Example 1

Example 1 Example 2 Example 3 Example 4 Example 5


Quiz


FEEDBACK

Rating


Message