Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display:inline vs display:block [duplicate]

Tags:

css

People also ask

What is the difference between display block and display inline?

Also, with display: inline-block , the top and bottom margins/paddings are respected, but with display: inline they are not. Compared to display: block , the major difference is that display: inline-block does not add a line-break after the element, so the element can sit next to other elements.

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 are the differences between inline-block and inline-block?

block: demands its own line, with whitespace around it. inline-block: can have elements before or after it, but there is whitespace around it. So inline-block is not "inline but behaves like block," it's a combination of both, as the name would imply: on the same line, but has borders.

Why do we use display block?

display: block An element that has the display property set to block starts on a new line and takes up the available screen width. You can specify the width and height properties for such elements. Examples of elements that are at block-level by default are <div> , <section> , <p> , and lots more.


display: block means that the element is displayed as a block, as paragraphs and headers have always been. A block has some whitespace above and below it and tolerates no HTML elements next to it, except when ordered otherwise (by adding a float declaration to another element, for instance).

display: inline means that the element is displayed inline, inside the current block on the same line. Only when it's between two blocks does the element form an 'anonymous block', that however has the smallest possible width.

Read more about display options : http://www.quirksmode.org/css/display.html


Block

Takes up the full width available, with a new line before and after (display:block;)

Inline

Takes up only as much width as it needs, and does not force new lines (display:inline;)


display: block - a line break before and after the element

display: inline - no line break before or after the element


Here is a comparison table:enter image description here

You can view examples here.


display: block; creates a block-level element, whereas display: inline; creates an inline-level element. It's a bit difficult to explain the difference if you're not familiar with the css box model, but suffice to say that block level elements break up the flow of a document, whereas inline elements do not.

Some examples of block level elements include: div, h1, p, and hr HTML tags.

Some examples of inline level elements include: a, span, strong, em, b, and i HTML tags.

Personally, I like to think of inline elements as typographical elements. This isn't entirely or technically correct, but for the most part inline elements do behave a lot like text.

You can read a more through article on the topic here. Seeing as several other people in this thread have quoted it, it may be worth a read.


Display : block will take the whole line i.e without line break

Display :inline will take only exact space that it requires.

 #block
  {
   display : block;
   background-color:red;
   border:1px solid;
  }

 #inline
 {
  display : inline;
  background-color:red;
  border:1px solid;
 }

You can refer example in this fiddle http://jsfiddle.net/RJXZM/1/.