The display property in CSS is fundamental for controlling the layout of elements on a web page. It helps in defining how elements are rendered on the screen.
Understanding the different values of the display property can greatly enhance your ability to control the layout and visibility of elements.
Property | Description |
---|---|
block | The element is rendered as a block-level element. It starts on a new line and takes up the full width available. |
inline | The element is rendered as an inline-level element. It does not start on a new line and only takes up as much width as necessary. |
inline-block | The element is rendered as an inline-level element, but it behaves like a block element. It does not start on a new line, but you can set width and height values. |
contents | The element itself is not rendered, but its child elements are rendered as if they were children of the parent element. |
flex | The element is rendered as a block-level flex container. It enables a flexible layout system for its children. |
grid | The element is rendered as a block-level grid container. It enables a grid layout system for its children. |
inline-flex | The element is rendered as an inline-level flex container. It behaves like an inline element, but its children are laid out using the flexbox model. |
inline-grid | The element is rendered as an inline-level grid container. It behaves like an inline element, but its children are laid out using the grid model. |
inline-table | The element is rendered as an inline-level table. It behaves like an inline element, but it has the characteristics of a table. |
list-item | The element is rendered as a list item. It typically has a bullet or a number. |
run-in | The element is rendered as either block or inline, depending on the context. |
table | The element is rendered as a block-level table. It behaves like a table element. |
table-caption | The element is rendered as a table caption. It is used within a table to describe its content. |
table-column-group | The element is rendered as a group of columns within a table. It allows you to apply styles to multiple columns at once. |
table-header-group | The element is rendered as a group of header rows within a table. It is typically used for styling table headers. |
table-footer-group | The element is rendered as a group of footer rows within a table. It is typically used for styling table footers. |
table-row-group | The element is rendered as a group of rows within a table. It is typically used for styling table body rows. |
table-cell | The element is rendered as a table cell. It is used within a table to represent a single cell. |
table-column | The element is rendered as a column within a table. It allows you to apply styles to individual columns. |
table-row | The element is rendered as a row within a table. It is used within a table to represent a single row. |
none | The element is not rendered at all and takes up no space in the layout. |
initial | The element inherits the initial value of the display property as defined by the CSS specifications. |
inherit | The element inherits the display property from its parent element. |
display: block; causes an element to behave like a block element. It starts on a new line and stretches out to the left and right as far as it can.
.block-element {
display: block;
width: 100%;
background-color: lightblue;
}
display: inline; makes the element lay out inline, meaning it will not start on a new line and only occupy just enough width it requires.
.inline-element {
display: inline;
background-color: lightgreen;
}
display: inline-block; elements are like display: inline; elements but they can have a width and height, which allows them to be adjusted without breaking the flow of the document.
.inline-block-element {
display: inline-block;
width: 200px;
height: 100px;
background-color: lightcoral;
}
display: contents; makes the container disappear, making the child elements children of the element the next level up in the DOM.
.contents-element {
display: contents;
}
display: flex; turns the element into a flexible container, allowing more complex, alignable layouts that can adjust their child elements' sizes to fit available space.
.flex-container {
display: flex;
justify-content: space-between;
background-color: lightgray;
}
.flex-item {
background-color: deepskyblue;
width: 100px;
height: 100px;
}
display: grid; enables a grid-based layout system that provides an easy way to design complex responsive layouts.
.grid-container {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
background-color: lightyellow;
}
.grid-item {
background-color: lightseagreen;
width: 100px;
height: 100px;
}
display: inline-flex; displays an element as an inline-level flex container.
.inline-flex-container {
display: inline-flex;
justify-content: space-between;
background-color: lightpink;
}
.flex-item {
background-color: lightcoral;
width: 100px;
height: 100px;
}
display: inline-grid; displays an element as an inline-level grid container.
.inline-grid-container {
display: inline-grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
background-color: lightgoldenrodyellow;
}
.grid-item {
background-color: lightgreen;
width: 100px;
height: 100px;
}
display: inline-table; displays an element as an inline-level table.
.inline-table {
display: inline-table;
background-color: lightblue;
}
display: list-item; makes the element behave like a li element.
.list-item {
display: list-item;
list-style-type: disc;
margin-left: 20px;
}
display: run-in; displays an element as either block or inline, depending on context.
.run-in {
display: run-in;
}
display: table; makes the element behave like a table element.
.table {
display: table;
width: 100%;
background-color: lightgray;
}
display: table-caption; makes the element behave like a caption element.
.table-caption {
display: table-caption;
caption-side: top;
background-color: lightyellow;
}
display: table-column-group; makes the element behave like a colgroup element.
.table-column-group {
display: table-column-group;
background-color: lightblue;
}
display: table-header-group; makes the element behave like a thead element.
.table-header-group {
display: table-header-group;
background-color: lightcoral;
}
display: table-footer-group; makes the element behave like a tfoot element.
.table-footer-group {
display: table-footer-group;
background-color: lightgreen;
}
display: table-row-group; makes the element behave like a tbody element.
.table-row-group {
display: table-row-group;
background-color: lightpink;
}
display: table-cell; makes the element behave like a td element.
.table-cell {
display: table-cell;
padding: 10px;
background-color: lightyellow;
}
display: table-column; makes the element behave like a col element.
.table-column {
display: table-column;
background-color: lightgray;
}
display: table-row; makes the element behave like a tr element.
.table-row {
display: table-row;
background-color: lightblue;
}
display: none; completely removes the element from the document's layout, meaning the document is rendered as if the element does not exist at all.
.none-element {
display: none;
}
display: initial; sets this property to its default value.
.initial-element {
display: initial;
}
display: inherit; inherits this property from its parent element.
.inherit-element {
display: inherit;
}