BEM CSS Block or modifier - css

I'm trying to adopt BEM CSS and have a component but I want small and large versions of it at the block level.
I'm unsure of what should be the block or what should be the modifier. Because then I would need two blocks which defeats the purpose of making a component. Now my elements are now modifiers whereas they should just be elements from either small or large.
<div class="c-sales c-sales__small p3">
<p class="c-sales__small--headlead">Limited time only</p>
<h1 class="c-sales__small--heading">FIRST 2 MONTHS <span style="color:#00ce7d;">FREE!</span></h1>
<h2 class="c-sales__small--subheading pt2">Only in September</h2>
<p class="c-sales__small--details">this is some text</p>
</div>

c-sales is clearly the block. Your elements are:
c-sales__headlead
c-sales__heading
c-sales__subheading
c-sales__details.
Now, for the modifier you have two options. The first, most inline with BEM, would be to create modifiers to all elements that are actually different from the base version, so you could end up with:
c-sales
c-sales__headlead c-sales__headlead--small
c-sales__heading c-sales__heading--small
c-sales__subheading
c-sales__details
The other option is less verbose, but BEM recommends against it: you can use nested selectors. That would technically be a theme instead of a modifier, but it achieves what you want (modify stuff at block level). Have a look at this: https://en.bem.info/methodology/css/#nested-selectors
c-sales c-sales-theme-small
c-sales__headlead
c-sales__heading
c-sales__subheading
c-sales__details

The best approach is to make a base version of the element first, and then add small or large modifiers to it:
<!-- Base version -->
<div class="c-sales p3">
<p class="c-sales__headlead">Limited time only</p>
<h1 class="c-sales__heading">FIRST 2 MONTHS <span style="color:#00ce7d;">FREE!</span></h1>
<h2 class="c-sales__subheading pt2">Only in September</h2>
<p class="c-sales__details">this is some text</p>
</div>
<!-- Small version -->
<div class="c-sales c-sales--small p3">
<p class="c-sales__headlead c-sales__headlead--small">Limited time only</p>
<h1 class="c-sales__heading c-sales__heading--small">FIRST 2 MONTHS <span style="color:#00ce7d;">FREE!</span></h1>
<h2 class="c-sales__subheading c-sales__subheading--small pt2">Only in September</h2>
<p class="c-sales__details c-sales__details--small">this is some text</p>
</div>
<!-- Large version -->
<div class="c-sales c-sales--large p3">
<p class="c-sales__headlead c-sales__headlead--large">Limited time only</p>
<h1 class="c-sales__heading c-sales__heading--large">FIRST 2 MONTHS <span style="color:#00ce7d;">FREE!</span></h1>
<h2 class="c-sales__subheading c-sales__subheading--large pt2">Only in September</h2>
<p class="c-sales__details c-sales__details--large">this is some text</p>
</div>

Related

Trying to Understand BEM

So I am trying to understand the BEM naming structure. So lets say I have the following html
<div class="banner">
<div class="banner__toprow">
<span class="banner__teamName">
{{team.name}}
</span>
<span class="banner__score">
{{team.score}}
</span>
</div>
<div class="banner__timeouts">
{{team.timeOuts}}
</div>
</div>
Now where I get confused is when you have nested divs, how that works. For example for banner__teamName, I would usually do banner__toprow__teamName. Now maybe I can do this, but does this break BEM?
From the BEM's FAQ
What would be a class name for an element inside another element? .block__el1__el2?
According to BEM method, block structure should be flattened; you do
not need to reflect nested DOM structure of the block. So, the class
names for this case would be:
.block {}
.block__elem1 {}
.block__elem2 {}
.block__elem3 {}
Whereas the DOM representation of the block may be nested:
<div class='block'>
<div class='block__elem1'>
<div class='block__elem2'>
<div class='block__elem3'></div>
</div>
</div>
Besides the fact that the classes look much nicer, it makes the elements be dependent on the block only. So, you can easily move them across the block when providing changes to the interface. The changes of the block DOM structure would not need corresponding changes to the CSS code.
<div class='block'>
<div class='block__elem1'>
<div class='block__elem2'></div>
</div>
<div class='block__elem3'></div>
I would suggest doing it like this:
<div class="banner">
<div class="banner__toprow">
<span class="banner__toprow-teamname">
{{team.name}}
</span>
<span class="banner__score">
{{team.score}}
</span>
</div>
<div class="banner__timeouts">
{{team.timeOuts}}
</div>
</div>
I don't believe it is correct to have two Elements (__) of Modifiers (--). I also try not to use camel case in my classes, that might be a personal preference but I feel it could cause problems with certain frameworks like Angular.

BEM naming issues

Recently I have started using BEM in my project to organize CSS but now I am up to the point where i have html like this:
<div class="container">
<div class="container__header">
<div>TITLE</div>
<div>SUBTITLE</div>
</div>
<div class="container__body">
</div>
</div>
As I am more familiar with SMACSS I treat container with header and body as layout related styles. But then content of header on each subpage will differ, sometimes it will have title/subtitle that need different styling sometimes it will be some completly different structure. In terms of SMACSS I would put it in module folder and manage there but now with BEM would be it good practice to now create element such as note-header that will have elements __title and __subtitle?
<div class="container">
<div class="container__header note-header">
<div class="note-header__title">TITLE</div>
<div class="note-header__subtitle">SUBTITLE</div>
</div>
<div class="container__body note-body">
...
</div>
</div>
The problem I have with this approach is that note-header will most of the time probably be empty class just for the sake of naming convention. What is best practice to handle such case
I personally use three types of elements, namely:
l- prefixed layout elements, such as containers, headers, footers.
b- prefixed block elements, such as a menu, a small header inside a layout, etc.
c- prefixed component elements, such as buttons, links etc.
This in conjunction with modifiers say: c-button--white and c-button--black works fine.
Consider the following structure for your example:
<div class="l-container">
<div class="b-note">
<div class="b-note__title">TITLE</div>
<div class="b-note__subtitle">SUBTITLE</div>
<div class="b-note__content">
<div class="c-button">
<button class="c-button__button">Submit</button>
</div>
</div>
<div class="b-slideshow">
<div class="b-slideshow__title"></div>
</div>
</div>
</div>
In my projects, it's vital to make sure general styling is separate. Therefore I try to minimise any blocks / classes being used throughout other elements. This avoids styling drama later on because you're using b-note-header__title somewhere else as well, that requires just a small adjustment, but changing the main rule messes up your other titles.

