9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【CSS】要素がはみ出る!そんな時はまずmin-widthを0にする

Last updated at Posted at 2025-11-10

image.png

子要素親要素をはみ出してしまうことがある。
そんなときは子要素側に以下のCSSを順番に適用するとよい

  1. min-w-0
  2. wrap-break-word
  3. overflow 系 (ex. overflow-hidden, overflow-scroll)

phase1: flex/gridの子要素を親要素内に収める

image.png

青色要素にflexやgridを使っているとき
赤色要素min-width: 0; が適用されると
コンテンツ幅親要素の横幅内に収まる

なぜ min-width の指定で解決するのか:

  • min-width のデフォルト値は auto である
  • 通常要素における min-width: auto; は実質的に 0 扱いとなる
  • ただしflexやgridの子要素では「コンテンツサイズに基づく最小幅」と解釈される
    • つまりテキスト量が多いと子要素が親要素からはみ出してしまう
      (横幅や折り返しの設定を明示的に指定していない場合)
    • 詳細: CSS Flexible Box Layout Module Level 1

phase2: テキストをコンテンツサイズ内に収める

image.png

要件に応じてテキストの表現処理を変える

  • (画像左側) wrap-break-word赤色要素に使うとテキストの折り返しが有効になる
  • (画像中央) overflow-scroll赤色要素に使うとテキストがスクロールバーで隠れる
  • (画像右側) truncateテキスト要素に使うと三点リーダで省略される

truncate を使うなら対象要素がブロックボックスでなければならない

付録

flexやgridなどの違いによって子要素の描画には微妙な違いが生じる
CodePenにてその一部を紹介する

Flexbox

See the Pen OverflowGuard - flex by hrel11 (@hrel11) on CodePen.

Grid Layout

See the Pen OverflowGuard - grid by hrel11 (@hrel11) on CodePen.

Block

See the Pen flex/grid by hrel11 (@hrel11) on CodePen.

参考

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?