tags:

views:

578

answers:

6

What is the differences between Float vs Display:inline? by default everything goes to left side in every browser. then 2 display inlined elements should be worked same like float:left.

http://www.w3schools.com/css/tryit.asp?filename=trycss%5Fdisplay%5Finline

A: 

display: inline means that the element acts like a <span> instead of a <div>: i.e. it is not a block.

Elements that are floated are not in the normal flow of the document. Here is a good description.

ETA:

Try this code. If you can't see the difference then I can't help you.

<html>
<head>
<style type="text/css">
p.inlinel {display:inline; width: 4em;}
p.inliner {display:inline; width: 4em; text-align: right;}

p.floatl {width: 4em; float:left;}
p.floatr {width: 4em; float: right;}
</style>
</head>
<body>

<p class='inlinel'>The is an inline paragraph with lots of words.</p>
<p class='inliner'>The is an inline paragraph with lots of words.</p>

<br/><br/>

<p class='floatl'>The is a left floated paragraph with lots of words.</p>
<p class='floatr'>The is a right floated paragraph with lots of words.</p>


</body>
</html>
dnagirl
no this is not clear answer
metal-gear-solid
@ephemient but see here http://www.w3schools.com/css/tryit.asp?filename=trycss_display_inline display:inline work like float
metal-gear-solid
@Jitendra: your w3schools link only *looks* like it's working like float because the <p>s are no longer block block level elements and therefore appear beside each other. Try defining the width of the <p> to something short (say 4em) to avoid obscuring the effect. Then do the w3schools demo first with `p {display:inline; width:4em;}` and then with `p.l {float:left; width:4em;} p.r {float:right; width:4em;}`
dnagirl
@dnagirl p{display:inline;width:4em;} works same like p {display:inline}
metal-gear-solid
@jitendra: and the floats work differently. p.r should be narrow and over on the right side of your screen.
dnagirl
yes but my question is float: left vs display:inline
metal-gear-solid
the point being that if you have 2 <p>'s and they are both inline, even if you right justify the 2nd one, it will *still* be exactly next to the first one. Whereas if you have the first <p> inline OR floated and the 2nd <p> floated right, the <p> will be on opposite side of the screen.
dnagirl
+3  A: 

There are two types of formatting: block level and inline. The block level formatting is done with a box model that uses a set of rules for layout.

Inline formatting is what text normally gets. It means, informally, that things are filled into lines.

At times, you want to change the default formatting an item will get. Something that normally might be block level formatted you might want to have treated as inline. A graphic of a key, for example, might need to be displayed within a sentence. One might then override its formatting with display:inline.

The CSS specification has a surprisingly simple definition for floats:

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

So a float is a sort of third category of formatting model. It relates to inline as being, informally put, a layout model.

DigitalRoss
"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"but in this example http://www.w3schools.com/css/tryit.asp?filename=trycss_display_inline if i change display:inline to float:left output should be same but it's not if i use float:left then second paragraph doesn't come in flow with first paragraph
metal-gear-solid
That's because the second paragraph is also floated left. Give the first paragraph an id of `first` and change the selector from `p` to `p#first`.
NickFitz
A: 

The normal workflow of a browser is to display the elements from the left to the right, each near the other (if inline elements) ... when a line ends because of the size of the web page, the browser starts again to display the elements from the left to the right but on the next line.

A "float" directive, on an element, forces the browser to display the element out of the normal workflow, on the top-left or the top-right side of the webpage.

The "display inline" directive forces the block elements to be displayed inline, so the browser manage these elements as explain above!

IN RESPONSE TO YOUR QUESTION: No! As I've written: the float:left force any element (block or not block), placed on any line of a web page, to be floated on the left side of the web page ... even if the text aligning is set to "right"! If the text align is set to left, applying a float:left seams that nothing happens ... instead even in that case the element is forced to go out of the normal workflow of the browser, in fact, usually, the margin left is reseted!

The display:inline doesn't affects the inline elements ... if the text align (of the elements' container) is set to "right", a display:inline doesn't float it on the left (notice, I'm referring to inline elements)... because the "display:inline" says to the browser only to display an element in the same line of the normal workflow ... so, if an element is inline (for example a link element), this property doesn't change its behavior!

So, the "display:inline" affects only the "block" elements! But even in this case, it doesn't float left the block element, but it force the element only to be displayed in the same line of the other inline elements!

ABOUT YOUR EXAMPLE: The div(s) are block elements, the normal workflow is not inline ... so the browser, by default, shows them one below the other, like in this example:

<div class="purple" style="float:left">Link one</div> 
<div class="purple">Link two</div> 
<div class="purple">Link three</div> 
<div class="purple">Link four</div>

