DEV Community

Jen C.
Jen C.

Posted on

CSS - display: flex vs inline-flex

inline-flex

A child container with display: inline-flex does not automatically fill the parent container. Its size depends on its content and any additional styles applied to it.

flex

A child container with display: flex automatically fills the parent container's width because flex behaves like a block-level element, which expands to fit the parent's available width by default.

Example

HTML

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="src/style.css" />
  </head>
  <body>
    <h1>inline-flex</h1>
    <div class="container">
      <div class="inline-flex-c">
        <div class="child">child 1</div>
        <div class="child">child 2</div>
      </div>
    </div>

    <h1>flex</h1>
    <div class="container">
      <div class="flex-c">
        <div class="child">child 1</div>
        <div class="child">child 2</div>
      </div>
    </div>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

CSS

body {
  background: transparent;
  color: #fcbe24;
  padding: 0 24px;
  margin: 0;
  height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
    Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

.container {
  background-color: white;
  color: black;
}

.inline-flex-c {
  display: inline-flex;
  background-color: palevioletred;
 }

.flex-c {
  display: flex;
  background-color: chocolate;
}

.child{
  border-color: greenyellow;
  border-style: solid;
}
Enter fullscreen mode Exit fullscreen mode

Result

The flex container stretches to occupy the full width of its parent container. In contrast, the inline-flex container only occupies the width required by its content.

Image description

Billboard image

Monitoring as code

With Checkly, you can use Playwright tests and Javascript to monitor end-to-end scenarios in your NextJS, Astro, Remix, or other application.

Get started now!

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay