Types of position layout
Over the last few chapters our focus was directly associated with items while we had no control over how they relate and react to other elements on the HTML page. We covered all the core rules of laying relatively things out in CSS and we are ready to move to a now topic: absolute positioning. By the end of this lesson you will know how to position items in absolute terms.
Video Transcript
00:00So far, everything we had on our HTML page
00:03was defined with relative paths.
00:06We might have not noticed it,
00:08but every time we put an item onto the stage,
00:10it automatically stacked underneath it
00:12based on the rules of the governing item that was above it.
00:16In this lecture, we're gonna learn about
00:18different types of layouts,
00:20layout types, and it will enable us
00:21to break out of that mold, creating absolute pathing
00:25and creating stacking of elements
00:27where we'll have one element and another one above it.
00:30By the end of this lecture,
00:31you're gonna quadruple your capabilities in CSS,
00:34and with it, we are gonna wrap up this chapter.
00:36So let's see it in action.
00:38All right, let's go ahead and start playing with positioning.
00:40I'm gonna go right into my footer
00:42and I'm gonna go ahead and add a positioning,
00:44changing that default position from being static,
00:48which was the default.
00:49Let's go ahead and change that to be relative.
00:52Now, the relative positioning, if I just save it
00:55and go and refresh my screen,
00:57we're gonna see that nothing changed.
00:59There's absolutely nothing that happened.
01:01But now that my positioning is relative,
01:03now I can take control over using the top, right, bottom,
01:06and left commands, which until now I couldn't.
01:09So that was the top, right, left, bottom.
01:16They did not exist for us.
01:17They did not do anything even if we've used them
01:19until we moved into relative.
01:22Now that we're in relative positioning,
01:23I could go ahead and change,
01:24let's say I wanna change my positioning now to be,
01:28and by the way, I'm still in relative positioning,
01:31but now I'm just gonna push my content
01:32and offset it based on whatever I want.
01:34So for example, I might wanna push it right by 40 pixels
01:38and I wanna make sure that maybe I wanna push it
01:40from the bottom by 100 pixels as well.
01:44And I'm just gonna go ahead and click on refresh
01:46and we're gonna see that now my item
01:47is gonna move offset from its position
01:51and it's not affecting other items,
01:52although it's sitting on top of the stacking system, all right?
01:57Now the next step that I wanna do,
01:59and it's really just based on the positioning of the item,
02:01but the next step that I wanna do
02:03is I wanna go ahead and talk about
02:05beyond relative positioning, which basically adheres
02:08to the rules of where we were at
02:09and just applies to them a relative change
02:12that it does not affect other elements in the display.
02:17The next item beyond relative is absolute.
02:20And absolute positioning is really great,
02:22but in this stage for absolute positioning,
02:24or let's say for example, I just wanna set my item
02:27to be at the bottom bottom position of zero pixels,
02:30basically saying that I want my item
02:32to be in an absolute position of a bottom.
02:35And notice how it's in the bottom of my visible screen
02:39of my visible viewport currently.
02:42Only problem with my absolute positioning
02:44is that this is gonna be absolute
02:46in this position forever.
02:47If I start scrolling down,
02:49that is not gonna change,
02:50it's gonna stick to where it was.
02:52So that's the difference between absolute and relative will,
02:56although relative and absolute will not move
02:57once they're in a certain position that they're at.
03:00The absolute positioning, if I set it based off
03:03of the bottom, but I could have set it
03:04with the top, right, left, or bottom,
03:07it would just sit in that specific spot
03:08and never move from that spot.
03:11But as my display is changing,
03:13it's gonna move away from my view.
03:16Contrary to that, there's one more option,
03:18which is very similar to absolute,
03:20only it is fixed and absolute,
03:23which is perfect for scenarios where you want your content
03:25to be always on your display.
03:28Now in this case, if I set it to be fixed
03:30and I click on refresh, you're gonna see
03:32that now not only my footer is positioned,
03:34but also when I'm scrolling,
03:35it's sticking to the exact same point,
03:37it's always staying there.
03:39And that's how a lot of sites create their headers
03:41and footers that don't move or that stay with the user
03:44as the user is scrolling using CSS.
03:47So those are really the different layout positioning options
03:51that you have, which with them,
03:53you could create a plethora of different options.
03:55So I'm just gonna go ahead here
03:57and I'm just gonna add into our comments here
04:01so you could also have the types of positioning
04:04that you could have.
04:05You could have a static, relative, absolute, fixed,
04:07or it could inherit what it was of its parent,
04:11whatever its parent had,
04:13so it could inherit that feature as well.
04:15All right, just like many other properties that exist.
04:18Now I wanna talk just about one more thing
04:20as if you could note in our CSS
04:22is that our element is sitting on top of everything.
04:25As you add many other items,
04:27you might wanna stack and decide the order of items
04:29and how they're being positioned.
04:31To do that, you would use the command called zindex.
04:34And with the zindex, you would put a property
04:37which would be a value, an integer value,
04:40could be negative, could be positive,
04:41and you could define by it, for example, 10,
04:44saying I want this to sit in position 10 in the stack.
04:47So if I have a few different items,
04:49I could then stack and decide using the zindex
04:52what position I want it to be.
04:54In this case, I really want it to be in a negative position
04:56and by the way, more than one item
04:58could be in the same stacking order.
05:01But what I really want to happen
05:02is for it to be underneath the default
05:04and the default is at zero.
05:06Whatever is in our normal flow is on zero,
05:08so now I've put my footer to be underneath it,
05:11making my content visible above my footer.
05:14All right, not very pretty for our scenario.
05:16In our scenario, we probably would want it to be positive
05:19even if it was one or leave it in the default
05:21which was in that positive realm anyways.
05:25And that's it, that covers really the basics of positioning
05:27and we're definitely gonna see it throughout our courses
05:30and throughout your practice,
05:32but those are the different layouts
05:34that you could choose from
05:35where you definitely wanna know how to work with static,
05:38relative, which are very similar,
05:39and how to work with absolute and fixed
05:41which will be giving you a completely different way
05:44to render applications more like applications would render
05:47where you're controlling
05:48where the elements are sitting exactly on the stage.
05:51And again, you could still use relative items.
05:53So even for the bottom top, you could use percentages.
05:55So you could create a layout
05:57that is based off percentages as well
05:59and have it refresh as the screen resizes.
06:03So we'll see you in the next chapter
06:05where we start talking about completely different things.
