Solved

Div ID Selectors or CSS classes for Layout

Posted on 2012-03-24
5
458 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
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
This video teaches users how to migrate an existing Wordpress website to a new domain.

713 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