JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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;
}
Also see: Tab Triggers