最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I use the CSS Adjacent sibling selector in Angular? - Stack Overflow

programmeradmin1浏览0评论

When using Angular ponents with styleUrl and separate stylesheet files, every CSS selector will be scoped to it's ponent in the output.

Now let's say I want to define a rule, that when a article is followed by another specific ponent, the later one needs a margin-top. In this case, when a article-card is followed by a article-group element.

The markup would be something like this:

<article-group
    [topArticlesOnly]="true"
    [articles]="homepage.top | slice:0:6">
</article-group>
<article-card
    *ngFor="let article of homepage.top | slice:0:6"
    [article]="article">
</article-card>

Both will contain a div with a specific class you'll see in the following examples.

I tried something like this:

[article-card] + [article-group] {
    margin-top: 20px;
}

article-card + article-group {
    margin-top: 20px;
}

And like this:

.article-card + .article-group {
    margin-top: 20px;
}

But both doesn't work because the browser output of the markup will be like this when Angular piles it:

<div _ngcontent-c3="">
    <article-card _ngcontent-c3="" _nghost-c4="" ng-reflect-article="[object Object]" ng-reflect-show-image-only="true">
        <article _ngcontent-c4="" class="article-card article-card--top-article article-card--image-only" ng-reflect-klass="article-card"
            ng-reflect-ng-class="[object Object]">
        </article>
    </article-card>
    <article-group _ngcontent-c3="" _nghost-c5="" ng-reflect-articles="[object Object],[object Object" ng-reflect-top-articles-only="true">
        <div _ngcontent-c5="" class="article-group article-group--top">

        </div>
    </article-group>
</div>

And the outputting CSS scoped like this:

[article-card][_ngcontent-c5]+ [article-group][_ngcontent-c5] {
    margin-top: 30px;
}

article-card[_ngcontent-c5] + article-group[_ngcontent-c5] {
    margin-top: 30px;
}

Maybe it needs some bination of :host-context or :host, but even then I never got my selectors to apply to the correct context.

So is there a way to use the Adjacent Sibling CSS Selector in Angular stylesheets?

When using Angular ponents with styleUrl and separate stylesheet files, every CSS selector will be scoped to it's ponent in the output.

Now let's say I want to define a rule, that when a article is followed by another specific ponent, the later one needs a margin-top. In this case, when a article-card is followed by a article-group element.

The markup would be something like this:

<article-group
    [topArticlesOnly]="true"
    [articles]="homepage.top | slice:0:6">
</article-group>
<article-card
    *ngFor="let article of homepage.top | slice:0:6"
    [article]="article">
</article-card>

Both will contain a div with a specific class you'll see in the following examples.

I tried something like this:

[article-card] + [article-group] {
    margin-top: 20px;
}

article-card + article-group {
    margin-top: 20px;
}

And like this:

.article-card + .article-group {
    margin-top: 20px;
}

But both doesn't work because the browser output of the markup will be like this when Angular piles it:

<div _ngcontent-c3="">
    <article-card _ngcontent-c3="" _nghost-c4="" ng-reflect-article="[object Object]" ng-reflect-show-image-only="true">
        <article _ngcontent-c4="" class="article-card article-card--top-article article-card--image-only" ng-reflect-klass="article-card"
            ng-reflect-ng-class="[object Object]">
        </article>
    </article-card>
    <article-group _ngcontent-c3="" _nghost-c5="" ng-reflect-articles="[object Object],[object Object" ng-reflect-top-articles-only="true">
        <div _ngcontent-c5="" class="article-group article-group--top">

        </div>
    </article-group>
</div>

And the outputting CSS scoped like this:

[article-card][_ngcontent-c5]+ [article-group][_ngcontent-c5] {
    margin-top: 30px;
}

article-card[_ngcontent-c5] + article-group[_ngcontent-c5] {
    margin-top: 30px;
}

Maybe it needs some bination of :host-context or :host, but even then I never got my selectors to apply to the correct context.

So is there a way to use the Adjacent Sibling CSS Selector in Angular stylesheets?

Share Improve this question edited Jul 13, 2017 at 15:12 René Stalder asked Jul 13, 2017 at 11:23 René StalderRené Stalder 2,5466 gold badges32 silver badges50 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Make use of the :host selector to refer to the "current" ponent. Like so:

article-card + :host {
    margin-top: 20px;
}

Try using view encapsulation.none so you don't get those extra [_ngcontent-c5]

import {
  Component, ViewEncapsulation
} from '@angular/core';

@Component({
  selector: 'app-something',
  encapsulation: ViewEncapsulation.None
})

Can you add classes, like this:

<article-group class="article-group"
    [topArticlesOnly]="true"
    [articles]="homepage.top | slice:0:6">
</article-group>

<article-card class="article-card"
    *ngFor="let article of homepage.top | slice:0:6"
    [article]="article">
</article-card>

and

.article-card + .article-group {
    margin-top: 20px;
}
发布评论

评论列表(0)

  1. 暂无评论