惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
K
Kaspersky official blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog
aimingoo的专栏
aimingoo的专栏
M
MIT News - Artificial intelligence
小众软件
小众软件
云风的 BLOG
云风的 BLOG
腾讯CDC
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Hugging Face - Blog
Hugging Face - Blog
S
SegmentFault 最新的问题
Stack Overflow Blog
Stack Overflow Blog
量子位
S
Secure Thoughts
G
GRAHAM CLULEY
C
CXSECURITY Database RSS Feed - CXSecurity.com
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
T
Threat Research - Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
G
Google Developers Blog
爱范儿
爱范儿
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
有赞技术团队
有赞技术团队
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Martin Fowler
Martin Fowler
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
C
Cisco Blogs
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 聂微东
宝玉的分享
宝玉的分享
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
Forbes - Security
Forbes - Security
Engineering at Meta
Engineering at Meta
S
Security Affairs
Help Net Security
Help Net Security
博客园 - 三生石上(FineUI控件)
AWS News Blog
AWS News Blog
博客园 - 叶小钗
Recent Commits to openclaw:main
Recent Commits to openclaw:main
V2EX - 技术
V2EX - 技术
Hacker News: Ask HN
Hacker News: Ask HN
Project Zero
Project Zero
H
Heimdal Security Blog
W
WeLiveSecurity
C
Check Point Blog

Ahmad Shadeed

Better fluid sizing with round() Use Cases for Field Sizing The Basics of Anchor Positioning Item Flow CSS Relative Colors Balancing Text In CSS Should masonry be part of CSS grid? CSS display contents CSS Grid Areas CSS Cap Unit An Interactive Guide to CSS Container Queries CSS :has() Interactive Guide CSS Nesting UX in DevTools CSS Nesting Future CSS: State Container Queries Rebuilding a comment component with modern CSS Conditional CSS with :has and :nth-last-child CSS Text balancing with text-wrap:balance CSS Masking Do we need CSS flex-wrap detection? My CSS Wishlist Conditional CSS CSS Style Queries Inside the mind of a frontend developer: Article layout Inside the mind of a frontend developer: Hero section CSS container queries are finally here The CSS behind Figma First Look At The CSS object-view-box Property Learn CSS Subgrid CSS :has Parent Selector Aligning Content In Different Wrappers Flexbox Dynamic Line Separator Hello, CSS Cascade Layers Building UI Components With SVG and CSS A Deep CSS Dive Into Radial And Conic Gradients Defensive CSS Building Real-life Components: Facebook Messenger Conditional Border Radius In CSS CSS Container Query Units Less Absolute Positioning With Modern CSS Aligning a Button Label Vertically Comparing Design Mockups To Code Result Using HSL Colors In CSS Custom Scrollbars In CSS Let CSS Container Queries For Designers The State of CSS Cross-Browser Development Overflow Issues In CSS Inspect Element As A Way To Increase Your Curiosity Handling Text Over Images in CSS Digging Into CSS Logical Properties Clipping Scrollable Areas On The inline-start Side Understanding Clip Path in CSS The Art of Building Real-life Components Handling Short And Long Content In CSS CSS Scroll Snap A Deep Dive Into CSS Grid minmax() CSS Variables 101 Finding The Root Cause of a CSS Bug Learn CSS centering How to detect browser support for Flexbox Gap CSS Mistakes While On Autopilot Digging Into the Flex Property Understanding CSS Multiple Backgrounds Aligning Logo Images in CSS Grid for layout, Flexbox for components Colors in CSS Thinking About The In-between Design Cases min(), max(), and clamp() CSS Functions Image Techniques On The Web Everything About Auto in CSS Learn Box Alignment Let Learn CSS Positioning Intrinsic Sizing In CSS CSS Grid Template Areas In Action Hiding Elements On The Web Creating a Variable Color Font From Scratch Building a Football Ticket With CSS and SVG Blending Modes in CSS CSS Variables With Inline Styles Implementing Dark Mode For My Website Rebuilding Apple Music Header in HTML & CSS Accessible Checkbox Layout Flickering On Browser Resize Enhancing The Clickable Area Size Custom Underlines with SVG Part 3: The Process of Implementing A UI Design From Scratch Part 2: The Process of Implementing A UI Design From Scratch Building An Old Nav Design I Used CSS Inline Flex For The First Time The Process of Implementing A UI Design From Scratch Common CSS Issues For Front-End Projects Handling Long and Unexpected Content in CSS How to Build Web Form Layouts With CSS Grid Grid Layout Ah-ha Moment Enhancing Our Components with CSS :empty Building Resizeable Components with Relative CSS Units CSS Writing Mode The Journey of Learning Front End Web Development on a Daily Basis
CSS Flexbox: 5 Real World Use Cases
Ahmad Shadeed · 2019-03-05 · via Ahmad Shadeed

The Layout Maestro

I spent years teaching CSS layout on this blog. I put everything I know into The Layout Maestro course: 70+ lessons and 150+ interactive examples that teach you how to think CSS layouts, not just memorize syntax.

Get the course

Flexbox is one of the best additions to CSS. I found it very useful to code elements and making them adapt easily to different screen sizes.

I made a design to show how many components could be built with Flexbox. This article assumes that you have a basic understanding of Flexbox. If you would like to learn more about it, then I recommend reading this guide on CSS Tricks.

Here is the design:

It looks like a normal header, right? It has two sections: Logo, Navigation and User.

The navigation is pushed to the far right. Let’s see how to make it like so.

