background preloader

CSS Floats 101

CSS Floats 101
The float property is a valuable and powerful asset to any web designer/developer working with HTML and CSS. Tragically, it can also cause frustration and confusion if you don’t fully understand how it works. Article Continues Below Also, in the past, it’s been linked to some pretty nasty browser bugs so it’s normal to get nervous about using the float property in your CSS rule sets. Let’s calm those nerves and ease that frustration. I’ll show you exactly what the float property does to your elements and how incredibly useful it can be once you master it. We see floats in the print world every time we pick up a magazine article with an image on the left or right with text flowing nicely around it. The definition#section1 Let’s start with the definition of a float. A float is a box that is shifted to the left or right on the current line. The float property has four values that we can apply to it: left, right, inherit, and none. How floats behave#section2 Let’s look at a few more examples.

Lesson 13: Floating elements (floats)entutorial - HTML.net An element can be floated to the right or to left by using the property float. That is to say that the box with its contents either floats to the right or to the left in a document (or the containing box) (see lesson 9 for a description of the Box model). The following figure illustrates the principle: If we for example would like to have a text wrapping around a picture, the result would be like this: How is it done? The HTML code for the example above, look as follows: <div id="picture"><img src="bill.jpg" alt="Bill Gates"></div><p>causas naturales et antecedentes, idciro etiam nostrarum voluntatum... To get the picture floating to the left and the text to surround it, you only have to define the width of the box which surrounds the picture and thereafter set the property float to left: Show example Another example: columns Floats can also be used for columns in a document. <div id="column1"><p>Haec disserens qua de re agatur et in quo causa consistat non videt... Show example Show example

CSS Positioning 101 If you’re a front end developer or a designer who likes to code, CSS-based layouts are at the very core of your work. In what might be a refresher for some, or even an “a-ha!” for others, let’s look at the CSS position property to see how we can use it to create standards-compliant, table-free CSS layouts. Article Continues Below CSS positioning is often misunderstood. Sometimes, in a bug-fixing fury, we apply different position values to a given selector until we get one that works. The CSS specification offers us five position properties: static, relative, absolute, fixed, and inherit. Get with the flow#section1 First, let’s take a step back to recognize the world we’re working in. Boxes in the normal flow belong to a formatting context, which may be block or inline, but not both simultaneously. Think of a “box,” as described by the spec as a wooden block—not unlike the ones you played with as a young whippersnapper. Static and relative—nothing new here#section2 In action#section6

CSS Positioning 101 If you’re a front end developer or a designer who likes to code, CSS-based layouts are at the very core of your work. In what might be a refresher for some, or even an “a-ha!” for others, let’s look at the CSS position property to see how we can use it to create standards-compliant, table-free CSS layouts. Article Continues Below CSS positioning is often misunderstood. Sometimes, in a bug-fixing fury, we apply different position values to a given selector until we get one that works. The CSS specification offers us five position properties: static, relative, absolute, fixed, and inherit. Get with the flow#section1 First, let’s take a step back to recognize the world we’re working in. Boxes in the normal flow belong to a formatting context, which may be block or inline, but not both simultaneously. Think of a “box,” as described by the spec as a wooden block—not unlike the ones you played with as a young whippersnapper. Static and relative—nothing new here#section2 In action#section6

Float Floating is often used to push an image to one side or another, while having the text of a paragraph wrap around it. This type of usage is often referred to as text wrapping and resembles what you might see in many magazines that have articles which wrap around images of various shapes and sizes. Wrapping text around an image is easy when using the CSS Float attribute. CSS Code: HTML Code: <body><img src="sunset.gif" class="floatLeft"><p>The images are contained with... Display: The images are contained within the paragraph tag. This second paragraph has an image that is floated to the right. If you were to simply float three images to the right, they would appear alongside one another. <body><img src="sunset.gif" class="floatRightClear"><img src="sunset.gif" class="floatRightClear"><p>The images are appearing... The images are appearing below one another because the CSS clear attribute was used with the value of "right".

Understanding CSS Floats Using css floats effectively can be confusing and it’s probably one of the things that trips up most people when they’re first learning css. However once you learn to control floated elements it opens up a whole new world of possibilities in your design and makes developing layouts much simpler. And best of all floats really aren’t that hard to work with once you understand a few key points. What is a Float? A float is a box that is shifted to the left or right on the current line. The most interesting characteristic of a float (or “floated” or “floating” box) is that content may flow along its side (or be prohibited from doing so by the ‘clear’ property).— w3.org The float property has 3 values; none, which is the default, left, and right. Floats sit as far left or as far right as possible within the immediate containing div. The Difference Between Floats and Positioning In css there are 3 types of positioning schemes. How to Effectively Use Floats There are two main ways floats are used.

