TL;DR: Do not exist Parent selectors in the CSS (yet).
There are several ways to dynamically select the parent of an element, but they all involve a bit of Javascript such as the method parent()
jQuery.
Using SASS, you can do something using the operator ampersand:
.filho{
position: absolute;
div &{
position: relative;
}
}
what generates
.filho {
position: absolute;
}
div .filho {
position: relative;
}
But note that you still need to somehow reference the parent selector, no matter how generic it is. Here comes some knowledge about cascading for maybe can simulate dynamic behavior, but this will never be 100% accurate.
This article talks about the problem in question, and emphasizes the fact that, using pure CSS3, it is impossible to make this selection of the parent element. There are even proposals for a new pseudo-element for the new specification (something like .teste:parent
), but this is still a distant dream.
Very good explanation. Thanks! ;)
– alan
@Lan, if the answer pleases you, do not forget to mark it as correct, if no other better! =)
– Caio Felipe Pereira
Oops! I didn’t know this option! But now it’s checked!
– alan