<header class="c-header">
  <h1 class="c-logo"><a href="#">iShadeed Store</a></h1>
  <div class="c-header__nav">
    <nav>
      <ul>
        <!-- Nav Items -->
      </ul>
    </nav>
    <a href="#" class="c-user">
      <span>Ahmad Shadeed</span>
      <img class="c-avatar" src="shadeed.jpg" alt="" />
    </a>
  </div>
</header>
.c-header {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.c-header__nav {
  margin-left: auto; /* Where the magic happens */
}

In Flexbox, when using an auto margin on the child element, it will make that element occupy the available space in its parent. In our case, the nav and user were pushed to the far right.

See the Pen Practical Flexbox: Header by Ahmad Shadeed (@shadeed) on CodePen.

2. Sidebar and Main

Generally, the sidebar has a fixed width, and the main element next to it has fluid width. Flexbox can help in achieving that easily.

<div class="wrapper">
  <aside>...</aside>
  <main>...</main>
</div>
.wrapper {
  display: flex;
  flex-wrap: wrap;
}

aside {
  flex-basis: 250px;
}

main {
  flex-grow: 1;
}

For this use case, CSS Grid is more suitable than Flexbox. However, it’s fine to use Flexbox as a base and then enhance with Grid when it’s supported.

@supports (display: grid) {
  .wrapper {
    display: grid;
    grid-template-columns: 250px 1fr;
    grid-gap: 24px;
  }
}

See the Pen Practical Flexbox: Sidebar and Main by Ahmad Shadeed (@shadeed) on CodePen.

3. Top Sellers Grid

For this part of the design, there are two uses for Flexbox:

  • The user component
  • The grid itself
<ul class="top-sellers">
  <li>
    <div class="c-seller">
      <img src="shadeed.jpg" alt="" class="c-avatar" />
      <div>
        <h3>John Smith</h3>
        <p>150 orders</p>
      </div>
    </div>
  </li>
  <li>...</li>
  <li>...</li>
  <li>...</li>
  <li>...</li>
</ul>
.top-sellers {
  display: flex;
  flex-wrap: wrap;
}

.top-sellers > * {
  flex: 0 0 25%;
}

A couple of things that I want to focus on:

  • I used a <ul> element for the grid wrapper
  • The grid styling is applied to the <li> element. The component .c-seller is living on its own.
  • .top-sellers > *: Select all the direct child elements of .top-sellers. In our case, this will select all <li> elements and give each a width of 25%.

It’s important to apply the above when there is enough space. Here is the complete CSS.

@media (min-width: 400px) {
  .top-sellers {
    display: flex;
    flex-wrap: wrap;
    margin-bottom: -1rem;
  }

  .top-sellers > * {
    flex: 0 0 50%;
  }
}

@media (min-width: 600px) {
  .top-sellers > * {
    flex: 0 0 33%;
  }
}

@media (min-width: 900px) {
  .top-sellers > * {
    flex: 0 0 25%;
  }
}

In addition to that, it’s better to use CSS Grid. So just like the sidebar and main example, I will use CSS @supports to enhance when Grid is supported.

I also used Flexbox to align the seller name and orders total vertically with the user avatar.

See the Pen Practical Flexbox: Top Sellers Grid by Ahmad Shadeed (@shadeed) on CodePen.

4. Article Cards

The two card components in the design are almost similar, but one is horizontal, and the other is vertical (Elements are stacked).

It’s handy to use Flexbox to accomplish both cases without duplicating the code. Let’s see how.

<ul class="articles-wrapper">
  <li>
    <article class="c-article">
      <a href="#">
        <img src="image.jpg" alt="" />
        <div>
          <h3>How to design in a logo in 5 steps</h3>
          <p>By Ahmad Shadeed</p>
        </div>
      </a>
    </article>
  </li>
  <li>...</li>
  <li>...</li>
  <li>...</li>
</ul>
.articles-wrapper {
  display: flex;
  flex-wrap: wrap;
}

.articles-wrapper > * {
  flex: 0 0 50%;
}

Note that each child item has a width of 50%. How to add spacing between the grid items? It’s possible to use margins, but this will make it harder. See the example below:

.articles-wrapper > * {
  flex: 0 0 calc(50% - 16px);
  margin-right: 16px;
}

A better approach is to add padding-right to each child item, and then we will add the same amount as a negative margin to the wrapper.

.articles-wrapper {
  display: flex;
  flex-wrap: wrap;
  margin-right: -16px;
}

.articles-wrapper > * {
  flex: 0 0 50%;
  padding-right: 16px;
}

Working on the stacked card

<ul class="articles-wrapper">
  <li>
    <article class="c-article stacked">....</article>
  </li>
  ...
</ul>
.c-article.stacked a {
  flex-direction: column;
  align-items: flex-start;
}

.c-article.stacked img {
  width: 100%;
  height: 150px;
  margin-bottom: 0.5rem;
  margin-right: 0; /* Resetting the margin to zero since the normal card has a margin-right */
}

See the Pen Practical Flexbox: Articles Cards by Ahmad Shadeed (@shadeed) on CodePen.

5. Search Form

Flexbox is the perfect solution for this design. The input needs to occupy the remaining space, while the button should have a fixed width.

<form action="">
  <input
    type="text"
    name=""
    id=""
    placeholder="What are you looking for?"
  />
  <button>Search</button>
</form>
form {
  display: flex;
  flex-wrap: wrap;
}

input {
  flex-grow: 1;
}

Note that the input and button are equal in height. By default, a Flex container will stretch the child items and make them equal in height.

See the Pen Practical Flexbox: Search Form by Ahmad Shadeed (@shadeed) on CodePen.

Thank you for reading.

{% include share.html text = “Practical Use Cases For Flexbox” link = “https://ishadeed.com/article/flexbox-real-world-use-cases/” %}