Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS CSS Basics (2014) Basic Selectors ID Selectors

jiwan gurung
jiwan gurung
4,248 Points

can u have an id in a <p> tag or it can be only in <div> tag?

can u have an id in a <p> tag or it can be only in <div> tag?

5 Answers

Hi there,

Just thought I'd give a little bit more detail here - you can put an id on anything, but they should really only be used for an item that only appears once per page, that will have its own styling or that might need to be easily targeted on its own in JavaScript. Classes are more flexible - they can be put on multiple elements to style or easily select them together.

I'm not going to say you should never use IDs, though - they have some benefits. One is using links to various points on the page - say you had a long document on your web page that had headings. You could provide table-of-contents-style navigation on the page that would link to that location on the page, so the user doesn't have to scroll and look for it - that is done with IDs. I also mentioned JavaScript - an ID can be useful if there is functionality that you only want to attach to a single element on a given page, because selecting by ID in JS is pretty simple and wouldn't be affected if you refactored your HTML or added more items to a class (as long as you don't remove the ID).

It's also important to note that an element can have both an ID and a class, so you can use them both as needed.

Patrik Horváth
Patrik Horváth
11,110 Points

As dragos busuioc told, you can use "#" ID to any Tags in HTML but make sure you have only 1 ID with same name because ID is unique and you cant have 2 Same IDs in your HTML code ( page )

using ID or Class it doesnt metter use whot you preffer :), sometimes are good Classes but sometimes are IDs

Classes for multiple changes in your code and repeateble

ID for single change/ hot fix / JavaScript / .....

dragos busuioc
dragos busuioc
24,908 Points

all tags can have an id.but is a bad practice.i suggest using class

jiwan gurung
jiwan gurung
4,248 Points

thank you guys...that really clears a lot...one more thing...how can i start building a site without using the workspace from treehouse?

For a site with HTML/CSS/JavaScript, you just need a text editor. Your OS might come with a basic one (like Windows NotePad), but most people like to use one that actually has code highlighting and such. A couple popular ones are Atom and Sublime Text. There are others too - feel free to do a quick Google and choose the one you like the look of. When you're building your site, just make sure you have the correct file extensions (.html, .css, .js), and you're good to go.

To implement additional things, like libraries (JQuery for example), just hop over to its website. There are usually instructions online for installing whatever you decide to practice with in the future.

jiwan gurung
jiwan gurung
4,248 Points

thank you so much...that really clears a lot...