473,383 Members | 1,739 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,383 software developers and data experts.

IDs versus Classes

I'm renewing my interest in CSS and downloaded some stylesheets.
However, the .css uses both an ID and class. It looks like something
like this.

..class1 {
/* CSS code */
}

#styleID1 {
/* CSS code */
}

#styleID2 {
/* CSS code */
}

<DIV Class="class1">some text</DIV>
<DIV ID="styleID1">some text</DIV>
<DIV ID="styleID2">some text</DIV>

..class1 is not part of any other known tag, so why wouldn't one simply
use IDs throughout? Basically, what is it that classes can do that IDs
can't? Thanks.
-- Michel
Jul 20 '05 #1
9 1843
Michel wrote:
However, the .css uses both an ID and class.

.class1 is not part of any other known tag,
Class can be applied to most elments in html.
so why wouldn't one simply use IDs throughout?
Did you mean that you only saw a class attribute used once in the
document?
what is it that classes can do that IDs can't?


An id must be unique in each document.
<h2 id="bar">sub heading</h2>

It can be used in modern browsers to access a part of a document via a
fragment uri.
http://www.example.com/foo.html#bar

It can also be used in a css selector
h2#bar {font-size: 200%}

Class can be used any number of times in a document to organize it.
<h2 class="faq">faq topic 1</h2>
<p>answer to topic 1</p>

<h2 class="faq">faq topic 2</h2>
<p>answer to topic 2</p>

<h2 class="faq">faq topic 3</h2>
<p>answer to topic 3</p>

It too can be used for a css selector, but the specificity is lower
than for id.

--
Brian
follow the directions in my address to email me

Jul 20 '05 #2
On 17 Oct 2003 13:49:54 -0700, Michel wrote:
Basically, what is it that classes can do that IDs can't?


The formidable Brian already explained the essentials. I just wanted
to add that the question is not quite right... ids and classes are two
different things -- it's like asking "what can a color do that an
address can't?" (both of a house, for example).

Ids are unique (within a page) identifiers of elements. Classes are
groups of elements -- elements that have something in common.

It may happen that you look at a document and you see only one single
instance of a class. But maybe that class gets used multiple times in
other documents that use the same stylesheet, or there were multiple
instances and modifications removed all but one, or the designer
planned for the future and saw the possibility of later modifications
introducing multiple instances.

Or maybe the designer simply decided that in this case, the associated
style was not a feature of _one_ unique element, but something that
could be a feature of a class of elements -- no matter whether there
actually are multiple instances of that class in the document.

Jul 20 '05 #3
In article <13**************************@posting.google.com > in
comp.infosystems.www.authoring.stylesheets, Michel
<m.********@att.net> wrote:
Basically, what is it that classes can do that IDs
can't?


class="x" may be used many times in a given document. ID="x" must
not be used more than once in any document.

--
Stan Brown, Oak Road Systems, Cortland County, New York, USA
http://OakRoadSystems.com/
HTML 4.01 spec: http://www.w3.org/TR/html401/
validator: http://validator.w3.org/
CSS 2 spec: http://www.w3.org/TR/REC-CSS2/
2.1 changes: http://www.w3.org/TR/CSS21/changes.html
validator: http://jigsaw.w3.org/css-validator/
Jul 20 '05 #4
In article <MP************************@news.odyssey.net>,
Stan Brown <th************@fastmail.fm> wrote:
Basically, what is it that classes can do that IDs
can't?


class="x" may be used many times in a given document. ID="x" must
not be used more than once in any document.


Also, the ID attribute can be addressed by CSS which will have a higher
authority over styles associated with classes. ID labeled elements can
also be easily targeted by JavaScript.

Just two practical advantages I thought worth mentioning.

--
Kris
kr*******@xs4all.netherlands (nl)
"We called him Tortoise because he taught us" said the Mock Turtle.
Jul 20 '05 #5
AD.
On Sat, 18 Oct 2003 16:20:06 -0400, Stan Brown wrote:
In article <13**************************@posting.google.com > in
comp.infosystems.www.authoring.stylesheets, Michel <m.********@att.net>
wrote:
Basically, what is it that classes can do that IDs can't?


class="x" may be used many times in a given document. ID="x" must not be
used more than once in any document.


And an element can also have more than one class eg: class="x y".

Cheers
Anton
Jul 20 '05 #6
CJM
I think the other guys have this thoroughly covered, but I thought of a
little addition that might be worth considering...

