Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Div ID Selectors or CSS classes for Layout

Posted on 2012-03-24
5
Medium Priority
?
483 Views
Last Modified: 2012-03-24
I've been using ASP.NET and CSS for Layout for a while now, but am not sure whether to use CSS classes or id selectors as the main means of controlling layout and style. For a while I've used mainly id selectors, but now I see there seems to be a trend towards CSS classes.

I guess the real question is when to use id selectors, and when to use classes. They both have their purposes, but it's easy to fall into using mainly one or the other. Microsoft samples seem to be using mainly classes these days??
0
Comment
Question by:mitdaniels
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 664 total points
ID: 37760142
'id's are supposed to unique on a page so that javascript can find the one and only instance of that id.  Classes are meant to be more general to be used where ever they are needed or wanted.

http://www.w3schools.com/css/css_id_class.asp
0
 
LVL 25

Accepted Solution

by:
Rouchie earned 1336 total points
ID: 37760241
To build on DaveBaldwin's excellent answer, and give you an example, use ID's on elements that occur once - so - a navigation bar might only appear at the top of the page, so that could have CSS applied via ID.
Another example might be a footer <div> containing copyright message.  It only appears once at the bottom, so ID could be used there too.

However, as you are aware, the above could use a CLASS instead.  It makes no difference really.  When using Javascript to locate elements (e.g. to perform functions on elements using JQuery, e.g. hide/show a footer based on a condition), finding elements by ID values is much faster than by class.  Therefore, by using CSS with ID, developers avoid having ID and CLASS together for certain elements.

Hope that helps.
0
 

Author Comment

by:mitdaniels
ID: 37760754
Thanks for the comments.

Therefore, by using CSS with ID, developers avoid having ID and CLASS together for certain elements.
Are you saying that we should try not to use ID selectors and classes together for the same DIV or element?
0
 
LVL 25

Assisted Solution

by:Rouchie
Rouchie earned 1336 total points
ID: 37760979
If you can avoid it, it just makes for easier maintenance and makes the code easier to read and/or debug
0
 

Author Closing Comment

by:mitdaniels
ID: 37760992
Thanks, just what I needed.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question