Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

#Intro or .Intro?

Question posted by: shapper (Guest) on June 27th, 2008 07:19 PM
Hello,

When styling a DIV as follows:

<div id="intro" class="Intro">Some intro text</div>

Should I use a class:
..Intro {...}

Or:
#Intro {...}

As far as I know if this div is unique I should use #Intro, if their
styles are not unique I should use .Intro.

But I read somewhere that I should use always .Intro and not
#Intro ...

Just checking.

Thanks,
Miguel
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Andy Dingley's Avatar
Andy Dingley
Guest
n/a Posts
June 27th, 2008
07:19 PM
#2

Re: #Intro or .Intro?
On 2 Jun, 13:36, shapper <mdmo...@gmail.comwrote:
Quote:
Originally Posted by
<div id="intro" class="Intro">Some intro text</div>
>
Should I use a class:
.Intro {...}
>
Or:
#Intro {...}


Select on the class. (Fairly) commonly discussed issue in this ng.

This doesn't affect the HTML though, it's quite reasonable to use both
in the document markup.

Jim Moe's Avatar
Jim Moe
Guest
n/a Posts
June 27th, 2008
07:19 PM
#3

Re: #Intro or .Intro?
On 06/02/08 05:36 am, shapper wrote:
Quote:
Originally Posted by
>
<div id="intro" class="Intro">Some intro text</div>
>
Should I use a class:
.Intro {...}
>
Or:
#Intro {...}
>

The advantage of #Intro:
- It can be linked as fragment <a href="#Intro">.
- Javascript can find it with DOM

Disadvantage:
- Must be unique

In general I prefer using a class unless one of the advantages for an ID
are compelling.

--
jmm (hyphen) list (at) sohnen-moe (dot) com
(Remove .AXSPAMGN for email)

Harlan Messinger's Avatar
Harlan Messinger
Guest
n/a Posts
June 27th, 2008
07:19 PM
#4

Re: #Intro or .Intro?
Jim Moe wrote:
Quote:
Originally Posted by
On 06/02/08 05:36 am, shapper wrote:
Quote:
Originally Posted by
><div id="intro" class="Intro">Some intro text</div>
>>
>Should I use a class:
>.Intro {...}
>>
>Or:
>#Intro {...}


If you have id="intro" as above, #Intro won't match it. IDs and class
names are case-sensitive.
Quote:
Originally Posted by
The advantage of #Intro:
- It can be linked as fragment <a href="#Intro">.
- Javascript can find it with DOM


- Its specificity in the CSS cascade is higher than that of .Intro.

Jeff's Avatar
Jeff
Guest
n/a Posts
June 27th, 2008
07:19 PM
#5

Re: #Intro or .Intro?
Andy Dingley wrote:
Quote:
Originally Posted by
On 2 Jun, 13:36, shapper <mdmo...@gmail.comwrote:
>
Quote:
Originally Posted by
><div id="intro" class="Intro">Some intro text</div>
>>
>Should I use a class:
>.Intro {...}
>>
>Or:
>#Intro {...}

>
Select on the class. (Fairly) commonly discussed issue in this ng.
>
This doesn't affect the HTML though, it's quite reasonable to use both
in the document markup.



Is that really the consensus here?

I don't see any hard and fast rules for naming conventions (aside
from only one ID per page).

I like to name these things to increase readability. If I assign an
ID to something, I know that it is a section of the page. Classes I
reserve for presentation issues. Others are more comfortable with
everything as a class.

I'm happiest with stylesheets that have almost no classes, with nearly
everything styled as a descendant of a section.

Jeff

Roderik's Avatar
Roderik
Guest
n/a Posts
June 27th, 2008
07:19 PM
#6

Re: #Intro or .Intro?
shapper schreef:
Quote:
Originally Posted by
Hello,
>
When styling a DIV as follows:
>
<div id="intro" class="Intro">Some intro text</div>
>
Should I use a class:
..Intro {...}
>
Or:
#Intro {...}
>
As far as I know if this div is unique I should use #Intro, if their
styles are not unique I should use .Intro.
>
But I read somewhere that I should use always .Intro and not
#Intro ...
>
Just checking.
>
Thanks,
Miguel


I would say .intro to say: this is of that type (it is an introduction)
And use #intro to say: this is that element (it is the introduction).

So for styling... it the depends a bit on how you look at it. It might
be unique in this case but you merely say it is of type intro. Therefore
I would prefer the class .intro in this case.

Sometimes I use both and then I use #intro for the positioning and
..intro for the styling. But that might be unneccesary overhead.

dorayme's Avatar
dorayme
Guest
n/a Posts
June 27th, 2008
07:19 PM
#7

Re: #Intro or .Intro?
In article <2uCdnRxysPj_3NnVnZ2dnUVZ_gGdnZ2d@giganews.com>,
Jim Moe <jmm-list.AXSPAMGN@sohnen-moe.comwrote:
Quote:
Originally Posted by
On 06/02/08 05:36 am, shapper wrote:
Quote:
Originally Posted by

<div id="intro" class="Intro">Some intro text</div>

Should I use a class:
.Intro {...}

Or:
#Intro {...}

The advantage of #Intro:
- It can be linked as fragment <a href="#Intro">.
- Javascript can find it with DOM
>
Disadvantage:
- Must be unique
>


Yes, good. I would add an extra advantage to using an id when the author
is consciously meaning to only have one reference per page. It helps the
author keep better track of what he is doing, it is information for him.

--
dorayme

 
Not the answer you were looking for? Post your question . . .
183,968 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors