Back to list
dev_to 2026年4月25日

CSS Flexbox のサイズ調整を理解する方法(実用的ガイド)

Understanding Sizing in CSS Flexbox (A Practical Guide)

Translated: 2026/4/25 3:39:56
flexboxcssresponsive-designfrontend-developmentweb-layout

Japanese Translation

Flexbox のサイズ調整を初めて深く理解しようと試みたとき、私は詰まってしまうことがありました。幅と高さの概念が理解できていたからではありません。flex-grow や flex-shrink も知っていましたが、それらさえも知っていましたが、私が思っていたことばくすべてのサイズに関する知識が突然適用されなくなったのです。通常のレイアウトでは、物事は予測可能です。ブロック要素は幅いっぱいに伸び、高さはコンテンツのサイズに依存します。幅と高さもまた、固執された指示のように振る舞います。そして、Flexbox が登場し、その法則を歪ませ始めます。私は一度、フレックスアイテムに width を設定し、通常のように振る舞うのを期待しました。それはしませんでした。次に height を調整しようとしましたが、依然として奇妙なものでした。align-items を試みると、予想外の伸び方をするものが増え始めました。その時点で、私はこう感じました。「Flexbox は幅と高さを尊重しないのか」。しかしそれは真実ではありません。Flexbox はそれらを尊重しますが、それらが慣れ親しんでいるような方法ではではありません。Flexbox は、水平軸、垂直軸、メイン軸、クロス軸という概念を思い通りに考えません。それを理解してから、すべてが理にかなうようになりました。まずは単純なことから始めましょう。その div は依然としてブロック要素です。それはその親の幅いっぱいに伸びます。その高さはコンテンツのサイズに依存します。display: flex は、コンテナの振る舞いそのものをどのように変化するかとは無関係です。これが私の最初の誤解でした。Flexbox は新しい概念を導入しました。軸です。flex-direction メイン軸 クロス軸行:水平列:垂直この一つの特性が、すべてを制御します。私は本当にすべてを言っています。軸が定義された後、サイズ調整は、数つの特性間のゲームになります。flex-basis: 初期サイズ(メイン軸)flex-grow: アイテムの拡大方法 flex-shrink: アイテムの縮小方法 align-items: クロス軸の振る舞い width / height: 物理的な寸法(時には無視される!).container { display: flex; flex-direction: row; } メイン軸:水平 クロス軸:垂直 幅には何が起こるのか?これは今メイン軸です。だから Flexbox は、あなたは幅を持っているのかどうかを問います。はい、それを使用点として使用してください。もしなければ、flex-basis があるのか?はい、それを使用してください。他に:コンテンツのサイズを使用してください。次に:余分なスペースがあるか?flex-grow で分配されます。スペースが足りないか?flex-shrink で減少させます。Flexbox はこれを 다르게 handles: もし高さ設定されているなら、それを使います。もしなければ、align-items: stretch(デフォルト)でコンテナの高さを満たします。他に、コンテンツの高さを使用します。.container { display: flex; flex-direction: column; } 今:メイン軸は垂直、クロス軸は水平です。すべてが逆転しました。今 Flexbox は、それまでの幅の扱い方を高さに適用します。高いのかどうかを問います。なければ flex-basis があるのか?なければコンテンツサイズを使用してください。次に:垂直の余分なスペースがあるか?flex-grow 不十分であれば、flex-shrink で減少させます。幅はクロス軸になりました。今、幅は以前の高さの扱い方を備えます。幅設定されているなら、それを使用します。なければ、align-items: stretch でコンテナ幅を満たします。他にコンテンツ幅を使用します。それだけを戻って行き、いくつもの回を往復した後、私は気がついたのです。Flexbox は特性をランダムに無視するのではなく、軸に基づいて優先順位を付与するということです。メイン軸は flex プロパティによって制御され、クロス軸は align-items で制御されます。そして、幅と高さは、計算されている軸と一致するときにのみ意味を持つだけです。私は以前こう思いました。「flex-basis は最小サイズです」。それは間違いです。実際には「成長や縮小前の初期サイズ」です。この小さな誤解は、私に多くの混乱をもたらしました。何かおかしいと感じるたびに、私はこう問いかけます。メイン軸は何ですか?初期サイズは何ですか(flex-basis/幅/高さ)?余分なスペースやオーバーフローがあるか?クロス軸を制御しているものは何ですか(align-items)?そして、その時点でそれはもう魔法ではありません。Flexbox は confusing 感じるのは、固定された寸法で考えるのを止め、関係性で考えるようあなたを求めからです。アイテムと軸の間、利用可能なスペースと制約の間。あなたはそれらの間の心理的なスイッチを入れます。Flexbox はあなたが戦い続けるのを止め、あなたと共に働き始めるようになります。

Original Content

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. I remember setting width on a flex item and expecting it to behave normally.It didn’t.Then I tried adjusting height. Still weird. 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. Flexbox doesn’t think in horizontal vs vertical main axis vs cross axis And once I accepted that, things started making sense. Let’s start simple. That div is still a block element. It still fills its parent’s width Its height still depends on content display: flex does not change how the container behaves outside This was my first misconception. 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. 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!) .container { display: flex; flex-direction: row; } Main axis: horizontal Cross axis: vertical What Happens to Width? This is the main axis now. So Flexbox asks: Do you have a width? Yes use it as a starting point If not, do you have flex-basis? Yes, use that Otherwise: use content size Then: Extra space? flex-grow distributes it Not enough space? flex-shrink reduces it Flexbox handles it differently: If height is set, it uses it If not: align-items: stretch (default), fills container height Otherwise, content height .container { display: flex; flex-direction: column; } Now: Main axis, vertical Cross axis, horizontal Everything flips. Now Flexbox treats height the way it treated width before. It asks: Do you have a height? Else, do you have flex-basis? 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 After going back and forth enough times, I realized: Flexbox doesn’t randomly ignore properties prioritizes based on the axis 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 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. Whenever something looks off, I ask: What is the main axis? What is the starting size (flex-basis / width / height)? Is there extra space or overflow? What is controlling the cross axis (align-items)? And suddenly, it’s no longer magic. 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.