Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 10:30 PM
Colin
Guest
 
Posts: n/a
Default 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

  #2  
Old July 20th, 2005, 10:30 PM
Barbara de Zoete
Guest
 
Posts: n/a
Default Re: class and id

Colin wrote:[color=blue]
> 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?
>[/color]
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

  #3  
Old July 20th, 2005, 10:30 PM
Jim Dabell
Guest
 
Posts: n/a
Default Re: class and id

Colin wrote:
[color=blue]
> 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?[/color]

Of course. They are two separate concepts, there's nothing about them that
would conflict.

[color=blue]
> 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.[/color]

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

  #4  
Old July 20th, 2005, 10:31 PM
Brian
Guest
 
Posts: n/a
Default Re: class and id

Barbara de Zoete wrote:[color=blue]
>
> 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>[/color]

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/

  #5  
Old July 20th, 2005, 10:31 PM
Barbara de Zoete
Guest
 
Posts: n/a
Default Re: class and id

Brian wrote:[color=blue]
> Barbara de Zoete wrote:
>[color=green]
>> 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>[/color]
>
> No. Both <p> elements have a border, because id has greater weight than
> class.[/color]

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles