<main>
<h2>Defining Relative Growth of Flex Items: <br><code>flex-shrink</code></h2>
<div class="btns">
<p>All 3 flex items below have their <code>flex-grow</code> property set to <code>1</code> and the <code>flex-basis</code> property is set at <code>500px</code>. The middle flex item has a <code>flex-shrink</code> value of <code>0</code>.</p>
<label for="flexshrink">Increase the <code>flex-shrink</code> value for flex item #2:<br><input type="number" value="0" min="0" max="3" step="0.05" id="flexshrink"></label>
</div>
<div class="container">
<div class="item">1</div>
<div class="item extra">2</div>
<div class="item">3</div>
</div>
<div class="dir">
<label for="ltr">left-to-right <input type="radio" value="ltr" name="direction" id="ltr" checked></label>
<label for="rtl">right-to-left <input type="radio" value="rtl" name="direction" id="rtl"></label>
</div>
</main>
body {
font-family: Arial, sans-serif;
font-size: 1.3em;
padding: 0 2em 3em;
line-height: 1.4;
}
main {
max-width: 800px;
margin: 0 auto;
}
h2 {
margin-top: 14px;
text-align: center;
}
code {
font-family: Consolas, monospace;
background: #ccc;
padding: 1px 3px;
}
.btns {
text-align: center;
}
.btns input {
width: 75px;
display: block;
margin: 12px auto 0;
}
.dir {
text-align: center;
}
label {
margin-right: 1.3em;
padding: 8px;
}
.container {
min-height: 200px;
border: solid 2px;
margin-bottom: 1em;
background: #b2d5ff;
display: flex;
}
.ltr {
direction: ltr;
}
.rtl {
direction: rtl;
}
.item {
background: #007ed3;
border: solid 2px #305077;
margin: 5px;
text-align: center;
font-size: 3em;
color: white;
font-weight: bold;
line-height: 2.4;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 500px;
}
.extra {
flex-basis: 500px;
flex-grow: 1;
flex-shrink: 0;
}
let input = document.querySelector('.btns').querySelector('input'),
dirs = document.querySelector('.dir').querySelectorAll('input'),
container = document.querySelector('.container'),
flexitem = document.querySelector('.extra');
input.addEventListener('input', function() {
flexitem.style.flexShrink = this.value;
}, false);
for (i of dirs) {
(function(i) {
i.addEventListener('click', function() {
clearClasses(container, dirs);
container.classList.add(i.value);
}, false);
})(i);
}
function clearClasses (el, cl) {
for (i of cl) {
el.classList.remove(i.value);
}
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.