Solved

Div ID Selectors or CSS classes for Layout

Posted on 2012-03-24
5
469 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 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 166 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 334 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 334 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

688 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