Learn CSS BEM (and avoid these common mistakes)

  Рет қаралды 3,922

Dmitry Mayorov

Dmitry Mayorov

Күн бұрын

In this video, we're diving into BEM, which stands for Block Element Modifier-a popular semantic CSS methodology. We'll break down the basics of BEM, including blocks, elements, and modifiers, and show you how they all work together. Plus, we'll cover some of the common questions people have and highlight mistakes to avoid.
CodePen: codepen.io/dmtrmrv/pen/vYMEGpx
Chapters:
00:00 - Intro
00:14 - What are CSS Methodologies?
00:49 - What is BEM?
01:13 - What is a Block?
01:59 - How to Name Blocks
02:37 - Semantic vs. Functional Blocks
03:00 - What is an Element?
04:14 - CSS Implementation of Blocks and Elements
04:44 - What is a Modifier?
05:49 - Can I Use a Modifier with an Element?
06:33 - Should I Use a Modifier or Create a New Block?
08:20 - Can a Block Be an Element at the Same Time?
08:59 - Can a Block Be Another Block at the Same Time?
09:50 - Can I Use Utility Classes with BEM?
11:03 - Don't Use Margins on Blocks
12:41 - Don't Nest Classes in CSS
13:23 - Don't Use BEM Entities as Utility Classes
13:53 - Don't Create Elements of Elements
14:22 - Don't Create Blocks with Too Many or Too Few Elements
15:11 - Wrapping Up and Thank You for 100 Subscribers!

