In my app.component.css I have the following:
* ~ * {
margin-top: 24px;
}
which does apply some margin-top to all elements following the first one.
This is not quite what I want, as I only need to target the direct children of my host.
So I did the following :
:host > * ~ :host > * {
margin-top: 24px;
}
Unfortunately that doesn't do anything. What am I missing here ?
You only need one :host > — the sibling combinator will relate the two * selectors for you:
:host > * ~ *
This reads as
Select any element
that is a following sibling of any element
that is a child of the host element.
... which implies that the subject of the selector is a child of the same host element as the element that it follows, since that's what the word "sibling" means.
Related
This question already has answers here:
What does the ">" (greater-than sign) CSS selector mean?
(8 answers)
Closed 7 years ago.
for example for this code: (I know about star but not '> *')
.row > * {
float: right;
}
or this one:
.row.uniform > * > :first-child {
margin-top: 0;
}
The > combinator separates two selectors and matches only those elements matched by the second selector that are direct children of elements matched by the first.
It selects all the elements where the parent is the one infront of > selector.
The * selects all elements, the .row > selects all elements where the parent is a .row element.
See the specifications.
The * selector is a wild card selector so will select all elements.
The > selector says "Only the next element of what you specify"
So > * will all select the next element no matter what it is.
The > selector in CSS is used to select child elements.
.row > * will style ALL elements within the parent class row.
">" means direct children elements. So that it will select all direct children of "row" elements.
I'm trying to select a specific and direct child of a specific and direct child of a div(I don't know how else to say that). However, in both cases, the elements could be anything so I need to use the "*" universal symbol to select these elements.
That may have been clear as mud, here is what I have:
.some-div>*:nth-child(2)>*:nth-child(1){
background-color: #d3fba9;
}
.some-div>*:nth-child(2)>*:nth-child(2){
background-color: #d3ffe9;
}
.some-div>*:nth-child(2)>*:nth-child(3){
background-color: #d3fac9;
}
As you can see I need to select the 1-3rd children of the 2nd child of a div, and apply style to them. Is it possible to do this?
Yes. The asterisk * is redundant since you have some specifier already in place for that particular element (the child and grandchild of .some-div.
.some-div > :nth-child(2) > :nth-child(1) - Selects the 1st child of the 2nd child of .some-div.
.some-div > :nth-child(2) > :nth-child(2) - Selects the 2nd child of the 2nd child of .some-div.
.some-div > :nth-child(2) > :nth-child(3) - Selects the 3rd child of the 2nd child of .some-div.
I'm trying to apply CSS to any immediate child of a parent container element. How do I use CSS's descendant < selector to select any immediate child regardless of type (div / span / etc).
I assume you mean the child selector. It's >, not <.
.parent > *
That will select any element. You can of course use any other selector as the child (an element, class, id, etc.)
If you are in SCSS then
.parent {
& > * {
}
}
ok I know how to do both these things separately:
#elemID { } /* selects only one element */
#elemID * { } /* selects all its children elements but not the element itself */
And I know I can do it like this:
#elemID, #elemID * { }
But is there a way to avoid this repeating ?
No, there is nothing shorter than that.
Note that if you really only want all the children of #elemID, and not all the descendants, you need to use the child combinator:
#elemID, #elemID > *
And as Šime Vidas has commented, some properties like color are automatically inherited by descendant elements by default. If you're trying to give text color to #elemID, you should not need to apply it explicitly and recursively to the elements inside it. See the SitePoint Reference on inheritance in CSS for details.
No. But you could select its parent if an equivalent selector exists:
.parent * { ... }
My HTML:
<p>Doggies</p>
<p class="green_guys">Froggies</p>
<p>Cupcakes</p>
<p>Piggies</p>
An all inclusive sibling selector (as I wish it to be), when used to select green_guys' siblings, would select the doggies cupcakes and piggies.
Other Selectors:
The + selector (a.k.a. adjacent sibling selector) would only select the cupcakes:
.green_guys + p {
/* selects the <p> element that immediately follows .green_guys */
}
The ~ selector (a.k.a. general sibling selector) would only select the cupcakes, and piggies:
.green_guys ~ p {
/* selects all <p> elements that follow .green_guys */
}
There is no sibling combinator that looks backward or around, only the adjacent and general sibling combinators that look forward.
The best you can do is determine a way to limit selection only to these p elements with the same parent, and then select the p children that are :not(.green_guys). If the parent element has an ID of #parent, for example, you can use this selector:
#parent > p:not(.green_guys) {
/* selects all <p> children of #parent that are not .green_guys */
}
However the above will still match your p elements even if none of them have the class. It is currently not possible to select the siblings of an element only given the existence of said element (which is the purpose of a sibling combinator — to establish a relationship between two sibling elements).
Selectors 4's :has() will hopefully rectify this without the need for a preceding-sibling combinator, resulting in the following solution:
p:has(~ .green_guys), .green_guys ~ p {
/* selects all <p> elements that are siblings of .green_guys */
}
This will not match anything if none of the children of the parent element have the class.
Not that I am aware of. There isn't a siblings selector either.
This might work, though:
#parent_of_green_guys > p:not(.green_guys) {
foo: bar;
}
Or if you aren't looking for ps with class attributes:
#parent_of_green_guys > p:not([class]) {
foo: bar;
}
My scenario was a little different but I wanted to select siblings of an input element to display one while it was active and another if it was left invalid.
My html was like this and I was unable to select the invalid text.
<input name="importantAnswer">
<div class="help-text"></div>
<div class="invalid-text"></div>
I was able to get around it by embedding the siblings in an adjacent one and using child selectors on that.
<input name="importantAnswer">
<div class="messages">
<div class="help-text"></div>
<div class="invalid-text"></div>
</div>
.help-text, .invalid-text {
visibility:hidden;
}
.input:active +.messages > .help-text {
visibility:visible;
}
.input.invalid:visited +.messages > .invalid-text {
visibility:visible;
}
And it worked.
I actually found 3 ways to do this:
Solution 1
.parent > p:not(.green_guys) {
text-decoration: line-through; /* or whatever you like */
}
Demo:
https://jsbin.com/cafipun/edit?html,css,output
PROS: quick and easy.
CONS: you need to know the parent selector (so that's not a super portable solution).
Solution 2
p ~ p:not(.green_guys),
p:first-child:not(.green_guys) {
text-decoration: line-through; /* or whatever you like */
}
Demo:
https://jsbin.com/seripuditu/edit?html,css,output
PROS: there is no need to know the parent selector (it can be very good to be used in generic cases).
CONS: it risks to be too generic (be careful, for example, if you have other p around your HTML!).
Solution 3
Small variant of the Solution 2 (to avoid the CONS). In this case you specify the sibling selector to get a more specific context.
p.siblings ~ p:not(.green_guys),
p.siblings:first-child:not(.green_guys) {
text-decoration: line-through; /* or whatever you like */
}
Demo:
https://jsbin.com/hakasek/edit?html,css,output
PROS: it is a portable solution, there is no need to know the parent selector (it can be very good in generic cases) and there is no worry to have conflict with other elements.
CONS: all siblings have to be well defined (with a class or an attribute for example).