
- CSS Tutorial
- CSS - Home
- CSS - Roadmap
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Types
- CSS - Measurement Units
- CSS - Selectors
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS - Cascading
- CSS Selectors
- CSS - Universal Selectors
- CSS - ID Selectors
- CSS - Group Selectors
- CSS - Class Selectors
- CSS - Child Selectors
- CSS - Element Selectors
- CSS - Descendant Selectors
- CSS - General Sibling Selectors
- CSS - Adjacent Sibling Selectors
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Advanced Features
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS Interview Questions
- CSS Online Quiz
- CSS Online Test
- CSS Mock Test
- CSS - Quick Guide
- CSS - Cheatsheet
- CSS - Properties References
- CSS - Functions References
- CSS - Color References
- CSS - Web Browser References
- CSS - Web Safe Fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - Combinators
CSS Combinators used to specify a particular relationship between selectors so that styling can be done based on relationship between elements in HTML structure. In other words combinators help you target elements based on their position and hierarchy within the HTML document.
For example, with combinators you can style paragraph element that palaced immediately after div element.
Table of Contents
Types of Combinators
There are four main types of CSS combinators:
- Descendant Combinator (whitespace)
- Child Combinator (>)
- Adjacent sibling Combinator (+)
- General sibling Combinator (~)
Now we will look forward to example usage of each of this combinators, before that make sure that you are well versed with CSS selectors.
CSS Descendant Combinator
The descendant combinators are used to select elements that are nested within another element, regardless of how deeply they are nested. These are often represented by a single space (" ").
For example you can target all <p> elements inside the <div> like this:
div p { /* CSS styles for all p elements inside div */ }
Similarly we can combine any selectors like classes, id etc. In the following example we combined a element selector with class selector.
Example
<!DOCTYPE html> <html> <head> <style> .parent { background-color: lightblue; padding: 20px; } .parent p { color: red; font-weight: bold; } </style> </head> <body> <div class="parent"> <h3> This is h3 tag inside parent element </h3> <p> This is a p tag inside parent element. </p> <p> This is a p tag inside parent element. </p> </div> <div> <p> This is a p tag outside parent element. </p> </div> </body> </html>
CSS Child Combinator
The child combinator (>) is used to select elements that are direct children of a specified element. It will not select elements that are further nested inside the child elements.
For example, you can target all direct <p> child elements inside the <div> like this:
div > p { /* CSS styles for all direct p children inside div */ }
Similarly, we can combine any selectors like classes, ids, etc. In the following example, we style all the p tags inside class parent.
Example
<!DOCTYPE html> <html> <head> <style> .parent { background-color: lightgreen; padding: 20px; } .parent > p { color: blue; font-weight: bold; } </style> </head> <body> <div class="parent"> <h3> This is an h3 tag inside parent. </h3> <p> This is a direct p tag inside parent. </p> <p> This is another direct p tag inside parent. </p> <div> <p> This is a p tag nested inside a div in parent element. </p> </div> </div> <div> <p> This is a p tag outside parent element. </p> </div> </body> </html>
CSS Adjacent Sibling Combinator
The adjacent sibling combinator (+) is used to select an element that is immediately preceded by a specified element. It only affects the element that directly follows the specified element.
For example, you can target a <p> element that immediately follows an <h3> element like this:
h3 + p { /* CSS styles for the p element immediately following an h3 */ }
In the following example, we target the <p> element that comes right after an <h3> element.
Example
<!DOCTYPE html> <html> <head> <style> .container { padding: 20px; background-color: lightyellow; } h3 + p { color: purple; font-style: italic; } </style> </head> <body> <div class="container"> <h3>This is an h3 tag</h3> <p> This is a p tag that immediately follows the h3 tag. </p> <p> This is another p tag, but it is not immediately after the h3 tag. </p> </div> </body> </html>
CSS General Sibling Combinator
The general sibling combinator (~) is used to select all elements that are siblings of a specified element and appear after it in the HTML structure. It will select all matching siblings, not just the one immediately following.
For example, you can target all <p> elements that follow an <h3> element like this:
h3 ~ p { /* CSS styles for all p elements following an h3 element */ }
In the following example, we target all <p> elements that are siblings of an <h3> element and appear after it.
Example
<!DOCTYPE html> <html> <head> <style> .container { padding: 20px; background-color: #f0f0f0; } h3 ~ p { color: darkorange; text-decoration: underline; } </style> </head> <body> <div class="container"> <p> This is a p tag before h3 tag </p> <h3> This is an h3 tag </h3> <p> This is a p tag that follows the h3 tag. </p> <p> This is another p tag that also follows the h3 tag. </p> <div> This is a div tag. </div> <p> This is a p tag after div tag </p> </div> <p> This is a p tag after h3 tag outside container. </p> </body> </html>
Combining Multiple Combinators
In CSS, you can combine multiple combinators to create more complex and specific selectors. By combining different combinators, you can target elements based on more complex relationships within the HTML structure.
For example, you can target a <p> element that is a direct child of a <div> and immediately follows an <h3> element like this:
div > h3 + p { /* CSS styles for p elements that are direct children of a div and immediately follow an h3 element */ }
In the following example, we combine the child combinator with the adjacent sibling combinator to target anchor elements inside navigation menu.
Example
<!DOCTYPE html> <html> <head> <style> nav { padding: 10px; background-color: #f0f0f0; } ul { list-style-type: none; padding: 0; } ul > li { display: inline-block; margin-right: 15px; } ul > li + a { color: red; text-decoration: underline; } ul > li > a { color: blue; text-decoration: none; } </style> </head> <body> <nav> <ul> <li><a href="#">Home</a></li> <li><a href="#">About</a></li> <li><a href="#">Services</a></li> <a href="#">Contact</a> </ul> </nav> </body> </html>