Curriculum
Course: CSS
Login

Curriculum

CSS

CSS INTRODUCTION

0/1

CSS Selectors

0/1

CSS Comments

0/1

CSS Padding

0/1

CSS Box Model

0/1

CSS Combinators

0/1

CSS Pseudo-classes

0/1

CSS Pseudo-elements

0/1

CSS Dropdowns

0/1

CSS Image Gallery

0/1

CSS Image Sprites

0/1

CSS Counters

0/1

CSS Website Layout

0/1

CSS Specificity

0/1

CSS Math Functions

0/1
Text lesson

CSS Layout – The display Property

The display Property

The display property dictates the presentation of an element within a webpage.

Each HTML element possesses a default display value, determined by its type, typically block or inline.

By altering the display property, you can modify the default presentation behavior of HTML elements.

Block-level Elements

A block-level element consistently begins on a fresh line and occupies the entire available width, extending to the left and right edges as much as possible.

The <div> element is a block-level element.

Examples of elements that behave as block-level:

  • <div>
  • <h1> – <h6>
  • <p>
  • <form>
  • <header>
  • <footer>
  • <section>

Inline Elements

An inline element doesn’t initiate on a new line and occupies only the width required for its content.

This is an inline <span> element inside a paragraph.

Examples of elements that behave as inline:

  • <span>
  • <a>
  • <img>

The display Property Values

The display property encompasses numerous values:

Value

Description

inline

Renders an element as an inline element.

block

Renders an element as a block element.

contents

Makes the container disappear, causing its child elements to become children of the parent element in the DOM.

flex

Render an element as a block-level flex container

grid

Render an element as a block-level grid container

inline-block

Renders an element as an inline-level block container. The element maintains inline formatting but allows height and width values to be applied.

inline-flex

Render an element as an inline-level flex container

inline-grid

Render an element as an inline-level grid container

inline-table

The element is rendered as an inline-level table.

list-item

Allow the element to function similarly to an <li> element.

run-in

Renders an element as either block or inline, based on the context.

table

Enable the element to function similarly to a <table> element.

table-caption

Allow the element to function similarly to a <caption> element.

table-column-group

Enable the element to function similarly to a <colgroup> element.

table-header-group

Allow the element to function similarly to a <thead> element.

table-footer-group

Enable the element to function similarly to a <tfoot> element.

table-row-group

Allow the element to function similarly to a <tbody> element.

table-cell

Allow the element to function similarly to a <td> element.

table-column

Enable the element to function similarly to a <col> element.

table-row

Allow the element to function similarly to a <tr> element.

none

The element is entirely eliminated.

initial

Resets this property to its default value.

inherit

This property inherits its value from its parent element.

Display: none;

display: none; is frequently employed with JavaScript to toggle the visibility of elements without removing or recreating them. Refer to the final example on this page for a demonstration of this technique.

By default, the <script> element utilizes display: none; styling.

Override The Default Display Value

As previously stated, each element is assigned a default display value, which can be overridden.

Converting an inline element to a block element, or vice versa, can be advantageous for achieving a desired appearance while adhering to web standards.

A typical scenario involves transforming inline <li> elements into block elements for horizontal menus.

Example

li {
  display: inline;
}
Please note: Altering the display property of an element solely affects its visual presentation, not its inherent type. Thus, an inline element with display: block; cannot contain other block elements within it.

The following example presents <span> elements as block-level elements.

Example

span {
  display: block;
}

The following example exhibits <a> elements as block-level elements.

Example

{
  display: block;
}

Hide an Element – display:none or visibility:hidden?

Concealing an element is achieved by setting its display property to none. This action hides the element, causing the page to appear as though the element is absent.

Example

h1.hidden {
  display: none;
}

visibility:hidden; also hides an element.

Nonetheless, the element will maintain its previous space allocation, persisting in its hidden state while continuing to impact the layout.

Example

h1.hidden {
  visibility: hidden;
}

CSS Display/Visibility Properties

Property

Description

display

Defines the display behavior of an element.

visibility 

Determines whether an element should be visible or not.