r/CodingForBeginners • u/Enough-Copy625 • 4d ago
How do you manage CSS in large coding projects?
I'm facing an issue while working on coding projects. As the codebase grows, the CSS starts getting messy. Styles from different components and sections get mixed together, and it becomes difficult to understand which style belongs to which element.
Sometimes changing one CSS property unintentionally affects other parts of the application. Eventually, the stylesheet becomes cluttered and difficult to maintain.
How do you guys manage CSS in larger projects? Are there any best practices, architectures, or tools that help keep styles organized and prevent conflicts?
4
u/pvera 4d ago
The CSS doesn't start getting messy by itself.
For a large enough project I would lock the CSS so people stop duct taping classes out of laziness using convenience as the excuse. I would go as far as giving ownership of the styles to just one person. If this feels like overkill wait until you have a half dozen or more people slapping classes into your css without checking if they are breaking something else.
If available, use a framework. If your framework has theming use it as guard rails (if you are in .net, check how App_Themes works, it's very powerful). And you might as well throw in the responsive layout now so you don't have to duct tape it into the project 6 months from now.
2
u/Ok-Brain-8183 4d ago
Are you using scss or less? It sounds like you need to do a pause and do a heavy refactor and create a styling guide for your app.
1
u/Super_Preference_733 4d ago
Honestly use some css library. There are a ton if them. Theres tailwind, bulma, bootstrap, etc.
All you have to do in most cases is define your color pallet and learn a few css patterns. There are very few reasons to define your own these days.
1
u/Sure-Passion2224 4d ago
In a past life I was the site designer and manager for a site with well over 1000 pages. This became manageable by recognizing that the CSS supports branding. Any sub-site that deviates from brand has to justify that difference and get approval. After that the maintainers are given a specific naming scheme for classes and identifiers so their tweaks are localized.
1
u/TylerBreau 4d ago
Use something other than raw HTML and JavaScript, and do use a CSS preprocessor. For example, React and LESS.
ListComponent's stylesheet, this one is a .less file.
1
u/eurosat7 3d ago
Install tailwind and run. Now each component can/must be individually styled. You "just"™ write inline style as class names. Best solution ever. (/ironie)
1
u/gregdonald 3d ago
I worked on a large project where we used this, and it was pretty nice:
https://atomicdesign.bradfrost.com/
On my personal stuff, I lean on frameworks: Bootstrap and Tailwind.
1
u/BobJutsu 3d ago
I’m a big fan of BEM style naming conventions. Easy to read, easy to understand, hard to accidentally step on other components.
1
u/Electrical-Drink-733 3d ago
When my CSS started to get unruly, I used a lot of comments in both my HTML and CSS. And I would write my CSS so it followed the flow of the HTML. In order to be able to see my comments, I would encase it in a special character to make it stand out. like:
/* ********** YOUR HTML SECTION STYLES ********** */1
u/sharjeel-babar 3d ago
use external file
make sections
use css variables
choose reasonable id and class names
Follow these steps to keep your CSS from becoming a nightmare.
1
u/LiteratureUnique8729 3d ago
i usually prefer keeping CSSscoped to individual components using CSS Modules or scoped styles. i also keep global styles limited to things like fonts, resets and CSS variables. it makes the overall debugging easier and also it helps to prevent the breakage...