They Say: Do Not Use Margins in CSS. How I Use Them Anyway
wolfoliver.medium.com
wolfoliver.medium.com
If you code HTML&CSS with W3C standards, you’ll be happier. Maybe I’m an old timer
The only thing that I tend to use is a reset.css, everything else I write on the fly.
Are they in container.css, in which case container knows of .reusable-component.
Or does reusableComponent.css know about each and every possible container?
Will there be a containerPlusComponentCombo.css that juggles common rules?
The problem remains even if you prefer grouping css rules in one large file over many small files. You end up tying yourself in to knots as either parent or child requires knowledge of the other.
Sometimes it is perfectly valid. But it is not truly reusable. It doesn’t scale well and requires unnecessary mental overhead.
Instead, why not build a layout container that works for everything?
.container > * + * {
margin-too: var(--sp-md);
}
(Use the new-ish gap rule if you like, but I forgot the details and I’m tired)It is just as semantic (whatever that means) as in the article and doesn’t needlessly couple things together.
It was last an issue with IE7 and 8 [0], an era we don’t like to talk about.
0 - https://web.archive.org/web/20130508170505/https://developer...
I’m pretty sure you’re wrong there.
Instead of...
.btn {
margin: 8px;
background: whitesmoke;
}
<button class="btn">Send</button>
Use utility classes in the markup rather than spacing in the reusable definition... .btn {
background: whitesmoke;
}
.m-8 {
margin: 8px;
}
<button class="btn m-8">Send</button>
This approach yields decent readability, fewer elements than if we needed containers wall around, and good flexibility for many contexts and situations. button {
margin: 1em;
}
or if you just want certain buttons .someparent button {
margin: 1em;
}
I tend to use classes only to specify the identity of a thing: so something like .close-button or similar.As to why not scope with .someparent, it can work, but if you already have spacing utility classes ready to go, it's easier to just add the three characters in the class. And as a bonus, when you add another button somewhere else nested way deep under .someparent, it won't accidentally inherit the margin you meant for the first button.
Anyways, I think the answer is "it depends" and there's no silver bullet when it comes to micro CSS details.
CSS is always controversial when it comes to how structure it and I don't think this type conversations are useful or add anything to front end. it's just a waste of time as the end result only matters.
we can argue for days to both sides on where to place margins and we will never find the "perfect" way.
I am a big believer front end developers will move to low code tools for CSS such as https://intab.io to style their CSS in the near future and focus more on the end result rather than waste time arguing on this micro details. take a way, focus on the end result rather than these trivial things!