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

Floating inline elements

From: CSS: Page Layouts

Video: Floating inline elements

So far we've focused on floating large block-level elements, as they are the building blocks of all of your initial page layouts. Now in this movie, we're going to focus on floating inline-level elements and how they affect the elements around them. So I have inline.htm open from the 03_05. Just to give you some idea as to what we're going to be doing, I'm going to go ahead and preview this in a browser. So here I've opened up the page in Safari. Now essentially, I have three sections that we're going to work with. First, we're going to take a look at using floats to wrap text around an image, so we have an image right here.

Floating inline elements

So far we've focused on floating large block-level elements, as they are the building blocks of all of your initial page layouts. Now in this movie, we're going to focus on floating inline-level elements and how they affect the elements around them. So I have inline.htm open from the 03_05. Just to give you some idea as to what we're going to be doing, I'm going to go ahead and preview this in a browser. So here I've opened up the page in Safari. Now essentially, I have three sections that we're going to work with. First, we're going to take a look at using floats to wrap text around an image, so we have an image right here.

We have some text following it. And right now, we've got a bunch of empty space. It would look a lot better if we could just sort of wrap the text around that image. The next thing we're going to do is create a drop cap here, and then finally we're going to be styling more of a complex region, like a product description, and seeing how floating can help us with that as well. So this is more about how floating could be used to help tweak layouts and sort of improve them a little bit. So we have those three sections. So I'm going to go back into my code and just look at the actual structure of the first section that we're going to work with, right here. You can see we have a section here with a class called textWrap.

Inside that, we have an image which is inside our first paragraph, and then we just follow that with some content. So what I'm going to do is I'm going to go up to about line 64 or 65 where I have this little comment. So the first thing we're going to do is we're going to target that specific image, and I'm going to do that by using the textWrap class selector, and then I'll write a descendent selector that adds the image to that. So it's saying, hey, find any image that is also in an element with the class of textWrap, so we're targeting that one specific element.

Okay, so what I'm going to do here is I'm going to make it look a little different. I'm going to apply a border to it. I'm going to put a 1-pixel solid border on it, and we're going to make that gray, so #333. To wrap the text around it, we're just going to go ahead and float that to the left, just that simple. So I'm going to save this, go back into my browser, and refresh, and indeed the text does wrap around it. There is my outline, and as you can see, oh, that doesn't look great. When we float text which is inside of a paragraph or even outside of a paragraph and it has paragraph text next to it, the text itself is going to wrap around the image, but it goes right up to the edge of it.

So how can we prevent that? Well, we can use margins to help create sort of a gap or a gutter, if you will, between the image and the text itself. So if I go back into my code, right after the float: left, I'm going to apply a margin to this. And the margin that I'm going to do here is 0 for top, 1em for the right-hand side, 1em for the bottom, and 0 for the left-hand side. So saving this and then previewing it again gives me that nice little sort of standoff if you will between the content and the image itself.

So that's really nice. But what happens if you don't want a specific element to wrap around this? So, for example, this headline. This headline really should go down. Maybe it's the beginning of another section and it doesn't pertain to this image. The same rules to clearing floats apply to inline elements as they do to block-level elements. So if I go back into my code, I can just very quickly write a selector that says textWrap h3 to target the heading, and then just tell it to clear both.

So if I save that, preview it, boom, now our headline drops right back down again. Now I want to clear up something, because typically, there is a little bit of a misconception with what's actually happening here. If you remember from our earlier demos, when we float something to the left, the content underneath it ignores it and then comes up under it. And now you might be asking yourself, how come the text just didn't go right up under the image? Well, that has everything to do with what we call a line box. What's a line box? Well, if I highlight this line of text, you can see it.

That is a line box. So when line boxes encounter a floated element, what they do is they shorten themselves so that they can appear beside the floated element. The paragraph itself did come up under the image. That may take you a second to sort of process that, but I can show you that visually, and I'm just going to do this really quick. This is going to be a really quick little demo of this concept that I'm talking about here. If I come down, I can create another style called textWrap p, and I'm just going to give it a background color. And I'll just give it a background color of say c4d4cb.