Aria roles group or listbox for Material design card?

For designs similar to this the image card used on Materialize: http://materializecss.com/components.html#
Screenshot: http://i.imgur.com/zvncUFz.png
Should I be using roles of a group or listbox for properly describing the content? I'm a tad lost reading through the aria accessibility specs.
Basic Structure:
<div class="card">
<div class="card-image">
<img src="images/sample-1.jpg">
<span class="card-title">Card Title</span>
</div>
<div class="card-content">
<p>Card Content</p>
<a href='#'>Authors</a>
</div>
<div class="card-action">
This is a link
</div>
</div>
Side question on possibly how to deal properly addressing the links for multiple authors.
I would consider this to be a figure with a figcaption.
I would mark this up as follows(in Jade):
figure
div.card-image
img(src="", alt="If needed, any descriptive text here will be spoken by a screen reader, but will not be visible")
span.card-title Card Title
figcaption Anything within this figcaption tag will automatically be spoken by the screen reader
a.card-action(href="#") This is a link
In this case, there is no need at all to use any ARIA attributes. All that is needed is semantic markup. I hope this helps.

HTML5 semantic vs styling issue

I'm trying to build a Wordpress theme, using media queries for responsive layout. I have some issues around getting the correct layout whilst maintaining some semantic mark up.
Basically, for the post date in pages less than, say, 764 pixels wide, I want to have the date, bullet, and post category displaying inline; if the page is above this size, then the post date should display as a column (entry-date), alongside the main post column (main-column).
I think I've managed to get there, but just wondered if this html code was valid, as I've tried to keep header and footer sections within the post to make it make sense semantically.
Does anyone have any feedback?
Thanks,
John
<article class="post">
<div class="entry-date">
<span><span>30<sup>th</sup></span> Sep 2013</span>
</div>
<div class="main-column">
<span class="bullet">●</span>
<header class="entry-header">
<span class="entry-category"><a href="#">Cars</span>
<h1 class="entry-title">A new car from Ferrari</h1>
<span class="entry-authors vcard">by John Smith </span>
<div class="featured-img"><img src="img/ferrari.jpg" class="" alt=""></div>
</header><!-- .entry-header -->
<div class="entry-content">
<p>Ferrari have released a great new car, which is very fast.</p>
<span class="continue-reading">Continue reading</span>
</div><!-- .entry-content -->
<footer class="entry-meta">
<span class="entry-tags"><a href='#'>Sports cars</a></span>
</footer><!-- .entry-meta -->
</div>
</article><!-- #post-1234 -->
The comment's are completely off, validation has nothing to do with semantics, that's a totaly different subject. Semantic is about giving meaning to your code.
Currently you have this
<div class="entry-date">
<span><span>30<sup>th</sup></span> Sep 2013</span>
</div>
This would be more semantic, avoid using unnecessary tags(don't add tags only for styling, look for css solutions).
<div class="entry-date">
30<sup>th</sup>Sep 2013
</div>

What to name my DOM elements for CSS purposes

I'm having a very hard time assigning intelligent class names to my DOM elements.
I feel like the example below is something my CSS commonly devolves into:
<div class="article">
<div class="title-container">
<div class="title>Something fantastic</div>
<div class="sub-title-controls">
<div class="btn-like"></div>
<div class="btn-google-plus-one"></div>
<div class="btn-share"></div>
</div>
</div>
</div>
But then later I'll want to add a different style of "Share" button lower on the page. So I end up doing:
<div class="article">
<div class="title-container">
<div class="title>Something fantastic</div>
<div class="sub-title-controls">
<div class="btn-like"></div>
<div class="btn-google-plus-one"></div>
<div class="btn-share-tiny"></div>
</div>
</div>
</div>
<div>...More stuff...</div>
<div class="btn-share-big"></div>
Then things spiral out of control from there.
I guess what I'm looking for is: Is there any kind of Chicago Manual of Style for CSS? Like when is a <div> really better described as a <section> or <footer>? How do you intelligently name non-visual DIVs that serve as containers?
My CSS is a nightmare after 2 years of coding without structure. Just about given up and gone all inline on this particular project. :) Tips appreciated.
SMACCS is style guide for CSS, and might be exactly what you're looking for. You can buy it, or alternatively much of the content is available free on the site.
Additionally, BEM is a framework developed by the guys at Yandex, which I found useful reading and is used (somewhat) by InuitCSS, which I personally am keen on.
You should find some answers here, in this article titled: What Makes For a Semantic Class Name?.
In a nutshell, when you can, use the new HTML5 tags:
<header></header>
<article></article>
<aside></aside>
<footer></footer>
for your example, it would be better to do something like this :
<div class="btn-share"></div>
...
<div class="btn-share bigBtn"></div>
and only change the size in the bigBtn class.

Resources