What is the difference between Float:left vs Displ

2020-01-28 02:43发布

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_display_inline

标签: css
8条回答
Deceive 欺骗
2楼-- · 2020-01-28 03:21

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.

查看更多
一夜七次
3楼-- · 2020-01-28 03:25

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 div containing content such as graphic of a key, for example, might need to be displayed within a sentence. One might then override its formatting with display:inline. Images are already displayed "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.

查看更多
三岁会撩人
4楼-- · 2020-01-28 03:26

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.

查看更多
等我变得足够好
5楼-- · 2020-01-28 03:32

Go to w3schools and try this in their editor (because the image links are entirely theirs, or you can replace the image src from your image source urls) Then resize the windows.

What you will notice is.. That in case of display:inline, the text will split into words and some words of the text will appear in the next line. Though in the case of float:left the whole text will be displayed together as an element and will not split.

The purpose of inline is just to display everything in a LINE, though the purpose of float is to arrange ELEMENTS aligning to some dimension.

<!DOCTYPE html>
<html>
<head>
<style>
.thumb 
{
float:left;
}
.thumbnail 
{
display:inline;
}
</style>
</head>

<body>
<h3>Image Gallery</h3>
<br>Below are the inline elements<br>
<img class="thumbnail" src="klematis_small.jpg" width="107" height="90">
<img class="thumbnail" src="klematis2_small.jpg" width="107" height="80">
<img class="thumbnail" src="klematis3_small.jpg" width="116" height="90">
<img class="thumbnail" src="klematis4_small.jpg" width="120" height="90">
<p class="thumbnail">Try resizing the window to see what happens when the images does not have enough room.</p>
<img class="thumbnail" src="klematis_small.jpg" width="107" height="90">
<img class="thumbnail" src="klematis2_small.jpg" width="107" height="80">
<img class="thumbnail" src="klematis3_small.jpg" width="116" height="90">
<img class="thumbnail" src="klematis4_small.jpg" width="120" height="90">
<br><br>Below is the floating elements<br><br>
<img class="thumb" src="klematis_small.jpg" width="107" height="90">
<img class="thumb" src="klematis2_small.jpg" width="107" height="80">
<img class="thumb" src="klematis3_small.jpg" width="116" height="90">
<img class="thumb" src="klematis4_small.jpg" width="120" height="90">
<p class="thumb">Try resizing the window to see what happens when the images does not have enough room.</p>
<img class="thumb" src="klematis_small.jpg" width="107" height="90">
<img class="thumb" src="klematis2_small.jpg" width="107" height="80">
<img class="thumb" src="klematis3_small.jpg" width="116" height="90">
<img class="thumb" src="klematis4_small.jpg" width="120" height="90">
</body>
</html>
查看更多
干净又极端
6楼-- · 2020-01-28 03:35

Although it's too late to answer, but one of the major differences that I can mentioned here is about: Clear

In float elements you should Clear your floats but in inline elements no clearing is required.

You can see a live example here: http://jsfiddle.net/K9PXF/

And this a great article about floats and clearing: http://css-tricks.com/all-about-floats/

查看更多
萌系小妹纸
7楼-- · 2020-01-28 03:42

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>
查看更多
登录 后发表回答