LoginSignup
0
0

gridレイアウトの中にflexレイアウトの要素の幅が取れない

Posted at

背景

カード要素をgridレイアウトで並べる。
カードのタイトルが幅よりも長くなったら省略表示を追加する。

カードイメージ.png

↑ これをやろうとしたら、長いタイトルでカードが横に伸びてしまった。

原因

子要素の幅が親要素お幅になっていない。
カードのレイアウトはgridレイアウト、タイトル部分の「タグ」「タイトル」「moreボタン」のレイアウトをflexにしていた。

.titleContainer {
  display: flex;
  flex: 1 1 calc(100% - 32px);

  .titleLabel {
    display: flex;
    gap: 4px;
    align-items: center;
    width: calc(100% - 32px);

    .titleLabelText {
      flex: 0 auto;
      max-width: calc(100% - 60px);
      overflow: hidden;
      font: Roboto;
      color: white;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
   }
  }

解決策

タイトル部分ををgridレイアウトに変える。

  .roomLabelContainer {
    display: grid;
    grid-template-columns: auto 32px;
    gap: 4px;
    align-items: center;

    .roomLabel {
      display: grid;
      grid-template-columns: 60px auto;
      gap: 4px;
      align-items: center;
      width: 100%;

      .roomLabelText {
        max-width: 100%;
        overflow: hidden;
        font: Roboto;
        color: white;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }

最後に

原因に気づくまで沼った。
gridレイアウトの子要素はgridレイアウトにしておくと良さそう。
Gapがやりやすいし、幅も高さもいい感じになってくれる。

0
0
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
0
0