Why so complicated? I don't know. And I also want to add a little bit of left margin to my image so that you can really kind of see that background color floating up behind the image. So if I save this and again preview it, you can see exactly what's happening here. The paragraph itself is moving up to occupy the space the image used to, because it's removed from normal document flow. So there is paragraph one. There is paragraph two. They're floating up underneath it. The heading is clearing it, so it doesn't do that. Now the line boxes themselves, however, instead of going up underneath it, the line boxes shorten themselves so they will float beside it.

So this is going to work perfectly as long as you don't have a background color behind your paragraph that might shine through like this. I'm going to get rid of that background color and I'm going to get rid of that 10 pixels' worth of margin. All right! So let's go down and talk about our drop caps. I'm going to refresh this. There we go. So we want to create a little drop cap here in this paragraph. And if I go down into the actual code, you can see that I have another section with the class of drop cap, and I want it to affect that first paragraph. So I'm going to go up to my styles, and I'm going to start doing this in the drop cap styles.

I'm going to write a pretty complex selector here. If you watched my previous course, the CSS: Core Concepts, I went into a lot of different selector types, so this will be sort of a refresher for that particular course. If you didn't take that course and you're curious about those types of selectors, you can go back and watch the section on targeting content. So I'm going to type in drop cap-- remember that's the class that's applied to the section--space, p, so that's targeting paragraphs inside the drop cap. Now here's where it gets complex. I'm going to do a colon and I'm going to do first-of-type.

So what this is saying is it's saying, hey, when you find the first paragraph inside this class, go ahead and apply it, so with the first paragraph inside this drop cap. Now we're not done yet because right after first-of-type, I'm going to type in another colon, and I'm going to type in first-letter. That is incredibly specific. We're saying, hey, go find the first paragraph inside of an element with drop cap applied and then inside that, find the first letter. There are other ways to do this. You could wrap that letter in a span tag, for example, and target the span tag, but this is a way of doing it without adding any additional markup to your code.

Now inside this, let's just go ahead and start styling it. We're going to float the letter to the left. That's going to wrap all the rest of the text around it. Then we're going to change things, like for example, I'm going to change the font-family. I'm going to change the font-family to Georgia, Times, and then serif. I'm going to set the font-size, obviously I want drop caps to be a little bit larger. I'm going to set font-size to 4ems. I'm going to control its spacing, or its placement, if you will, with margins. So top margin I'm going to do 0.

Right margin I'm going to do 5 pixels-- that'll hold the text away from the drop cap a little bit--and then 0 for the rest of them. So really all I'm trying to do there is give it a little offset. I'm going to have to do something here because of the way that we're doing this. And so instead of targeting an element in a span tag, we use this first letter selector. And that causes a little bit of problems with cross browsers and believe it or not, it has nothing to do with Internet Explorer. It has everything to do with WebKit-based browsers like Safari and Chrome that we're going to be using, and Firefox. Here's how this works.

We're going to go ahead and do line-height. line-height is essentially the space between lines or the height of the box that the text is contained inside of. And I'm going to set that to 0.7. So here's essentially what happens. In Firefox, you can't put line-height on a first-letter pseudo-selector. What happens is, anytime you use this, the line-height is inherited from the paragraph that it's contained inside of, so I can't do that. But WebKit allows me to. So Safari and Chrome will allow me to do that.

So Safari and Chrome compute the value on the letter itself, whereas Firefox gives it the computed value of the parent. So that means that in Firefox and inside Google Chrome, their line-heights won't match up, so they won't align to each other. If you were to test this page without doing that, in both of those browsers you would see that the drop cap doesn't line up. So essentially, you have to pass a value for line-height in there and kind of play around with that value so that Chrome and Safari will match up with Firefox. I typically start it at about 0.8 and kind of play around with it from there.

That seems to be a good starting point. I'm going to save this, go back into my browser, and test it. And boom! There's our drop cap. So by taking that one letter and floating to the left and wrapping the text around it, all that's really required is just doing some additional styling. Do we want to change the font, do we want to change the color, how big do we want to make it, that sort of thing. Okay, now the final thing we're going to do here with floating inline elements is create a more complex region down here. So what if this were like a product description? And we have a little shot here, maybe it's a print that's for sale.

