[CSS] Choose between Grid layout and flexbox

1. Grid: by default showing content in Y axis (column), Flex: by default showing content in X axis.

Exp: If you want to style a header.. you can use flexbox, since it shows in X axis.

    <div class="navbar display-flex">
      <span class="h3">✍️ NoteTaker</span>
      <nav>
        ...
      </nav>
    </div>    

The 'span' & 'nav' are showing in a row.

 

2. If you need 'Gap' between elements, you need to use grid.

复制代码
    <div class="navbar display-flex display-flex--wrap">
      <span class="h3">✍️ NoteTaker</span>
      <nav>
        <ul class="list-unstyled display-grid display-grid--columns">
          <li>
            <a href="javascript:;" class="button button--small">Pre-Order</a>
          </li>
          <li><a href="javascript:;">About</a></li>
          <li><a href="javascript:;">Contact</a></li>
        </ul>
      </nav>
    </div>
复制代码
.display-grid {
  display: grid;
  grid-gap: 16px;

  &--columns {
    grid-auto-flow: column;
  }
}

 

3. If you need to wrap elements, you can use flexbox

复制代码
    <div class="navbar display-flex display-flex--wrap">
      <span class="h3">✍️ NoteTaker</span>
      <nav>
        <ul class="list-unstyled display-grid display-grid--columns">
          <li>
            <a href="javascript:;" class="button button--small">Pre-Order</a>
          </li>
          <li><a href="javascript:;">About</a></li>
          <li><a href="javascript:;">Contact</a></li>
        </ul>
      </nav>
    </div>
复制代码
.display-flex {
  display: flex;

  &--wrap {
    flex-wrap: wrap;
  }
}

 

4. Sometime, 'wrap' behavior can be done by using media query + grid:

width > 60rem:

 

width < 60rem:

 

So only apply 'grid-auto-flow: column' when the screen size > 60rem

复制代码
    <header class="my-lg display-grid container mx-auto alignitems--center">
      <div>
        <h1 class="mb-none">Take Notes Like Never Before</h1>
        <p class="lead mt-sm mb-none">
          Pre-order what will be the #1 note-taking app on your preferred device
          today!
        </p>
      </div>
      <div>
        <h2 class="h3">Subscribe to Launch Updates</h2>
        <form action="/" class="display-grid">
          <label for="email1">Enter your email:</label>
          <input id="email1" name="email" type="email" />
          <button class="button button--secondary" type="submit">
            Subscribe
          </button>
        </form>
      </div>
    </header>
复制代码
复制代码
// Components
.navbar {
  justify-content: space-between;
  padding: $unit * 2 $unit * 3;
}

header.display-grid {
  grid-gap: $unit * 4;

  @media (min-width: 60rem) {
    grid-auto-flow: column;
  }
}

// Display
.display-flex {
  display: flex;

  &--wrap {
    flex-wrap: wrap;
  }
}

.display-grid {
  display: grid;
  grid-gap: $unit * 2;

  &--columns {
    grid-auto-flow: column;
  }
}

.alignitems--center {
  align-items: center;
}

.container {
  max-width: 80rem;
  padding-left: $unit * 3;
  padding-right: $unit * 3;
}
复制代码

 

4. When you found yourself need to use multi media query to do reposive layout, use Grid:

.  

grid-template-columns: repeat(auto-fit, minmax(30ch, 1fr));

Once the element size is smaller than 30ch, it will move to a new row.

posted @   Zhentiw  阅读(63)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2020-03-08 [React] Avoiding state flickers
2019-03-08 [Node.js] Load balancing a Http server
2019-03-08 [Node.js] Child Process with fork() to handle heavy calculation process
2019-03-08 [Algorithm] Write your own Math.pow function in Javascript, using Recursive approach
2019-03-08 [Functional Programming] Fst & Snd, Code interview question
2019-03-08 [React GraphQL] Pass Parameters to urql's useQuery React Hook
2019-03-08 [Algorithm] Find first missing positive integer
点击右上角即可分享
微信分享提示