HTML and CSS Tutorials CSS Selectors

Creating CSS Selectors

CSS selectors are used to select the content you want to style. Selectors are the part of CSS rule set. CSS selectors select HTML elements according to their id, class, type, attribute, etc.

There are several different types of selectors in CSS.

  1. CSS Element Selector
  2. CSS Class Selector
  3. CSS Id Selector
  4. CSS Universal Selector
  5. CSS Group Selector
css selectors

Element Selector

The element selector selects elements based on the element name.

You can select all elements on a page like this (in this case, all elements will be center-aligned, with a red text color):

Class Selector

The class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class.

In the example below, all HTML elements with class=”center” will be red and center-aligned:

CSS Class Selector Example

ID Selector

The id selector uses the id attribute of an HTML element to select a specific element. The id of an element should be unique within a page, so the id selector is used to select one unique element! To select an element with a specific id, write a hash (#) character, followed by the id of the element.

The style rule below will be applied to the HTML element with id=”para1″:

Grouping Selectors

If you want to have the same styles on different you can use selectors like this.

CSS Comments

Comments are used to explain the code and may help when you edit the source code at a later date. Comments are ignored by browsers.

A CSS comment starts with /* and ends with */. Comments can also span multiple lines:

Universal Selector

The universal selector works like a wild card character, selecting all elements on a page. Every HTML page is built on content placed within HTML tags. Each set of tags represents an element on the page. Look at the following CSS example, which uses the universal selector:

Compound CSS Selectors or Descendant Combinator

The descendant selector or compound selector lets you combine two or more selectors so you can be more specific in your selection method. This type of selector has a higher priority than the other methods. 

Examples:

  • #some-id .some-class has higher priority over .some-class
  • #some-id .some-class div has higher priority over .some-class div

Attribute Selector

The attribute selector targets elements based on the presence and/or value of HTML attributes, and is declared using square brackets:

Pseudo Class Selector – Hover

A pseudo-class uses a colon character to identify a pseudo-state that an element might be in—for example, the state of being hovered, or the state of being activated. a:hover – a link when the user mouses over it

Let’s look at a common example:

CSS Selector Task 1

CSS Selector Task 1

CSS Selector Task 2

CSS Selector Task 2

CSS Selector Task 3

CSS Selector Task 3

CSS Selector Task 4

CSS Selector Task 4

CSS Selector Task 5

CSS Selector Task 5

Take Your Free Live Demo Class Now

Enter Your DETAILS BELOW

To Get Complete Information or We Will Call You

We Will Send You the Course Fees Details In Below Number

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

We Will Call Back You Shortly

Enter Your DETAILS BELOW

Enter Your DETAILS BELOW TO take

free demo class