Home

How to Use the Floating Layout Mechanism for HTML5 and CSS3 Programming

|
Updated:  
2016-03-26 13:16:21
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

CSS3 supplies a couple techniques for layout. The preferred technique for most applications is a floating layout. The basic idea of this technique is to leave the HTML layout as simple as possible, but to provide style hints that tell the various elements how to interact with each other on the screen.

In a floating layout, you don't legislate exactly where everything will go. Instead, you provide hints and let the browser manage things for you. This ensures flexibility because the browser will try to follow your intentions, no matter what size or shape the browser window becomes. If the user resizes the browser, the page will flex to fit to the new size and shape, if possible.

Floating layouts typically involve less code than other kinds of layouts because only a few elements need specialized CSS. In most of the other layout techniques, you need to provide CSS for every single element to make things work as you expect.

How to use float with images

The most common place to use the float attribute is with images.

image0.jpg

It's more likely that you want the image to take up the entire left part of the paragraph. The text should flow around the paragraph,

When you add a float:left attribute to the img element, the image tends to move to the left, pushing other content to the right. Now, the text flows around the image. The image is actually removed from the normal flow of the page layout, so the paragraph takes up all the space. Inside the paragraph, the text avoids overwriting the image.

image1.jpg

How to add the float property

The code for adding the float property is pretty simple:

image2.jpg
<!DOCTYPE html>
<html lang = "en-US">
 <head>
 <meta charset = "UTF-8">
 <title>imgFloatLeft.html</title>
 <style type = "text/css">
  img {
  float: left;
  }
 </style>
 </head>
 <body>
 <p>
  <img src = "ball.gif"
   alt = "ball" />
  The image now has its float attribute set to left. That means
  that the text will flow around the image as it normally does
  in a magazine.
  The image now has its float attribute set to left. That means
  that the text will flow around the image as it normally does
  in a magazine.
  The image now has its float attribute set to left. That means
  that the text will flow around the image as it normally does
  in a magazine.
  The image now has its float attribute set to left. That means
  that the text will flow around the image as it normally does
  in a magazine.
  The image now has its float attribute set to left. That means
  that the text will flow around the image as it normally does
  in a magazine.
 </p>
 </body>
</html>

The only new element in the code is the CSS float attribute. The object has a float:left attribute. It isn't necessary to change any other attributes of the paragraph because the paragraph text knows to float around the image.

Of course, you don't have to simply float to the left.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].