How to Float Inline Page Layout Elements in CSS
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.
CSS: Core Concepts2,443,762 Views
CSS: Styling Navigation507,519 Views
CSS for Developers1,116,558 Views
XHTML and HTML Essential Training2,972,644 Views
CSS: Frameworks and Grids247,593 Views
PHP with MySQL Essential Training4,259,922 Views
CSS: Core Concepts2,443,762 Views
HTML Essential Training3,794,051 Views