[英]Apply component style to its last occurrence in a page
I have a custom component and it's formatting itself using the accompanying style file. 我有一个自定义组件,它使用随附的样式文件对自身进行格式化。 In the containing component, I generate a series of the custom ones. 在包含组件中,我生成了一系列自定义组件。 I'd like the last element to have a slightly different style but I'd prefer to control it from within the component itself. 我希望最后一个元素的样式稍有不同,但我更希望从组件内部进行控制。 Is it possible at all? 有可能吗?
I asked this question earlier but got answers based on direct application of CSS while I'm looking for an Angular component encapsulating the style. 我之前曾问过这个问题,但是在寻找封装样式的Angular组件时,我得到了基于CSS直接应用的答案。
The closest I got was applying :host(:last-child)
but didn't really got it working. 我得到的最接近的是应用:host(:last-child)
但并没有真正起作用。
div.lower{ border-top: 1px solid gold; ... }
div.lower:host(:last-child){ border-top: 1px solid olive; ... }
I prepared a Blitzy to laborate with. 我准备了一起闪电战。 The aim is to make the last element not to have a golden divider in the middle. 目的是使最后一个元素在中间不具有金色分隔线。
Since the div
is a descendant of the component host element, you can use the following selectors: 由于div
是组件宿主元素的后代,因此可以使用以下选择器:
:host(:last-child) div.lower // Applies the style on the last component instance
:host:not(:last-child) div.lower // Applies the style except on the last component instance
To avoid the golden separator on the last component instance, try these styles: 为避免最后一个组件实例出现金色分隔符,请尝试以下样式:
div.lower {
display: flex;
justify-content: center;
min-width: 300px;
}
:host:not(:last-child) div.lower {
border-top: 1px solid gold;
}
See this stackblitz for a demo. 有关演示,请参见此堆叠闪电战 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.