Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Floats

From: CSS Fundamentals

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.

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.

Show transcript

This video is part of

Image for CSS Fundamentals
CSS Fundamentals

36 video lessons · 75181 viewers

James Williamson
Author

 

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
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.


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.

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 preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.