CSS Display:inline|block|inline-block Differences

Source: Internet
Author: User

Display:inline|block|inline-block the difference and connection of three attributes, block is to display elements as block-level elements, inline is to display elements as inline elements, CSS display: Inline-block objects are presented as inline objects, but the contents of the objects are presented as block objects. Adjacent inline objects are presented in the same line, allowing spaces.

A detailed explanation of the differences between CSS Display:inline|block|inline-block

CSS Display:block

CSS Display:block is the display of elements as block-level elements.

The block element is characterized by:

Always start on a new line;
Height, row height and top and bottom margin can be controlled;
The width defaults to 100% of its container unless a width is set
<div>,<p>,

CSS Display:inline

CSS Display:inline is to display elements as inline elements.

The features of the inline element are:

And the other elements are on one line;
Height, row height and top and bottom margin can not be changed;
Width is the width of the text or picture, and it cannot be changed.
<span>,<a>,<label>,<input>,,<strong> and <em> are examples of inline elements.

CSS Display:inline and blocks can control the line width of an element with high characteristics, the need to switch the situation is as follows:

Let an inline element start from the new line;
Keep The block elements and other elements on one line;
Controls the width of the inline element (especially useful for navigation bars);
Controls the height of the inline element;
You can set the background color of a block element to the same width as the text without having to set a width.

The CSS display:inline-block the object as an inline object, but the object's contents are presented as a block object. Adjacent inline objects are presented in the same line, allowing spaces.

Features of CSS display:inline-block elements:

Renders an object as an inline object, but the contents of the object are presented as block objects. Adjacent inline objects are presented in the same line, allowing spaces. (accurately, the elements that apply this attribute are rendered as inline objects, the surrounding elements remain on the same line, but you can set the properties of the width and height parcel elements)

Not all browsers support this property, currently supported browsers are: Opera, safari in IE the use of inline elements Display:inline-block,ie is not known, but using display: Inline-block in IE will trigger layout, so that inline elements have the CSS Display:inline-block attributes of the table disease. From the above analysis, it is not difficult to understand why ie, the block element set Display:inline-block property can not achieve inline-block effect. When the block element is only triggered by the CSS Display:inline-block layout, and it is the row layouts, so after the trigger, the block element is still the row layout, but not as the block elements in opera as an inline object.

How does the IE block element realize the effect of CSS Display:inline-block?

There are two ways of doing this:

1, first use the CSS Display:inline-block property to trigger the block element, and then define the Display:inline, so that the block elements presented as inline objects (two display to be placed in two CSS declarations have effect, this is a classic IE bug, If Display:inline-block is defined first, then the display is set back to inline or block,layout does not disappear). The code is as follows (...). Other attribute content omitted):

Div {display:inline-block; ...}   Div{display:inline

2, directly let the block element set to inline object presentation (set property CSS Display:inline), and then trigger the layout of the block elements (such as: zoom:1, etc.). The code is as follows:

Div {display:inline; Zoom:1; ... ..

Here is an example to illustrate the difference and usage of the three:

<style>Div,span{Background-color:Green;margin:5px;Border:1pxsolid#333;padding:5px;Height:52px;Color:#fff;}. b{Display:Block;}. I{Display:inline;}Div.ib{Display:Inline-block;}Div.ib{Display:inline;}A.ib{Display:Inline-block;}A.ib{Display:Block;}SPAN.V{padding:0;margin:0;Border:0;vertical-align:Middle;Height:100%}  </style>    <spanclass= "Body_cls">The IS error</span>    <Div>Div Display:block</Div>     <Divclass= "I">Div Display:inline</Div>    <Divclass= "IB">Div Display:inline-block</Div>    <span>Span Display:inline</span>    <spanclass= "B">Span Display:block</span>    <span>        <aclass= "IB">A Display:block</a>    </span>
<
BR/>    <Div>    <spanclass= "V"></span>Vertical-align:middle</Div>

CSS Display:inline|block|inline-block Differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.