CSS Class Naming and BEM

There are only two hard things in computer science: cache invalidation and naming things.

– Phil Kalton

Why is naming important?

A poorly chosen name will cause confusion and delays the project. So a good name will help write understandable code which forms a solid structure. It also makes things easier and reusable for developers involved in the project.

What is CSS Class Naming?

CSS Class Naming is one of the most fundamental and important parts of developer activities. It makes the CSS selectors as informative and readable as possible which helps in the development and other issues that come when creating HTML based layouts.

Three categories of class names.

  1. Functional Class Names
  2. Content-Based Class Names
  3. Presentational Class Names

1. Functional Class Names

The styling of these elements is based on their function or meaning. There is a strong connection between the class name, the styles it applies, and the reason the styles are being applied. That class name is associated with these styles because of this reason.

Example

 <button class="important-text">Send Message</button>
<!-- class name “important-text” describes as functional of the content -->

2. Content-Based Class Names

Content-based class names are class names that describe the content they contain. If you’ve ever seen a class name like submit-button, intro-text, or profile-photo, you were looking at a content-based class name.

Example:

<button class="submit-button">Send Message</button>
<!-- class name “submit-button” describes the content-->

3. Presentational Class Names

Presentational class names describe the way an element looks–like green-button or big-text. The name itself is describing the styles that are being applied.

Example:

<button class="green-button">Send Message</button>
<!-- class name "green-button" describes the presentational or a look and feel-->

How to properly name CSS classes?

It is better to choose a good name for HTML elements.

Example:

<div class="submit"></div>
<!-- do not do? -->
<input class="submit" type="text" />
<!-- would be better written as -->

It is better used has- or is- prefix for the state. State manipulation happens very often. (bis) So adhere to a strict naming convention for the state will be very helpful

For Example:

//this selector targets the logo header
.c-header-logo {...}

Try to avoid camelCase rather than using word hyphen(-)

For Example:

//do not do?
.redBox {
    border: 1px solid red;
}

// would be better written as
.red-box {
    border: 1px solid red;.
}

It is a better way to use descriptive words so try to avoid abbreviation, apart from nav, txt, btn, url etc
Try BEM(Block, Element, Modifier). It is a component-based approach to web development.

For Example

<!-- Do not do this -->
<button class="btn--secondary"></button>

CSS

.btn--secondary {...}

HTML

<!-- Would be better written as -->
<button class="btn btn--secondary"></button>

CSS

.btn{...}

.btn--secondary {...}

Try to avoid more than two words for a given name. The name must be self-descriptive in one or two words, or code will be hard to maintain.

Example

/* Do not Do this */
.button-dropdown-one{...}

/* Would be better written as */
.button {...}
.button-dropdown {...}

Try to separate parent from children. If a class has too many responsibilities, split it into 2 separate properties.

Example

 /* Do not do this */
.post {
    .title {...}
}
/* Would be better written as */
.post {...}
.post-title {...}

CSS Class Naming Methodologies

CSS Methodologies are a structured way of writing and organizing your CSS. It helps us make our code more readable and predictable. When it comes to code modification and debugging, conventions help us to locate the part of the code easily.
The most popular CSS Class naming methodologies are SMACSS, OOCSS and BEM.

SMACSS

SMACSS stands for Scalable and Modular Architecture for CSS. It helps to increase the semantic value of a section of HTML and content and decrease the expectation of a specific HTML structure.

In SMACSS, the selectors are divided into five categories:

1. Base – Using elements selectors, the base is applied to an element.

Example:

html, body, form { margin:0; padding:0;}

2. Layout – It divided into major and minor styles. Major layout styles like header and footer are styled using ID selectors. Minor layout styles will use class names instead of IDs so that the styles can be used multiple times on the page.

Example:

#header, #footer {.....}
.l-fixed {....}
// fixed layout with  l- or layout- prefixes

3. Module – It contain content and reusable part of markup .

Example

.callout {...}

