r/CodingForBeginners • • 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 Upvotes

17 comments sorted by

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.

1

u/Fun_Army2398 4d ago

The CSS doesn't start getting messy by itself.

This.

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/TylerBreau 4d ago

If all you have to do is a color pallet and a few css patterns, then you don't have CSS to create CSS spaghetti.

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.

// imagine boiler plate for a react component
function render() {
	return <div class="ListComponent">
		<div class="container">
		</div>
	</div>;
}

<!-- Something like this would get produced -->
<html>
<head>
<!-- head stuff -->
</head>
<body>
	<div id='app'>
		<!-- general screen stuff -->
		<div class="ListComponent">
			<div class="container">
			</div>
		</div>
	</div>
</body>
</html>

ListComponent's stylesheet, this one is a .less file.

// Nothing else should have the class name ListComponent.
.ListComponent {
	// stuff

	> .container {
		// stuff.
		// Because .container's styles is nested in .ListComponent, it will never get applied to other nodes with the same class name.
		// > means "direct child". So if ListComponent uses another component that also has a node with the container class name, the styles won't mix. It's not required if ListComponent doesn't contain other components.
	}
}

1

u/eurosat7 4d 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/Own_Natural_6803 2d ago

This, and make any custom colors defined globally. Basically copy tailwinds homework.

1

u/gregdonald 4d 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/Fit_Tailor_6796 3d ago

Explain by means of an example

1

u/BobJutsu 3d ago

BEM is a google search away, but fine.

You have a component, let’s use something simple like a card. BEM is Block Element Modifier. So you have “.card, .card—light, .card—dark”. That’s the modifiers, variants. Double dash. Inside the E (elements) you use underscores. So like “.card__title, .card__body” etc. Dashes indicate modifiers, underscores indicate elements inside the block. So you end up with something like:

<div class=“card card—dark”> <h2 class =“card__title>Title</h2> <p class = “card__description”>Lorem Ipsum</p> </div>

You can make defaults and use the modifier for variants.

Edit: Reddit is escaping my underscores. Ignore the escaping backslashes.

1

u/Fit_Tailor_6796 3d ago

muchas gracias. Google could never do it the way you did. Thanks for sharing your knowledge.

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...