学习库CSSFlexbox

Flexbox

Arrange items along one main axis and handle alignment, gaps, and wrapping.

更新于 适用于 Modern CSS

Flexbox lays out a group primarily in one direction. It is ideal for navigation bars, button rows, media objects, and component internals where content size should influence distribution.

Start with the axes

header.css
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

flex-direction defines the main axis. justify-content distributes space on that axis; align-items aligns items across it.

Let items grow and shrink

toolbar.css
.toolbar { display: flex; gap: 0.75rem; }
.toolbar__search { flex: 1 1 18rem; min-width: 10rem; }
.toolbar__actions { flex: 0 0 auto; }

The three flex values are grow, shrink, and basis. A flexible search field can absorb free space while actions keep their intrinsic width.

Wrap when space runs out

tags.css
.tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

Wrapping is useful when the number or width of items varies. For row-and-column alignment across multiple lines, Grid is usually a better fit.

Keep this

Use Flexbox for one-dimensional relationships, understand both axes, prefer gap for spacing, and keep DOM order meaningful.

学习位置已保存在此设备上。