Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Beginner Question – Div id/Class

Featured Replies

Hi Guys,

 

I have a small question. I have been watching this tutorial on how to build your first site and found it very helpful, I mean, I managed to recreate what this guy on the video did quite successfully. However, afterwards I checked the code and all the divs that had been done were all <div class= and not <div id=. Now i understood it that 'ids' are used for unique items, yet whether they were unique items or not this guy in the tutorial just did them all as a classes? Is this just back workmanship or can you get away with it?

 

Cheers

Andy

Edited by 19at

Lately I've started to use classes even for things i would normally use an ID for.

There are many reasons i'd go in to but unless you're using ID specifically to target Javascript, i'd just use classes.

 

Sticking to classes certainly helps out with specificity as well. Always a bonus.

I personally use ID's for things such as specific/unique containers or elements that I know I will only be calling once, or as mentioned above that I want to be able to easily target with javascript. It's not necessarily bad workmanship to primarily use classes, it is just not my personal preference; we all have our own coding styles. With a little planning and foresight it is easy to figure out where you would want to use and ID over a class and vice versa.

 

You are ahead of the game just by knowing that there is a difference between ID's and classes, so cheers to you for that!

 

 

There are many reasons i'd go in to but unless you're using ID specifically to target Javascript, i'd just use classes.

 

Do you mind explaining a few? Maybe you can give me some insight to a new perspective I have not thought about before and possibly improve my methods. If you do not have that time, that is totally understandable.

Do you mind explaining a few? Maybe you can give me some insight to a new perspective I have not thought about before and possibly improve my methods. If you do not have that time, that is totally understandable.

What I've learnt about Classes vs IDs

 

Specificity problems when styling.

ID's have a much higher (I don't know how to explain it properly) control over styles. For example, it would take 255 classes on an element to override the style given to it by an ID, and that's just because of memory limits in CSS. e.g. Example. This could introduce problems like this here. There is only one summer drinks list and multiple favourites so using ID's and classes there is justified, but it doesn't work out for styling.

 

 

Multiple classes.

You can add more than one class to an element which makes styling much easier and reusable (modular css).

 

Browser functionality.

Classes don't do anything in the browser but ID's do, hash values. For example, on this page http://www.webdesignerforum.co.uk/topic/65800-beginner-question-div-idclass/?do=findComment&comment=415417, will take you to Mike's post.

 

From these things I got into the habit of only using classes, especially when it comes to styling. The only time I use an ID is when I need one, anchor links and form labels are good examples because they both need IDs for them to work.

 

Hope this is useful to anyone :)

 

Sources/Useful links:

http://css-tricks.com/the-difference-between-id-and-class/

http://css-tricks.com/specifics-on-css-specificity/

I am going to check out the provided link after I get out of the office. I was unaware about the issue with hash values. I am nearing the end stages of a massive-retail project that I have been working on, might be worth going back and optimizing a bit in my down time; really wouldn't take too long to do. In all honesty I really do not use ID's too often but I am always looking to improve my abilities.

 

You are totally right about style control when concerning ID's gets annoying at times. Hmmmm, thanks to both of you it honestly never occurred to me to "keep it classy" :).

 

Thanks again!

I find that just using classes is a great way to go, or ID's if I just feel like getting inventive.

Since I started coding I haven't really known any specific differences, and just found it too menial to try to differentiate.

Oh well, they both work for most functions that you as the coder could have in mind, save for the details that our mods can cook up :p

  • Author

Thanks guys! I will be sticking to classes then... well until I need to venture into anything more advanced like trying to target Javascript.

 

 

I am trying to build my very first site and taking elements from tutorials I have read and watched... but for some reason I am having problems with certain classes popping out of my overall wrap div? I didn't think this was possible as the overall wrap div is meant to contain everything??

 

Sorry it is probably not the best explanation... but is there a place I could paste in the code so if anyone had a mo they could pinpoint where I have gone wrong?

 

Cheers

  • Author

Heydan, cheers for sorting out my above issue!

 

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.