SMACSS, Semantic Class Names and Nested Elements

前端 未结 4 1022
情深已故
情深已故 2021-02-06 05:46

I\'ve just read Scalable and Modular Architecture for CSS and it\'s got me thinking about a few things. Snook\'s two principle tennets are:

  1. Increase the se
4条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 06:46

    I prefer using a class like .title rather than selecting just the h5 element, as I feel it decouples the HTML from the CSS some more. If, for whatever reason, you decide an h4 or and h6 is more appropriate for the document outline, or some other element/reason, you would have to refactor your CSS if you are selecting the element(h5) vs select a class(.title).

    This is why I prefer to class ALL THE THINGS vs. element selectors because the markup may change over time. If your working on a smaller site that isn't seeing many updates, this may not be a concern, though it's good to be aware of.

    As far as if there is anything wrong with using the same class in completely different contexts if you know it will be namespaced by a previous item in the selector, I have mixed thoughts on it.

    On one side, some concerns come to mind, for instance the use/meaning/role of .title could be empty without it's parent selector. (.pageHeader .title) While I think that .title is a perfectly fine class name, it may be difficult for other devs to understand it's meaning comes from a parent selector like .pageHeader .title

    Another concern is if you are using the class .title for multiple instances of "titles" in a module, you can run into issues. The basic idea is that the local scoping descendant selectors provide can sometimes be too restricting if trying to stretch the reusability of .title within the module. I set up a code demo to show what I'm talking about here.

    On the other side, descendant selectors are one of the most used selectors, specifically for their scoping purpose.

    Tim Murtaugh, who is one of the developers (not sure if he is the lead dev or not) for A List Apart uses descendant selectors for the majority of his styles on his personal site.

    I've recently been exploring this and BEM and would agree with your conclusion, "this is preferable to namespacing each classname."

    I prefer .pageHeader .title{...} vs. .pageHeader__title{...} but there are always instances where one approach may be more beneficial in a situation over the other.

    As with most things, it depends, but I think if you don't let the nesting go too deep and are thoughtful with how you use them, descendant selectors are powerful and good solution for providing locally scoped styles while also offering a solution for globally scoping styles.

    For example:

    /* Globally scoped styles for all .title(s) */
    .title{
       font-family: serif;
       font-weight: bold;
    }
    
    /* Locally scoped styles for individual .title(s) */
    .pageHeader .title {
        color: #f00;
    }
    
    .leadArticle .title {
        color: #00f;
    }
    
    .productPreview .product .title {
       color: #0f0;
    }
    

提交回复
热议问题