<main>
<h2>Defining the Basis for Growing<br>and Shrinking Flex Items: <code>flex-basis</code></h2>
<div class="btns">
<label for="flexbasis">Increase the <code>flex-basis</code> value (in px) for flex item #1:<br><input type="number" value="0" min="0" max="700" step="50" id="flexbasis"></label>
</div>
<div class="container">
<div class="item extra">1</div>
<div class="item">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: auto;
}
.extra {
flex-basis: content;
}
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.flexBasis = this.value + 'px';
}, 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.