473,320 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Id v Class Selectors

I have been getting to grips with css recently (very slowly), and one area I
have a problem is when to use class selectors or id selectors. Are there any
guidelines when to use each type ? The key difference is, AFAICS, is that id
selectors can only be used once, whereas class selectors can be used
repeatedly ? Is this the main differentiator ?

Regards
Tony B

Jul 20 '05 #1
1 2600
Tony Benham wrote:
I have been getting to grips with css recently (very slowly), and one area
I have a problem is when to use class selectors or id selectors. Are there
any guidelines when to use each type ? The key difference is, AFAICS, is
that id selectors can only be used once, whereas class selectors can be
used repeatedly ? Is this the main differentiator ?


Well from a technical point of view, yes. But that doesn't mean that if you
only have one you should use id. Think of it this way:

id="foo" => This element is called 'foo'
class="foo" => This element is of type 'foo'

Now just because there may be only one element of type 'foo' on the page, it
doesn't mean that the element's _identity_ is 'foo'. Apart from anything
else, it's a pain to change it all around when you decide to add another
element of that class to a document.

There is also a difference in the way specificity is computed. Selectors
with ids in are more specific than selectors with classes in.

--
Jim Dabell

Jul 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Stoic | last post by:
Hi, I'm relatively new to CSS. I'm trying to get all the urls in a group of text bracketed by <p></p> tags to be underlined and everywhere else to NOT be underlined. As of now my code is like...
6
by: Jeff Thies | last post by:
We can do this: <tr class="data odd"> Can we make a rule that applies when only both classes are present? I suppose I could do this: <table class="data">
0
by: Robert | last post by:
Stephen, I think I figured out the problem. I was able to get Check Boxes and Option Buttons to work on my form by TURNING OFF RECORD SELECTORS on the form. Not sure why this would make a...
6
by: Rhino | last post by:
What's the simplest way to determine which, if any, of my selectors are not needed in a given stylesheet? I have a small number of HTML pages that share two stylesheets; one stylesheet is for...
4
by: planb | last post by:
Hi, I'd like to have a rollover like effect when a input field has the focus, any idea of how to do this with just CSS (easy enough with javascript)? What I'm thinking of is having a tips box...
2
by: PJ6 | last post by:
Is there a way of specifying the CSS class of all cells within a table without having to set the class attribute for each cell individually (server-side or client-side)? Paul
9
by: pbd22 | last post by:
In a manner of speaking. I just want to know if it is possible to style a page without using the class attribute? If so, how is this done? Thanks.
7
by: John Dann | last post by:
I'm unclear as to how best to use what I'm terming the top-level CSS selectors, by which I mean selectors like *, html and body. I'm coming at this from trying to understand how best to set font...
36
by: Roedy Green | last post by:
The only browser I have encountered that supports <colgroup><col class="behold"></colgroup> to apply a CSS style to a whole column, is Microsoft Internet Explorer. I have been told it SHOULD NOT...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.