The Next Big Thing in CSS: margin-trim!

  Рет қаралды 17,842

Dmitry Mayorov

Dmitry Mayorov

Күн бұрын

Пікірлер: 69
@dmtrmrv
@dmtrmrv 6 ай бұрын
Hey team! I've been asked a few times why I didn't use the gap property in the demos. I definitely should have covered it in the video, but I'll answer here. Gap is a perfectly valid approach for spacing, but it's not always the best tool for every situation. For instance, when styling article content, you might need different spacing between various elements, like more space before headings. Gap won't work for that because you can't set different values for different elements within the same container without resorting to hacks. I discuss this in more detail in this video: kzbin.info/www/bejne/pWOaZKWamJmsn5Y.
@MrJettann
@MrJettann 6 ай бұрын
You can use gap with margins to achieve different spacing between elements, for example, if you have 5 fields and 2 or 3 spaces between them are equal, use gap and add extra margin to field with different spacing
@scnix
@scnix 6 ай бұрын
If you need different custom spaces between elements, just use gap and that custom space for that 1 element that needs it?
@JoeSnellPDX
@JoeSnellPDX 5 ай бұрын
Gap is perfectly reasonable under certain contexts. However, I find with using margin (now especially with margin-trim) I have more control and resilience on vertical rhythm that I want to be content relative with a single property. Again, it depends more on context and the problem I'm trying to solve. For me, I tend to lean in preference on using margin over gap. I love these videos and the corresponding discussions. Great stuff!
@jackwright517
@jackwright517 5 ай бұрын
Gap to me also doesn't feel like the most semantic way to handle it either. I see gap as a layout mechanism, but margin is for flow. Gap always requires a parent be set to grid or flex, but margin doesn't. Gap feels like a cheap win at first, but it's not really as sustainable!
@dmtrmrv
@dmtrmrv 5 ай бұрын
@@jackwright517 Yeah, gap is great for grids. Vertical rhythm is much better achieved with margins, couldn't agree more!
@greendsnow
@greendsnow 6 ай бұрын
Use gap or space instead ?
@dmtrmrv
@dmtrmrv 6 ай бұрын
Yes, gap is a perfectly valid option, but for cases when you can't use it (see pinned comment), margin trim will be a game changer.
@Jan-yn4tl
@Jan-yn4tl 5 ай бұрын
Avoid margin top and adjust that padding of the parent (keep this logic for all things). The small padding bottom is useful in many other cases too, plus the margin-top is bad approach in general - thats why most browser have larger “p” margin bottom at least larger if not only on bottom. h tags as well. It is also mentally closer to the paragraph expectation. Enter .. space below not on top of paragraph etc.
@soumiil
@soumiil 6 ай бұрын
You're the one who improved my CSS writing skills, literally feeling that finding your channel was the luckiest moment for me. Love from India bro 🇮🇳❤
@dmtrmrv
@dmtrmrv 6 ай бұрын
Thank you Soumil, so happy to hear that!
@seankennedy7970
@seankennedy7970 6 ай бұрын
Literally been dealing with this at work this week. This will make styling h# tags so much simpler once we can use it.
@dmtrmrv
@dmtrmrv 6 ай бұрын
Yeah, gonna be a game-changer!
@tropheecommerce
@tropheecommerce 6 ай бұрын
Also possible to control the specificity using :where pseudo-selector, but this new property is really nice, thanks for the share
@scream_loud
@scream_loud 6 ай бұрын
I semi-like new features in CSS. The reason why is every new feature require users to update their browsers. "Oh, there's a new cool feature with margins!" ends up me writing backwards compatibility code, 'cause that "new feature" covers X% of modern browsers, so there's even more code in my CSS file. I'd love to see a simple, yet - imo - powerful solution like jQuery has. One library that will cover most, if not every, feature without updating browsers, reseting selectors, so they'll have same values in every browser etc. One library that will tell browser how to render things. It won't be super required, since many browsers already have built-in features like "grid", "gap", etc., but things like "margin-trim", "container queries", "viewport units" etc. and many, many more will actually work even on older browsers, up to IE11 (same as jQuery, i believe).
@Zmeu213
@Zmeu213 6 ай бұрын
What's your problem? .content-flow { display: flex; gap: 2rem }
@tanis6371
@tanis6371 6 ай бұрын
and flex-direction:column;
@dmtrmrv
@dmtrmrv 6 ай бұрын
Gap also works, but it's not the best option for some cases (see pinned comment). For those, margin-trim will make it easier to style the component.
@KOBE42__
@KOBE42__ 6 ай бұрын
@@tanis6371or just use display:grid - grid by default is a column
@lvekua
@lvekua 6 ай бұрын
Thank you Dimitry! Really like your content. keep up the good work! 🙌
@dmtrmrv
@dmtrmrv 6 ай бұрын
My pleasure! Thank you for the comment!
@jubinka
@jubinka 5 ай бұрын
I remembered to use margin-trim today but it didn't work, so I came back to your video to see if I'd done something wrong only to find out that it was an experimental feature 🥲 tysm for covering it and hope to see more tips and tricks soon!
@dmtrmrv
@dmtrmrv 5 ай бұрын
Yeah, we have to wait for this one. Hope we'll see more support soon.
@VeitLehmann
@VeitLehmann 6 ай бұрын
Yay, this is neat! Thanks for sharing!
@dmtrmrv
@dmtrmrv 6 ай бұрын
You bet!
@Dorchwoods
@Dorchwoods 6 ай бұрын
Any particular reason why you wouldn't just use flex or grid with the gap property to achieve this same thing?
@dmtrmrv
@dmtrmrv 6 ай бұрын
Hey @Dorchwoods, thanks for the question! Using gap is a valid approach, but sometimes it's just not enough. Imagine creating a vertical rhythm inside an article component; you might want different spacing between elements within the same container. I talk more about gap vs. margin in a previous video. Check it out starting at 7:28. Let me know if this answers your question!
@X6millionways2di
@X6millionways2di 6 ай бұрын
Because it wont work with Safari If your using Flex grid gap
@Dorchwoods
@Dorchwoods 6 ай бұрын
@@X6millionways2di Hmm it seems to work fine for me. I even built a utility web component to make it even easier, ie: I may look into adjusting it to use margin-trim eventually
@CaiN805
@CaiN805 5 ай бұрын
Привет, классный контент, рад видеть коллегу из 74 региона)
@asdasdasd21314131
@asdasdasd21314131 6 ай бұрын
Is there a reason to go for this approach instead of using flex with gap?
@dmtrmrv
@dmtrmrv 6 ай бұрын
@asdasdasd21314131 Yeah, you'd go for this approach if you need to have different space between the elements in the container. I talk more about it in this video: kzbin.info/www/bejne/pWOaZKWamJmsn5Y
@znkdev
@znkdev 6 ай бұрын
sorry my ignorance, why not using flex col gap?
@dmtrmrv
@dmtrmrv 6 ай бұрын
Good question! Gap works perfectly fine for some layouts but not for others. Check out this pinned comment and also this video: kzbin.info/www/bejne/pWOaZKWamJmsn5Y. I go into more detail about the differences between margin and gap there.
@nonamenosurname666
@nonamenosurname666 6 ай бұрын
Unfortunately margin-trim is only supported on Safari browser and that's all... So what is a purpose to use this css property when websites and web application should work well with all browsers or at least with the most popular ?
@dmtrmrv
@dmtrmrv 6 ай бұрын
We should wait to use this one; that's why I started the video with the current approaches. It'll be great to use when the support is more widespread, though.
@ItsEricPhung
@ItsEricPhung 6 ай бұрын
I'd use Flex Gap or Grid Gap all day.
@codefinity
@codefinity 5 ай бұрын
Safari the first to adopt? 🤩
@dmtrmrv
@dmtrmrv 5 ай бұрын
Yeah, quite unusual
@patrykkowalski8355
@patrykkowalski8355 6 ай бұрын
what about support before i will say how cool is that :) same problem with all nice stuff, as i see on can i use, it 13.97% so almost nothing is supported, weird
@dmtrmrv
@dmtrmrv 6 ай бұрын
Yeah, for this one we'll have to wait until it's supported.
@enenotowitch628
@enenotowitch628 5 ай бұрын
flex col + gap?
@dmtrmrv
@dmtrmrv 5 ай бұрын
Gap is a valid option for some layouts, not a replacement for margins though. See pinned comment.
@ico0z
@ico0z 6 ай бұрын
p+p and h1 margin-block-end... cant see the benefit there
@groovebird812
@groovebird812 6 ай бұрын
There are so many solutions for this case. Why is this the next big thing?
@dmtrmrv
@dmtrmrv 15 күн бұрын
It is a big improvement for components that handle spacing of inner components. Makes it much easier to read and understand the CSS.
@bozzhik
@bozzhik 6 ай бұрын
use tailwind class space-x-[ ] or-space-y-[ ]
@dmtrmrv
@dmtrmrv 6 ай бұрын
Under the hood those classes also margins between child elements, very similar to the > * + * selector.
@bozzhik
@bozzhik 6 ай бұрын
@@dmtrmrv yeah. I found this very interesting
@jenssvanfelt
@jenssvanfelt 6 ай бұрын
div > *:nth-child(n+2) { margin-top: 1rem; }
@dmtrmrv
@dmtrmrv 6 ай бұрын
Yep, there are multiple ways to do it.
@ukyo6195
@ukyo6195 6 ай бұрын
Why don’t use margin-bottom? 2rem And the last one no one cares. Or last-child: 0
@Twajn13
@Twajn13 5 ай бұрын
Just use flex and gaps and you ll be happy man
@dmtrmrv
@dmtrmrv 5 ай бұрын
Happiness is the journey, not just margins, flex, and gaps. 😉
@ginozem9569
@ginozem9569 5 ай бұрын
Lol supported by safari first is a terrible idea. Safari and firefox are always behind on useful css changes.
@dmtrmrv
@dmtrmrv 15 күн бұрын
Not in this case I guess, Safari jumped ahead!
@CristianKirk
@CristianKirk 6 ай бұрын
Regardless of this becoming standard or not (most likely not), it's far from being a "big thing."
@dmtrmrv
@dmtrmrv 6 ай бұрын
Thanks for the comment. Big or small, it can depend on one's perspective.
@Fanmade1b
@Fanmade1b 6 ай бұрын
@@dmtrmrv that's what she said! (sorry, couldn't stop myself ^^) Very good video. I like your approaches, even though I probably would just have added a margin-bottom to these elements instead :) One recommendation though: You could include the caniuse link in the video description so that people can easily check at any time in the future whether the support for the property has improved.
@dmtrmrv
@dmtrmrv 6 ай бұрын
Thank you for the suggestion, added the link!
@jkcrawl
@jkcrawl 6 ай бұрын
Call me dumb, I'm a noob so I'm not saying this to be a smart ass so feel free to correct me if I'm wrong: why not just put a margin-bottom on the paragraphs in that div? div p{ margin-bottom: 1rem; }
@robinheyer708
@robinheyer708 6 ай бұрын
You've got the right idea but the content-flow class is written as a utility class so it can be applied anywhere. So in an article like he showed us. But you could use it in a card as well and then you could have very different tags like h3, img, p, all getting a standard spacing.
@dmtrmrv
@dmtrmrv 6 ай бұрын
No dumb questions on this channel! I second what @robinheyer708 said-the approach with the "all" selector works really well in cases of vertical rhythm where you need to add space between any elements and then tweak it for selected ones. Just adding margin-bottom, like in your example, will also add it to the last element, which could be unwanted.
@scnix
@scnix 6 ай бұрын
Meh, I'd give this upcoming property a 5/10. Nice to have but not very useful.
@dmtrmrv
@dmtrmrv 6 ай бұрын
Thanks for the comment, why do you think it's not useful?
@scnix
@scnix 6 ай бұрын
@dmtrmrv mainly because the use case for it highly specific. The only reason why someone uses it is to override the 1st child without pseudo classes, but the only reason why you need to do that in the 1st place is to have a class/style that's repeating. If you need something that's repeating, use gap, which eliminates the need for margin-trim. If you need a special case in the middle of the repeating style, then just add another class to override the repeating style where needed.
Learn CSS BEM (and avoid these common mistakes)
15:36
Dmitry Mayorov
Рет қаралды 8 М.
CSS Tip: Extend a Child Beyond Parent Width (the better way)
12:29
Dmitry Mayorov
Рет қаралды 1,1 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 10 МЛН
The CSS Display Property is Changing Forever
15:20
Web Dev Simplified
Рет қаралды 47 М.
The Story of shadcn/ui
8:28
CodeSource
Рет қаралды 38 М.
CSS Anchor Is The Best New CSS Feature Since Flexbox
15:39
Web Dev Simplified
Рет қаралды 377 М.
CSS Subgrid Layouts Are Here!
9:09
Dmitry Mayorov
Рет қаралды 8 М.
Tailwind vs Vanilla CSS - should you reconsider?
14:25
Maximilian Schwarzmüller
Рет қаралды 12 М.
Master CSS Spacing: Margin, Padding, and Gap Explained
17:18
Dmitry Mayorov
Рет қаралды 1 М.
Learn This Flexbox Item Alignment Trick!
7:18
Dmitry Mayorov
Рет қаралды 12 М.
Using CSS custom properties like this is a waste
16:12
Kevin Powell
Рет қаралды 182 М.
小丑女COCO的审判。#天使 #小丑 #超人不会飞
00:53
超人不会飞
Рет қаралды 10 МЛН