Fluid Images Things are looking good so far: we’ve got a grid-based layout, one that doesn’t sacrifice complexity for flexibility. I have to admit that the first time I figured out how to build a fluid grid, I was feeling pretty proud of myself. Article Continues Below But then, as often happens with web design, despair set in. So what happens when we introduce fixed-width images into our flexible design? Fig 3.0: Our flexible grid is finally finished. not a pixel value in sight, and we didn’t have to skimp on the aesthetics. Going back, back to markup, markup#section1 To find an answer, let’s do a quick experiment: let’s drop an image directly into our blog module, and see how our layout responds. Remember our little blockquote, comfortably tucked into our blog article? <div class="figure"><p><img src="robot.jpg" alt="" /><b class="figcaption">Lo, the robot walks</b></p></div> Nothing fancy: an img element, followed by a brief but descriptive caption wrapped in a b element. Fluid images#section2 Me?

CSS float tutorial This article or chapter is incomplete and its contents need further attention. Some information may be missing or may be wrong, spelling and grammar may have to be improved, use your judgment! Page created by Daniel K. Learning goals Understand CSS float Be able to create simple layouts using floats Prerequisite CSS box model tutorial Concurrent CSS tutorial (summary) Moving on Level and target population Beginners Teaching materials (Examples) Remarks This tutorial is intended for students in a field that is technology intensive (e.g. computer applications or educational technology). 1 Introduction This is a green float This is a yellow float The float property was meant to create boxes around which other text can flow, e.g. something like the little green box to the right of this page. Below is the code for both the float:left and float:right example <p style="background-color:green; float:right; width:2.5cm"> This is a green float </p> 2 Simple fluid layouts using the float property Life examples:

2 Column CSS Layout: Fixed Width And Centered One of the more common css layout patterns you’ll come across is the fixed width 2 column layout. It’s relatively easy to code and understand, though it sometimes trips up developers new to css layouts. A few weeks ago I promised to offer boilerplate code for some common css layout patterns. This is the first of that boilerplate code and I’ll continue to offer more in the coming weeks. If you prefer to skip the explanation and get right to the code you can view the source of the demo. The HTML The html for this layout is rather simple as you can see below. We have a single div for each of the 4 main areas of the layout — header, content, sidebar, and footer. One thing to note is the structure of the html above follows the structure of the page itself. Later in this post I’ll offer two ways to change the layout above so the sidebar is to the left of the content. The CSS If you’ve already viewed the source code of the demo you’ll note the css like the html is rather simple. Floated Elements

How To Use CSS3 Media Queries To Create a Mobile Version of Your Website Understanding Floats A mini-tutorial on how the CSS float and clear properties work, and how to use them in your page. Note: all examples in this tutorial use inline-CSS to illustrate what is going on. You should not do this in your pages, but should instead abstract the styles out into a separate style sheet. For more information, see the "Separate Style from Content" tip in another tutorial of mine, "How to Develop with CSS". What float does The float property causes the item to which it is applied to 'float' to the side of the content which comes after it, with this content wrapping around it. <p><span style="float:right; background:#ccf">I'm a little floater</span> I'm 'normal' content, and I wrap around any floats which appear before me in source code. I'm a little floater I'm 'normal' content, and I wrap around any floats which appear before me in source code. Note that block-level elements will still wrap around the float: <p style="float:right; background:#ccf; width:10em">I float! I float Hello World

Fix Your Site With the Right DOCTYPE! You’ve done all the right stuff, but your site doesn’t look or work as it should in the latest browsers. Article Continues Below You’ve written valid XHTML and CSS. You’ve used the W3C standard Document Object Model (DOM) to manipulate dynamic page elements. This little article will provide you with DOCTYPEs that work, and explain the practical, real–world effect of these seemingly abstract tags. WHY A DOCTYPE? Per HTML and XHTML standards, a DOCTYPE (short for “document type declaration”) informs the validator which version of (X)HTML you’re using, and must appear at the very top of every web page. As mentioned in previous ALA articles (and in other interesting places), DOCTYPES are also essential to the proper rendering and functioning of web documents in compliant browsers like Mozilla, IE5/Mac, and IE6/Win. Clearly, this is not what you want. WHERE HAVE ALL THE DOCTYPES GONE? W3.org is not A List Apart, WebReference, or Webmonkey. The DOCTYPE you’d actually want to use is:

Related: