Is it beneficial to merge several CSS classes when attempting to design self-contained components?

For a while now, I have been using a syntax similar to BEM in my projects. Recently, as I was revisiting some CSS articles, I came across this interesting read about the single responsibility principle:

https://i.sstatic.net/HdCXL.png

The concept discussed involves not only incorporating styles specific to the header__button class into that particular class, but also relying on styles from the general button class. This raises a question of whether we are creating a coupling between the element of header and the button class. Essentially, any changes made to the button class in the future would require careful consideration of all instances where it is being used.

While this approach may be justified in certain scenarios, such as maintaining consistent styles, how does it fare with more complex components like layout elements? Consider a situation where you have a Menu class for arranging children vertically, and a Sidebar class that also styles these children. The concern arises when both classes are utilized together:

menu.css

Menu {
}
Menu__item {
}

sidebar.css

Sidebar {
}
Sidebar__item {
}

index.html

<div class="Menu Sidebar">
  <div class="Menu__item Sidebar__item">
  </div>
  <div class="Menu__item Sidebar__item">
  </div>
</div>

If the positioning logic for items is solely contained within the Sidebar class, any alterations to the code in the Menu class could potentially disrupt the functionality of the Sidebar class. Conversely, duplicating code across both classes violates the single responsibility principle. Lately, in my own projects, I have been leaning towards duplication in order to maintain separate responsibilities. Is this the best practice, though?

Answer №1

There's a helpful chapter that addresses some of the issues raised in your question:

In this scenario, I believe that Sidebar should be responsible for the positioning of the Menu, while remaining unaware of the specifics of the Menu__item. Conversely, Menu should not concern itself with its own placement, but rather focus on positioning its individual items.

If there is a need to adjust the positioning of Menu__item when it is nested inside Sidebar, you can utilize nested selectors like so:


.Menu {
}
.Menu__item {
}
.Sidebar {
}
.Sidebar .Menu__item {
}

Answer №2

It's essential to make a decision between using a menu or sidebar and apply the appropriate class. If there are slight variations between the blocks, consider using modifiers. In cases where using modifiers is not feasible, opt for applying two distinct classes.

When utilizing modifiers:

// styling for your menu
.menu { }
.menu__item { }

// styling for your sidebar with a modifier
.menu--some-modifier { }
.menu__item--some-modifier { }
//
.sidebar__menu {
    // specify styles (margins, positioning) for .menu
}

Alternatively, when dealing with two independent blocks:

// styling for your menu
.menu { }
.menu__item { }

// styling for your separate sidebar block
.sidebar { }
.sidebar__item { }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is the radio functionality and dropdown menu experiencing technical difficulties?

I'm having issues with my code, specifically in the JS function when I try to retrieve values from radio buttons and dropdown menus. Can someone help me figure out what's going wrong? CSS: #mytable { width:400px; border: 1pt solid blac ...

What is the best way to ensure my component doesn't get hidden behind my NavBar when using react-scroll?

Currently, I am utilizing react-scroll for smooth navigation through four components. However, when I select a component from the NavBar, it scrolls that component to the top of the screen instead of aligning it with the bottom of the NavBar. To illustrate ...

The Material UI text field on mobile devices causes the screen to zoom in, cutting off the top content and displaying the bottom of the page

Whenever I click on the Material UI text Field during login, the screen gets pushed down and cuts off content at the top of the page. View image description here -I have examined my CSS to see if setting the container height to 100vh is causing this issue ...

I desire for the body to utilize the full height of the screen

My website consists of a header div and a body div. I am trying to make the body div expand to fill the entire screen height. When I set the height to 100vh, it expands correctly but it does not take into account the height of the header div, resulting in ...

Error loading CSS file on Google App Engine

