STYLING RECIPES
Nir Kaufman
FOR ANGULAR COMPONENTS
Nir Kaufman
Google Developer Expert
Frontend Consultant at 500Tech
Worldwide speaker & trainer
Community leader
@nirkaufman
MULTIPLE CHOICES
STYLES
& styleUrls
BEMhttp://getbem.com/introduction/
STYLEcss, scss, less, sass
@Component({
selector : 'app-root',
styleUrls: [`./app.component.css`],
template : `
<h1>Styled Components</h1>
`
})
external styles
@Component({
selector : 'app-root',
styleUrls: [`./app.component.css`],
template : `
<h1>Styled Components</h1>
`
})
external styles
@Component({
selector : 'app-root',
styles : [`h1 { color: red }`],
template : `
<h1>Styled Components</h1>
`
})
inline styles
@Component({
selector : 'app-root',
styles : [`h1 { color: red }`],
template : `
<h1>Styled Components</h1>
`
})
inline styles
@Component({
selector : 'app-root',
styleUrls: [`./app.component.css`],
styles : [`h1 { color: red }`],
template : `
<h1>Styled Components</h1>
`
})
both inline & external
@Component({
selector : 'app-root',
styleUrls: [`./app.component.css`],
styles : [`h1 { color: red }`],
template : `
<h1>Styled Components</h1>
`
})
both inline & external
ViewEncapsulation.Native
native components
CLASSES
& NgClass
@Component({
selector: 'app-root',
template: `
<h1 [class]="className">Styles Components!</h1>
`
})
export class AppComponent {
public className: string;
}
bind to class
@Component({
selector: 'app-root',
template: `
<h1 [class]="className">Styles Components!</h1>
`
})
export class AppComponent {
public className: string;
}
bind to class
@Component({
selector: 'app-root',
template: `
<h1 [class.text-danger]="flag">Styles Components!</h1>
`
})
export class AppComponent {
public flag: boolean;
}
bind to specific class
@Component({
selector: 'app-root',
template: `
<h1 [class.text-danger]="flag">Styles Components!</h1>
`
})
export class AppComponent {
public flag: boolean;
}
bind to specific class
@Component({
selector: 'app-root',
template: `
<h1 [ngClass]="className">Styles Components!</h1>
`
})
export class AppComponent {
public className: string;
}
ngClass: class name
@Component({
selector: 'app-root',
template: `
<h1 [ngClass]="className">Styles Components!</h1>
`
})
export class AppComponent {
public className: string;
}
ngClass: class name
@Component({
selector: 'app-root',
template: `
<h1 [ngClass]="classNames">Styles Components!</h1>
`
})
export class AppComponent {
public classNames: string[];
constructor() {
this.classNames = ['text-danger', 'display-1'];
}
} ngClass: array
@Component({
selector: 'app-root',
template: `
<h1 [ngClass]="classNames">Styles Components!</h1>
`
})
export class AppComponent {
public classNames: string[];
constructor() {
this.classNames = ['text-danger', 'display-1'];
}
} ngClass: array
@Component({
selector: 'app-root',
template: `
<h1 [ngClass]="getClasses()">Styles Components!</h1>
`
})
export class AppComponent {
public getClasses() {
return {
'text-danger': true,
‘display-1’ : true,
};
}
} ngClass: Object
@Component({
selector: 'app-root',
template: `
<h1 [ngClass]="getClasses()">Styles Components!</h1>
`
})
export class AppComponent {
public getClasses() {
return {
'text-danger': true,
‘display-1’ : true,
};
}
} ngClass: Object
STYLE
& NgStyle
@Component({
selector: 'app-root',
template: `
<h1 [style.color]=“backgroundColor">Title</h1>
`
})
export class AppComponent {
public color: string;
}
style: string
@Component({
selector: 'app-root',
template: `
<h1 [style.color]=“backgroundColor">Title</h1>
`
})
export class AppComponent {
public color: string;
}
style: string
@Component({
selector: 'app-root',
template: `
<h1 [ngStyle]="styles">Title</h1>
`
})
export class AppComponent {
public styles = {
color: 'red',
textDecoration: 'underline'
};
} ngStyle: object
@Component({
selector: 'app-root',
template: `
<h1 [ngStyle]="styles">Title</h1>
`
})
export class AppComponent {
public styles = {
color: 'red',
textDecoration: 'underline'
};
} ngStyle: object
@Component({
selector: 'app-root',
template: `
<h1 [ngStyle]="styles">Title</h1>
`
})
export class AppComponent {
public styles = {
color: 'red',
textDecoration: 'underline',
'fontSize.px' : 22
};
} ngStyle: object
@Component({
selector: 'app-root',
template: `
<h1 [ngStyle]="styles">Title</h1>
`
})
export class AppComponent {
public styles = {
color: 'red',
textDecoration: 'underline',
'fontSize.px' : 22
};
} ngStyle: object
HOSTS
& children
@Component({
selector: ‘app-root',
styles: [` :host { color: blue } `],
template: `
<h1>Title</h1>
`
})
export class AppComponent {
public color: string;
} :host style
@Component({
selector: ‘app-root',
styles: [` :host { color: blue } `],
template: `
<h1>Title</h1>
`
})
export class AppComponent {
public color: string;
} :host style
@Component({
selector: ‘app-root',
styles: [` :host(.sky) { color: blue } `],
template: `
<h1>Title</h1>
`
})
export class AppComponent {
public color: string;
} :host class
@Component({
selector: ‘app-root',
styles: [` :host(.sky) { color: blue } `],
template: `
<h1>Title</h1>
`
})
export class AppComponent {
public color: string;
} :host class
@Component({
selector: 'app-root',
styles: [`:host(.green) {color: green}`],
template: `
<h1>Component Styling Recipes</h1>
<input type="checkbox"
(change)="flag = $event.target.checked">
`,
})
export class AppComponent {
@HostBinding('class.green') flag = false;
} @HostBinding class
@Component({
selector: 'app-root',
styles: [`:host(.green) {color: green}`],
template: `
<h1>Component Styling Recipes</h1>
<input type="checkbox"
(change)="flag = $event.target.checked">
`,
})
export class AppComponent {
@HostBinding('class.green') flag = false;
} @HostBinding class
@Component({
selector: 'app-root',
styles: [`::ng-deep h2 {color: green}`],
template: `
<h1>Component Styling Recipes</h1>
<app-child></app-child>
`,
})
export class AppComponent {
public title: string;
} @HostBinding class
@Component({
selector: 'app-root',
styles: [`::ng-deep h2 {color: green}`],
template: `
<h1>Component Styling Recipes</h1>
<app-child></app-child>
`,
})
export class AppComponent {
public title: string;
} @HostBinding class
BINDING
TO STYLE
@Component({
selector: 'app-root',
template: `
<h1 [style]="style">Component Styling Recipes</h1>
`,
})
export class AppComponent {
public style =`
background-color: red
`;
}
bind to style
@Component({
selector: 'app-root',
template: `
<h1 [style]="style">Component Styling Recipes</h1>
`,
})
export class AppComponent {
constructor(private sanitizer: DomSanitizer) {}
public style = this.sanitizer.bypassSecurityTrustStyle(`
background-color: red
`);
}
bind to style
NEXT STEPS
ANGULAR SWAT
advanced Angular workshops.
@nirkaufman
THANK YOU!
ngnir.life

Styling recipes for Angular components