4. State – it describes how modules and sections appear in particular states(hidden/expand, active/inactive with a prefix “is”.

Example

.is-tab-active {...}

5. Theme – It describe how layout and module might look. They aren’t use most of project

Example

//module-name.css.mod { border: 1px solid;}
//theme.css
.mod { border-color: blue;}

OOCSS – Object-Oriented CSS

OOCSS is a coding paradigm that styles “objects” or “modules” — nestable chunks of HTML that define a section of a webpage—with robust, reusable cascading styles. CSS class are defined to HTML objects that help define the design and flow of the HTML objects. It helps write a simple and DRYer CSS. This is very popular and Bootstraps were initially written following this paradigm.

Example

/* Without OOCSS Methodology */
.button { width: 100px; border: 1px solid black;}
.widget { width: 200px; overflow: hidden; border: 1px solid black;}

/* After applying OOCSS strategy */
.skin { border: 1px solid black;}
.button { width: 100px;}
.widget { width: 200px; overflow: hidden;}

Separate container from content: It allows the re-use of elements and classes no matter where you are in the DOM. A styled element should never be dependent on where it’s at on a page. In this context, content refers to elements such as images, paragraphs and div tags that are nested within other elements, which serve as containers. Most containers can be represented by a structured class.

// the standard way of setting up the elements that make up a sidebar
#sidebar { padding: 2px; left: 0; margin: 3px; position: absolute; width: 140px;}
#sidebar .list { margin: 3px;}
#sidebar .list .list-header { font-size: 16px; color: red;}

// the same coding with the content and containers separated
.sidebar {padding: 2px; left: 0; margin: 3px; position: absolute; width: 140px;}
.list {margin: 3px;}
.list-header {font-size: 16px; color: red}

BEM (Block, Element, Modifier)

Block

The B in ‘BEM’ stands for Block.

A functionally independent page component that can be reused the existing code which is represented by the class attribute. It can contain elements and other blocks. It can also contain one or more modifier. Words are separated by a hyphen (-) or one word. The block could represent a header, menus, image, footer or any other block design.

Example

HTML

<form class="site-search">...</form>

CSS

.site-search {...}

Here an ideal class name for this component would be .stick-man, such as a block of design.
The component should be styled like:

.stick-man {...}

Element

The E in ‘BEM’ stands for Element.

An element is a part of the block and has no standalone meaning. It can contain other elements and block. It can also contain one or more modifiers. The element name is separated from the block name with two underscores: “–“. The element could represent a header subtitle, menu item, image caption etc.

Example

HTML

<form class="site-search">
    <input class="site-search__button" type="submit" value="search">        
</form>

CSS

.site-search__button {...}

The stick-man has a head, two arms, and feet.

For Example

.stick-man__head {...}
.stick-man__arms {...}
.stick-man__feet {...}

The head, arms and feet are all elements within the component.

Modifier

The M in ‘BEM’ stands for Modifier.

A Modifier is used to change the appearance, behaviour or state of a block or element. It is separated from the block or element name by a single underscore (_) or by adding two hyphens(–). The element could represent a theme, active/inactive state, alignment etc.

Example

HTML

<form class="site-search site-search--themed">
    <input class="site-search__button--large" type="submit" value="search">    
</form>

CSS

.site-search--themed {...}
.site-search__button--large {...}

When we have a stick man in blue and red colours, a blue and red are modifications of the component.

For Example

.stick-man--blue {...}
.stick-man--red {...}

What does BEM CSS solve?

It helps you figure out what your design is made of if you thinking in block, element and modifier. This methodology gives your CSS code a solid structure that remains simple and easy to understand between the team members. If you pre-planned a design with independent blocks, it helps reduce the maintenance time and get the project up and running in no time. It helps smaller projects and larger projects. However, maintaining larger projects becomes much easier over the period of time. With the help of pre-processor like SASS, maintenance nightmare for the bigger teams become trivial.

Good Colour Schemes

Good Colour Schemes

Eyelearn.org
This is one of the good examples with a good colour scheme which keeps things clean and minimal as a time portrays warmer touch. Light greyish orange as the background, combined with the strong orange coloured text looks very attractive.

Hotelchocolat.com
The combination of header background colour with white logo design goes very well with the colour theme adopted. Their product design images used on the website also goes very well with the colour theme. The images used on the website with the combination of warm colours like red and orange go with cool colours like grey, sky blue, violet and so on.

Pumpstreetbakery.com
This is one of the good examples with a good colour scheme which keeps things clean and minimal as a time portrays warmer touch. Light greyish orange as the background, combined with the strong orange coloured text looks very attractive.

Typography

Three Good TYpography

Laravel Website

Laravel Website

 

I have chosen laravel.com as a good typography website. All the text used are sans-serif typeface, the spacing between lines and letter are consistent and well presented, making it readable. The font size of the header, navigation and body is well presented.

Aws Amazon Website

AWS AMAZON

 

Aws.amazon.com also used sans-serif typeface. Even though there are lots of icons but it is displayed beautifully, making it attractive. I think the spacing between lines of different elements is adjusted.

Rightmove Website

Rightmove

 

The spacing between the lines used in rightmove.com is incorrect proportion for elements like <h1..h6> tags. HTML elements are correctly placed on the page in terms of hierarchy. Font colours go with the theme nicely.

Three Bad Typography

Bexley Council Website

Bexley council

 

Bexley.gov.uk has used HTML elements inconsistently in terms of hierarchy. There is too much spacing between the texts which looks inappropriately used. The line-height between <a> tags look wrong and confusing.

Gates N Fences Website

Gates N Fences

 

Gatesnfences.com is using only Arial font. There are no alternative fonts as fall back if the Arial font is not available. The browser may be used default fonts which may not display the text on the browser as owner’s intention. Normal text in the <body> uses a bold font-weight which is difficult to read.

Meta Tech Website

Metatech

 

Randomly placed text and images inconsistent in metatech.org page make it difficult to browse. Font colours do not give the nice look and feel. HTML tags are incorrectly used in terms hierarchy and most elements are wrapped with <div> elements instead semantic markup

What I learned this week

It was a great opportunity to see the presentation of the final year of completing students. I was impressed by their experience they gained over a year. It has inspired me to follow their footsteps.

I learned about semantic of HTML markup and how to structure the HTML pages with HTML elements such as header, nav, main, footer, h1, p etc. I learned how to name HTML file and organise the folder structure.

In summary, it has been a useful week.

Three good websites

Three good websites

Rightmove

rightmove
Dropbox Website

Audi Car

Audi car

Rightmove website helps the users to find to rent, buy and sell a property.

Read more…

Dropbox allows storing the file on the cloud with automatic sync. It has a clear user interface allowing the user different type of services.

Read more…

I am choosing this website because of appealing design.

Read more…

Three good designs

Three good design objects

Samsung Galaxy 8

Camera Tripod

Bosch Coffee Machine

I am choosing Samsung Galaxy 8 to be one of my objects because this model filled with good design and features. It is slim and lightweight. It is portable and easy to fit in a pocket or purse.

Read more…

This tripod is my second choice of object for a good design aspect. It is compact and lightweight with full of features.

Read more…

I am choosing a Bosch coffee machine as a third object for its elegant design and useful features.

Read more…