/    Sign up×
Community /Pin to ProfileBookmark

which is better to use "id=" or "ul=" ??????????????????????

Hello,

I have been using elance contracts to design the more complex css elements of my website (the simple stuff I can do) and am a bit perplexed by one question.

Every contractor uses something like this:
<ul id=”navbar”>
<li></li>
</ul>
And not something like this:
<ul class=”navbar”>
<li></li>
</ul>

I thought that an “id=” cannot be attached to the same css rule more than once on the same page due to stability issues. For example, you could only use id=”navbar” once on the same page, but could use class=”navbar” repeatedly on the same page.

Thus, I have been switching the id=”navbar” to class=”navbar”. Both seem to work and using class=”navbar” means I can use a css rule repeatedly on the same page and not have to create two css rules that are the same but with different names to use them on the same page – reducing my css file size.

But the fact the elance contractors, who know more about css than me, are all using “id=” causes me to question is they are doing so for a good reason. Does anybody know the anwser?

Thank you!!!!!!!!!!!!!!

to post a comment
CSS

6 Comments(s)

Copy linkTweet thisAlerts:
@cbVisionOct 19.2012 — You're correct in using class here.

I typically try to use class whenever possible and use ID's when I need to target very specific things for JavaScript targeting. For example, I may have a submit payment button. I want it to use the default button class I have set up, but I also want it to do something special in jQuery. I'd have my HTML set up like this:

[CODE]<a class="button" id="btn-submit-payment">Submit Payment</a>[/CODE]

I wouldn't put any CSS for the #btn-submit-payment. All the CSS would be on the button class and can be used for anything. I would however target this element in Javascript to handle some onclick action. Here's an example of how I'd use it in jQuery:

[CODE]$('#btn-submit-payment').click(function(){
// function to make the button submit the payment
});[/CODE]


I would not want to use the class because it would execute this function for all elements with the button class. For example:

[CODE]$('.button').click(function(){
// cannot put the submit payment stuff here
});[/CODE]


So, to sum things up, I like using ID's when I need to use JavaScript for unique events and I try to always use classes for CSS. Some people may argue that using IDs such as #navbar will target faster than .navbar ... making the code run faster. I recall reading a study that tested this theory and the results didn't seem to make a difference.
Copy linkTweet thisAlerts:
@jokeOct 22.2012 — I totally agree with your usage, an ID should be used only once a page (cause if you call something by it's ID you want that one specific item you identified with your ID to answer) and class can be used more often (although you can also use it once).

On the other hand, why would you want to use a navbar more than once on the page?
Copy linkTweet thisAlerts:
@tracknutOct 22.2012 — 
I thought that an "id=" cannot be attached to the same css rule more than once on the same page due to stability issues. [/QUOTE]


By the way, it isn't "... due to stability issues". It's the way ID's (think of them as a noun) and classes (think of them as an adjective) are supposed to work.

Dave
Copy linkTweet thisAlerts:
@CowGirlauthorOct 23.2012 — Thank you all for your helpful explanations!

Joke, I only used "navbar" as an example and would not use "navbar" more than once on the page to avoid any confusion, given my pages only have one navbar.
Copy linkTweet thisAlerts:
@cbVisionOct 23.2012 — One case a navbar could be used multiple times is for navigation/options pertaining to certain sections.

For example:

[CODE]
<div>
<ul class="navbar">
<li><a href="#">Print</a></li>
<li><a href="#">Email</a></li>
<li><a href="#">Share</a></li>
</ul>
<h2>Section Heading</h2>
<p>Section Content</p>
</div>
<div>
<ul class="navbar">
<li><a href="#">Print</a></li>
<li><a href="#">Email</a></li>
<li><a href="#">Share</a></li>
</ul>
<h2>Section Heading 2</h2>
<p>Section Content 2</p>
</div>
<div>
<ul class="navbar">
<li><a href="#">Print</a></li>
<li><a href="#">Email</a></li>
<li><a href="#">Share</a></li>
</ul>
<h2>Section Heading 3</h2>
<p>Section Content 3</p>
</div>
[/CODE]
Copy linkTweet thisAlerts:
@tracknutOct 23.2012 — Or maybe they need to be styled differently in the different areas of the page:
<i>
</i>&lt;div&gt;
&lt;ul class="navbar top"&gt;
&lt;li&gt;&lt;a href="#"&gt;Print&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Email&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Share&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Section Heading&lt;/h2&gt;
&lt;p&gt;Section Content&lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul class="navbar middle"&gt;
&lt;li&gt;&lt;a href="#"&gt;Print&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Email&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Share&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Section Heading 2&lt;/h2&gt;
&lt;p&gt;Section Content 2&lt;/p&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;ul class="navbar bottom"&gt;
&lt;li&gt;&lt;a href="#"&gt;Print&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Email&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="#"&gt;Share&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Section Heading 3&lt;/h2&gt;
&lt;p&gt;Section Content 3&lt;/p&gt;
&lt;/div&gt;
×

Success!

Help @CowGirl spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...