Home >Web Front-end >CSS Tutorial >CSS - display: flex vs inline-flex

CSS - display: flex vs inline-flex

DDD
DDDOriginal
2024-11-30 15:12:10629browse

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>



<p>CSS<br>
</p>

<pre class="brush:php;toolbar:false">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;
}

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.

CSS - display: flex vs inline-flex

The above is the detailed content of CSS - display: flex vs inline-flex. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn