New Feature: Playlist Center! Pick a topic and let our playlists guide the way.

Start learning with our library of video tutorials taught by experts. Get started

CSS Fundamentals
Illustration by

Element positioning


From:

CSS Fundamentals

with James Williamson

Video: Element positioning

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.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
CSS Fundamentals
3h 14m Beginner Sep 26, 2011 Updated Dec 13, 2011

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.

Topics include:
  • Understanding basic selector types
  • Integrating CSS with HTML
  • Examining browser rendering differences
  • Exploring CSS specifications
  • Checking browser support
  • Understanding the box model
  • Adjusting margins and padding
  • Positioning elements
  • Exploring basic layout concepts
  • Understanding media queries
  • Introducing CSS3
  • Using CSS Reset
Subjects:
Web Web Foundations
Software:
CSS
Author:
James Williamson

Element positioning

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.


Expand all | Collapse all
please wait ...
Q: This course was updated on 12/13/2011. Can you tell me what has changed?
A: One movie called "Who is this course for?" was added to provide information on what you can expect to get from the course, depending on your level of familiarity with CSS.
 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed CSS Fundamentals.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.