Software, your way.
burger menu icon
WillMaster

WillMaster > LibraryTutorials and Answers

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

A Short CSS 'display' Property Tutorial

With a div or other HTML container element (such as p, pre, or span), the display property determines if, where, and how the content in the element is displayed.

Without the display property, your web page would be a mishmash of haphazardly displayed content. (All HTML elements that contain content visible on a web page have a default display property if no other value is assigned.)

One short tutorial can't cover the basics of every display property value. This tutorial addresses values:

  • none
  • block
  • inline
  • inline-block
  • table
  • table-cell

Although the display property can be used in any HTML container element, div will be used for the examples.

The none Value

The CSS display:none; declaration undisplays the content.

Example:

<div style="display:none;">HELLO!</div>

With the above in the web page source code, the content "HELLO!" won't be published.

It can be seen in the web page source code with a browser's "view source" menu item. But it won't be rendered on the page. Not even the space it would otherwise occupy is rendered. As far as the rendered web page is concerned, the div with "HELLO!" doesn't exist.

The block Value

The CSS display:block; declaration displays the content after a line break and ends with a line break. Unless a CSS declaration tells it otherwise, the div extends horizontally the entire width of the container, from left margin to right margin.

Example:

<div style="display:block; border:1px solid black; padding:3px;">HELLO!</div>

The code renders like this:

HELLO!

Many HTML container elements are display:block; by default. Examples are div, h3, p, and pre. Other HTML container elements have different default display values. Examples mentioned below are span, table, and td.

The inline Value

The CSS display:inline; declaration displays content within the line. There's no line break before or after unless it happens in the natural flow of the content.

Example:

He shouted 
<div style="display:inline; border:1px solid black; padding:3px;">HELLO!</div> 
three times.

The code renders like this:

He shouted
HELLO!
three times.

The span element is display:inline; by default. Other elements, like div in the example, can be assigned the CSS display:inline; declaration so they behave like a span element would.

The inline-block Value

The CSS display:inline-block; declaration is like a display:inline; declaration except it can have a width and a height like display:block; declarations can.

Example:

He shouted 
<div style="display:inline-block; border:1px solid black; padding:3px;">HELLO!
<br>very
<br>LOUD</div> 
three times.

The code renders like this:

He shouted
HELLO!
very
LOUD
three times.

I'm aware of no standard HTML elements with a default display of display:inline-block;.

The table Value

The CSS display:table; declaration displays the content as if it was in an HTML table element. The width of the div is only as wide as required for the content. It causes a line break just like the HTML table element would.

Example:

<div style="display:table; border:1px solid black; padding:3px;">
HELLO!
</div>

The code renders like this:

HELLO!

The table element is display:table; by default. Other elements, like div in the example, can be assigned the CSS display:table; declaration so they behave like a table element would.

The table-cell Value

The CSS display:table-cell; declaration displays the content as if it was in an HTML table's td element. The width of the div is only as wide as required for the content. It does not insert a line break just like a table's td element wouldn't insert a line break.

Example:

<div style="display:table-cell; border:1px solid black; padding:3px;">
HELLO!
</div>
<div style="display:table-cell; border:1px solid black; padding:3px;">
HELLO!
</div>
<div style="display:table-cell; border:1px solid black; padding:3px;">
HELLO!
</div>

The code renders like this:

HELLO!
HELLO!
HELLO!

The HTML table's td element is display:table-cell; by default. Other elements, like div in the example, can be assigned the CSS display:table-cell; declaration so they behave like an HTML table's td element would.

Specifying a CSS display:table-cell; declaration for a div does not require an enclosing div with a CSS display:table; declaration. This may be counter-intuitive because of the "td elements must be inside a table element" requirement for HTML tables. But a CSS display:table-cell; declaration in a div doesn't have that requirement — it's independent.

Other display Properties

There are more values that can be assigned to the CSS display property. This article covered only the ones I think you're most likely to use.

Here's a list of CSS display properties not covered in this article. CSS Display Page has information and links for many of them.

display ValueComment
flexNew in CSS3. See Exact Centering With Flex for an example of use.)
inline-flexNew in CSS3. To display an element as an inline-level flex container.
inline-tableTo display a container element as an inline-level table.
list-itemTo make the container element's display behave like a li list element
run-inTo make the container element display depend on context, either block or inline
table-captionTo make the container element's display behave like a caption element of a table
table-column-groupTo make the container element's display behave like a colgroup element of a table
table-header-groupTo make the container element's display behave like a thead element of a table
table-footer-groupTo make the container element's display behave like a tfoot element of a table
table-row-groupTo make the container element's display behave like a tbody element of a table
inheritTo make the element's value is the same as the parent element's value. (The parent element is the element containing this element.)
initialTo set the element's display value to default.

You have examples and explanations for what are likely to be your most-used display property values. The table provides a hint of what else is possible.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC