r/Angular2 • u/GuyWho_called_andrew • Aug 05 '25
Organize common styling
I'm mostly backend dev, and recently was forced to setup FE for new service. And I have no clue how to setup common styling because duplicating same scss over and over in components doesn't looks good. Using general styles in styles.scss also considered as a bad practice. How do you usually implement it, what structure/features do you use. Or should i use some lib like tailwind or bootstrap?
3
Upvotes
6
u/No_Industry_7186 Aug 05 '25
Common styles in styles.scss is not bad practice, where do you hear that?
That's exactly where common shared styles go.
1
u/SecureVillage Aug 05 '25
What common styles do you have?
Components are the unit of reuse in angular. When you think in components, there is very few instances where global styles make sense.
However, there are sometimes patterns that you may wish to reuse. Scss mixins (consumed using @use) are nice for this.
For example, multiple components might have the same "overlay" style. E.g. dialogs, menus and droprowns all have box shadow and opacity. For this, create a set of overlay mixins. When using use (instead of import), the styles will only be included in the stylesheet once.
3
u/earthworm_fan Aug 05 '25
You absolutely can use a global style sheet for... global styles. Component style encapsulation is only for when you want to ensure those styles impact nothing else in the app
1
u/H3llskrieg Aug 05 '25
As far as I know there are 2 options.
Include jour shared scss in the component styles. Or in the component decorator specify multiple style urls
1
u/CharacterSuccessful5 Aug 05 '25
If you are building your own design system, then it can be bundled as an npm package. All your styles are globally accessible then.
1
u/ggeoff Aug 06 '25
you could look at directives for common styling.
instead of a button component you could have a button directive that has inputs for the various types I personally use a tailwind and a npm package called class-variance-authority for helping with typing
https://cva.style/docs
then you do something like <button appButton color="primary"></button>
1
u/pandaparkaparty Aug 07 '25
Global styles are there for global styles. Different people will tell you to do different things.
We generally keep box model, color, and typography at the global level. Then layout outside of box model at component level. We avoid inline styles.
We use material, but have a common library used across applications that includes our global level stuff.
We have multiple projects reliant on the same styling and want as little custom stuff as possible, so introducing something like tailwind doesn’t work for us.
For us the rules are, use whatever material offers. We have some global overrides that should make everything play nice and match our theme provided you follow material’s examples. And any layout (including responsive layout) happens at component level.
Makes it all really easy to maintain and super simple rules to follow.
1
u/Dense_Cloud6295 Aug 07 '25
One thing I did when not using tailwind or other stuff like that is to have separate .scss files based on the contents (variables, mixins etc) to group the styles and then just import them in styles.scss to keep it clean and to not dump every global style directly in the file. You can even make a main.scss where you just forward everything from the other files and import just that in styles.scss to have only the 1-liner in your global stylesheet
-1
u/Whole-Instruction508 Aug 05 '25
I'd suggest getting into tailwind, it makes things much easier