<main>
<h2>Auto Placement of Grid Items with <code>grid-auto-flow</code></h2>
<p></p>
<div class="btns">
<label for="row">row <input type="radio" name="autoflow" value="row" id="row" checked></label>
<label for="column">column <input type="radio" name="autoflow" value="column" id="column"></label>
<label for="dense">dense <input type="radio" name="autoflow" value="dense" id="dense"></label>
<label for="rowdense">row dense <input type="radio" name="autoflow" value="rowdense" id="rowdense"></label>
<label for="columndense">column dense <input type="radio" name="autoflow" value="columndense" id="columndense"></label>
</div>
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</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: 20px;
padding: 0 20px 30px;
line-height: 1.4;
}
main {
max-width: 800px;
margin: 0 auto;
}
h2 {
text-align: center;
}
code {
font-family: Consolas, monospace;
background: #ccc;
padding: 1px 3px;
}
.btns {
text-align: center;
margin-bottom: 1em;
}
button {
margin: 0 auto 1em;
}
.container {
border: solid 2px;
background: #f8ddf0;
margin-bottom: 1em;
padding: 4px;
display: grid;
grid-template-areas: "head head ...."
"nav main main"
"nav main main"
"... foot foot";
grid-auto-flow: dense;
}
.row {
grid-auto-flow: row;
}
.column {
grid-auto-flow: column;
}
.dense {
grid-auto-flow: dense;
}
.rowdense {
grid-auto-flow: row dense;
}
.columndense {
grid-auto-flow: column dense;
}
.item:nth-child(1) {
grid-column: auto/span 2;
}
.item:nth-child(2) {
grid-column: auto/span 2;
}
.ltr {
direction: ltr;
}
.rtl {
direction: rtl;
}
.dir {
text-align: center;
}
label {
margin-right: 1.3em;
padding: 8px;
}
.item {
background: #c565a8;
border: solid 2px #305077;
text-align: center;
font-size: 2em;
margin: 4px;
color: white;
font-weight: bold;
}
let btns = document.querySelector('.btns').querySelectorAll('input'),
dirs = document.querySelector('.dir').querySelectorAll('input'),
container = document.querySelector('.container');
for (i of btns) {
(function(i) {
i.addEventListener('click', function() {
clearClasses(container, btns);
container.classList.add(i.value);
}, false);
})(i);
}
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.