Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Selectors and Combinators

User image

Published by

sanya sanya

Published at: 28th May, 2023
2.74 mins read

Firstly, before moving to the CSS Selectors and CSS Combinators. We must have to know that the CSS could be applied with the key pair values. Also, the key with their values must be in the {} as mentioned in the code below.

div {
   color: black;
   font-size: 2rem;
}

CSS Selectors

The Selectors in CSS are used to group the elements, tags, and classes to commonly apply CSS over them. There are five types of selectors available in the CSS and descriptively all are mentioned below -

  1. Universal Selectors - The `Universal Selectors` are used to apply the CSS to the whole webpage. We use `*` to use Universal Selector as mentioned in the code below -

  2. *{
    
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
  3. Element Selectors - The `Element Selectors` are used for selecting the elements and applying the same CSS over the selected elements as mentioned in the code below -

  4. h1, h2, h3{
    
      margin: 3px;
      padding: 1px;
      color: cyan;
    }
    

  5. ID Selectors - The `ID Selectors` are used to style the element as per the `id` given to them. ID is used for giving a special name to the element. The code showing ID Selector is mentioned below -

  6. <h1 id="name">Code Skiller</h1>
    
    #name {
          margin: 3px;
          padding: 1px;
          color: cyan;
    }
    

  7. Class Selectors - The `Class Selectors` are used to style the elements as per the `class` given to them. `Class` is used for giving a special name to a group of elements. The code showing Class Selector is mentioned below -

  8. <h1 class="name">Code Skiller</h1>
    
    <p class="name">a b c d e</p>
    
    .name {
          margin: 3px;
          padding: 1px;
          color: cyan;
    }
    



  9. Attributes Selectors - The `Attributes Selectors` are used to select the attributes in the Tags. The code for the `Attributes Selectors` is mentioned below -
    a[target="_blank"] {
    
      background-color: yellow;
    }
    

CSS Combinators

CSS combinators are selectors that allow you to target specific elements based on their relationship with other elements in the HTML structure. Combinators are used to create complex and specific CSS selectors. Here are the four main types of CSS combinators.

  1. Descendant Selector (space) - A descendant selector selects an element that is a descendant of another element. It matches any element that is a descendant of the specified parent element, no matter how deeply nested.

  2. .parent-element .child-element { //Style }

  3. Child Selector (>) - A child selector selects an element that is a direct child of another element. It matches any element that is a direct child of the specified parent element.

  4. .parent-element > .child-element { // style }


  5. Adjacent Sibling Selector (+) - An adjacent sibling selector selects a directly adjacent element (immediately follows) another element. It matches any element that immediately follows the specified element.

    .element1 + .element2 { // Style }

  6. General Sibling Selector (~) - A general sibling selector selects an element that is a sibling of another element. It matches any element that follows the specified element, regardless of whether it is immediately adjacent or not.

  7. .element1 ~ .element2 { // Style }

Library

WEB DEVELOPMENT

Basic

HTML - Hyper Text Markup Language

CSS - Cascading Style Sheets

Cascading Style Sheets (CSS)

Selectors and Combinators

CSS Transforms

Units

CSS Icons and Colors

CSS Borders

CSS Margin

CSS Padding

CSS Dimensions

CSS Box Model

CSS Background

Pseudo Classes in CSS

CSS Positions

CSS Flexbox

Responsiveness in CSS

CSS Transitions

CSS Animations

Other CSS Properties

Var()

CSS Frameworks

CSS Grids

CSS Display Properties

JavaScript

Frontend

Backend

Interview Questions

FAANG QUESTIONS

On this page

CSS Selectors

CSS Combinators