43

I've made the jump from HTML table layout for designing webpages to CSS about a week ago and have since been reading more about it. Yesterday, I read a long post here on Stack overflow where users were knocking float and how deprecated they are for layout. There was a lot of talk about inline-block being used in its place.

I have an HTML5 design that I just finished and it looks fantastic in Firefox and Chrome, but when tested in Internet Explorer 7, 8, and 9, the design absolutely explodes. It seems to me that anything in this design that I've floated right is not honored in IE. It just seems to wrap under whatever is to the left of it.

I'd like to know if I'm OK with floats or if I should I be using inline-block instead. An example of how to have two divs next to one another where one is on the left side and the other on the right, using inline-block would be nice.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Muzz
  • 667
  • 1
  • 5
  • 11
  • 3
    Floats aren't deprecated at all, although there may be better alternatives given the [law of the instrument](http://en.wikipedia.org/wiki/Law_of_the_instrument). Please link to your source. – zzzzBov Mar 19 '12 at 19:49
  • 1
    Solid read on IE and how it handles floating elements: http://css-class.com/articles/explorer/floats/floatandcleartest1.htm – MetalFrog Mar 19 '12 at 19:51
  • I cannot seem to find the post that talks about floats being depreciated. There were about 6 people knocking it and unfortunately, because I'm fairly new to pure css layouts, I'm just not sure what to believe. – Muzz Mar 19 '12 at 19:55
  • 1
    Given that you're new to CSS, I recommend reading the [CSS specification](http://www.w3.org/TR/CSS2/). It's definitely dry reading, so I recommend jumping around to the interesting parts (like specificity and the cascade). – zzzzBov Mar 19 '12 at 20:04
  • 1
    Nothing wrong with floats, `inline-block` may be the current fashion, but floats are still a valuable tool in the toolbox. As for testing various versions of IE, I'd first recommend updating to SP3, this would allow you to run IE8 (and use the developer tools to run in IE6 and IE7 mode) and Microsoft has [Internet Explorer Application Compatibility VPC Images](http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=11575) that will allow you to run IE9. – steveax Mar 19 '12 at 20:05
  • "when tested from other computers running explorer versions 7, 8 and 9, the design absolutely explodes. It seems to me that anything in this design that I've floated right is not honored in IE. It just seems to wrap under whatever is to the left of it." - is your page in quirks mode? To find out, hit F12 in IE8/9 to bring up the Developer Tools. – thirtydot Mar 19 '12 at 20:17
  • @thirtydot, I can check. There is a computer upstairs running IE8. Where do I look to see if it's in quirks mode? – Muzz Mar 19 '12 at 20:22
  • 2
    @Muzz: You say its an HTML5 site that 'explodes' on earlier versions of IE. Are you using HTML5 elements like `header`, `footer`, `article`, `sections`, etc? If yes, that's probably your issue. Earlier versions of IE do not default them to block elements. There are easy fixes for these. Google is your friend – Ayush Mar 21 '12 at 05:20

4 Answers4

69

Floats were never meant for layout.

They’re simply meant to take an element, put it to one side, and let other content flow around it. That’s all.

Eric A. Meyer, in Floats Don’t Suck If You Use Them Right

The early web was influenced by print/academic publications where floats are used to control the flow of text around figures and tables.

So why did we use them for layout?

Because you can clear a footer below two floated columns, float layout came into being. If there had ever been a way to “clear” elements below positioned elements, we’d never have bothered to use floats for layout.

Today, the CSS Flexible Box Layout Module flex and the CSS Grid Layout Module grid are optimized for user interface design and complex layouts and are expected to complement each other.

Grid enforces 2-dimensional alignment, uses a top-down approach to layout, allows explicit overlapping of items, and has more powerful spanning capabilities. Flexbox focuses on space distribution within an axis, uses a simpler bottom-up approach to layout, can use a content-size–based line-wrapping system to control its secondary axis, and relies on the underlying markup hierarchy to build more complex layouts.

Flexbox and Grid are—as of this writing—W3C candidate recommendation and candidate recommendation draft, respectively. Flexbox is supported by all major browsers and has known issues in IE11. Grid is supported by all major browsers but IE11 supports an older version of the spec.

