display:inline vs display:block

Css

Css Problem Overview


What is the basic difference between the following CSS:

display:inline

and this:

display:block

Using these separately on an element, I get the same result.

Css Solutions


Solution 1 - Css

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

Solution 2 - Css

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;)

Solution 3 - Css

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

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

Solution 4 - Css

Here is a comparison table:enter image description here

You can view examples here.

Solution 5 - Css

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.

Solution 6 - Css

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/>;.

Solution 7 - Css

block elements expand to fill their parent.

inline elements contract to be just big enough to hold their children.

Solution 8 - Css

display:block

takes the entire row(100%)of the screen ,it is always 100%of the screen size

display block example

display:inline-block takes up as much width as necessary ,it can be 1%-to 100% of the screen size

display inline-block example

that's why we have div and span

Div default styling is display block :it takes the entire width of the screen

span default styling is display:inline block :span does not start on a new line and only takes up as much width as necessary

Solution 9 - Css

Add a background-color to the element and you will nicely see the difference of inline vs. block, as explained by the other posters.

Solution 10 - Css

Display:block It very much behaves the same way as 'p' tags and it takes up the entire row and there can't be any element next to it until it's floated. Display:inline It's just uses as much space as required and allows other elements to be aligned alongside itself.

Use these properties in case of forms and you will get a better understanding.

Solution 11 - Css

a block or inline-block can have a width (e.g. width: 400px) while inline element is not affected by width. inline element can span to the next line of text (example http://codepen.io/huijing/pen/PNMxXL resize your browser window to see that) while block element can't.

 .inline {
      background: lemonchiffon;
      div {
        display: inline;
        border: 1px dashed darkgreen;
      }

Solution 12 - Css

Block Elements: Elements liked div, p, headings are block level. They start from new line and occupy full width of parent element. Inline Elements: Elements liked b, i, span, img are inline level. They never start from new line and occupy width of content.

Solution 13 - Css

By default, inline elements do not force a new line to begin in the document flow. Block elements, on the other hand, typically cause a line break to occur you can refer this link

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNazmulView Question on Stackoverflow
Solution 1 - CssPranay RanaView Answer on Stackoverflow
Solution 2 - CssRavishView Answer on Stackoverflow
Solution 3 - Cssxj9View Answer on Stackoverflow
Solution 4 - CssNiko BellicView Answer on Stackoverflow
Solution 5 - CssDamien WilsonView Answer on Stackoverflow
Solution 6 - CssAarthi ChandrasekaranView Answer on Stackoverflow
Solution 7 - CssJames CurranView Answer on Stackoverflow
Solution 8 - CssHarshitView Answer on Stackoverflow
Solution 9 - CssJanick BernetView Answer on Stackoverflow
Solution 10 - CsskunalView Answer on Stackoverflow
Solution 11 - CssEKanadilyView Answer on Stackoverflow
Solution 12 - CssAvinash MalhotraView Answer on Stackoverflow
Solution 13 - CssRohan DevakiView Answer on Stackoverflow