ADVERTISEMENTS

CSS Selector

In CSS (Cascading Style Sheets), selectors are used to target HTML elements that you want to apply styles to. Selectors can be based on various attributes of HTML elements such as tag name, class name, ID, attributes, or even their position in the HTML hierarchy. Here we have explained CSS selectors:

  • Tag Selector: Selects elements based on their tag name. For example, p selector targets all <p> elements in the HTML document.
  • Class Selector: Selects elements based on their class name. For example, .classname selector targets all elements with class="classname" in the HTML document.
  • ID Selector: Selects elements based on their ID attribute. For example, #idname selector targets the element with id="idname" in the HTML document.
  • Attribute Selector: Selects elements based on their attribute value. For example, [attribute=value] selector targets all elements that have attribute="value" in the HTML document.
  • Descendant Selector: Selects elements that are descendants of other elements. For example, div p selector targets all <p> elements that are descendants of <div> elements in the HTML document.
  • Child Selector: Selects elements that are direct children of other elements. For example, div > p selector targets all <p> elements that are direct children of <div> elements in the HTML document.
  • Adjacent Sibling Selector: Selects the element that is immediately next to another element. For example, h1 + p selector targets the first <p> element that immediately follows an <h1> element in the HTML document.
  • General Sibling Selector: Selects all elements that come after another element. For example, h1 ~ p selector targets all <p> elements that come after an <h1> element in the HTML document.

By using selectors in combination with CSS properties and values, you can customize the appearance of HTML elements on a web page.

ADVERTISEMENTS