1

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 ?

3
  • The styles of your component are about styling the component itself, not siblings or other components outside the current component. So I don't think this is even supposed to work. You would need to add this style to the parent component. Commented Aug 16, 2017 at 12:20
  • Can you please try to explain better what you exactly want to happen, instead (or in addition to) what you not want? Commented Aug 16, 2017 at 12:22
  • I think you need to replace the 2nd :host with the actual selector (tag name) of your component. I don't know how to address the direct children of a component, because you need to use ::ng-deep to pierce through the component boundary and use > to address direct children. I don't know if and how these can be combined. Commented Aug 16, 2017 at 12:26

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

To me it looks like he tries to address recursive children (childs are the same component types as current component) but it's not really clear.
@Günter Zöchbauer: "This is not quite what I want, as I only need to target the direct children of my host." I read that as the asker wants to apply the * ~ * logic only to children of the host element. Seems fairly clear to me.
The answer was exactly what I was looking for. Cheers to both of u

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.