Exploring Styles in Angular Build Files
When you delve into your build files, you'll discover that the styles of components are compiled within the main.js
file. To locate it, simply navigate to the network tab of your browser's developer tools.
While you may come across a file named styles.css
, bear in mind that this will solely encompass your global styles. This segregation is attributed to Angular's view-encapsulation approach towards styling per component. It’s worth noting that altering the view-encapsulation strategy (Emulated, ShadowDOM, None) may bring about changes in Angular’s behavior.
It's advisable to proceed with caution when considering such modifications.
If desired, you can convert your SASS files to CSS through a command-line tool that can be installed following the instructions provided on the official Sass website. Alternatively, there are online SASS converters like this one that you can utilize.
For those focused solely on global styles, here's a reference on how to switch from SCSS to CSS format in your browser.
Sample Scenario
app.component.scss
p {
background-color: orange;
}
styles.scss
@import 'default';
p {
color: red;
&:hover {
color: blue;
}
}
default.scss
h1 {
color: teal;
}
Generated Output in Build
styles.css:
h1 {
color: teal;
}
p {
color: red;
}
p:hover {
color: blue;
}
main.js:
AppComponent.ɵfac = function AppComponent_Factory(t) { return new (t || AppComponent)(); };
AppComponent.ɵcmp = /*@__PURE__*/ _angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵdefineComponent"]({ type: AppComponent, selectors: [["app-root"]], decls: 1, vars: 0, template: function AppComponent_Template(rf, ctx) { if (rf & 1) {
_angular_core__WEBPACK_IMPORTED_MODULE_0__["ɵɵelement"](0, "lib-my-lib");
} }, directives: [my_lib__WEBPACK_IMPORTED_MODULE_1__.MyLibComponent], styles: ["p[_ngcontent-%COMP%] {\n background-color: orange;\n}\n/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImFwcC5jb21wb25lbnQuc2NzcyIsIi4uXFwuLlxcLi5cXC4uXFxBbmd1bGFyJTIwUHJvamVjdHNcXGxpYi1leGFtcGxlXFxzcmNcXGFwcFxcYXBwLmNvbXBvbmVudC5zY3NzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0VBQ0Usd0JBQUE7QUNDRiIsImZpbGUiOiJhcHAuY29tcG9uZW50LnNjc3MiLCJzb3VyY2VzQ29udGVudCI6WyJwIHtcclxuICBiYWNrZ3JvdW5kLWNvbG9yOiBvcmFuZ2U7XHJcbn1cclxuIiwicCB7XG4gIGJhY2tncm91bmQtY29sb3I6IG9yYW5nZTtcbn0iXX0= */"] });
*Take note of the orange background-color highlighted in the final line.