Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                .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;
}

              
            
!

JS

              
                
              
            
!
999px

Console