CSS Display

The display property in CSS specifies how an element is displayed in the document. It controls the layout behavior, visibility, and positioning of elements on a web page.

Display Values

Common display property values include:

Block Display

Block-level elements start on a new line and occupy the full width of their parent container.

div {
  display: block;
}

Inline Display

Inline elements flow with text and only take up as much width as needed.

span {
  display: inline;
}

Inline-Block Display

Inline-block elements behave like inline elements but allow setting width and height.

button {
  display: inline-block;
  width: 150px;
  height: 40px;
}

None Display

Setting display: none; hides an element completely from the page layout.

div.hidden {
  display: none;
}

Flex Display

Flexbox allows flexible layouts with alignment and spacing control.

.container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

Grid Display

CSS Grid allows creating complex layouts with rows and columns.

.container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
}

Best Practices for Display

Common Mistakes with Display

Tip: Understanding display is key to mastering CSS layouts and creating responsive web designs.