search
HomeWeb Front-endCSS TutorialHow to Prevent Flex Items from Stretching to Container Width?

How to Prevent Flex Items from Stretching to Container Width?

How to Force Flex Items to Fit Content, Not Container Width

In flex layouts, elements naturally stretch to fill the width of their parent container. This can be undesirable when you want child elements to wrap their contents only. Here's how to enforce this behavior:

Solution Using align-items/align-self

Set align-items: flex-start on the container, or align-self: flex-start on the flex items. This overrides the default align-items: stretch, which causes items to expand to fill the container's horizontal space when the flex-direction is column.

How it Works

By setting align-items: flex-start, you force items to align themselves to the starting edge of the container. Since the column direction is set, the starting edge is the left edge. This forces items to stay as wide as their content only.

Example:

.container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}
a {
  padding: 10px 40px;
  background: pink;
}

Related Concepts

  • Cross Axis: The axis perpendicular to the primary layout axis (horizontally in column layouts).
  • align-items: Aligns flex items along the cross axis.
  • align-self: Similar to align-items, but specific to individual flex items.

The above is the detailed content of How to Prevent Flex Items from Stretching to Container Width?. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
What are pseudo-classes in CSS?What are pseudo-classes in CSS?Apr 30, 2025 pm 03:13 PM

The article discusses pseudo-classes in CSS, which enhance styling by targeting element states or positions, improving interactivity and design.

What are CSS Combinators?What are CSS Combinators?Apr 30, 2025 pm 03:12 PM

CSS combinators define relationships between selectors, enhancing specificity and targeting in CSS rules. The article discusses four types: descendant, child, adjacent sibling, and general sibling combinators, and their uses in styling nested HTML el

How can we center an image in CSS?How can we center an image in CSS?Apr 30, 2025 pm 03:11 PM

Article discusses CSS methods for centering images, including horizontal, vertical, and responsive centering techniques using text-align, margins, Flexbox, and media queries.

How can we vertically center a text in CSS?How can we vertically center a text in CSS?Apr 30, 2025 pm 03:10 PM

The article discusses various CSS methods for vertically centering text, including using line-height, padding, position with transform, flexbox, and CSS grid, each suitable for different layout needs.Character count: 159

What does display:inline-block do?What does display:inline-block do?Apr 30, 2025 pm 03:09 PM

The article discusses display:inline-block in CSS, which blends inline and block element features, allowing for flexible layout control. It contrasts with display:inline and examines its use in creating grid-like structures.

What does the CSS float property do?What does the CSS float property do?Apr 30, 2025 pm 03:08 PM

The article discusses the CSS float property, its effects on webpage layout, common uses, and limitations. It argues that while useful, floats have drawbacks compared to modern techniques like Flexbox and Grid.

What is CSS overflow?What is CSS overflow?Apr 30, 2025 pm 03:07 PM

The article discusses CSS overflow, a property that manages content exceeding its container's boundaries. It explains how to control overflow using values like visible, hidden, scroll, and auto, and highlights common use cases in web design.

What are the various positioning properties in CSS?What are the various positioning properties in CSS?Apr 30, 2025 pm 03:06 PM

The article discusses CSS positioning properties: static, relative, absolute, fixed, and sticky. It explains how to apply them and their impact on webpage layout, including document flow, space allocation, and responsive design.(159 characters)

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor