<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Gaurav Sharma on Medium]]></title>
        <description><![CDATA[Stories by Gaurav Sharma on Medium]]></description>
        <link>https://medium.com/@gaurav.sharma.blogs08?source=rss-517ed4cff7af------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Hvz07DOLB9vhpwlpowZx6w.jpeg</url>
            <title>Stories by Gaurav Sharma on Medium</title>
            <link>https://medium.com/@gaurav.sharma.blogs08?source=rss-517ed4cff7af------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 14:44:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gaurav.sharma.blogs08/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Introduction to BEM — Block Element Modifier: A Beginner’s Guide]]></title>
            <link>https://medium.com/@gaurav.sharma.blogs08/introduction-to-bem-block-element-modifier-a-beginners-guide-a9fec8603568?source=rss-517ed4cff7af------2</link>
            <guid isPermaLink="false">https://medium.com/p/a9fec8603568</guid>
            <category><![CDATA[css]]></category>
            <category><![CDATA[css-methodology]]></category>
            <category><![CDATA[accessibility]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[bem]]></category>
            <dc:creator><![CDATA[Gaurav Sharma]]></dc:creator>
            <pubDate>Tue, 13 Jun 2023 20:21:08 GMT</pubDate>
            <atom:updated>2023-06-13T20:21:08.472Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Introduction to BEM — Block Element Modifier: A Beginner’s Guide</strong></h3><p>Are you tired of messy and disorganised CSS code? Do you struggle to read, maintain, and scale your frontend development projects? If so, you’re not alone. The good news is that there is a solution to these problems, and it’s called BEM.</p><p>BEM, short for Block Element Modifier, is a popular CSS methodology that simplifies the process of writing CSS code. By breaking down styles into blocks, elements, and modifiers, BEM makes it easier to understand and manipulate your CSS code. In this article, we’ll introduce you to BEM and explain how it works. You’ll come away with a basic understanding of BEM and learn how to start using it in your own projects.</p><p><strong>What is BEM?</strong></p><p>BEM stands for Block Element Modifier, which is a popular CSS methodology used for frontend development. Its main goal is to help organize, maintain, and scale CSS code in large projects. This methodology breaks styles down into three main components:</p><p>1. Blocks<br>2. Elements<br>3. Modifiers</p><p>Let’s explore each of these components in more detail.</p><p><strong>Blocks</strong></p><p>Blocks are the main building blocks of BEM. They represent a standalone entity that carries all the relevant HTML and CSS within it. For example, a header, navigation menu, or a section with related content could be a block.</p><p>A block should be named according to its purpose, and the name should be unique within the entire project. It’s recommended to use a hyphenated naming convention for blocks, like “header-container” or “nav-menu”.</p><p>Blocks are defined by a class name that starts with the block’s name, followed by two underscores. For example, to create a block for a header, we would use the class name “header__”. This tells us that any CSS styles that follow are related to the header block.</p><p><strong>BEM block example:</strong></p><pre>&lt;div class=&quot;header__&quot;&gt;</pre><p>The double underscore is a separator for elements that belong to the block. We’ll cover this in more detail in the next section.</p><p><strong>Elements</strong></p><p>Elements are the children of blocks and are used to create more specific styling. In BEM, you use a <strong><em>double underscore (__)</em></strong> to separate the block and the element. Elements also have their own separate class names, which makes them easy to identify in your CSS code.</p><p>For example, let’s say you have a block called “navigation”. Within that block, you have several elements such as “menu”, “link”, and “dropdown”. To style the “link” element, you would use the class name “navigation__link”. This tells the browser that this element belongs to the “navigation” block and is an element within that block called “link”.</p><p>Using elements in BEM helps to keep your code organized and easy to read. It also makes it easier to make changes to specific elements without affecting other parts of your code.</p><p>When selecting elements in your CSS code, it’s important to use the BEM naming convention so that your code remains consistent and easy to understand.</p><p><strong>Modifiers</strong></p><p>Modifiers are a powerful feature of BEM that allow you to create variations of blocks and elements without having to create new class names. Modifiers are used to change the appearance of an element or block based on certain conditions, such as state, theme, or size.</p><p>Modifiers are denoted by a double dash ( — ) followed by the modifier name. Modifiers can be applied to either blocks or elements, and can also be combined with other modifiers to create a more complex effect.</p><p>For example, let’s say you have a block called “button” that you want to style differently when it’s in a “disabled” state. You could create a modifier for the “disabled” state like this:</p><p><strong><em>&lt;div class=”button button — disabled”&gt;Disabled Button&lt;/div&gt;</em></strong></p><p>The “button — disabled” class would contain the styles necessary to style the button in a disabled state. By keeping the modifier as a separate class, you can easily add or remove it using JavaScript or other code without affecting the base “button” styles.</p><p>You can also use modifiers to create variations of elements within a block. For example, let’s say you have a block called “card” that contains an image. You could create a modifier for the image to make it circular like this:</p><pre>&lt;div class=&quot;card&quot;&gt;<br>&lt;img class=&quot;card__image card__image - circular&quot; src=&quot;image.jpg&quot; alt=&quot;Image&quot;&gt;<br>&lt;div class=&quot;card__content&quot;&gt;<br>&lt;p&gt;Card content goes here&lt;/p&gt;<br>&lt;/div&gt;<br>&lt;/div&gt;</pre><p>The “card__image — circular” class would contain the styles necessary to make the image circular, while the “card__image” and “card__content” classes would contain the base styles for the image and content respectively.</p><p>In this way, modifiers can help you create complex and flexible designs without sacrificing clarity or maintainability.</p><p><strong>Code Example of BEM</strong></p><p>Here’s an example of how you can structure a block using the Block Element Modifier (BEM) methodology in HTML and CSS:</p><pre>// HTML Code:<br>&lt;div class=&quot;block&quot;&gt;<br>&lt;h2 class=&quot;block__title&quot;&gt;Block Title&lt;/h2&gt;<br>&lt;p class=&quot;block__text&quot;&gt;Block content goes here&lt;/p&gt;<br>&lt;div class=&quot;block__element&quot;&gt;<br>&lt;! - Element content goes here →<br>&lt;/div&gt;<br>&lt;/div&gt;</pre><pre>// CSS Code:<br>.block {<br>/* Block styles */<br>}<br>.block__title {<br>/* Block title styles */<br>}<br>.block__text {<br>/* Block text styles */<br>}<br>.block__element {<br>/* Block element styles */<br>}</pre><p>In this example, we have a block with the class “block”. Inside the block, we have a block title with the class “block__title”, block text with the class “block__text”, and a block element with the class “block__element”. Each element within the block follows the BEM naming convention by using the double underscore “__” to separate the block and the element.</p><p>Modifiers can also be added to elements and blocks to represent different states or variations. Here’s an example of a modified block:</p><pre>// HTML Code:<br>&lt;div class=&quot;block block - highlighted&quot;&gt;<br>&lt;h2 class=&quot;block__title&quot;&gt;Block Title&lt;/h2&gt;<br>&lt;p class=&quot;block__text block__text - large&quot;&gt;Block content goes here&lt;/p&gt;<br>&lt;div class=&quot;block__element block__element - hidden&quot;&gt;<br>&lt;! - Element content goes here →<br>&lt;/div&gt;<br>&lt;/div&gt;</pre><pre>// CSS Code:<br>.block {<br>/* Block styles */<br>}<br>.block - highlighted {<br>/* Modifier styles for highlighted block */<br>}<br>.block__title {<br>/* Block title styles */<br>}<br>.block__text {<br>/* Block text styles */<br>}<br>.block__text - large {<br>/* Modifier styles for large block text */<br>}<br>.block__element {<br>/* Block element styles */<br>}<br>.block__element - hidden {<br>/* Modifier styles for hidden block element */<br>}</pre><p>In this modified block example, we added the modifier “block — highlighted” to the main block, the modifier “block__text — large” to the block text, and the modifier “block__element — hidden” to the block element. The CSS rules for these modifiers are then defined separately to apply the desired styles.</p><p><strong>Why use BEM?</strong></p><p>BEM is a popular methodology for frontend development that offers many benefits over other approaches. Here are some reasons why you should consider using BEM:</p><p><strong>Improved CSS organisation</strong></p><p>BEM’s modular approach makes it easy to organize your CSS code and keep it clean and readable. By breaking down styles into blocks, elements, and modifiers, you can create a clear and structured hierarchy that is easy to navigate and maintain.</p><p><strong>Increased readability</strong></p><p>With BEM’s naming conventions, it’s easy to understand what each class does and how it relates to other elements on the page. This makes it easier to work with large codebases and collaborate with other team members.</p><p><strong>Easier collaboration on large projects</strong></p><p>BEM’s modular structure and naming conventions make it easy to work on large projects with multiple developers. By using the same naming conventions and structure, everyone can work more efficiently and avoid conflicts.</p><p>Overall, BEM is a powerful methodology that can help you create more organized, readable, and maintainable CSS code. By adopting BEM in your projects, you can improve your workflow and create better websites and applications.</p><p><strong>Using BEM in your projects</strong></p><p>Now that you have a basic understanding of BEM, it’s time to start using it in your own projects. Here are some tips for implementing BEM effectively:</p><p><strong>Structure your classes</strong></p><p>When creating BEM class names, it’s important to structure them in a way that makes sense and follows a consistent pattern. Use block names that are descriptive and easy to understand, and be consistent with your naming conventions.</p><p><strong>Write clean code</strong></p><p>One of the main advantages of BEM is that it makes your CSS code easier to read and maintain. However, this only works if you write clean, well-organized code. Make sure to use indentation and spacing to make your code easy to follow.</p><p><strong>Avoid common mistakes</strong></p><p>There are a few common mistakes that developers make when using BEM, such as using too many modifiers or creating overly complex class names. Be sure to follow best practices and avoid these mistakes to get the most out of BEM.</p><p><strong>Integrate BEM with other CSS methodologies</strong></p><p>BEM is just one of many CSS methodologies out there. However, it can be used in conjunction with other methodologies to create a more comprehensive approach to CSS organization. For example, you could use BEM for naming conventions and SMACSS for structuring your CSS files.</p><p>By following these tips and best practices, you can start using BEM to organize your CSS code and improve the readability and maintainability of your projects.</p><p><strong>BEM vs other CSS methodologies</strong></p><p>While BEM is just one of many CSS methodologies, it stands out for its approach to CSS organization and scalability. Other popular methodologies, like SMACSS and OOCSS, have their own benefits, but many developers have found BEM to be the most effective for their needs.</p><p><strong>BEM advantages</strong></p><p>One of the main advantages of using BEM is its focus on modularity. By breaking down styles into blocks, elements, and modifiers, BEM makes it easy to read and maintain CSS code, even as projects grow larger and more complex.</p><p>BEM also encourages the use of descriptive class names, which makes it easy to understand the purpose of each CSS rule. This can be especially useful for larger teams, who may need to collaborate on the same codebase.</p><p><strong>SMACSS vs BEM</strong></p><p>SMACSS (Scalable and Modular Architecture for CSS) is another popular CSS methodology, which focuses on creating CSS rules that can be reused across an entire project. While SMACSS and BEM have some similarities, SMACSS places more emphasis on the separation of concerns, with styles organized into Base, Layout, Module, State, and Theme categories.</p><p>While SMACSS can be effective for smaller projects, many developers have found BEM to be more robust and scalable for larger projects. BEM’s focus on creating standalone blocks and descriptive class names can make it easier to maintain and update code as projects grow in size and complexity.</p><p><strong>OOCSS vs BEM</strong></p><p>OOCSS (Object Oriented CSS) is another CSS methodology that focuses on creating reusable CSS components. OOCSS places more emphasis on the separation of structure and skin, with styles organized into objects and abstractions.</p><p>While OOCSS and BEM have some similarities, BEM’s approach to CSS organization and naming conventions can make it easier to read and understand code. BEM’s focus on modularity can also make it easier to maintain and update code over time.</p><p><strong>BEM and Responsive Design</strong></p><p>Responsive design has become an essential part of modern web development. With the ever-increasing number of devices and screen sizes, it’s important to ensure that your site looks great on all screens. BEM can help you create responsive designs that are easy to maintain and scale.</p><p>The key to responsive design is using CSS breakpoints. These are points in your code where you can specify different styles for different screen sizes. For example, you might want your navigation menu to change from a horizontal layout to a vertical layout on smaller screens.</p><p>With BEM, you can easily add breakpoints to your code by using modifiers. For example, you might create a modifier called — small to apply specific styles to a block or element on smaller screens. This makes it easy to write clean and readable code that adapts to different screen sizes.</p><p>BEM also works well with grid systems, which are a common tool for creating responsive designs. Grid systems allow you to define a set of columns and rows that your content can be placed into. With BEM, you can create blocks and elements that correspond to these grid columns and rows, making it easy to organize your content and ensure that it looks great on all screens.</p><p><strong>BEM and Responsive design examples</strong></p><p>Let’s say you have a block called .feature that contains some feature content. You could add a modifier called — small to make the content stack vertically on smaller screens:</p><pre>&lt;div class=&quot;feature feature - small&quot;&gt;<br>&lt;h2 class=&quot;feature__title&quot;&gt;Feature Title&lt;/h2&gt;<br>&lt;p class=&quot;feature__description&quot;&gt;Feature Description&lt;/p&gt;<br>&lt;/div&gt;</pre><p>Similarly, you might have a block called .grid that contains a set of grid columns. You could add modifiers called — 1of2, — 1of3, and so on, to specify different column widths at different screen sizes:</p><pre>&lt;div class=&quot;grid&quot;&gt;<br>&lt;div class=&quot;grid__column grid__column - 1of2 grid__column - small - 1of1&quot;&gt;<br>Column 1<br>&lt;/div&gt;<br>&lt;div class=&quot;grid__column grid__column - 1of2 grid__column - small - 1of1&quot;&gt;<br>Column 2<br>&lt;/div&gt;<br>&lt;/div&gt;</pre><p>By using BEM and CSS breakpoints, you can create responsive designs that are easy to maintain and scale. With a little practice, you’ll be able to create beautiful and functional sites that look great on all devices.</p><p><strong>BEM and Accessibility</strong></p><p>Accessibility is an important consideration for all web developers. By using BEM, you can make your sites more accessible to users with disabilities. Here are some best practices to keep in mind:</p><ul><li>Use semantic HTML to improve accessibility. BEM works well with semantic HTML, which helps screen readers and other assistive technologies to understand your content.</li><li>Add ARIA roles to your HTML elements. ARIA roles provide additional context for assistive technologies, making it easier for users with disabilities to navigate your site.</li><li>Use meaningful class names. As we explained earlier, BEM uses descriptive class names that make it easier to understand your CSS code. This also helps users with disabilities to navigate your site more easily.</li><li>Test your site with a screen reader. Screen readers are an essential tool for users with visual impairments. By testing your site with a screen reader, you can ensure that it is accessible to all users.</li></ul><p>By following these best practices, you can ensure that your site is accessible to all users, regardless of their abilities.</p><p><strong>Common mistakes when using BEM</strong></p><p>While BEM is a powerful methodology for organizing your CSS code, there are some common mistakes that developers make when using it. Here are some of the most frequent errors to avoid:</p><p><strong>Solution</strong></p><p>· Using overly complex class names</p><p>· Keep your class names simple and easy to read. Avoid using long chains of modifiers.</p><p>· Inconsistent naming conventions</p><p>· Establish clear naming conventions for your team and stick to them. Consistency is key.</p><p>· Using BEM for everything</p><p>· BEM is not suitable for every project. Use it when it makes sense, but don’t force it if it’s not necessary.</p><p>· By avoiding these common mistakes, you can use BEM effectively and get the most out of this powerful CSS methodology.</p><p><strong>Advanced BEM techniques</strong></p><p>Now that you have a solid understanding of the basics of BEM, it’s time to explore some advanced techniques. These techniques will help you take your CSS skills to the next level and create even more sophisticated designs with BEM.</p><p><strong>Using Pseudo-elements</strong></p><p>Pseudo-elements are a powerful tool for creating more complex designs in BEM. They allow you to add additional elements to your HTML markup without having to change the underlying structure of your page. You can use pseudo-elements to create custom styles for things like drop-down menus, tooltips, and more.</p><p>When using pseudo-elements with BEM, it’s important to follow the same naming conventions as you would for regular elements. You should prefix the pseudo-element with the name of its parent block or element, and use a double underscore to separate the parent from the pseudo-element.</p><p><strong>BEM naming convention</strong></p><p><strong>CSS selector</strong></p><p>Block with pseudo-element</p><p>.block__pseudo-element</p><p>Element with pseudo-element</p><p>.block__element__pseudo-element</p><p><strong>Creating complex layouts with BEM</strong></p><p>BEM is also great for creating complex layouts that require multiple levels of nesting and positioning. You can use BEM to create grid systems, responsive layouts, and even custom page layouts.</p><p>When working with complex layouts in BEM, it’s important to keep your naming conventions consistent and easy to understand. Use clear names for your blocks and elements, and avoid creating overly complex class names.</p><p><strong>Integrating BEM with other CSS methodologies</strong></p><p>Finally, you can also use BEM in conjunction with other CSS methodologies to create even more powerful styles. For example, you can combine BEM with SMACSS to create a hybrid methodology that leverages the advantages of both approaches.</p><p>When integrating BEM with other methodologies, it’s important to ensure that your naming conventions and class structures are consistent across both approaches. Use clear and descriptive class names, and avoid creating overly complex structures that are difficult to read and maintain.</p><p>With these advanced techniques, you can take your CSS skills to the next level and create even more powerful and sophisticated designs with BEM.</p><p><strong>FAQ about BEM</strong></p><p>Here are some frequently asked questions about BEM:</p><p><strong>What makes BEM different from other CSS methodologies?</strong></p><p>BEM is different from other CSS methodologies in that it focuses on creating blocks, elements, and modifiers, which are easy to understand and manipulate. BEM also provides a clear structure for naming classes, which helps to keep your CSS organized and easy to read.</p><p><strong>Is BEM difficult to learn?</strong></p><p>BEM is not difficult to learn, especially if you already have some experience with CSS. Once you understand the basics of BEM, you can start using it in your own projects. However, like any new approach, it may take some time to get used to.</p><p><strong>Can BEM be used for all types of web projects?</strong></p><p>Yes, BEM can be used for all types of web projects, including small and large projects. However, it may be especially useful for larger projects, where CSS can quickly become disorganized and difficult to manage.</p><p><strong>Can BEM be integrated with other CSS methodologies?</strong></p><p>Yes, BEM can be integrated with other CSS methodologies, like SMACSS and OOCSS. However, it’s important to choose a methodology that works well with BEM and to be consistent in your approach.</p><p><strong>How can I avoid common mistakes when using BEM?</strong></p><p>To avoid common mistakes when using BEM, it’s important to follow best practices, like keeping your classes as concise as possible, avoiding overuse of modifiers, and being consistent in your naming conventions. It’s also important to take the time to understand how BEM works before you start using it in your projects.</p><p><strong>Is BEM suitable for responsive design?</strong></p><p>Yes, BEM is suitable for responsive design. In fact, BEM can help you create responsive designs that look great on all devices. By using CSS breakpoints and other techniques, you can build responsive layouts that are easy to manage and maintain.</p><p><strong>How can BEM help with accessibility?</strong></p><p>BEM can help with accessibility by providing a clear structure for organizing your HTML and CSS. By using semantic HTML and ARIA roles, you can create websites that are easier to navigate for users with disabilities. It’s also important to follow other best practices, like providing alternative text for images and using large, legible fonts.</p><p><strong>What are some advanced techniques I can use with BEM?</strong></p><p>Once you’ve mastered the basics of BEM, there are many advanced techniques you can use to take your CSS skills to the next level. Some of these techniques include using pseudo-elements, creating complex layouts, and integrating BEM with other CSS methodologies.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a9fec8603568" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>