A Complete Guide to CSS Combinators
Posted By : Gaurav Arora | 26-Jun-2017
Hi Guys, In this blog we are going to understand the CSS combinators.
The first question rises what are the CSS Combinators?
We can say that a combinator is something that defines the relation between the CSS selectors.
Combinators are of four types:
- Descendant Selector
- Child Selector
- Adjacent Sibling Selector
- General Sibling Selector
Descendant Selector : It is generally denoted by (space), this selector will match all the elements that are descendant of a particular element.
A basic usage of Descendant Selector is :
.main p{
font-size: 15px;
}
The above property will apply to all the p elements inside the div with class main.
Child Selector : It is generally denoted by (>), this selector will select the immediate children of a particular element.
A basic usage of Child Selector is :
.main > p{
font-size: 15px;
}
The above property will apply only to the first and second p element as the third element is directly not the p element, So this property will not apply to the third element.
Adjacent Sibling Selector : This selector is generally denoted by (+), this selector will select all the elements that are adjacent siblings of a particular element.
.main + p{
font-size: 15px;
}
The above properties will apply to the p element which is just after the div with class main.
General Sibling Selector : This selector is generally denoted by (~), this selector will select all the elements that are siblings of a particular element.
Lorem Ispum
Lorem Ispum
Lorem Ispum
.main ~ p {
font-size: 15px;
}
The above properties will apply to the p element that is outside the div or we can say that the sibling.
These are the basics of CSS Combinators, hope they are useful.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Gaurav Arora
Gaurav is an experienced UI developer with experience and capabilities to build compelling UI's for Web and Mobile Applications.