It may be the case, in this example, that the styles are used only once, and
that specificity is not an issue. In which case, the author could as easily
opted to use IDs throughout. However, the author may well be planning
ahead... he may have further pages coming that will use these styles again
and again. Better he build this in now, than to have to go back and changes
his IDs to Classes later on.

On the other hand, I might be talking out of my pants, and the situation is
simply that the author has make an arbitrary decision to simply use Classes
in this example! :)

Chris

"Michel" <m.********@att.net> wrote in message
news:13**************************@posting.google.c om...

.class1 is not part of any other known tag, so why wouldn't one simply
use IDs throughout? Basically, what is it that classes can do that IDs
can't? Thanks.
-- Michel

Jul 20 '05 #7
In article <pa****************************@privacy.net> in
comp.infosystems.www.authoring.stylesheets, AD. <me@privacy.net>
wrote:
And an element can also have more than one class eg: class="x y".


Yes, but MSIE doesn't do the right thing. I can't remember whether
it ignores all the classes or all after the first.

--
Stan Brown, Oak Road Systems, Cortland County, New York, USA
http://OakRoadSystems.com/
HTML 4.01 spec: http://www.w3.org/TR/html401/
validator: http://validator.w3.org/
CSS 2 spec: http://www.w3.org/TR/REC-CSS2/
2.1 changes: http://www.w3.org/TR/CSS21/changes.html
validator: http://jigsaw.w3.org/css-validator/
Jul 20 '05 #8
Stan Brown <th************@fastmail.fm> wrote:
In article <pa****************************@privacy.net> in
comp.infosystems.www.authoring.stylesheets, AD. <me@privacy.net>
wrote:
And an element can also have more than one class eg: class="x y".


Yes, but MSIE doesn't do the right thing. I can't remember whether
it ignores all the classes or all after the first.


Which version of MSIE?

I can't remember what IE4 did but IE5+ applies all the classes. Try it
and see for yourself.

Steve

--
"My theories appal you, my heresies outrage you,
I never answer letters and you don't like my tie." - The Doctor

Steve Pugh <st***@pugh.net> <http://steve.pugh.net/>
Jul 20 '05 #9
In article <cp********************************@4ax.com> in
comp.infosystems.www.authoring.stylesheets, Steve Pugh
<st***@pugh.net> wrote:
I can't remember what IE4 did but IE5+ applies all the classes. Try it
and see for yourself.


I'll be happy to take your word for it. I know this has come up in
the past and I was warned off multiple classes because _some_
browser in fairly common use screws that up.

I's like to try MSIE5, but I can't because Microsoft won't let us
have multiple versions of MSIE installed on the same machine.

--
Stan Brown, Oak Road Systems, Cortland County, New York, USA
http://OakRoadSystems.com/
HTML 4.01 spec: http://www.w3.org/TR/html401/
validator: http://validator.w3.org/
CSS 2 spec: http://www.w3.org/TR/REC-CSS2/
2.1 changes: http://www.w3.org/TR/CSS21/changes.html
validator: http://jigsaw.w3.org/css-validator/
Jul 20 '05 #10

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

Similar topics

6
by: Kevin | last post by:
Hi! I recently completed a graduate level Java class at a local university. For our class project my group built a web-based application. Basically, the application would let a manufacturing...
2
by: BillyO | last post by:
I need a general mechanism for passing parameters to an API function I am designing, call it foo. Foo processes data, the operation used to process the data and the parameters for that operation...
3
by: Brad Simon | last post by:
My Client asked me to put together some reasons as to what determines if an application would be developed as a windows application or web application. I have started working on it, but I would like...
9
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
2
by: Andrew Robinson | last post by:
I need to create a shared static field for use within a number of different classes. Which one should I be using or are they all really the same thing? public class Widget { private Widget() {}...
4
by: Mark Kamoski | last post by:
Hi Everyone. What is the real difference between writing a Sub versus writing a Function with no return value? It seems to me that both of these would need to compile to the same IL, so it...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
13
by: André | last post by:
Hi, i'm developping asp.net applications and therefore i use VB.net. I have some questions about best practises. According what i read about class and module and if i understand it right, a...
1
by: kilik3000 | last post by:
I've been using reflector to look into some of the core MS .NET assemblies. I have noticed this pattern in the naming of classes that wrap p-Invoked Win32 api functions. I like this pattern. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.