3

I'm trying to work out what order Angular 2 uses for its Css once its all compiled.

What I'm trying to work out is does it get all of the SCSS files from the individual components and then add them after the main styles.scss file? This would seem to be the obvious thing but does it do it the other way round and put the componet styles first then the main styles?

I know if I specify files in the styles array in the angular-cli.json file Angular will process them in that order but obviously component styles aren't specified there.

Apologies if this sounds a bit odd but I can't work it out and can't seem to find any info on it anywhwere.

2 Answers 2

2

In general, we don't have to ensure a given style order. Component styles (by default) will work independently of other styles in other components and in the order that the styles are processed.

The processing order of the Sass files is not guaranteed in the CLI, there is an order which is the order in which the CLI plugins (Webpack based) process the files but that could change over time as the CLI evolves.

One of the main goals of Angular styling is to provide style encapsulation, meaning that the styles on the SASS file associated to the component are only applicable to that style.

To understand how this works and why in general we don't need to worry about style order, let's take as an example this style in a component SASS file:

.active {
    border-radius:4px;
}

This will be converted at runtime to something like:

.active[_concontent-c1] {
    border-radius:4px;
}

What goes inside the angle brackets is a unique attribute identifier that increases the specificity of the style and ensures its only applicable to elements inside the template of that particular component.

One of the main benefits of this mechanism is that the order on which the component styles are processed is not critical because the unique identifier will ensure they will not interfere.

Note: There are ways to do not have that unique ID applied to the style if needed

Have a look at this video to see more details of this mechanism in action.

Sign up to request clarification or add additional context in comments.

Comments

0

You may need to look at how ViewEncapsulation work and few other things. This might help:

https://scotch.io/tutorials/all-the-ways-to-add-css-to-angular-2-components

1 Comment

Thanks Fahah, I've read that article but it doesn't really say what order things get put together in. I'm just trying to work out in a bog standard Angular cli project if I have some styles in my main style.scss file and some component styles in my component.scss file, what order webpack put them in when it creates the bundle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.