Selectors & Properties

Q: What is CSS?

Cascading Style Sheets fondly referred to as CSS, is a simply designed language intended to simplify the process of making web pages presentable. CSS allows you to apply styles to web pages.

Q: What is the syntax for CSS?

A CSS style rule consists of a selector, property, and its value. The selector points to the HTML element where CSS style is to be applied. The CSS property is separated by semicolons.

selector { 
    Property: value; 
}

Q: What are the different types of CSS selectors?

CSS selectors are used to target HTML elements to apply styles. There are several types:

  1. Element selectors: Target HTML elements directly (e.g., p, h1, div).
  2. Class selectors: Target elements with a specific class attribute (e.g., .my-class).
  3. ID selectors: Target an element with a specific ID attribute (e.g., #my-id). IDs should be unique within a document.
  4. Attribute selectors: Target elements with a specific attribute or attribute value (e.g., [type="text"], [data-attribute~="value"]).
  5. Pseudo-classes: Target elements in a specific state (e.g., :hover, :active, :focus).
  6. Pseudo-elements: Target specific parts of an element (e.g., ::before, ::after, ::first-letter).
  7. Combinators: Select elements based on their relationship to other elements (e.g., descendant selector div p, child selector div > p, adjacent sibling selector h2 + p, general sibling selector h2 ~ p).