Home > Web Front-end > CSS Tutorial > How Can I Easily Control Spacing Between Flexbox Items?

How Can I Easily Control Spacing Between Flexbox Items?

DDD
Release: 2024-12-20 06:59:11
Original
860 people have browsed it

How Can I Easily Control Spacing Between Flexbox Items?

Managing Flexbox Item Spacing

Question:

In flexbox layouts, how can I adjust the distance between items without resorting to intricate margins?

**Answer:

CSS Gap Property:

For advanced browsers, the gap property can be utilized to establish spacing both vertically and horizontally in flexbox, grid, and multi-column layouts.

#box {
  display: flex;
  gap: 10px;
}
Copy after login

CSS Row-Gap Property:

For vertical spacing specifically in flexbox and grid layouts, the row-gap property is an option.

#box {
  display: flex;
  row-gap: 10px;
}
Copy after login

CSS Column-Gap Property:

In multi-column, flexbox, and grid layouts, the column-gap property controls horizontal spacing.

#box {
  display: flex;
  column-gap: 10px;
}
Copy after login

Example:

The following snippet demonstrates the use of the gap property to create spacing between flexbox items:

<div>
Copy after login
#box {
  display: flex;
  flex-wrap: wrap;
  width: 200px;
  background-color: red;
  gap: 10px;
}

.item {
  background: gray;
  width: 50px;
  height: 50px;
  border: 1px black solid;
}
Copy after login

The above is the detailed content of How Can I Easily Control Spacing Between Flexbox Items?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template