32
loading...
This website collects cookies to deliver better user experience
@HostBinding
to attach styles to the host element.<h3>{{title}}</h3>
<div class="content">
<ng-content></ng-content>
</div>
import { Component, Input } from '@angular/core';
@Component({
selector: 'app-card',
templateUrl: './card.component.html',
styleUrls: ['./card.component.css']
})
export class CardComponent {
@Input() title = '';
}
:host {
padding: var(--card-padding, 10px 10px);
border: var(--card-border, 1px solid grey);
box-shadow: var(--card-box-shadow, 1px 1px 4px 1px grey);
display: inline-block;
}
h3 {
color: var(--card-title-color, black);
padding: 0;
margin: 0;
font-weight: bold;
font-size: 16px;
}
.content {
margin: 5px 0;
font-size: 12px;
}
--card-padding
, --card-border
, --card-box-shadow
and --card-title-color
. You can also note that we provide fallback values with the syntax: color: var(--card-title-color, black /* <- fallback value */ );
.<app-card [title]="'Card #1'" class="card-1">
<p>This is card #1.</p>
</app-card>
<app-card [title]="'Card #2'" class="card-2">
<p>This is card #2.</p>
</app-card>
app-card {
margin: 3px;
--card-box-shadow: 0 0 4px 0px grey;
}
app-card.card-1 {
--card-title-color: red;
}
app-card.card-2 {
--card-title-color: blue;
}
--card-box-shadow
of the cards should be 0 0 4px 0px grey
. .card-1
gets a red title and .card-2
a blue one.:root
selector.:root {
--card-box-shadow: 0 0 4px 0px grey;
--card-title-color: red;
--card-border: 1px solid grey;
}