惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
I
InfoQ
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
L
LangChain Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Recent Announcements
Recent Announcements
GbyAI
GbyAI
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
腾讯CDC
美团技术团队
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
WordPress大学
WordPress大学
T
The Blog of Author Tim Ferriss
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
M
MIT News - Artificial intelligence
D
Docker
MongoDB | Blog
MongoDB | Blog
F
Fortinet All Blogs
博客园 - 叶小钗

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Understanding Sizing in CSS Flexbox (A Practical Guide)
Abdulmujeeb · 2026-04-25 · via DEV Community

The first time I really tried to understand sizing in Flexbox, I got stuck. Not because I didn’t understand width and height.Not because I didn’t know flex-grow or flex-shrink. But because everything I thought I knew about sizing suddenly stopped applying.

In normal layout, things feel predictable:

  • Block elements fill width
  • Height depends on content
  • width and height behave like firm instructions

Then Flexbox shows up… and starts bending those rules.


The Moment Things Started Breaking

I remember setting width on a flex item and expecting it to behave normally.It didn’t.Then I tried adjusting height. Still weird.
Then I added align-items, and suddenly things stretched in ways I didn’t expect.

At that point, it felt like:

“Flexbox doesn’t respect width and height.”

But that’s not true.

Flexbox does respect them — just not in the way we’re used to.


What up with Flexbox?

Flexbox doesn’t think in horizontal vs vertical
It thinks in main axis vs cross axis

And once I accepted that, things started making sense.


Step 1: The Container Is Not the Problem

Let’s start simple.

<div style="display: flex;"></div>

Enter fullscreen mode Exit fullscreen mode

That div is still a block element.
flex container

  • It still fills its parent’s width
  • Its height still depends on content

display: flex does not change how the container behaves outside
It only changes how its children are laid out

This was my first misconception.


Step 2: Everything Depends on Direction

Flexbox introduces a new idea: axes.

flex-direction Main Axis Cross Axis
row horizontal vertical
column vertical horizontal

This one property controls everything.

And I mean everything.

flex axes

Step 3: The Real Players in Sizing

Once axes are defined, sizing becomes a game between a few properties:

  • flex-basis: starting size (main axis)
  • flex-grow: how items expand
  • flex-shrink: how items shrink
  • align-items: cross-axis behavior
  • width / height: physical dimensions (sometimes ignored!)

Let’s Talk About Row (Where Most People Start)

.container {
  display: flex;
  flex-direction: row;
}

Enter fullscreen mode Exit fullscreen mode

  • Main axis: horizontal
  • Cross axis: vertical

What Happens to Width?

This is the main axis now.

So Flexbox asks:

  1. Do you have a width? Yes use it as a starting point
  2. If not, do you have flex-basis? Yes, use that
  3. Otherwise: use content size

Then:

  • Extra space? flex-grow distributes it
  • Not enough space? flex-shrink reduces it

What Happens to Height? This is cross-axis now.

Flexbox handles it differently:

  • If height is set, it uses it
  • If not:

    • align-items: stretch (default), fills container height
    • Otherwise, content height

flex items sizing

Then I Switched to Column… and Everything Broke Again

.container {
  display: flex;
  flex-direction: column;
}

Enter fullscreen mode Exit fullscreen mode

Now:

  • Main axis, vertical
  • Cross axis, horizontal

Everything flips.


Column Direction (The Part That Confuses Everyone)

Height Becomes the Main Axis

Now Flexbox treats height the way it treated width before.

It asks:

  1. Do you have a height?
  2. Else, do you have flex-basis?
  3. Else use content size

Then:

  • Extra vertical space, flex-grow
  • Not enough,flex-shrink

Width Becomes Cross Axis

Now width behaves like height did before:

  • If width is set,use it
  • If not:

    • align-items: stretch, fill container width
    • Else, content width

flex item sizing

The Realization That Tied Everything Together

After going back and forth enough times, I realized:

Flexbox doesn’t randomly ignore properties
It just prioritizes based on the axis


The Rule That Finally Made It Click

Main axis, controlled by flex properties
Cross axis, controlled by alignment

And:

width and height only matter when they align with the axis being calculated


One Last Correction I Wish I Knew Earlier

I used to think:

flex-basis is the minimum size”

That’s wrong.

It’s actually:

The starting size before growing or shrinking

That small misunderstanding caused a lot of confusion for me.


How I Debug Flexbox Now

Whenever something looks off, I ask:

  1. What is the main axis?
  2. What is the starting size (flex-basis / width / height)?
  3. Is there extra space or overflow?
  4. What is controlling the cross axis (align-items)?

And suddenly, it’s no longer magic.


Final Thought

Flexbox feels confusing because it asks you to stop thinking in fixed dimensions and start thinking in relationships:

  • Between items
  • Between axes
  • Between available space and constraints

Once you make that mental switch, Flexbox stops fighting you…
and starts working with you.