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

Floats


From:

CSS Fundamentals

with James Williamson

Video: Floats

Any discussion on positioning elements would be incomplete without mentioning floating. Floating is by far the most popular CSS layout technique; however, it's also one that many new web designers struggle with. That's because how the technique works changes based upon the relationship of the elements that are being floated. Let's take a closer look at floating and how it works. So, what's a float? Essentially, it's an element that is shifted to the right or the left of it's current position. You assign floating, logically enough, by using the float property.

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

Floats

Any discussion on positioning elements would be incomplete without mentioning floating. Floating is by far the most popular CSS layout technique; however, it's also one that many new web designers struggle with. That's because how the technique works changes based upon the relationship of the elements that are being floated. Let's take a closer look at floating and how it works. So, what's a float? Essentially, it's an element that is shifted to the right or the left of it's current position. You assign floating, logically enough, by using the float property.

You can use the left, right, or none keywords to define the direction of the float. Here is how it works. Remember that in normal document flow, block-level elements simply stack one on top of another. They are commonly refer to as boxes, and that's actually really good way of thinking about them. When you float an element, it moves as far over as it can, either it to the right or the left, depending upon the property that's been assigned to it. The elements below it then move up to occupy the space that the floated element used to occupy.

In that sense, floating an element removes it from normal document flow. As you can see, floating an element to the right and then allowing the content below it to move up beside it, it's pretty easy way to create a two-column layout. But you can probably guess, there is a lot more to floats than just this. For example, let's take a look at what happens when you float an element to the left. Now, the default element alignment is to the left edge of their containing element, so if you float one of those top boxes to the left, it doesn't appear to move it all, since it's already aligned to the left of the page.

However, it's still removed from normal document flow. And that means, the element below it would move up to occupy what they see as empty space. The result? Well, those elements would actually go underneath to floated element, at least partially. You see, individual parts of an element will react differently to floated elements. To best demonstrate this, let's take a look at another example. Let's say we have an image contained within a paragraph. If you were to float the image to the left, it looks as if the paragraph is wrapping around the image instead of going underneath it, kind of weird.

The truth is, what we are seeing here is how those inline boxes, or those lines of text, react to floated elements. Inline boxes, such as the lines of text within a paragraph, will wrap around floated elements if they have enough room. Now, if the paragraph had a background color, you would still see that the paragraph itself did indeed move up and underneath the image. Once you have learned the ins and outs of floats, you can do all kinds of cool things with them. For example, if you float a series of elements to the same side, like these series of boxes that have all been floated to the left, you end up with the horizontal display of elements.

This technique is used frequently to transform a list of links into a horizontal menu, for example. You can also use floats to create multiple columns of elements, drop caps in paragraphs, pull quotes that have text wrapped around them, product description details, and a whole lot more. I also want to make sure that we talk about clearing floats. And if you remember from earlier, I discussed how floated elements affect all the elements below it in normal flow. At times, that's less than ideal. You might, for example, create a two-column layout by floating one containing element to the left and one to the right, as you see here. So we have got this content element floated to the right, we have the sidebar element floated to left.

However, based on the way floats work, any remaining content on the page, like our footer here, would move up an underneath the floated elements. By using the clear property, you can tell the browser to stop floating elements and go back to normal document flow. The clear property can be set to prevent elements from floating to the left or right of an element, or set to both. So you can use the left, right, or both values to prevent any float from affecting the cleared element. The best way to think of clearing is just think of it as your way of telling the browser, hey, thanks, but I need to start going back to stacking elements again.

Now, obviously mastering how browsers handle floated elements requires a little bit of time. You will need to become comfortable with how floated elements affect their parent elements, how clearing works, how margins work with floated elements, and how the width of parent elements can impact the behavior of floated elements-- for example, making them break down to the right or left, instead of floating up beside them. You will then need to take that a bit further and explore the best practices that have evolved over the years regarding using floats for layout. Now, the good news about that is that a fair amount of ground has already been covered for you.

Floats are by far the most popular CSS layout technique currently in use, so there are plenty of examples in tutorials out there for you to learn from, including many of the CSS titles here in the lynda.com Online Training Library.

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.