Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
This course contains a high-level overview of Cascading Style Sheets, while exploring the basic concepts, terminology, and tools of the language. Beginning with an exploration of CSS syntax, author James Williamson explains how CSS modifies text, borders, backgrounds, and color; demonstrates CSS and HTML integration; and contextualizes the current state of CSS. The course also tours some of the most popular CSS editors and frameworks and lists online tools and resources for further study. This course is for people who want a big-picture overview before taking hands-on courses.
Perhaps of all the various aspects of CSS, the hardest to learn and master is layout. There have been numerous books, web sites, and courses dedicated to nothing but CSS layout. With that in mind, I want to introduce you to some of those concepts in broader terms, so that the terminology and overall concepts of CSS layout aren't foreign to you as you begin to experiment with it and learn the various nuances around controlling page layout with CSS. I want to start with the concept of element positioning. Positioning allows you to take an element on the page and control where and how it's positioned relative to things such as its original starting position, other elements, or even the viewport itself.
CSS defines three positioning schemes: Normal Flow, Element Floating, and Absolute Positioning. Now, we are going to discuss floating in more detail in another movie in just a moment, so for now, let's focus on Normal Flow and Absolute Positioning. Normal Flow is exactly what happens when you do nothing to control page layout. You can think of this as the default layout method of your browser, and it's more powerful than you think. Essentially, normal document flow takes the content in the order that it's found in the HTML and then stacks it, one element right on top of another.
Block-level elements like headings, paragraphs, and section elements like divs, sections, articles, and asides, those things take up their own space in a document's normal flow, and they just stack one on top of another much like blocks. Inline elements like images, links, or span tags will appear inside of those block level elements, and they simply stack themselves based on the flowing of line boxes, and what's a line box? Well, think of those as the lines of text in a paragraph and the way that they stack themselves. They stack themselves only once they've gone as wide as they can go.
So just think of like the lines in a paragraph. Now, believe it or not, Normal Flow can and should handle the majority of your layout needs by letting elements stack in the order that they are found. It's a lot easier to make some minor tweaks to create columns or reorder a single element or two. Now, if you want elements to arrange themselves based on normal document flow, you don't really need to do anything. The browser is just going to handle it automatically. If, on the other hand, you do want to tweak or drastically alter the positioning of your elements, you can then use the position property.
Exploring its values is also going to allow us to explore our other positioning models. The position property accepts one of five values: static, relative, absolute, fixed, and inherit. Inherit simply means that the position value from the element's parent should be used. Now, static tells the element to position itself using normal document flow, and you can think of that as, like, the default value. However, relative positioning is still considered part of normal document flow, but it allows you to tweak an element's position based on offset values that you could give it.
Offset values can be given for top, left, bottom, or the right of an element. Now, giving offset values of top and left, for example, would offset the element from its top-left corner. Let me show you what I mean. So here, we have an element called box 1, position is set to relative, and then we are giving it an offset of left of 100 pixels, top of 50 pixels, so what it will do is move over 100 pixels and down 50 pixels. So using this method, you can simply nudge an element over a little or reposition it all the way over on to the other side of the page if you wanted to.
Now, you may have noticed this, but what's really peculiar about relative positioning is that it creates kind of like a hole where the element would normally be found. And instead of having elements below it move up to take its place, an empty space where the element would be is left behind. This empty space would even change sizes if the element would change size as well. Most relative positioning is actually set without any offsets at all. So here we would just say box 1, position relative, with no left and top values. Now what does that do? Well, it just merely gives the element a positioning value.
Why is this so important? Well, for that, we need to look at absolute positioning. Absolute positioning is not considered part of normal document flow. In fact, it removes the element from normal flow and repositions it based on those offset values given. So let's take a look at an example of that. So here we have the same box 1, position: absolute, and its left set to 100 and its top set to 50. Now, any element below the absolute position element now moves up to take the element's place. So unlike relative positioning where you had that sort of hole left above it, notice that in this case box 2 and box 3 move up because box 1 has been removed from normal document flow.
The browser really doesn't see it there anymore. Where absolute positioning elements get really tricky is how to calculate where the element is going to be positioned. Essentially, an absolutely positioned element looks to the nearest parent element that has positioning. If no elements are positioned above it, it simply looks to the body tag. Now, that means that the actual viewport itself is used to position the element. Let me give you an example here. If I took this div tag and positioned it using an absolute positioning at the top-left value of 20 and 40, it's going to start at the top-left corner of the screen, go 20 pixels over to the right, and then 40 pixels down.
Notice that this happened because the parent element, content in this case, wasn't positioned, so it went up to the body tag. If, on the other hand, the div's parent element was positioned, for example, here giving content a position relative, notice that div would then be offset from its top left-hand corner, not the page's. If you look at other people's CSS, you are going to see this technique used over and over again as a way of positioning child elements within their parents. So it will give the parent element, the section element position of relative, then they could use absolute positioning to move an element or two around inside of it.
Now, you may remember that we had one more positioned value, fixed. Fixed elements are considered to be absolutely positioned, but they're always positioned relative to the active viewport. Now, what does that mean? Well, the important part of that statement is what is meant by the active viewport. If you position, for example, a fixed element at the top-right offset of 50 pixels and 100 pixels, the top-right corner of the element will be positioned 50 pixels over to left and 100 pixels down. So that's just like absolute positioning.
But since it's relative to the active viewport, scrolling page content would not affect this element at all. It would remain in that position, no matter how the screen was resized or scrolled. It truly is fixed. Another thing that's very important to point out here when discussing positioning is how positioned elements can stack one on top of the other. This layering can even be controlled by the z-index property, which allows you to control which elements are on top of others. Now, by default, positioned elements will appear on top of non-positioned elements if those two elements overlap.
Well, this barely scratches the surface of what's possible when positioning elements. But hopefully this has been a solid introduction for you into the concept of positioning elements and how you can manipulate a document's normal flow to reposition elements based on your specific layout needs.
Find answers to the most frequently asked questions about CSS Fundamentals.
Here are the FAQs that matched your search "":
Sorry, there are no matches for your search ""—to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.