Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:07:12+00:00 2026-05-13T08:07:12+00:00

I didn’t see anything on here about it with a quick search, if there

  • 0

I didn’t see anything on here about it with a quick search, if there is let me know.

Here is an example of a CSS selector I’d write.

div#container div#h h1 { /* styles */ }
div#container div#h ul#navi { /* styles */ }
div#container div#h ul#navi li.selected { /* styles */ }

I write all my CSS like. This allows me to stop from having styles floating around and I can technically re-use the same class name easily. For instance, I might use .selected on multiple elements across the site.

I also specify the element type (div, ul, etc) before the class/id unless the style is used for multiple elements. I also specify the element before id’s even though there will only ever be one id because it allows me to know the element easily when reading my CSS. For instance I’ll know right off the bat that em#example will most likely have a font-style of italic.

This isn’t a question about CSS formating, it’s about writing selectors.

I’d love to hear opinions on this approach, as I’ve used it for years and I’m reevaluating my stying.

Although it’s somewhat off topic I also write my selectors like this for selector libraries (like jQuery for instance). Although I haven’t looked into jQuery’s internals to see if there is performance issue with specifying the element with an ID.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:07:13+00:00Added an answer on May 13, 2026 at 8:07 am

    I think it really depends on what the selector is for.

    Almost every site has one or a few style sheets that “skin” the site – fonts, text colour, link colour/hover, line spacing, and so on, and you don’t want these selectors to be very specific. Similarly, if you have a component or element that’s reused in many pages and always needs to look the same – like let’s say the tags right here on SO – then it’s going to be a pain to maintain if you use selectors based on the ID.

    I would always use the ID in the selector if it refers to a specific element on a specific page. Nothing’s more frustrating than trying to figure out why your rules don’t seem to be working because of a conflict with some other rule, which can happen a lot if everything is classes. On the other hand, I think that nesting the IDs as you are (#container #h) is redundant, because the purpose of an ID is to be unique. If you have more than one element with the same ID on the same page then you can end up with all sorts of problems.

    It does make the CSS slightly easier to understand when your selectors provide some idea of the “structure” that’s being represented, but, to be honest, I think that goes against the idea of separation of concerns. The #navi might be moved outside the #h for perfectly legitimate reasons, and now somebody has to update the style sheet for #navi, even though nothing about it has changed.

    A bit subjective as Darrell pointed out but that’s my two cents.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Didn't see a question similar to this, so here goes: Let's say I'm running
I didn't see any similar questions asked on this topic, and I had to
I didn't see the option to point the workspace (or it's VS equivalent, I'm
My visual assist x didn't work after I installed visual c# 2008. Is there
i didn't find a recent question so somethign might have changed: there is any
I didn't want to resort to asking here but after hours of frustration I
I didn't get the answer to this anywhere. What is the runtime complexity of
I didn't upgrade to Vista until May or so and one of the things
I didn't attend PDC 2008, but I heard some news that C# 4.0 is
Why didn't languages such as C end up being using for web dev? Surely

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.