Tips & Tricks

Flexbox with Claude Code

Learn about flexbox using Claude Code. Practical tips and code examples included.

Flexboxの頻出パターンをClaude Codeで一気に実装

Flexboxは1次元レイアウトの定番技術です。Claude Codeに実装を依頼するとき、よく使うパターンを知っておくと指示が具体的になり、より正確な結果を得られます。

ナビゲーションバー

> Flexboxでレスポンシブなナビゲーションバーを作成して。
> ロゴ左寄せ、メニュー右寄せ、モバイルでハンバーガーメニュー。
/* ヘッダーナビゲーション */
.navbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.5rem;
  height: 60px;
}

.navbar__logo {
  flex-shrink: 0;
}

.navbar__menu {
  display: flex;
  gap: 1.5rem;
  list-style: none;
}

.navbar__actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-left: auto;
}

センタリングパターン

/* 完全中央配置 */
.center-perfect {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

/* テキストとアイコンの垂直中央揃え */
.inline-center {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* 複数行テキストの中央配置 */
.text-center-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
}

カードレイアウト

/* 等幅カード */
.card-row {
  display: flex;
  gap: 1.5rem;
}

.card-row .card {
  flex: 1;
  min-width: 0; /* テキスト溢れ防止 */
}

/* フッター固定カード */
.card {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.card__body {
  flex: 1; /* 残りのスペースを占有 */
}

.card__footer {
  margin-top: auto; /* 常に下に配置 */
}

Holy Grailレイアウト

/* 伝統的な3カラムレイアウト */
.holy-grail {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.holy-grail__body {
  display: flex;
  flex: 1;
}

.holy-grail__main {
  flex: 1;
  min-width: 0;
  padding: 1.5rem;
}

.holy-grail__sidebar {
  flex: 0 0 250px;
  padding: 1.5rem;
}

.holy-grail__sidebar--left {
  order: -1;
}
/* フッターを常にページ下部に */
.page-wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.page-content {
  flex: 1;
}

/* footer は自動的に下部に */

タグ・チップのラップ配置

/* タグ一覧 */
.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tag {
  flex-shrink: 0;
  padding: 0.25rem 0.75rem;
  border-radius: 9999px;
  font-size: 0.875rem;
  background: #e5e7eb;
}

入力フィールドとボタンの結合

/* 検索バー */
.search-bar {
  display: flex;
}

.search-bar__input {
  flex: 1;
  border: 1px solid #d1d5db;
  border-right: none;
  border-radius: 0.5rem 0 0 0.5rem;
  padding: 0.5rem 1rem;
  min-width: 0;
}

.search-bar__button {
  flex-shrink: 0;
  border-radius: 0 0.5rem 0.5rem 0;
  padding: 0.5rem 1rem;
  background: #3b82f6;
  color: white;
}

レスポンシブ切り替え

/* デスクトップ:横並び → モバイル:縦並び */
.responsive-flex {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (width >= 768px) {
  .responsive-flex {
    flex-direction: row;
  }
}

/* 逆順表示(モバイルで画像を上に) */
@media (width < 768px) {
  .feature-section {
    flex-direction: column-reverse;
  }
}

Flexboxのデバッグ Tips

/* デバッグ用:全要素にボーダー */
.debug-flex * {
  outline: 1px solid rgba(255, 0, 0, 0.2);
}

/* 各アイテムのサイズを視覚化 */
.debug-flex > * {
  position: relative;
}

.debug-flex > *::after {
  content: attr(class) " | " attr(style);
  position: absolute;
  top: 0;
  left: 0;
  font-size: 10px;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  padding: 2px 4px;
}

Summary

FlexboxはCSS Gridと使い分けることがポイントです。1次元のレイアウトにはFlexbox、2次元のレイアウトにはCSS Gridが適しています。Claude Codeに「Flexboxで横並びにして」と指示するだけで、gap設定やmin-width: 0の考慮まで含めた実装を生成してくれます。CSS変数と組み合わせてデザイントークンを活用するのもおすすめです。Flexboxの仕様詳細はCSS Flexible Box Layout Moduleを確認してください。

#Claude Code #Flexbox #CSS #レイアウト #frontend