even if you apply a float:left to the first div, seams that nothing happens only why it is already on the top-left corner ... where should it go otherwise!!!???

The second example ...

<div class="red" style="float:left">Link one</div> 
<div class="red" style="float:left">Link two</div> 
<div class="red" style="float:left">Link three</div> 
<div class="red" style="float:left">Link four</div>

When you apply a float:left to adjacent div(s), force the browser to display them out of the normal workflow (As I sad, block elements appears, by default, one below each other!), floating the div(s) on the left side ... in this case one near each other. Notice that as I sad the margin are reseted ... because the div(s) are not on a ordinary line of the browser, but are only floated on the left ... again, out of the normal workflow!

In fact, the next example confirm what I sad in theory ... the display:inline force the browser to display block elements (div) on the same line respecting their default margin and padding:

<div class="brown" style="display:inline">Link one</div> 
<div class="brown" style="display:inline">Link two</div> 
<div class="brown" style="display:inline">Link three</div>

But the display:inline doesn't force elements to float on the left but only to be managed as inline elements, to clarify this concept look at the big difference between the two example below!

FIRST:

<div style="width:800px; text-align: right;">
    <div class="brown" style="display:inline">Link one</div> 
    <div class="brown" style="display:inline">Link two</div> 
    <div class="brown" style="display:inline">Link three</div> 
    <div class="brown" style="display:inline">Link four</div> 
</div>

SECOND:

<div style="width:800px; text-align: right;">
    <div class="brown" style="float:left">Link one</div> 
    <div class="brown" style="float:left">Link two</div> 
    <div class="brown" style="float:left">Link three</div> 
    <div class="brown" style="float:left">Link four</div>  
</div>

ABOUT THE WIDTH: the display:inline applied on a block element without a fixed width, it force the width to collapse to the minimum value (width of the content + padding), because this is the normal behavior of an inline element.

The div element, by default, has a width of 100% ... so, when you apply a float:left the width is still set to 100% but the floating on the left force the browser to manage and display its width in an unordinary way! In this case there isn't a general rule, each element has a different behavior!

BitDrink
See every browser has default alignment is left for everything. if i use display inline on 2 element then both would be in same line
metal-gear-solid
Yes, that's what I've written!!!
BitDrink
then display:inline will work same like float:left?
metal-gear-solid
What is the relation of float with width?
metal-gear-solid
Can you clarify your question?
BitDrink
http://jsbin.com/amiso see this example Why first group of doesn't behave like second group http://jsbin.com/ijiya/edit
metal-gear-solid
@BitDrink Why <p> under floated div doesn't behave same like anoter <div> under floated div, while both are bloack level element see this example http://jsbin.com/onemo
metal-gear-solid
A: 

I always remember what floating is by remembering the original <img align="left" /> which acts very similar to float:left. Basically float, floats the image to the left and wraps the text or other content around it. Without float it would display as a piece of text.

Float works similar to other document tools where you can have the text wrap around the image (or HTML element).

Darryl Hein
A: 

display:inline means a element is will "stack" next to other items, like images, spans or the bold tag. This is opposed by block level elements (display:block) which take up horizontal space, imply a line break, and will not sit next to one another, like divs, paragraphs, or tables.

Think of it this way...

<img /><img /><img />

<div />
<div />
<div />

float is a different notion altogether, moving content either left or right. By default, a floated element is inline, and floated elements will stack up next to one another.

All of these types of elements are part of the normal document flow, meaning they take up "space" in the design that cannot be shared.

Frank DeRosa
so what would happen if i set disply:inline to all <div>?
metal-gear-solid
they would act like `span`s...
Funka
<li> is a block level element but multiple <li> with display:inline act like float left but multiple div doesn't act same if i apply display:inline to moutiple div, why?
metal-gear-solid
That may depend on how wide the divs are. Most inline elements will stack up next to one another, "in a line", if you will.
Frank DeRosa
A: 

Every browser has a "flow". The flow sort of emulates a word processor in that elements flow from left to right, top to bottom. Elements that do not fit at the end of a "line", wrap to the next "line", so to speak.

Block elements take up the full line. Inline elements only take up as much space as they need, so other elements can sit next to them on the same line. Unless there is a width declared, that doesn't happen with block elements.

Floating an element takes the elements out of the normal flow and shifts it to the left/right. The space that the object occupied before it was floated is empty, and collapses. If I float two elements that take up more space than the line can hold, one may fall to the next "line".

@Jitendra - displaying two spans inline will put them together in the flow, yes. Floating two elements that do not take up the space of the full line will appear to do the same thing. They definitely have different uses, though.

If I wanted to have text flow around an image in a paragraph, I would float the image not use display:inline. If I wanted to create a horizontal menu from list item elements, I would use display:inline, not float.

tahdhaze09