{"id":219,"date":"2012-08-30T03:03:51","date_gmt":"2012-08-30T03:03:51","guid":{"rendered":"http:\/\/a.parsons.edu\/~dejongo\/12-fall\/?p=219"},"modified":"2018-01-22T15:29:57","modified_gmt":"2018-01-22T15:29:57","slug":"04-floating-layouts","status":"publish","type":"post","link":"https:\/\/b.parsons.edu\/~dejongo\/04-floating-layouts\/","title":{"rendered":"04 Floating Layouts"},"content":{"rendered":"<p><img decoding=\"async\" style=\"float: right; padding: 0 0 10px 10px;\" src=\"http:\/\/b.parsons.edu\/~dejongo\/12-fall\/stuff\/images\/mondrian2.jpg\" alt=\"float picture demo\"\/><\/p>\n<p>Page Layouts in CSS are not as straight forward as one might expect, but then, tables were not exactly straight forward either.\n<\/p>\n<p>As layouts became more complex, a mix between tables and CSS was used, and by about 2007, the bugs had been removed enough that the New York Times, a standard-bearer, could go to a table-less layout based on floats. That broke the ice and after that almost everyone migrated to an all CSS layout\n<\/p>\n<p><a href=\"http:\/\/css-tricks.com\/all-about-floats\/\">CSS Tricks on Floats<\/a>\n<\/p>\n<p>A float is easy enough, and it was introduced by Netscape Navigator 1.1. There is also a clear, to clear the float. It was originally intended to float pictures. The code that floats this example is <code>&lt;img style=\"float: right; padding: 0 0 10px 10px;\" src=\"http:\/\/b.parsons.edu\/~dejongo\/12-fall\/stuff\/images\/mondrian2.jpg\" alt=\"clear float picture demo\"><\/code><br \/>\n<img decoding=\"async\" style=\"float: left; padding: 10px 10px 10px 0;\" src=\"http:\/\/b.parsons.edu\/~dejongo\/12-fall\/stuff\/images\/mondrian3.jpg\" alt=\"float picture demo\"\/>\n<\/p>\n<p>The next paragraph has clear applied to it. That forces the next element to clear the float. The second paragraph above, for example, does not clear, and continues to flow to the side of the picture.\n<\/p>\n<p style=\"clear:left;\">There are a number of methods to achieve the same goal, but most of these build on the ability to float elements to the right or the left of the containing box. The problem using this method is getting the parent containing element to expand to hold the floated children, as the children can become unruly, and stick out below the parent box (see below for an example).\n<\/p>\n<p>To prevent that, all kinds of fixes were created, like floating the parent container or the <code>.clearfix<\/code> method to push the parent box below its floated children. There is a faux column solution that uses a background element to give the impression that all the columns are the same size, and a method called the one true layout that uses margins to push the content into columns, which is similar to one called the holy grail, but they have mostly been supplanted by the overflow: auto method detailed below. For more information and links to tutorials, you can go to <a href=\"http:\/\/css-tricks.com\/795-all-about-floats\/\">CSS tricks<\/a> or <a href=\"http:\/\/coding.smashingmagazine.com\/2007\/05\/01\/css-float-theory-things-you-should-know\/\">smashing magazine<\/a>.<\/p>\n<h2>Floating Columns<\/h2>\n<div style=\"width: 450px; border: 1px solid black; padding: 10px; margin: 10px auto;\">\n<div style=\"font-size: 80%; line-height: 1.1em; width: 140px; height: 140px; border: 1px solid black; float: right; margin: 0 0 10px 10px; padding: 15px; background-color: #bbb;\">\nwidth: 140px; height: 140px; border: 1px solid black; float: right; margin: 0 0 10px 10px; background-color: #bbb;<\/div>\n<div style=\"font-size: 80%; line-height: 1.1em; width: 140px; height: 140px; border: 1px solid black; float: right; margin: 0 0 10px 10px; padding: 15px; color: white; background-color: #999;\">\nwidth: 140px; height: 140px; border: 1px solid black; float: right; margin: 0 0 10px 10px; padding: 5px; color: white; background-color: #999;<\/div>\n<div style=\"font-size: 80%; line-height: 1.1em; width: 140px; height: 140px; border: 1px solid black; float: right; clear: right; margin: 0 0 15px 15px; padding: 5px; background-color: #ddd;\">\nwidth: 140px; height: 140px; border: 1px solid black; float: right;  margin: 0 0 10px 10px; padding: 5px; background-color: #ddd; <span style=\"color: red\">clear: right;<\/span><\/div>\n<p style=\"margin: 0;\">When you apply float to an element, you make a box, take it out of the normal document flow, and shift it to the right or left of the containing box. The remaining content flows around the floated element. The floating elements always needs an explicit width. The #bbb and #999 boxes demonstrate what happens when you float boxes to the right, and they are handled as if they were inline elements so that the boxes float next to one another till there is no more space. To stop an element from flowing around the floated element, you have to clear it, as in <code style=\"color: red\">clear: right;<\/code>. That way the boxes can float under one another, as demonstrated by the #ddd box. The problem with building a complicated layout with floats lies in browser incompatibility, for it is possible that the page does not degrade properly. To not invite any more problems than necessary, make sure you reset the CSS.\n<\/p>\n<\/div>\n<h2>Unruly Children<\/h2>\n<div style=\"width: 450px; border: 1px solid black; padding: 10px; margin: 10px auto; background-color: aliceblue;\">\n<div style=\"font-size: 80%; line-height: 1.1em; width: 140px; height: 140px; border: 1px solid black; float: right; margin: 0 0 10px 10px; padding: 5px; background-color: #bbb;\">\nwidth: 140px; height: 140px; border: 1px solid black; float: left; margin: 0 0 10px 10px; background-color: #bbb;<\/div>\n<div style=\"font-size: 80%; line-height: 1.1em; width: 60px; height: 302px; border: 1px solid black; float: left; margin: 0 10px 10px 0; padding: 5px; color: white; background-color: #999;\">\nwidth: 60px; height: 302px; border: 1px solid black; <span style=\"color: blue\">float: left;<\/span> margin: 0 10px 10px 0; padding: 5px; color: white; back ground-color: #999;<\/div>\n<div style=\"font-size: 80%; line-height: 1.1em; width: 140px; height: 140px; border: 1px solid black; float: right; clear: right; margin: 0 0 10px 10px; padding: 5px; background-color: #ddd;\">\nwidth: 140px; height: 140px; border: 1px solid black; float: right;  margin: 0 0 10px 10px; padding: 5px; background-color: #ddd; <span style=\"color: red\">clear: right;<\/span><\/div>\n<p style=\"margin: 0;\">So floats can be used to create columns, but there is another issue to consider. <\/p>\n<\/div>\n<p style=\"clear: both;\">A containing box will not expand unless there is content. For some reason, when you float a child element, the containing element fails to keep track of it. This becomes unsightly when there is a border or background color on the containing box, and the floating box extends below the border like this.\n<\/p>\n<p style=\"margin: 0; clear: both;\">As you can see, this is not acceptable. The W3 recommends that you put in additional markup and <code style=\"color: red\"> clear: both;<\/code>, but that is a bit of a hack and requires additional markup introducing an unwanted element at bottom. All kinds of clever solutions were found to create a CSS only solution to that problem, which I need not go into as <a href=\"http:\/\/pmob.co.uk\/pob\/three-columns-fluid.htm\">someone<\/a> discovered that  adding <code> overflow: auto;<\/code> to the containing box did the trick. <\/p>\n<h2>A 3 Column Layout that Works<\/h2>\n<div style=\"width: 450px; border: 1px solid black; padding: 10px; margin: 10px auto; overflow: auto; background-color: aliceblue;\">\n<div style=\"font-size: 80%; line-height: 1.1em; width: 140px; height: 140px; border: 1px solid black; float: right; margin: 0 0 10px 10px; padding: 5px; background-color: #bbb;\">\nwidth: 140px; height: 140px; border: 1px solid black; float: left; margin: 0 0 10px 10px; background-color: #bbb;<\/div>\n<div style=\"font-size: 80%; line-height: 1.1em; width: 60px; height: 302px; border: 1px solid black; float: left; margin: 0 10px 10px 0; padding: 5px; color: white; background-color: #999;\">\nwidth: 60px; height: 302px; border: 1px solid black; <span style=\"color: blue\">float: left;<\/span> margin: 0 10px 10px 0; padding: 5px; color: white; back ground-color: #999;<\/div>\n<div style=\"font-size: 80%; line-height: 1.1em; width: 140px; height: 140px; border: 1px solid black; float: right; clear: right; margin: 0 0 10px 10px; padding: 5px; background-color: #ddd;\">\nwidth: 140px; height: 140px; border: 1px solid black; float: right;  margin: 0 0 10px 10px; padding: 5px; background-color: #ddd; <span style=\"color: red\">clear: right;<\/span><\/div>\n<p style=\"margin: 0;\">It appears that all is well in CSS multiple column land. If you want more columns, you can make the floated child a parent to another floating child, so nesting floated children inside of floated children. <\/p>\n<\/div>\n<h2>3 Column Float Demonstration<\/h2>\n<div class=\"handson_demo\">\n<h3>CSS Code View<\/h3>\n<pre>\r\n<style contenteditable=\"true\">\r\n#cd #wrap {\r\n\/*background: url(http:\/\/b.parsons.edu\/~dejongo\/12-fall\/stuff\/04-background_cols.gif);*\/\r\nmargin: 0;\r\npadding: 0;\r\n}\r\n#cd #wrap  h3, \r\n#cd #wrap p {\r\npadding: 10px;\r\nmargin: 0;\r\n}\r\n#cd header {\r\nmargin: 0 0 0 0;\r\nbackground: #eee;\r\nborder-bottom: 3px solid #888;\r\n}\r\n#cd aside {\r\n\/*float: left;*\/\r\nwidth: 150px;\r\ncolor: #fff;\r\nbackground: #aaa;\r\n}\r\n#cd aside #side-b {\r\n\/*float: right;*\/\r\nwidth: 150px;\r\nbackground: #ddd;\r\n}\r\n#cd article { \r\n\/*float: left;*\/\r\nwidth: 200px;\r\nbackground: #ddd;\r\n}\r\n#cd footer {\r\nclear: both;\r\nbackground: #eee;\r\nborder-top: 1px solid silver;\r\n}\r\n<\/style><\/pre>\n<\/div>\n<div id=\"cd\" style=\"width: 500px; margin: 30px auto; border: 10px solid silver; background: #fff;\">\n<div id=\"wrap\">\n<header>\n<h3>HEADER<\/h3>\n<\/header>\n<div id=\"cd-container\">\n<aside>\n<p>Instructions: Remove the comment tags from the float properties, and these boxes become a three column layout. This box will float to the left.\n<\/p>\n<\/aside>\n<article>\n<p>You will see that the columns fit the content, and that they do not all go to the bottom. This is a problem if you want to have columns with different backgrounds. The <a href=\"http:\/\/www.alistapart.com\/articles\/fauxcolumns\/\">solution<\/a> is to use an image that repeats vertically all the way to the bottom, which I have all ready for you to uncomment. You then have to comment-out the background colors of the columns themselves before you can see it.\n<\/p>\n<\/article>\n<aside id=\"side-b\">\n<p>This box will float to the right when the comments are removed.\n<\/p>\n<\/aside>\n<\/div>\n<footer>\n<p>FOOTER\n<\/p>\n<\/footer>\n<\/div>\n<\/div>\n<div style=\"width: 500px; margin: 40px auto;\">\n<h3>HTML markup for the example<\/h3>\n<pre>\r\n&lt;div id=\"cd-wrap\">\r\n&lt;div id=\"cd-header\">\r\nHEADER\r\n&lt;\/div>\r\n&lt;div id=\"cd-container\">\r\n&lt;div id=\"cd-side-a\">\r\nSIDE A\r\n&lt;\/div>\r\n&lt;div id=\"cd-content\">\r\nCONTENT\r\n&lt;\/div>\r\n&lt;div id=\"cd-side-b\">\r\nSIDE B\r\n&lt;\/div>\r\n&lt;\/div>\r\n&lt;div id=\"cd-footer\">\r\nFOOTER\r\n&lt;\/div>\r\n&lt;\/div>\r\n&lt;\/div>\r\n<\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Page Layouts in CSS are not as straight forward as one might expect, but then, tables were not exactly straight forward either. As layouts became more complex, a mix between tables and CSS was used, and by about 2007, the &hellip; <a href=\"https:\/\/b.parsons.edu\/~dejongo\/04-floating-layouts\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts\/219"}],"collection":[{"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/comments?post=219"}],"version-history":[{"count":17,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts\/219\/revisions"}],"predecessor-version":[{"id":4042,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/posts\/219\/revisions\/4042"}],"wp:attachment":[{"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/media?parent=219"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/categories?post=219"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/b.parsons.edu\/~dejongo\/wp-json\/wp\/v2\/tags?post=219"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}