Clever, but this smells like a performance anti-pattern. Adopting this means you could, at worst, add 4 spacer divs per component. While most sites may never really feel a sting, for complex apps where reusability is a larger concern you've doubled to quadrupled the size of an already large DOM and you will be hit a death-by-one-thousand-cuts situation. Now you've got more... - html over the wire - html to parse for…
Use spacer components instead of CSS margins (2020)
171–180 of 230 posts
Re: Use spacer components instead of CSS margins (2020)
#172Ban layout styling from your components entirely if you can. Make everything headless (eg https://headlessui.dev/ , https://www.radix-ui.com/ , plain old HTML, etc). Leave the way things look up to the app that uses them.
The problem with CSS, and HTML is that content structure, content layout, and its styling is intertwined like spaghetti. A ground up HTML rewrite is needed.
Re: Use spacer components instead of CSS margins (2020)
#173Clever, but this smells like a performance anti-pattern. Adopting this means you could, at worst, add 4 spacer divs per component. While most sites may never really feel a sting, for complex apps where reusability is a larger concern you've doubled to quadrupled the size of an already large DOM and you will be hit a death-by-one-thousand-cuts situation. Now you've got more... - html over the wire - html to parse for…
Can't you just use the spacing properties of the container? Like flexbox's space-around and space- between?
Re: Use spacer components instead of CSS margins (2020)
#174Re: Use spacer components instead of CSS margins (2020)
#175That's very reductionist approach. Sure, margin is a sharp tool and you need to use it responsibly. I avoid putting margins on "first"-level selector. So instead of .button { margin-left: 16px } I do .parent > .button { margin-left: 16px } Difference is that I can reuse '.button' elsewhere without modifications. Another point to consider is that margin is not the only CSS property that affects layout. Both grid, flex…
Is there a easy way to do this with a utility framework like tailwind? It just makes it so tempting to throw the margin on a component.
You can use plain old CSS for layout purposes and mix it with utility classes for appearance.
You can do:
Buy now
Add to bag
And then: .actions {
display: flex;
justify-content: flex-end;
align-items: center;
}
.actions > * + * {
margin-left: 16px; // or better use var from tailwind to set sizes consistently.
@apply: ml-5; // or do this https://tailwindcss.com/docs/reusing-styles#extracting-classes-with-apply
}
This should combine those techniques, but I haven't used it in practice. And I'm sure in this case you will be labeled as heretic by both people who don't use utility frameworks and those who do ;)Re: Use spacer components instead of CSS margins (2020)
#176Earlier quoted context omitted.
> it's funny that the suggested solution (spacers) are basically back to the old 1px transparent gif trick Not really. The Stack component he uses as an example is actually just a thin abstraction over some reusable css. In plain html, it could look like ... ... ... There are no "spacer gifs" anywhere, it's only that you're lifting the margin responsibility to the parent. I may be wrong, but I think the whole stack c…
Stacks started in UIKit (UIStackView) and before that were cribbed from Android, IIRC.
Re: Use spacer components instead of CSS margins (2020)
#177Earlier quoted context omitted.
>Tendency towards semantic web is unfortunately dead. Not dead, but under severe attack. We can and will fightback
Can you elaborate a bit why? I've don't think I've seen a time where most sites were generally semantic
Re: Use spacer components instead of CSS margins (2020)
#178That's very reductionist approach. Sure, margin is a sharp tool and you need to use it responsibly. I avoid putting margins on "first"-level selector. So instead of .button { margin-left: 16px } I do .parent > .button { margin-left: 16px } Difference is that I can reuse '.button' elsewhere without modifications. Another point to consider is that margin is not the only CSS property that affects layout. Both grid, flex…
Is there a easy way to do this with a utility framework like tailwind? It just makes it so tempting to throw the margin on a component.
Re: Use spacer components instead of CSS margins (2020)
#179Re: Use spacer components instead of CSS margins (2020)
#180Hum, just use `box-sizing: border-box` and you solve the problem that the author has without using a complete anti-pattern.