Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Thoughts on the most elegant way to structure complex css?

Thoughts on the most elegant way to structure complex css?

Problem

Here's an example of some markup:

In my stylesheets, my rules might then look like this:

.document .main .title { ... }
.document .sections .section .description { ... }

I like this. This approach has always worked well for me, and I think the increasingly specific selectors make it very clear what is what (almost like reading a sentence).

Recently however I was told that this approach is bad, and that I should do something like the following instead:

The reason is that it avoids any ambiguity in what the class 'title' refers to.

I am curious what people here think? Is my approach bad (ie confusing, problematic) ? Is there a preferred way to format css for complex markup?

Problem courtesy of: user1031947

Solution

I've started to use LESS stylesheets and can't recommend them enough. The nested selectors make your code incredibly readable by structuring it like your HTML:

.document {
    .main {
        .title {
            ...
        }

        .foo {
            ...
        }
    }

    .sections {
        .section .description {
            ...
        }

        // Same as `.sections.hidden`
        &.hidden {
            ...
        }
    }
}

As for your HTML, there's no need to be redundant. The title class refers to an element that acts as a title and the type of title can be inferred by looking at the parent element. There's really no benefit from using the long names if you don't find them appealing.

Solution courtesy of: Blender

Discussion

View additional discussion.



This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

Thoughts on the most elegant way to structure complex css?

×

Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription

×