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

  • SEARCH
  • Home
  • 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 6580995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:05:49+00:00 2026-05-25T16:05:49+00:00

Working with class/id selectors in CSS and in jQuery, I often see two distinct

  • 0

Working with class/id selectors in CSS and in jQuery, I often see two distinct approaches:

1. Just the class or id:

CSS:

.foo{}

#bar{}

jQuery:

$(".foo")

$("#bar")

2. The class or id with its tag:

CSS:

div.foo{}

div#bar{}

jQuery:

$("div.foo")

$("div#bar")

My question is: Barring the use of the tag to further refine the selector, is there anything wrong with placing the tag with the class/id? Which is proper syntax?

I’ve heard some that say that unless the tag is needed for specificity, it is dead wrong to place it. While others say it makes no difference, and in fact prefer it as it provides further information concerning the selector.

What do you think?

  • 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-25T16:05:50+00:00Added an answer on May 25, 2026 at 4:05 pm

    With the tag included

    $("div.foo") or div.foo{}

    you are giving the browser a hand, telling it not to search every element with a certain class or ID. Instead, in the examples above, it would just search divs.

    Although the performance may be negligible on a single element or a small page, it could add up if you are talking about a document with thousands of tags and several different css or jQuery calls.

    Distinguishing between two elements

    In some cases you may need it, too, to distinguish between two elements with the same class.

    For Specificity

    Plus, I think that you should include the elements when possible, as a way to make your CSS (and jQuery) as specific as possible… keeping the surprises to a minimum!

    Better for shared code/troubleshooting/updating

    It is also much easier to find/change/edit rules when the element is included in the rule.

    EDIT

    To respond to @stefmikhail’s comment about @YoTsumi’s benchmark test, here is the difference:

    Searching for a unique ID will always be the fastest thing, as there should only be one ID on a page and the engine needs to look for it and it alone. However, as @BoltClock mentioned, there is more to this question than performance.

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

Sidebar

Related Questions

I'm using several plugins attributed to css selectors and has been working properly. The
Does anyone have a working class or function to create the hashed email that
using System; using System.IO; using System.Net; using System.Text.RegularExpressions; namespace Working { class Program4 {
I have a working wrapper class for Dallmeier camera devices, It contains a callback
I'm working on a class that inherits from another class, but I'm getting a
I'm working on a class that is storing a 2D array of class MyType
I'm working on a class assignment that started small, so I had it all
I am working on a class library using C#. I have designed 3 main
I'm working on a class project to build a little Connect4 game in Java.
I'm working on a class-based php web app. I have some places where objects

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.