We want to float the text next to it, and then we have this More information and $19.95. Maybe we want those to appear sort of side by side, but style them very differently. Floating can really help us with that. So I'm going to get back into our code, just so we can see the structure of what it is that we're going to be styling. We have a section with a class of complex. It has an image inside the paragraph. And then we have a paragraph for the class of more, that's our More information, and a paragraph for the class of price. So going back up to our complex styles, you can see already we have what's controlling the background of that and the border around it and that sort of thing.

Well, we're going to need to add some things here. So after that one, I'm going to type in .complex img, so we're targeting any images inside the section with the class of complex. And for that, we're going to float images to the left. This will help wrap the text around it. And then I'm going to apply a margin to the right of 1em. And of course that's going to help us hold the text away from it. So if I save this and test it, it's something we've already done, so we're kind of expecting how that should look, and it looks exactly the way we expect it to. Our text is wrapping around it, but More information and $19.95, we're going to have to do some additional tweaking of that to get it to look the way that we want it to.

So I'm going to come down and create another selector for more, so .more. I'm going to float that text to the left. I'm going to set its margin to 0, but I'm going to give it a margin to the left. So I'm basically clearing out any existing margins that that paragraph might have and then just coming in and setting a margin to the left. And I'm going to do 14ems. Now, that's a lot. I'm going to show you why I'm doing it in just a moment. Now for line-height, I'm going to go ahead and set line-height to 1.6ems.

So essentially again, that's going to give it sort of a taller line box that it's going to be sitting inside of. For padding, I'm going to do 0 padding top and bottom, 5 pixels right and left, and then I'm going to give it a background, so that's one of the reasons why we needed padding because we're giving it a background color. And the background color is going to be #3b2f24. And then finally, the color of the text itself will be white. Okay, now if I save this and I test it, you can see More information. It's floated to the left, but it's got this really wide margin left that's pushing it away from these other elements.

Now why did I do that? Because I want price to show up on the right-hand side of that, so we're going to need to style price a little bit differently. So I'm going to come in and I've got one more selector to make, and that's going to be the .price selector. So for that one, I'm going to float it to the right. I'm going to set its margin to 0, so stripping out any default margins that might exist for the paragraph. I'm going to change its font size to 1.6ems because we're really trying to emphasize that price. I'm going to set the font-weight to bold. And then finally, I'm going to set the margin-top to -.3ems.

So that's going to move it up a little bit. So I'll save that, preview it. It looks great. Oh, but it doesn't. More information and $19.95 are showing up the way I want them to, lining up right side of each other, but there is that pesky container collapse showing up again. So I go back into my code, I'm going to go back up to where our complex is, and I'm just going to set overflow to hidden. Now you're probably wondering, hey, wait a minute. You went through that whole clearfix thing last time. Would that have worked? Sure! I've actually got the clearfix class right here.

So I could have come down and applied clearfix to that, and it would have worked just fine as well, but this way I don't have to apply another class. And if this is static, meaning I know exactly the content that's going to do inside of it-- I know it's not going to have any special effects like drop shadows or things like that that could get clipped off-- then overflow hidden works just fine. So I'm going to save this, preview that in my page, and then boom! There is my completed complex region. I want to point something out about the price and More information. You can see that More information comes first, price comes second.

And I definitely wanted price over here on the right-hand side and I wanted More information on the left-hand side. I could've floated both of them to the right and then added the margin this way, or I could have done it the way I did it within here, which is floating More information to the left, applying a big margin to it to kind of move it over as far as I want, and then floating the price to the right. There's always more than one way to style a layout. So I guess from this, you can probably guess that the purpose of this exercise wasn't really how to teach you how to style drop caps or wrap text or even create a product detail region.

That really wasn't the goal. What I wanted to illustrate was how flexible the float property is when styling various elements all the way throughout your design, and to show you how often you have to be creative when dealing with some of the details. Now keep in mind how important the use of margins are when floating inline elements and when to utilize the clear property to go ahead and reestablish normal document flow.

