In CSS, the display property is crucial as it determines how an element is displayed and laid out on a page. Here are some common values of the display property and their effects:
-
none:- Effect: Completely hides the element and does not reserve space for it.
- Example: When you want to hide certain elements under specific conditions, such as dynamically hiding or showing content with JavaScript.
-
block:- Effect: Makes the element behave as a block-level element, occupying the full width of its line, and subsequent elements appear on a new line.
- Example: Used for layout, such as creating self-contained content blocks, like paragraphs, headings, and containers.
-
inline:- Effect: Makes the element display inline, not occupying a full line, with its width determined solely by its content.
- Example: Used for formatting text, such as
<span>or<a>elements, to display them inline within paragraphs.
-
inline-block:- Effect: Combines the characteristics of
inlineandblock, not occupying a full line but allowing width and height to be set. - Example: When you need to display multiple blocks in a single line and control their size, such as each item in a navigation menu.
- Effect: Combines the characteristics of
-
flex:- Effect: Makes the element a flex container, allowing its child elements to utilize the powerful features of flex layout.
- Example: Used to create responsive layouts where child elements' sizes and order can be flexibly adjusted.
-
grid:- Effect: Makes the element a grid container, allowing you to define rows and columns for creating complex two-dimensional layouts.
- Example: Used for designing complex page layouts, such as magazine or newspaper-style layouts.
-
table,table-row,table-cell, etc.:- Effect: These values mimic the behavior of HTML table tags, allowing page content to be laid out in a table format.
- Example: When you want to present table data using CSS, you can choose these values.
-
list-item:- Effect: Makes the element behave as a list item, typically displayed with list markers.
- Example: Used to customize the appearance of lists, such as custom list item symbols or layouts.
These are some commonly used values of the display property. Additionally, many other values and property combinations can be used to meet specific layout requirements. As web technologies evolve, the CSS specification continues to introduce new display types to address more complex design challenges. Continuing to explain more values of the display property:
-
inline-flex:- Effect: Makes the element an inline flex container, meaning it can be laid out within a text line like an
inlineelement, while its child elements can use the flexbox model. - Example: If you want a small layout unit to be laid out within a text line while using flexbox layout internally, such as a small card within a paragraph.
- Effect: Makes the element an inline flex container, meaning it can be laid out within a text line like an
-
inline-grid:- Effect: Makes the element an inline grid container, combining the characteristics of
inlineandgrid. - Example: When you need to embed a small grid layout within a text flow, such as a complex mathematical formula or chart.
- Effect: Makes the element an inline grid container, combining the characteristics of
-
contents:- Effect: Makes the child elements appear as if directly placed at the position of the parent element, with the parent itself not rendered as any box model, but its child elements are displayed normally.
- Example: When you need a container for semantic organization without creating a new layout level.
-
run-in:- Effect: Depending on the context, the element may behave as a
blockorinlineelement. - Example: This value is relatively rare and can be used for layout between headings and paragraphs in certain cases.
- Effect: Depending on the context, the element may behave as a
-
flex-start,flex-end,center,space-between,space-around,space-evenly:- Effect: These values are primarily used with flex container properties like
align-items,align-content, andjustify-content, not thedisplayproperty, to define alignment of flex items along the main or cross axis. - Example: When you need to align or distribute child items within a flex container.
- Effect: These values are primarily used with flex container properties like
-
grid-auto-columns,grid-auto-rows:- Effect: These values are used on
gridcontainers to define the size of implicitly created rows or columns. - Example: When you have a dynamic number of grid items and need automatic row or column sizes.
- Effect: These values are used on
-
grid-template-columns,grid-template-rows:- Effect: These values are used on
gridcontainers to define the size and number of explicitly created rows or columns. - Example: When designing a specific grid layout where you need to specify the size of each column or row.
- Effect: These values are used on
-
grid-column-start,grid-column-end,grid-row-start,grid-row-end:- Effect: These values are used on
griditems to define their position and span across columns or rows. - Example: When you need to place an element in the grid that spans multiple columns or rows.
- Effect: These values are used on
The CSS display property is a complex and powerful tool capable of handling various layout requirements. As the CSS specification continues to evolve, new display values and layout models like Flexbox and Grid provide unprecedented flexibility and control.