New Low-key Style Features in Angular šŸ˜®

December 01, 2023 | 5 Minute Read

In Angular 17 we have a couple of new ways to include styles within our components. In this post weā€™ll take a close look at these new features, and weā€™ll learn how to use them. Alright, letā€™s get to it!

You may have noticed this in the past and thought it was odd, or you may have never given it any thought, but when using the styles property in component metadata, we needed to provide an array of style strings.

styles: [
    `.styles-1 {
        color: red;
    }`,
    `.styles-2 {
        color: blue;
    }`
]

Iā€™m willing to bet that almost all of those of you whoā€™ve used this property never added more than one string. I personally canā€™t think of a good reason to do this.

Adding Styles to a Component as an Array of Strings in Metadata With the styles Property

Letā€™s look at an example so that youā€™re clear on what Iā€™m talking about. Here, in this componentā€™s metadata, Iā€™m going to add the styles property. It accepts an array of strings, so Iā€™ll add that. Then, in this string, Iā€™ll add some styles for the host. And, Iā€™ll increase the font size for the h1.

@Component({
    selector: 'app-root',
    template: `
        <h1>New Angular Component Style Features</h1>
    `,
    styles: [`
        :host {
            background-color: #151515;
            display: grid;
            height: 100%;
            place-items: center;
            text-align: center;
            color: #ff495d;
        }

        h1 {
            font-size: 300%;
        }
    `]
})
export class App {
}

See what I mean? Thereā€™s really no reason to add another string to this array as far as I can tell. Iā€™m sure thereā€™s some use cases out there but much of the time itā€™s probably not needed.

New Feature: Adding Styles to a Component as a Single String in Metadata With the styles Property

Well now, as of Angular version 17, this property will accept both a single string or an array of strings. So, we can simply remove the square brackets in this case. Weā€™re no longer required to provide an array.

@Component({
    selector: 'app-root',
    template: `
        <h1>New Angular Component Style Features</h1>
    `,
    styles: `
        :host {
            background-color: #151515;
            display: grid;
            height: 100%;
            place-items: center;
            text-align: center;
            color: #ff495d;
        }

        h1 {
            font-size: 300%;
        }
    `
})
export class App {
}

So, not a huge deal but definitely more straight forward than the old way. Good to know it can be done this way now.

Converting Styles Metadata to an Externally Referenced Stylesheet

Ok, along these lines, thereā€™s a new feature for including an external stylesheet too. Up until Angular version 17, for including external stylesheets, weā€™d use the styleUrls property which required an array of stylesheet file path strings, but most of the time you probably only needed to include a single stylesheet. Letā€™s look at an example.

Weā€™ll add a new stylesheet file, weā€™ll name it ā€œapp.component.cssā€. Now letā€™s move our styles to this stylesheet. And letā€™s change the background color and font color to make this change more obvious.

app.component.css

:host {
  background-color: #4e368b;
  display: grid;
  height: 100%;
  place-items: center;
  text-align: center;
  color: white;
}

h1 {
  font-size: 300%;
}

Adding Multiple Stylesheets to a Component as an Array of Strings in Metadata With the styleUrls Property

Back in the component metadata we used to need to add these with the styleUrls property. It requires an array of strings, so we add square brackets, and then a string with the path to our stylesheet.

@Component({
    selector: 'app-root',
    template: `
        <h1>New Angular Component Style Features</h1>
    `,
    styleUrls: ['./app.component.css']
})
export class App {
}

Ok, so now our style sheet is properly included but, in this case and in most other cases too, we only need to include a single stylesheet.

New Feature: Adding a Single Stylesheet to a Component as a Single String in Metadata With the New styleUrl Property

Well, we now have the styleUrl property. Thatā€™s styleUrl singular as opposed to the existing styleUrls plural property. This property only accepts a single string.

@Component({
    selector: 'app-root',
    template: `
        <h1>New Angular Component Style Features</h1>
    `,
    styleUrl: './app.component.css'
})
export class App {
}

So again, nothing major, but just a little bit more straight forward for most use cases. Something new to be aware of, but everything still exists as it did previously.

You can still provide an array of styles to the styles metadata property and the styleUrls property can still be used with an array of stylesheets so you wonā€™t need to change anything if you donā€™t feel itā€™s necessary but you can if you like the new way better.

Itā€™s completely up to you.

Want to See It in Action?

Check out the demo code and examples of these techniques in the in the stackblitz example below. If you have any questions or thoughts, donā€™t hesitate to leave a comment.

Found any of this Stuff Helpful?

If you found any this helpful and want to show some love, you can always buy me a coffee!