<div class="container">
<h1>Full-Bleed Layout Using CSS Grid</h1>
<p>Oftentimes, we want to limit the width of an element relative to its parent, and at the same time, to make it dynamic. So having a base width or height with the ability to make it extend based on the available space.</p>
<div class="full">
<img src="https://picsum.photos/1920/300?random" alt="" width="1920" height="300">
</div>
<p>Oftentimes, we want to limit the width of an element relative to its parent, and at the same time, to make it dynamic. So having a base width or height with the ability to make it extend based on the available space.</p>
</div>
<div class="form">
<label for="gap">gap:</label>
<input type="range" id="gap" name="gap" min="0" max="50" value="20" steps="1" />
<output id="gap-output">20px</output>
<label for="min-width">min-width:</label>
<input type="range" id="min-width" name="min-width" min="45" max="75" value="60" steps="1" />
<output id="min-width-output">60ch</output>
</div>
@import url("https://fonts.googleapis.com/css2?family=Exo:wght@600&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
width: 100vw;
min-height: 100vh;
font-family: "Exo", Arial, sans-serif;
line-height: 1.5;
background-color: #557;
display: grid;
grid-template-rows: 1fr auto;
color: #fff;
align-items: start;
font-size: clamp(1rem, 1.2rem + 1vw, 1.25rem);
}
.form {
display: flex;
justify-content: center;
align-items: center;
flex-wrap: wrap;
gap: 10px;
background-color: rgb(0 0 0 / 0.5);
padding: 10px;
}
:root {
--gap: 20px;
--minWidth: 60ch;
}
.container {
display: grid;
gap: 20px;
grid-template-columns: 1fr min(var(--minWidth), calc(100% - var(--gap) * 2)) 1fr;
}
.container > * {
grid-column: 2;
}
.container > .full {
grid-column: 1 / -1;
}
.full img {
display: block;
max-width: 100%;
height: auto;
object-fit: cover;
object-position: center;
}
const rootElement = document.documentElement;
const gapInput = document.getElementById("gap");
const gapOutput = document.getElementById("gap-output");
const minWidthInput = document.getElementById("min-width");
const minWidthOutput = document.getElementById("min-width-output");
gapInput.addEventListener("input", (etv) => {
rootElement.style.setProperty("--gap", `${etv.target.value}px`);
gapOutput.textContent = `${etv.target.value}px`;
});
minWidthInput.addEventListener("input", (etv) => {
rootElement.style.setProperty("--minWidth", `${etv.target.value}ch`);
minWidthOutput.textContent = `${etv.target.value}ch`;
});
View Compiled
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.