Пікірлер: 48
@bls512
@bls512 2 күн бұрын
Extremely helpful, especially your code pen. I'm curious how you approach a larger project using Utopia. Huge thanks!
@KOBE42__
@KOBE42__ Ай бұрын
For a new channel, this video was so well done and I’ve saved it for future reference. A lot to take in but I feel like anything, once you do a couple of projects it becomes second nature. Thanks
@dmtrmrv
@dmtrmrv Ай бұрын
Thank you! Yeah, once it "clicks" you don't have to think about it!
@valery3149
@valery3149 16 күн бұрын
Oh my god! I'm looking for this video for a while ! Very very interesting and well explained! I thank you a lot!
@dmtrmrv
@dmtrmrv 16 күн бұрын
Thank you! Glad it was helpful!
@AJ-vy4yu
@AJ-vy4yu Ай бұрын
This deserves a million view. I had used BEM the wrong way.
@dmtrmrv
@dmtrmrv Ай бұрын
Thank you @AJ-vy4yu! Curious what did you do differently?
@AJ-vy4yu
@AJ-vy4yu Ай бұрын
@@dmtrmrv I created elements of elements and blocks with too many elements, to name a few. I had really long-ass class names😆
@jclewis33
@jclewis33 Ай бұрын
Great video. Very helpful!
@dmtrmrv
@dmtrmrv Ай бұрын
Thank you. Glad it was helpful!
@VdLifts
@VdLifts Ай бұрын
Great video. Keep it up!
@dmtrmrv
@dmtrmrv Ай бұрын
Thank you, glad it was helpful!
@Denis091294
@Denis091294 Ай бұрын
Great advices for working with BEM. Thank you!
@dmtrmrv
@dmtrmrv Ай бұрын
Thank you for the kind words, happy that the video was helpful!
@seankennedy7970
@seankennedy7970 2 ай бұрын
Great video. Keep it up, I've been a frontend dev for 15 years and have flirted with BEM in work projects but never fully committed - this video has inspried me on my next project
@dmtrmrv
@dmtrmrv 2 ай бұрын
Happy to hear that! Yeah, BEM can be a bit tricky and definitely has its downsides (might be a good topic for another video). But I'm still picking it for every new project.
@EvilArtifact
@EvilArtifact Ай бұрын
Thanks for the video. I’ve been using BEM since 2015 and love it, but videos like this help to get my mind back to the fundamentals that keep my code clean.
@dmtrmrv
@dmtrmrv Ай бұрын
You are welcome! Glad you found it helpful. I agree, fundamentals are super important!
@web-atelier
@web-atelier Ай бұрын
Excellent, thanks 👌
@dmtrmrv
@dmtrmrv Ай бұрын
You are welcome!
@rogerpence
@rogerpence Ай бұрын
A very good explanation. I am a big fan of CSS nesting and this is the first BEM video I've seen that rationally compares the two. I really like your component-based approach (both to CSS and your video topics) and am gonna work a little hard a BEM in my next project.
@dmtrmrv
@dmtrmrv Ай бұрын
I'm glad you liked the explanation! I believe there's nothing inherently wrong with nesting; in fact, some methodologies even encourage it. Based on my experience though, both reading and writing styles become easier when nesting is limited and done purposefully and specificity is maintained at the lowest possible level. Feel free to reach out if you have further questions!
@pareshkamaliya2493
@pareshkamaliya2493 Ай бұрын
Wow today I learned something new. Could you create a beginner-friendly CSS playlist, if possible?
@dmtrmrv
@dmtrmrv Ай бұрын
Hey Paresh, thank you! Are there any specific aspects of CSS you find challenging to grasp? I'm developing a content plan for the upcoming months, and your feedback would be really helpful.
@pareshkamaliya2493
@pareshkamaliya2493 Ай бұрын
@@dmtrmrv There are good tutorials available on KZbin, but it's important to understand how much CSS is sufficient and how it's applied in the industry. Essentially, create something that is beginner-friendly yet meets industry-level standards, which puts us a step ahead of others.
@dmtrmrv
@dmtrmrv Ай бұрын
@@pareshkamaliya2493 Thank you for these ideas. I'll def address this in future videos.
@JuanCastro-C
@JuanCastro-C Ай бұрын
Hi Dmitry, I like the way you explain, great content, thank you. New subscriber here!
@dmtrmrv
@dmtrmrv Ай бұрын
Hey Juan, thank for the kind words! Welcome aboard!
@CohenNikesh
@CohenNikesh 2 ай бұрын
awesome vid!
@dmtrmrv
@dmtrmrv 2 ай бұрын
Glad you enjoyed it
@as.abdulazeem
@as.abdulazeem 2 ай бұрын
Thanks, awesome.
@dmtrmrv
@dmtrmrv 2 ай бұрын
You are welcome! Thank you for asking about BEM in the first place!
@hudinigosh7862
@hudinigosh7862 Ай бұрын
Thanks for making css video, i really want to learn css. you explain so well please make a css playlist.
@dmtrmrv
@dmtrmrv Ай бұрын
Thank you for the kind words! Are there any specific topics or concepts within CSS you'd like to see covered?
@moeinmnia
@moeinmnia Ай бұрын
💯
@vertigoz
@vertigoz 5 сағат бұрын
Nada como CSS do BEM
@anneyo5627
@anneyo5627 Ай бұрын
Never use margins on blocks...but whats then the best practice to create space?
@dmtrmrv
@dmtrmrv Ай бұрын
@anneyo5627 That's a great question. Ideally the spacing is created with properties like "gap" or by using the "lobotomized owl" technique. Check out the CodePen: codepen.io/dmtrmrv/pen/vYMEGpx?editors=1100 You can find examples of both: .grid { gap: var(--step-1); } .content-flow > * + * { margin-block-start: var(--step-1); } If that's not enough, we can always use margins on elements, because they aren't reused on their own. ".button" in the same CodePen is a good example. Look at the ".button__icon" element: .button__icon { margin-inline-start: var(--step--5); } All this allows us to reuse components in different contexts without any spacing overrides. Let me know if this answers the question!
@anneyo5627
@anneyo5627 Ай бұрын
Thank you so much! This was helpfull. First time i've heard of the 'lobotomized owl', interesting!
@dmtrmrv
@dmtrmrv Ай бұрын
@@anneyo5627 You are welcome! You can read more about it here: alistapart.com/article/axiomatic-css-and-lobotomized-owls/ The article is 10 years old, but it's still relevant. "gap" was not around at the time, but it's still not a full replacement.
@vincentjacquet2927
@vincentjacquet2927 Ай бұрын
Nice video. While I think BEM is still useful as methodology, I wonder if its implementation, i.e. the naming convention to keep the specificity at (0,1,0), is still relevant considering that we now have :where() to set the specificity to (0,0,0). What do you think?
@dmtrmrv
@dmtrmrv Ай бұрын
That's a great question. I tend to use :where() for setting up the default, most common look of elements. And I actually find it quite handy that blocks and elements get this slight increase in specificity-it helps them stand out against the defaults. Sort of sit on top. I'm curious to know how you'd use :where() with/instead of BEM.
@vincentjacquet2927
@vincentjacquet2927 Ай бұрын
​@@dmtrmrvFirst a little disclaimer: I am not a designer, I mainly work on web applications and the styling takes less than 5% of my time. I want the markup to be as simple as possible, so BEM implementation is annoying because I have to set classes everywhere. But I understood that this makes it easy to style a "breadcrumb-item". I am stil experimenting but now I tend to create parametrizable styles. Say I want to build a breadcrumb and the markup I want to have is the one from ARIA APG breadcrumb example. I would set up custom properties on the .breadcrumb "block" class, use :where to rely on the DOM to find the "elements" without increasing the specificity, and apply the styles on them using the custom properties. .breadcrumb > :where(ol) > :where(li + li)::before { padding-inline: var(--breadcrumb-item-padding-x); ... }
@dmtrmrv
@dmtrmrv Ай бұрын
I see; that's more of a CUBE flavor (cube.fyi/). Another methodology I really like. The downside to writing styles like this is if anything in the DOM moves, the styles will break. However, in the case of components like Breadcrumbs, which have a super standard structure and rarely change, it's perfectly fine to write it even simpler: .breadcrumb li + li::before {}. This is because the likelihood of the breadcrumb component having two lists with two different types of elements is very low. It's also a matter of consistency. If the rest of the project is written in BEM, it's still advisable to add classes; otherwise, it's a perfectly acceptable approach. Glad you brought it up! I like conversations like this!
@gaming_with_raza7867
@gaming_with_raza7867 3 күн бұрын
I learned html css and js. Now i am learning backend but the point is i cannot make Good looking layout specially i stuck in responsiveness Anyone any Suggestion?
@patunolaibukun2506
@patunolaibukun2506 Ай бұрын
where have you been
@MrAbbo11
@MrAbbo11 Ай бұрын
praise the lord for tailwind so i could away from this completely. I know tailwind is probably not suitable for all type of projects, depending on its size. That being said, I'd only ever use BEM if I absolutely had to.
@dmtrmrv
@dmtrmrv Ай бұрын
Like you said, there's a time and place for everything. Both BEM and the utility-first approach have their drawbacks and advantages. I'm curious if you've come across this article: nuejs.org/blog/tailwind-vs-semantic-css/?
Avoid These 5 Awful CSS Mistakes
20:42
Kevin Powell
Рет қаралды 186 М.
蜘蛛侠这操作也太坏了吧#蜘蛛侠#超人#超凡蜘蛛
00:47
超凡蜘蛛
Рет қаралды 44 МЛН
Write less code with these 5 CSS tips
15:38
Kevin Powell
Рет қаралды 39 М.
CSS Subgrid Layouts Are Here!
9:09
Dmitry Mayorov
Рет қаралды 5 М.
How to take control of Flexbox
16:01
Kevin Powell
Рет қаралды 74 М.
Make Sense of Box-Sizing in CSS (and avoid this major mistake!)
13:59
Using CSS custom properties like this is a waste
16:12
Kevin Powell
Рет қаралды 154 М.
Top 10 CSS One Liners That Will Blow Your Mind
13:34
developedbyed
Рет қаралды 873 М.
Is Mobile-First Design Still Relevant in 2024?
6:53
Dmitry Mayorov
Рет қаралды 1,5 М.
STACKING CONTEXT in CSS, Simplified | Solving Z-INDEX problems
17:19
Deeecode The Web
Рет қаралды 1 М.
BEM 101: How to Make Web Design Organized & Scalable
1:01:37
Kevin Geary
Рет қаралды 18 М.