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
Petr Holusa
Courses Plus Student 4,041 PointsI don't understand usage for ~
Hi,
I try to understand what it is good this ~ for in example as is mentioned in the video.
h1 ~ label { ... }
Why we can't just write
label { ... }
or
form label { ...
}
Thanks.
2 Answers
Jason Anders
Treehouse Moderator 145,863 PointsThe ~ (tilde) is a General Sibling Selector, and it can make a huge difference if you use it or not.
If you write p ~ a it will only select the anchors that are siblings of a paragraph (any a child of any paragraph), but will NOT select any anchors that are not siblings of a paragraph. So if you use links in your paragraphs, you can style only those and NOT touch the links in your navigation menu.
Please read the definition and usage provided by the MDN
Keep Coding! :)
rhysadams
4,847 PointsHey Petr, you can also just write:
h1 label{....}
There's no problem with that either. It might just be a preferred way to write the code.
Petr Holusa
Courses Plus Student 4,041 PointsPetr Holusa
Courses Plus Student 4,041 PointsThanks for clarify. But does it have to be inside of any tag? I still don't get it what's the relation between H1 and label?
Jason Anders
Treehouse Moderator 145,863 PointsJason Anders
Treehouse Moderator 145,863 PointsFor that code snippet, if you did
h1 ~ labelit would select any labels that are Siblings of theh1.If you had more code and put a label inside of an h2 tag, that label would not be affect by the CSS styling. So there really is only a relationship between the h1 and the label because you want there to be, and no relation between an h2 tag and a label tag.
You could just do
label {color: blue}but then every label on your page would be blue. Withh1 ~ label {color: blue}only the one you have in the code will be blue and the one in an h2 tag will not.Just another tool for specificity.
I hope that makes sense.
Petr Holusa
Courses Plus Student 4,041 PointsPetr Holusa
Courses Plus Student 4,041 PointsSo you say, anything above the child with this css selector, I can create a connection between parent and child. Right? Like:
h2 ~ ul{...}Sorry for messy code. That markdown sheet doesn't work :(
Jason Anders
Treehouse Moderator 145,863 PointsJason Anders
Treehouse Moderator 145,863 PointsThat's correct. Except they are siblings not Parent/Child (I added the mark down to your code for you. If you click edit comment for your comment it should show how the proper formatting is done :).
So the ul element that is the sibling of the h2 element will be the only element affected by the CSS style.
If you had another ul somewhere else on the page, (as long as it was not also a sibling of an h2) ... for example a list in an h4 element
None of these would be changed/styled because they are part of an h4 and NOT an h2.
Petr Holusa
Courses Plus Student 4,041 PointsPetr Holusa
Courses Plus Student 4,041 PointsInteresting. I need to understand more about parenting. I though the children have to be wrapped with some tag (div, p...) Thanks Jason.
Jason Anders
Treehouse Moderator 145,863 PointsJason Anders
Treehouse Moderator 145,863 PointsI apologize, you are right about the children. I was using children and siblings interchangeably, and that is incorrect.
The ul is a sibling of the h2 and that is why you select those with the ~ (General Sibling Selector)
I edited my answers to use "Sibling" instead of "Children" (The reasons are the same, I just used the wrong terms).
I'm really sorry for the confusion on my part. And any further confusion I caused you.
Jason Anders
Treehouse Moderator 145,863 PointsJason Anders
Treehouse Moderator 145,863 PointsSo Petr,
Please read through the comments again with my correction. I was thinking of children as a group (i.e. brothers and sisters) and therefore confused the term children and sibling in my answers.
Let me know if this still makes sense! And again, I apologize.
Petr Holusa
Courses Plus Student 4,041 PointsPetr Holusa
Courses Plus Student 4,041 PointsCool Jason! Thanks. I do the CSS Selector course in this moment, so I will see how to understand that deeply. Thanks a lot.