Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between display: inline and display: inline-block?

Tags:

css

display

What exactly is the difference between the inline and inline-block values of CSS display?

like image 913
Logesh Paul Avatar asked Jan 23 '12 09:01

Logesh Paul


People also ask

What is difference between inline inline-block and block?

Block Elements occupy the full width irrespective of their sufficiency. Inline elements don't start in a new line. Block elements always start in a line. Inline elements allow other inline elements to sit behind.

What does display inline-block mean?

“display: inline-block” Property: This property is used to display an element as an inline-level block container. The element itself is formatted as an inline element, but it can apply height and width values. It is placed as an inline element (on the same line as adjacent content).

What is the difference between display flex and display inline-block?

The main difference between display: flex and display: inline-flex is that display: inline-flex will make the flex container an inline element while its content maintains its flexbox properties. In the below picture, the flex container comprises Computer, Science, Portal, and the yellow area. Example: html.

What does it mean to display inline?

display: inlineAn element with a display property set to inline will not start on a new line and it will take up the remaining/available screen width. It just takes up the space such an element would normally take.


2 Answers

A visual answer

Imagine a <span> element inside a <div>. If you give the <span> element a height of 100px and a red border for example, it will look like this with

display: inline

display: inline

display: inline-block

display: inline-block

display: block

enter image description here

Code: http://jsfiddle.net/Mta2b/

Elements with display:inline-block are like display:inline elements, but they can have a width and a height. That means that you can use an inline-block element as a block while flowing it within text or other elements.

Difference of supported styles as summary:

  • inline: only margin-left, margin-right, padding-left, padding-right
  • inline-block: margin, padding, height, width
like image 187
splattne Avatar answered Oct 13 '22 11:10

splattne


display: inline; is a display mode to use in a sentence. For instance, if you have a paragraph and want to highlight a single word you do:

<p>     Pellentesque habitant morbi <em>tristique</em> senectus     et netus et malesuada fames ac turpis egestas. </p> 

The <em> element has a display: inline; by default, because this tag is always used in a sentence. The <p> element has a display: block; by default, because it's neither a sentence nor in a sentence, it's a block of sentences.

An element with display: inline; cannot have a height or a width or a vertical margin. An element with display: block; can have a width, height and margin.
If you want to add a height to the <em> element, you need to set this element to display: inline-block;. Now you can add a height to the element and every other block style (the block part of inline-block), but it is placed in a sentence (the inline part of inline-block).

like image 40
Wouter J Avatar answered Oct 13 '22 10:10

Wouter J