Selectors

Types of selectors:

  • Simple Selectors
  • Combinator Selectors

Simple Selectors

  1. Selecting elements based on their id, name and class.

  2. * is a universal selector

  3. we can group elements with commas

  4. Id, class name cannot start with numbers

  5. with element.class we can also style some elements

Example

  • * { color: grey; }

    Universally selected Example

  • h3, h4, h5 { text-decoration: underline; color: darkseagreen; }

    group example

    group example

    group example
  • .example { color: black; font-size: 18px; border: 1px solid grey; }

    element.class example, this para elemets have a class name of example

  • #names { color: darkcyan; letter-spacing: 4px; }

    This para has a id of names

  • This para element has a class of hello, css only selects a para element with class hello

Combinator Selectors

  1. Using combinators(" ", >, +, ~) we can define the relation between the selectors and style them

  2. Descendant selectors like (div p) with spaces, selects all the p elements in the div
  3. child selector selects only direct childs of a parent element(div > p)
  4. Adjacent Sibling Selector is used to select the very next specified.They both should have same parent element(div + p), The next p element will be selected.
  5. General Sibling selectors will select all the elements. Like div ~ p, all the p elements after p will be selected.