Show transcript

This video is part of

Image for CSS: Page Layouts
CSS: Page Layouts

71 video lessons · 41871 viewers

James Williamson
Author

 
Expand all | Collapse all
  1. 4m 20s
    1. Welcome
      54s
    2. How to use the exercise files
      3m 26s
  2. 1h 39m
    1. Box model review
      8m 47s
    2. Calculating element dimensions
      11m 11s
    3. Understanding margin collapse
      7m 59s
    4. Calculating em values
      7m 41s
    5. Calculating percentage values
      7m 51s
    6. Normal document flow
      13m 3s
    7. Controlling element display
      8m 53s
    8. Using CSS Resets
      7m 11s
    9. Fixed, fluid, and responsive layouts
      9m 9s
    10. CSS debugging tools
      6m 46s
    11. Using the Firebug Inspector and the WebKit Web Inspector
      11m 5s
  3. 53m 15s
    1. Page design workflow
      3m 6s
    2. Page design tools
      4m 56s
    3. Determining page structure
      7m 18s
    4. Creating image assets
      8m 58s
    5. Creating initial page structure
      7m 3s
    6. Adding meaning with classes and IDs
      5m 23s
    7. Structuring content with HTML5
      6m 6s
    8. Building internal structure
      10m 25s
  4. 1h 36m
    1. Floating elements
      7m 50s
    2. Clearing floats
      7m 28s
    3. Containing floats
      7m 50s
    4. Clearfix technique
      10m 38s
    5. Floating inline elements
      14m 34s
    6. Two-column floated layouts
      8m 17s
    7. Three-column floated layouts
      11m 30s
    8. Column height considerations
      7m 3s
    9. Creating equal-height columns
      10m 42s
    10. Floats: Lab
      5m 25s
    11. Floats: Solution
      5m 21s
  5. 51m 42s
    1. Relative positioning
      7m 59s
    2. Absolute positioning
      8m 59s
    3. Fixed positioning
      4m 23s
    4. Controlling stacking order
      8m 31s
    5. Clipping content
      8m 21s
    6. Controlling content overflow
      5m 38s
    7. Positioning elements: Lab
      3m 59s
    8. Positioning elements: Solution
      3m 52s
  6. 48m 46s
    1. Design considerations for fixed layouts
      3m 28s
    2. Establishing the layout grid
      7m 57s
    3. Defining column spacing
      9m 30s
    4. Applying the grid through CSS
      8m 56s
    5. Creating grid-based assets
      8m 26s
    6. Grid design resources
      6m 22s
    7. Building fixed layouts: Lab
      4m 7s
  7. 44m 35s
    1. Designing for flexible layouts
      2m 30s
    2. Calculating percentage values
      8m 45s
    3. Setting flexible width values
      6m 6s
    4. Making images flexible
      8m 10s
    5. Setting minimum and maximum widths
      7m 24s
    6. Building flexible layouts: Lab
      4m 53s
    7. Building flexible layouts: Solution
      6m 47s
  8. 49m 36s
    1. Responsive layout overview
      3m 49s
    2. Using media queries
      7m 16s
    3. Organizing styles
      8m 39s
    4. Making content responsive
      8m 33s
    5. Mobile design considerations
      7m 32s
    6. Building responsive layouts: Lab
      4m 23s
    7. Building responsive layouts: Solution
      9m 24s
  9. 1h 22m
    1. Creating multi-column text
      6m 36s
    2. Using borders to enhance design
      13m 59s
    3. Rounding corners
      6m 56s
    4. Adding drop shadows
      10m 35s
    5. Working with opacity
      6m 8s
    6. Utilizing the background property
      15m 5s
    7. Working with CSS sprites
      7m 58s
    8. Enhancing page design: Lab
      6m 22s
    9. Enhancing page design: Solution
      8m 38s
  10. 6m 25s
    1. Additional resources
      6m 25s

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.

Join now Already a member? Log in

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: Page Layouts.

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

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.