css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <h1>How to use flex to position two adjacent elements</h1>
<subtitle>One child with a minimum width, the other to fill the remaining space</subtitle>

<div class="parent">
  <div class="child--dynamic">85%</div>
  <div class="child--min">15%</div>
</div>

<div class="parent">
  <div class="child--dynamic">Subtotal</div>
  <div class="child--min">£1.23</div>
</div>

<div class="parent">
  <div class="child--dynamic">Subtotal</div>
  <div class="child--min">£1,234.34</div>
</div>

<h4>Problem:</h4>
<ul>
  <li>Two adjacent children within a parent container.</li>
  <li>One child has a fixed minimum width, but can expand if the contents overflow this <code>min-width</code> value.</li>
  <li>The second child fills the remaining space. This child shrinks in width if its sibling expands.</li>
</ul>

<p>The examples above show the minimum-width child on the right, and the dynamic width child on the left. The minimum-width of the child is 15%.</p>

<h4>Solution:</h4>

<ul>
  <li>Apply <code>display: flex</code> to the parent.</li>
  <li>Give the dynamically-sized child (the one that should shrink if necessary) <code>flex: 1</code>.</li>
  <li>Don't apply <code>flex</code> to the min-width child. It automatically sits adjacent to its sibling because its parent has <code>display: flex</code>. It will use the min-width value or resize to fit its contents if the contents overflow.</li>
</ul>

<p>Try experimenting with the contents of the fixed width child and see what happens to the width of the sibling.</p>

<p>Note that it's important to consider the potential contents of the dynamic child. How will it display if it contains a lot of contents? Or if the min-width sibling expands a lot? See <a href="https://codepen.io/claireparker/pen/MrWOwO">my other codepen</a>  for how to use an ellipsis to truncate long content.</p>
            
          
!
            
              .parent {
  display: flex; /* This is all you need on the parent! The rest is for decoration. */
  width: 400px;
  border: 3px solid #EC407A;
  text-align: right;
  font-size: 18px;
  margin: 20px;
  padding: 3px;
}

.child--dynamic {
  flex: 1;
  border: 3px solid #4CAF50;
  padding: 3px;
}

.child--min {
  min-width: 15%;
  border: 3px solid purple;
  padding: 3px;
}

/* Codepen decoration */
body {
  background-color: #f6f6f6;
  color: #111;
}

code {
  font-family: monospace;
  background-color: #E0E0E0;
  padding: 0 5px;
}

li {
  line-height: 22px;
}

* {
  box-sizing: border-box;
  font-family: sans-serif;
}

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console