Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Selectors Selectors - Beyond the Basics Child, Adjacent, and General Sibling Combinators

Kailash Seshadri
Kailash Seshadri
3,087 Points

Difference between using a space and a ~ symbol

What is the difference between using a space

h1 label {
  background: tomato;
  color:white;
  padding: 5px;
}

and using the ~ symbol

h1 ~ label {
  background: tomato;
  color:white;
  padding: 5px;
}

in the css?

2 Answers

Ante Vujčić
Ante Vujčić
11,306 Points

When you use space it means that all labels inside h1 element will be affected with added styles, while tilda ( ~ ) means that only labels after h1 element will be affected with style. This example of "h1 label {} " is not the best to show the difference but for example if you have

<p>blabla</p>
<span> batman </span>
<p> is red </p>

and you write css like this for example

span ~ p {
color: red;
}

only paragraph after span element will be affected with red color. Enjoy coding

Steven Parker
Steven Parker
231,236 Points

A space is a descendant selector, the item on the right must be inside the item on the left.

A tilde ("~") is a sibling selector, the item on the right must share a common parent, and must follow the item on the left (but there may be other sibling elements in between).

There's also the plus sign (+), which is the adjacent sibling selector. It works like the general sibling selector except that there may not be any other elements in between.