02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Talking to HTML items with IDs

Over the last few lectures we discovered how to define rules that would apply to all elements or specific elements with a class reference. We now turn our attention to talking directly with unique items. By the end of this lesson, you will know how to define ids to html elements and how to implement styles for them.

Video Transcript

00:00We are finally at the third last major rule.
00:03Now, the first rule we saw, or the first selector,
00:06enabled us to select every element.
00:09The next rule was every class,
00:11which enabled us to have multiple things selected
00:13at the same time where we controlled it.
00:15The last rule is even more simple
00:17where it's when you want to select
00:18only one specific item exclusively.
00:21In that case, we use IDs,
00:23which are something that is known to you probably in HTML,
00:25and if not, it's literally the word ID
00:28in your HTML attributes, and then you define it a name,
00:31and it has to be unique.
00:33Once it has a unique ID, name, excuse me,
00:36you could then control it via CSS
00:38and talk to it directly, specifically, personally,
00:40and you could only have one ID per HTML page.
00:43Remember, if you have multiple HTML pages,
00:46it's fine for you to have that same ID on multiple pages,
00:49such as maybe your header ID or your footer ID
00:52because they are unique
00:53and they would not repeat themselves in that page again.
00:56So let's see it in action.
00:58We're ready to talk about the last building block.
01:01As we said, we talked about classes.
01:04Now IDs, contrary to a class,
01:07is when we want to set something
01:08that could only happen once in an application.
01:11We're giving a specific ID to an element.
01:14For example, this div has a name,
01:17and its name is question.
01:19Now, if I wanted this question div
01:21to have a background that is, let's say, blue,
01:23how would I do that if I wanted
01:25to do it in traditional HTML?
01:28How would I do it with a class?
01:29How would I do it with an ID?
01:31It's something we're gonna look at in the next lesson.
01:33But what I wanna look at right now
01:34is how can I talk to it directly using its ID?
01:38Now, to do that is incredibly easy.
01:40What I'm gonna do is I'm gonna start it with a hashtag,
01:42and then I'm gonna set the name that's inside of the ID.
01:45In this case, it's gonna be hashtag question.
01:48Now, I could set IDs to any item,
01:50but really the rule is one ID that is unique per item,
01:53so you can't have the same ID being used
01:55on the same HTML page more than once.
01:59So now that we set our element,
02:00all that's left for me to do is set a background color,
02:02and I'll go ahead here and just set it to be blue,
02:04so I'll set my red to be zero, green to be zero,
02:08and my blue to be completely full.
02:11If I go ahead here and I'll just also delete
02:13this extra semicolon because we don't need it,
02:16and if I go ahead and save this and click on refresh,
02:18this time around we're gonna see that our item,
02:21our question itself is now being highlighted,
02:23and it's blue, obviously a very horrible color,
02:27but let's soften it up a little bit maybe
02:30by changing the color to be a beautiful
02:33little velvety type of blue.
02:36All right, so that's it about IDs,
02:38and I just wanna summarize again
02:40the types of things that we saw.
02:42We saw directly accessing items,
02:45we saw how to approach items using a class
02:47and how to create a class,
02:49and in this video we've seen how to create an ID
02:52and how to reference an item with an ID.
02:55So again, if I wanted to, for example,
02:57use in my footer ID instead of a class,
03:00that makes sense, I could literally just say,
03:02okay, this is gonna be an ID,
03:03because there's really only one footer in every application
03:06that would never be more than one footer,
03:08so I could go ahead and define it
03:10and configure it and set it with the ID of footer,
03:13which would mean that whenever I'm gonna start
03:15creating different footer rules,
03:17I would probably wanna make sure
03:18that those rules are defined using our ID
03:22instead of our class reference.
03:26So if I'm gonna go ahead here and I'm gonna create,
03:28let me create something here from my footer,
03:30I would go ahead and set the hashtag footer,
03:33and I would say, let's say I want my background color
03:36to be a color that I already pre-configured earlier,
03:39so I'm gonna go ahead and just copy that color,
03:42and I'm gonna go ahead here and just copy it,
03:46and I'm gonna go ahead and add here another item
03:48that I want its width to be 98%,
03:54and I want my padding to be 1%.
03:56Notice how I'm using now this time
03:58around relative positioning.
04:01Now that I set my relative positioning,
04:03all that's left for me to do is to save this,
04:05and if I go back into my element and click on refresh,
04:08we'll see that now my footer has a defined color.
04:11It's taking the whole space of my element
04:14because I used a property which is called width,
04:16and I said I want to use 98% of the screen
04:19and add a padding of 1%, which added a padding
04:21both above, below, and left and right.
04:25Now don't worry about the technical details
04:27of specific items right now.
04:29I want to continue on doing a little bit more,
04:31no, that's enough for now, right?
04:33In the next lesson, I want to go back
04:35and revisit the code that we had so far,
04:37adding a little bit more code
04:39as we explore everything we've done so far
04:42in a little bit of rehearsal as we build up
04:44and get our application closer and closer
04:47to where we want it to look like in our final page, right?
04:51So see you in the next lesson
04:52when we continue exploring what we learned already.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!