10 CSS Selectors to focus while building a website

10 CSS Selectors to focus while building a website

The first impression is always the most important one. When it comes to making a better first impression on a webpage user, the user interface is the first thing that comes up for any website. It is key to a website’s success. CSS selectors jumps in here to define the styles applied to the elements accordingly on the webpage. Selectors define the style and scope of an element, which in turn helps us better understand how a webpage is structured.

CSS selector banner

What are CSS Selectors?

CSS selectors are patterns used to select elements to style. You may be familiar with some basic CSS selectors, but knowing a few more will help you get where you want to go faster. Using the appropriate CSS selectors cuts down on code, increases readability, and simplifies CSS maintenance in the future.

There are several categories of CSS selectors. Understanding which one you’ll need can help you pick the correct way to do things. We’ll get into the different types of selectors in better context.

1. Element or Group Selector

The Element Selector is the most fundamental in CSS. If you need to style several elements at single shot, you may group them and apply styling to all of them at once. Instead of creating a class for each element, it is a good practice to limit the amount of CSS code you write. The element selector chooses HTML elements based on their names (or tags), such as p, h1, div, span, and so on

Element CSS selector

2. ID Selector

The ID selector is the strongest in CSS. To use an ID selector in CSS, start with a hashtag (#) and then the element’s ID. Though it appears more straightforward, there is one downside to using the ID selector. You can use each ID name only once. Using IDs as CSS selectors isn’t a good practice since you’ll have to rewrite the CSS if another element on the page has the same/similar style. Even if it doesn’t seem that you have more than one element with that style, it might come later. As a result, using class is always a better choice.

ID selector

3. Class selector

The class selector is the one that all developers use the most. To select elements with a specific class, use a period (.) followed by the class name. The class selector is identical to the ID selector, except it allows you to target many elements on a single page. You can also define that a class only affects specific HTML elements. To do so, start by writing the element’s name, then a period (.)

Class CSS selector

4. Attribute selector

The CSS Attribute Selector helps to find elements with specific attributes or values. It’s a useful technique for designing HTML elements by grouping them based on specific attributes and selecting those with similar attributes using the attribute selector. The following are some of the several types of attribute selectors:

Syntax:

  1. [attr] – Represents elements with the name attr as an attribute name.
  2. [attr=value] – Represents elements with the attribute name attr and the value is exactly value.
  3. [attr~=value] – The value of an attribute with the name attr is a whitespace-separated list of words, one of which is exactly value.
  4. [attr|=value] – Elements with the attr attribute must have a value that is a whole word, either alone or followed by a hyphen.
  5. [attr^=value] – Represents elements with the attr attribute name and a value that begins with the specified value.
  6. [attr$=value] – Represents elements with the attr attribute name and a value that ends in the specified value.
  7. [attr*=value] – Represents elements with the attr attribute name, whose attribute value is the specified value that may be present anywhere.

Attribute CSS selector

5. Universal Selector

The * selector in CSS is used to select all the elements on a page. It also selects all elements that are inside another element’s container. It is commonly expressed as an asterisk (i.e., “*”) followed by a selector.

Universal CSS selector

6. Descendant selector

The descendant selector matches elements that are direct descendants of a specified element.

Descendant selector

7. Child selector

The child selector (>) is used to select all elements that are children of a specific element. It only matches elements that match the second selector and are direct children of elements that match the first.

Child CSS selector

8.1 Adjacent Sibling Selector

The adjacent sibling selector is used to select an element that is immediately following another.

Adjacent CSS selector

8.2 General Sibling Selectors

Generic sibling selector is used to select all elements that are next siblings of a specified element.

General sibling selector

9.1 Pseudo-classes

An element’s unique state is specified using a pseudo-class. In CSS, you can define a pseudo-class by adding a colon (:) after a selector, followed by a pseudo-class such as a hover, focus, or active.

Pseudo class

9.2 Pseudo element

To style specified parts of an element, use a CSS pseudo-element. And to add a special style to the first line of a text, use ::first-line pseudo-element.

Pseudo element

10.1 nth-of-type()

The CSS pseudo-class :nth-of-type() matches elements based on their relative position among siblings of the same type (tag name).

nth of type

10.2 :nth-child(n)

The :nth-child(n) selector matches every element that is the nth child of its parent.

nth child

Subscribe to blogs

Get our latest blogs directly to your inbox.



    Leave a Reply

    Your email address will not be published.Required fields are marked *