I have the style border-bottom: 1px solid pink
set on the outer-most div
in HTML for a component. The component is used like rows creating the illusion of a table. I'd like the last one in a series of them to have a different style (i.e. make the border invisible).
I was hoping to use :last-child
but noticed that every element got the style because within the scope of a component, there's only one outer-most div. I found this solution based on last
value in ngFor
but that's no good in my case because the rows may be a part of an array but also added one-by-one after an array that's ngfor'ed.
What I need is a facility to mark the last occurrence of a component on the page and set a style accordingly but do it from within the component itself. Is it possible at all?
Below is a snippet based on the answer below. The second object doesn't get the extra style on the last element while the first one does (by being assigned class last
).
div.last {
background-color: yellow;
}
div:host(:last-child) {
background-color: yellow;
}
div {
padding: 5px;
margin: 5px;
border: 2px solid black;
max-width: 300px;
display: flex;
flex: 1;
justify-content: center;
}
div.outer {
border-color: purple;
}
div.middle {
border-color: olive;
}
div.inner {
border-color: orange;
}
div.multi {
border-color: pink
}
div.last {
background-color: yellow;
}
div:host(:last-child) {
background-color: yellow;
}
<div class="outer">
<div class="middle">
<div class="inner">
<div class="multi">A</div>
<div class="multi">B</div>
<div class="multi last">C</div>
</div>
</div>
</div>
<div class="outer">
<div class="middle">
<div class="inner">
<div class="multi">A</div>
<div class="multi">B</div>
<div class="multi">C</div>
</div>
</div>
</div>