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

What is the DIV tag and how ot use it

In the last few video sessions we explored how to create and use styles using CSS. In this session we will take it one step further by introducing the DIV tag. The div tag on it's own doesn't have a meaning. All the DIV is is a grouping of a complete group of elements. It is commonly used to group visual elements together that don't have a descriptive value so that we can treat them all as one.

Video Transcript

00:00Alright, last we left off, we were stylish. I think we were. But one of the problems with
00:04our styling was that we couldn't really control how fantastic it looks like as our browser
00:11changes in its sizes. And one of the main reasons was because our alignment of this
00:15element and this element were completely different and separated from each other.
00:20Wouldn't it be nice if we could cluster them up together and turn them into this one mega
00:25element that we could control? We definitely could. The way we do that is using the div
00:30attribute or the div HTML element or even the style or even the span element. But let's start
00:36working with the div element and understanding how it works. So I'm going to go into my editor
00:42and this time around I'm going to basically just come into this area, take this whole block of copy
00:47and I want to group them up together and I'm going to use the div tag.
00:50Now the div tag itself on its own doesn't have any meaning. It's just kind of like a grouping
00:58that is not in line. Basically it's a grouping of a complete thought you could say or grouping
01:04of a complete element. In HTML5 there was a push to try to use more descriptions such as an
01:11article or an article. Basically there's a lot of different tags that came to kind of represent
01:20content. Now if it doesn't have a description and you don't have a way to give it a name and give it
01:26a specific functionality name then your best bet is a div usually. Now when you create a div for us
01:32to be able to talk to it in CSS we have to give that div name an id. So we could then approach it
01:37in CSS and start interacting with it because there could be many different divs on a page.
01:42To do that I'm going to put inside the id attribute and the id attribute I could give
01:47any name that I want. In this case scenario I'll call this style geek. Alright so this is going to
01:55be our id is going to be style geek. By the way we could give an id to every single internal
02:00HTML element inside of our body and then control it in CSS the way we're going to do right now.
02:06Now that we know that our element is called id we don't have to approach it using the
02:12name of the attribute itself we could instead approach it by setting it to its actual name,
02:18its actual id by setting the hashtag and then its id. By doing that everything that we do inside of
02:24here is only going to to actually work on our element that has that specific id. One more
02:31thing to note is you could only have one id's are unique you can have an id geek style geek
02:38and have another something with style geek. Basically you're saying this is a unique and
02:42only occurrence of that specific style. Alright so now that we know that we're talking to the style
02:48geek we could start maybe thinking about how do we lay out our our elements. So for one I'm going
02:53to just undo kind of a big portion of our control that we had over here and first of all I want
03:00to take out all the different overall controls and overall sizes and I'm going to go to my
03:06div element and define my div element with that width. So I'm going to say that everything
03:11inside of this div element will have a maximum width of 800 pixels. By doing that if I go back
03:17into my browser and I click on refresh you'll first and I click on refresh nothing will happen
03:26and the reason nothing is happening is because we didn't link our new div file with the
03:32html file so I want to go up here and change the linkage to that div. So link is not going to be
03:37link anymore it's going to be div so now we're kind of connecting the new file that we have
03:42with the new styling. If we go back into our application now we should see that our elements
03:46should now be boxed inside of 800 pixels which are not enough for our b stylish html so we
03:52might or make our stylish smaller or make our div area bigger but because we know that we have
03:57at least a thousand pixels on our screen I'm going to set it to be a thousand pixels.
04:02Just to make sure that our div we're going to make even more and make a thousand one hundred
04:07because we really just want to get it all in and if we're kind of with a thousand one hundred then
04:10we might need to make our style smaller. So I'm going to go here and make it maybe a hundred
04:14points instead because I really want everything to fit inside of my box that I created. What did
04:19I do? I closed the browser. All right so we refreshed where I reopened my browser you can
04:25see that it's starting to not really shape up but we're grouping we're starting to group our
04:29elements together. Now let's start working on our design all over from scratch now that we have one
04:34built-in element. We're not going to worry too much about the overall positioning yet. We want
04:38to first figure out the positioning internally inside of this div area. Now to make it even
04:43more obvious what I want to do is not because we're really going to do this in the real world
04:47I'm just going to put your background just to make it really really clear where our box is.
04:51I'm going to set it to be red which is RGB so I put on my red to be completely full and my green
05:01and blue to be completely empty at zero which will make it really really red and if I go back into my
05:06app again click on refresh we'll see that our box itself now we can see exactly where our box is.
05:12Now that we see where our box is though I want to start working around and kind of like tweaking
05:16our layout. First of all I want my copy this copy to be to take less space really and really for
05:21our sake we probably want this copy to be a little bit bigger we want it to take that full
05:25box area so to make it take the full box area or close to that full box area I'll raise it to 110
05:31pixels and I'm going to go into my paragraph and I am going to push my paragraph up a bit and I'm
05:36going to use that using the margin top and I'm going to set my margin top to be minus 100
05:42pixels. I'm going to save that click on refresh I just want to see that we're getting closer and
05:47we're definitely getting closer things are getting tidied up the way we want it to be.
05:52The next thing is we really don't want our copy to be as long as our stylish copy so again I'm
05:56going to go into my copy area and I'm going to set a more defined width to it that is smaller
06:02so I'm going to set its width to be maybe 800 pixels you know about 200 300 pixels less than
06:08our main large image above but still we are inside of the same box component. Now if I move push
06:15this aside in some level or some form that's still fine so I could go inside of here and basically
06:21add another margin margin left this time around which is very similar to what we did previously.
06:27The only difference between the two now is that I'm doing these margin changes inside of my box
06:32which means that whenever I decide to move the box itself as well if I decide to move the box
06:38everything inside of the box is going to move with it. Oh excuse me microphone for pushing you
06:42again. All right so I'm relatively happy with the position itself all that's left for me now
06:46to do is to try to push my content a little bit. To push my content what I want to do is I want
06:52to go to my div element itself and I'm going to go to my stylish div element and I could set
06:57to it maybe text align and I could maybe send it to center maybe taking the whole div element
07:04and centering it that way. So first of all I'm right so I'm basically and basically by doing
07:15that by the way by setting it to center I don't need to set my paragraph to center anymore because
07:20as soon as I set my div to center it will automatically center all the content that's
07:25inside of my um children elements. So in that case my paragraph doesn't need that anymore it's
07:31just an overkill and because I don't need it anymore if I go back into my element click on
07:37refresh my element will be it won't change this would be centered as well it's in the right
07:42position they're all centered really all that's left is if I wanted to I could now move my box
07:47one way I could move it I'm just going to show you one way because this course is not about CSS
07:50but it's about HTML and the real point of this video was really just about getting you familiar
07:55with the div tag but I want to do one more thing so the one more thing that I want to do
07:59is I want to show you something kind of cool you could set your um your position in your in
08:06of the element I could say that I want the position to be absolute once I set it to be
08:10absolute I could now tell it exactly where I want it to be so I could say to that I want it to be
08:15100 pixels from the top I want it to be 100 pixels from the pickles from the left so it's
08:21100 pixels from the top pick and it will push my content to be 100 and 100 pixels away
08:29so it's exactly 100 pixels away on both edges from the top
08:34all right yeah that's about it really so that's really our div element in the next video we'll
08:39talk about the span and the difference between the div and the span and why do you need to know
08:43the difference between them

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!