I attempted to add a CSS file as a static file in my project just to experiment with how it functions, but encountered difficulties right from the start. The content of the CSS file is: body { background:#00FF00; } In addition, here is my configurat ...

Ways to show dropdown menu link exclusively on mobile browsers and not on desktop browsers

<li class="megamenu-content"> <div class="megamenu-content-wrapper clearfix"> <ul class="col-lg-3 col-sm-3 col-md-3 "> <li class="cat-header">DISPLAY MEMBERS</li> ...

Customizing jQuery dialog: What is the best way to change the appearance of the close button?

I am struggling to style the close tag in my jQuery dialog box. I have looked through the documentation and tried various CSS alterations, but nothing seems to be working. Any suggestions or insights would be greatly appreciated. My code is provided below ...

Is there a way to make FullPage and Lightbox compatible with each other?

Currently utilizing Fullpage.js for my website, I am looking to incorporate a lightbox in one of the sections. However, upon attempting to load the script and CSS, my fullpage layout breaks and the sections collapse. I have experimented with placing the ...

Next JS and the power of media queries

I've been searching for a while now, but I just can't seem to find a solution to my problem. I'm attempting to utilize different files for my media query breakpoints in Next JS, but they don't appear to be working. I created the files ...

Tips for resizing images without using media queries in CSS

I have a name plate image labeled (PAST CHAMPIONS) for the plaque, and it appears fine on most screens except for iPhone portrait mode, where it gets cut off. Is there a way to make this image scale without using media CSS? Here is the link to the homepa ...

Modify the appearance of a Javascript file that is parsing data from a text file

I am working on an HTML project where I have a JavaScript file that reads from a text file. Currently, the text from the file is displaying in my HTML file, but I would like to style it using CSS. Can anyone provide guidance on how to achieve this? I am st ...

Troubleshooting issue with changing class based on input values

It appears that there is an issue with the functionality when switching values on page load. Initially, I was able to make it work for a single switch, but now that there are multiple switches on the page, toggling affects all of them. How can I modify it ...

A guide to toggling the check/uncheck status of a list box by clicking on a checkbox using

I am using a div and CSS id to control the checkbox check and uncheck functionality, but I am not getting the desired outcome from my source code Step 1: By default, the checkbox is unchecked and the listbox is disabled Step 2: When the checkbox is check ...

The pre-loader is hidden behind the block content and navigation bar

I've encountered an issue with my preloader where it's loading in front of the <body> element but not in front of the site's <navbar> or the {% block content %}. The CSS snippet to increase the z-index of the preloader is as foll ...

Mastering Bootstrap: Achieving flawless column stacking in succession

I am currently integrating bootstrap into my existing HTML code, but I only require it in two specific sections to avoid rewriting the entire code. Everything looks fine on my 1080p screen as intended. However, when I resize the screen slightly The titl ...

Endless looping of numerous consecutive animations

Is it possible to apply two animations to an element using only CSS and without the need for JavaScript? These animations should run sequentially an infinite number of times. @keyframes show { from, to { z-index: 100; } } @keyframes wait { ...

A guide on alternating text input between two colored boxes using HTML

Currently, I have a code that can create two boxes and four buttons. However, only one button, the start button, triggers a popup requesting a name input. Once you enter a name, it will appear in the first box. <html> <head> <script ...

reduce opacity of specified div background

Objective / Purpose I am working with multiple div elements and I want to assign two distinct classes to each of them, each serving a different purpose within my application. One class should determine the level, influencing the color of the div. The oth ...

The collapse of HTML elements converging inwards within the Bulma Framework and customized with SASS

Messy Screen Recently delving into the world of Bulma, I decided to take a shot at customizing my CSS. Unfortunately, it seems that after some tweaking, my screen layout has gone haywire. Can anyone shed light on why two elements might refuse to maintain ...

Tips for getting validation to function properly on Bootstrap v4 using Bootstrap validator

Recently, I made the switch from Bootstrap v3 to v4, and although the JavaScript part of Bootstrap Validator is still functioning properly, the styling seems to have disappeared. The bootstrap validator that I utilized can be found here. This is how it c ...