Css Selectors With Examples

Css Selectors With Examples

CSS SELECTORS.

Article on Css selectors #learncodeonline

CSS selectors are used to "find" (or select) the HTML elements you want to style.

We can divide CSS selectors into five categories:

Simple selectors (select elements based on name, id, class).

Combinator selectors (select elements based on a specific relationship between them).

Pseudo-class selectors (select elements based on a certain state).

Pseudo-elements selectors (select and style a part of an element).

Attribute selectors (select elements based on an attribute or attribute value.

  1. The CSS id Selector-

The id selector uses the id attribute of an HTML element to select a specific element.

The id of an element is 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.

Example The CSS rule below will be applied to the HTML element with id="para1":

#para1 { text-align: center; color: red; }

2.Element(Tag) Selectors-

Element selector or Tag selector, works on HTML tag level. When we add styling for an HTML tag directly, in our CSS file, it is known as element selectors.

For example:

HTML code

I am webdev Tauseef

CSS code

image.png

In the above CSS code, we have directly assigned styling to the h1 tag/element, this way of tag selection and styling is known as Element Selectors, as we assign style to the element directly.

The above CSS code, will change the font color of the text inside all the h1 tags in the HTML file, to red. This mode of selection should be used when you want to apply base styling to any element/tag, like if you want all the paragraph text in calibri font, you write the following CSS rule:

image.png

3.The CSS class Selector:

The class selector selects HTML elements with a specific class attribute.

To select elements with a specific class, write a period (.) character, followed by the class name.

Example

In this example all HTML elements with class="fancy" will be white and background color:Black

image.png

CSS CODE:

image.png

You can also specify that only specific HTML elements should be affected by a class.

HTML elements can also refer to more than one class.

Example In this example the

element will be styled according to class="center" and to class="large":

This paragraph refers to two classes.

4.The CSS Universal Selector:

The universal selector (*) selects all HTML elements on the page.

Example The CSS rule below will affect every HTML element on the page:

image.png

5.The CSS Grouping Selector- The grouping selector selects all the HTML elements with the same style definitions.

Look at the following CSS code (the h1, h2, and p elements have the same style definitions):

image.png

image.png

image.png

It will be better to group the selectors, to minimize the code.

To group selectors, separate each selector with a comma.

Example:

In this example we have grouped the selectors from the code above:

image.png