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

class and id

I'm afraid I'm bursting with questions this week.

Here's another:

Is it possible to give an element a class AND an id?

I'm thinking of a situation where I might want to give
some style via the class which is applied more generally, but
would want to change the style of that particular element
via javascript by referring to that id. I have tested this
with mixed results and was wondering what the general theory was.

-Colin

Jul 20 '05 #1
4 3572
Colin wrote:
I'm afraid I'm bursting with questions this week.

Here's another:

Is it possible to give an element a class AND an id?

Yes, it is. You can combine classes with eachother and classes with
id's. Take care to know which attributes of the CSS are higher up in the
cascade. For example, if you have an id high up that sets a border and
than add a class which is further down your stylesheet that explicitely
gives a border:0 the element to which you apply the id and class will
have no border (unless of course it has !important added at the id
settings):

CSS:
#example { border:2px outset; }

#example1 ( border:2px outset !important; }

..example2 { border:0; }

HTML:

<p id="example" class="example2">This will have _no_ border.</p>

<p id="example1" class="example2">This will _have_ a border.</p>

Hope this helps.
--

Barbara

http://home.wanadoo.nl/b.de.zoete/html/weblog.html
http://home.wanadoo.nl/b.de.zoete/html/webontwerp.html

Jul 20 '05 #2
Colin wrote:
I'm afraid I'm bursting with questions this week.

Here's another:

Is it possible to give an element a class AND an id?
Of course. They are two separate concepts, there's nothing about them that
would conflict.

I'm thinking of a situation where I might want to give
some style via the class which is applied more generally, but
would want to change the style of that particular element
via javascript by referring to that id. I have tested this
with mixed results and was wondering what the general theory was.


Use id to identify particular elements, use class to group a range of
elements.

Don't forget that you aren't limited to labelling everything with class and
id attributes, you can select elements in other ways. It seems to me that
one of the biggest things that doesn't occur to people is that they can use
the document structure much more than they initially realise. For example,
instead of putting class="navigation" on all your navigation links, put
them in a <div id="navigation">, and simply select them with:

#navigation a { ... }

(or use something more appropriate than a <div> element, <ul> elements are
popular.)
--
Jim Dabell

Jul 20 '05 #3
Barbara de Zoete wrote:

Take care to know which attributes of the CSS are higher up in the
cascade. For example, if you have an id high up that sets a border and
than add a class which is further down your stylesheet that explicitely
gives a border:0 the element to which you apply the id and class will
have no border (unless of course it has !important added at the id
settings):

CSS:
#example { border:2px outset; }

#example1 ( border:2px outset !important; }

.example2 { border:0; }

HTML:

<p id="example" class="example2">This will have _no_ border.</p>

<p id="example1" class="example2">This will _have_ a border.</p>


No. Both <p> elements have a border, because id has greater weight
than class.

--
Brian (follow directions in my address to email me)
http://www.tsmchughs.com/

Jul 20 '05 #4
Brian wrote:
Barbara de Zoete wrote:
CSS:
#example { border:2px outset; }

#example1 ( border:2px outset !important; }

.example2 { border:0; }

HTML:

<p id="example" class="example2">This will have _no_ border.</p>

<p id="example1" class="example2">This will _have_ a border.</p>


No. Both <p> elements have a border, because id has greater weight than
class.


You're right. I didn't think of that. The combination only works with
all classes. Thanks for pointing that out.

--

Barbara

http://home.wanadoo.nl/b.de.zoete/html/weblog.html
http://home.wanadoo.nl/b.de.zoete/html/webontwerp.html

Jul 20 '05 #5

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

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
13
by: Bryan Parkoff | last post by:
I have created three classes according to my own design. First class is called CMain. It is the Top Class. Second class and third class are called CMemory and CMPU. They are the sub-classes....
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
8
by: Bryan Parkoff | last post by:
I find an interesting issue that one base class has only one copy for each derived class. It looks like that one base class will be copied into three base classes while derived class from base...
21
by: Jon Slaughter | last post by:
I have a class that is basicaly duplicated throughout several files with only members names changing according to the class name yet with virtually the exact same coding going on. e.g. class...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
0
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass...
1
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...
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: 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.