melhosseiny
  • 9,992
  • 6
  • 31
  • 48
  • I have a hunch you forgot to use the [HTML5 enabling script](http://remysharp.com/2009/01/07/html5-enabling-script/) and a modern [CSS reset](http://meyerweb.com/eric/tools/css/reset/) that accounts for the HTML5 display-roles. – melhosseiny Mar 19 '12 at 20:37
  • I'm actually using the latest reset script from the original developer. (I can't remember his name). Now for the HTML enabling script, I'm not using that script from google but I do have these in my head tag: ` ` Is this OK? – Muzz Mar 19 '12 at 20:45
  • I see, so you're using [HTML5 Boilerplate](http://html5boilerplate.com/)? This should be fine. I suggest you ask another question on SO describing your technical issues in detail. – melhosseiny Mar 19 '12 at 20:51
  • OK, but you know what? All I really used from boilerplate was the html template. I tossed out the css and javascript. :) Could this be why? – Muzz Mar 19 '12 at 20:53
  • Yes, it's useless without the CSS and JS. – melhosseiny Mar 19 '12 at 20:57
  • melhosseiny, can you tell me what the bare-bones minimum files are from boilerplate that I need to include? It's a little confusing to me because I'm using a reset and then I'm supposed to include their css as well. Won't the reset css file just overwrite it? What javascript files are needed for 5 to work properly? – Muzz Mar 19 '12 at 21:09
  • I dont get the "So why are we using them for layout?" Part – Muhammad Umer Mar 10 '13 at 22:24
  • Since floats were not meant for layout but is being abused by us for exactly that purpose because there's nothing else to do, the W3C are working on new standards for layout (check the linked working drafts for more info). This is still work in progress, some of these drafts will not make it and some of them might be merged. We can not use them reliably yet, as most of them have not even been implemented. – melhosseiny Mar 11 '13 at 06:32
  • @melhosseiny You state that "Floats were never meant for layout.", but then state "They’re simply meant to take an element, put it to one side, and let other content flow around it. That’s all."... isn't that the definition of layout? – Matt K Apr 22 '14 at 20:25
  • I love the "*X wasn't intended for layout*" mantra that constantly appears in discussions about web design. *Tables* weren't meant for layout. *Floats* weren't meant for layout. *Inline-block* isn't meant for layout. Okay... so HOW EXACTLY should a web designer actually code layout? Apparently nothing in CSS or HTML is meant for layout, except for things like flexbox which don't really exist yet for all practical purposes. – Siler Dec 21 '14 at 17:49
  • @Siler That's true. If you can't make it work with flexbox (support is pretty good) and a fallback for IE8 and IE9 or other browsers that don't support flexbox, then by all means use floats and inline blocks. They're preferable to tables because with tables there's more unsemantic markup. – melhosseiny Dec 21 '14 at 17:58
  • 1
    @MattK Floats are mostly a typesetting thing to control the flow of content such as images and tables in text. It is not powerful enough to solve any layout problem and layout is not limited to putting an element to one and letting other content flow around it. So no, that's not the definition of layout. – melhosseiny Dec 21 '14 at 18:09
  • @melhosseiny Actually that's probably the dictionary definition of what "layout" means. Just because it's not powerful enough to solve every layout problem doesn't mean it's not a form of layout. – Matt K Dec 22 '14 at 15:19
  • @MattK The semantics of that do not matter. You're missing the point. The web has a lot of influences from print, and floats are one of them. In scientific documents (which was pretty much all there is, back then), floats are used to control the flow of figures and tables in text. Today, they're being used for large scale layout, which is causing a lot of issues for web devs/designers. I guess a more accurate statement is, "Floats were never meant for large scale web layout." – melhosseiny Dec 25 '14 at 07:04
  • So, the problem with floats is if you use them inside of a container, and suddenly the requirements changed in that way that the text within of the container should wrap around a new image or other block, you will be fighting a lot trying to solve that problem! Shortly: floats are not componetizable and can't be nested. – Brian Cannard Oct 24 '16 at 20:44
  • In my opinion, floats break the [Principle of Least Astonishment](http://wiki.c2.com/?PrincipleOfLeastAstonishment) as other elements behave differently around floats. This is in contrast to something such as `position: absolute` because that property simply just affects the targeted element, rather than changing the behavior of adjacent elements. – mattLummus Mar 31 '17 at 19:00
  • very good explanation! – Eldshe Mar 01 '22 at 10:04
45

This question is from 2012 and the other answers are outdated.

Floats should not be used for layout anymore (though you can still use them for the original purpose - floating text around images).

Flexbox is now widely supported and is better for layout.

MGOwen
  • 6,562
  • 13
  • 58
  • 67
0

Floats should work fine, although it depends on how you've used it - how about a link to your design?

inline-block doesn't work correctly in Internet Explorer versions less than 8: http://www.quirksmode.org/css/display.html

callumacrae
  • 8,185
  • 8
  • 32
  • 49
0

You can use this example in inline

<div id="firstdiv">
    That is the first div
</div>
<div id="seconddiv">
    That is the second div
</div>

style.css

 #firstdiv{
        display:inline;
        background-color: #f00;
    }

    #seconddiv{
        display:inline;
        background-color: #00f;
    }

it will be work at IE8 and higher but if you wanna use it in IE6 and 7 make the following code in style.css

#firstdiv{
    display:block;
    background-color: #f00;
    float: left;
}

#seconddiv{
    display:block;
    background-color: #00f;
    float: right;
}

if you use HTML5 and CSS3 and you want it work with IE6 read the following article 5 Tools to Make IE Play Nice With CSS3 and HTML5 in WordPress

you can read that article too it is very useful difference between block, inline and inline-block

Mostafa Maklad
  • 349
  • 2
  • 13
  • thank you for the example and the links. I will look into this. What is the difference between `inline` and `inline-block`? – Muzz Mar 19 '12 at 20:30
  • 1
    you can read that article it will help http://dustwell.com/div-span-inline-block.html it is a pretty good – Mostafa Maklad Mar 19 '12 at 20:42