Skip to content

Buzzingz

Unleashing the Power of Code

  • HTML
  • CSS
  • Toggle search form

CSS Combinators

Posted on March 15, 2023March 15, 2023 By shani No Comments on CSS Combinators

CSS combinators are selectors that allow you to select elements based on their relationship with other elements in the document tree. There are four types of CSS combinators:

  • Descendant combinator (space) – It selects all the elements that are descendants of a specified ancestor. The syntax is:
ancestor descendant {
    /* CSS rules */
}

For example, to select all the p elements that are descendants of a div element, you would use:

div p {
    /* CSS rules */
}

Child combinator (>) – It selects only the direct children of the specified parent element. The syntax is:

parent > child {
    /* CSS rules */
}

For example, to select all the li elements that are direct children of a ul element, you would use:

ul > li {
    /* CSS rules */
}
  • Adjacent sibling combinator (+) – It selects the element immediately following the specified element. The syntax is:
element1 + element2 {
    /* CSS rules */
}

For example, to select the span element that immediately follows a div element, you would use:

div + span {
    /* CSS rules */
}
  • General sibling combinator (~) – It selects all the sibling elements that come after the specified element. The syntax is:
element1 ~ element2 {
    /* CSS rules */
}

For example, to select all the p elements that come after a h2 element, you would use:

h2 ~ p {
    /* CSS rules */
}

These combinators allow you to target specific elements and create complex CSS rules by combining selectors.

Here are some additional examples of how combinators can be used in CSS:

  • To select all the em elements that are descendants of a p element that is a child of a div element, you would use:
div > p em {
    /* CSS rules */
}
  • To select the label element that immediately precedes an input element, you would use:
input + label {
    /* CSS rules */
}
  • To select all the li elements that come after the first li element in a ul element, you would use:
ul li ~ li {
    /* CSS rules */
}

By using combinators, you can create more specific and targeted CSS rules that apply to only the elements that you want to style. This can help you create more complex layouts and designs that are easier to maintain and update over time.

CSS

Post navigation

Previous Post: CSS Layout – Horizontal & Vertical Align
Next Post: CSS Pseudo-classes

Leave a Reply Cancel reply

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

CSS Tutorial

  • Basic CSS Tutorial For Beginners
  • CSS Introduction
  • CSS Syntax
  • CSS Selectors
  • How To Add CSS
  • CSS Comments
  • CSS Colors
  • CSS Backgrounds
  • CSS Borders
  • CSS Margins
  • CSS Margin Collapse
  • CSS Padding
  • CSS Height, Width and Max-width
  • CSS Box Model
  • CSS Outline
  • CSS Text
  • CSS Fonts
  • CSS Web Safe Fonts
  • CSS Font Fallbacks
  • CSS Font Style
  • CSS Font Size
  • CSS Google Fonts
  • CSS Great Font Pairings
  • CSS Icons
  • CSS Links
  • CSS Lists
  • CSS Tables
  • CSS Layout – display: inline-block
  • CSS Layout – float and clear
  • CSS Layout – Horizontal & Vertical Align
  • CSS Layout – Overflow
  • CSS Layout – The display Property
  • CSS Layout – The z-index Property
  • CSS Layout – width and max-width
  • CSS Layout – The position Property
  • CSS Attribute Selectors
  • CSS Image Sprites
  • CSS Image Gallery
  • CSS Dropdowns
  • CSS Horizontal Navigation Bar
  • CSS Vertical Navigation Bar
  • CSS Navigation Bar
  • CSS Opacity / Transparency
  • CSS Pseudo-elements
  • CSS Pseudo-classes
  • CSS Combinators
  • CSS Math Functions
  • CSS The !important Rule
  • CSS Specificity
  • CSS Units
  • CSS Website Layout
  • CSS Counters
  • CSS Forms
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions

Copyright © 2023 Buzzingz.

Powered by PressBook WordPress theme