Dr. Greg Bernstein
Updated September 14th, 2021
grid
layoutgrid
layout terminologyGrid Garden, “Welcome to Grid Garden, where you write CSS code to grow your carrot garden!”
From CSS Tricks:
CSS Grid Layout is the most powerful layout system available in CSS. It is a 2-dimensional system, meaning it can handle both columns and rows… You work with Grid Layout by applying CSS rules both to a parent element (which becomes the Grid Container) and to that elements children (which become Grid Items).
rows, columns, gutters:
From MDN example0
<!-- html, head, and body omitted to save space -->
<body>
<h1>Simple grid example</h1>
<div class="container">
<div>One</div>
<div>Two</div>
<div>Three</div>
<div>Four</div>
<div>Five</div>
<div>Six</div>
<div>Seven</div>
</div>
</body>
From MDN
display: grid
to the container; No visible effect since the default is a single columngrid-template-columns: 200px 200px 200px;
to the container to set up three fixed sized column to hold the entries.grid-row-gap
, grid-column-gap
, and grid-gap
fr
and gap
Example.container {
display:grid;
grid-template-columns: 1fr 1fr 2fr;
grid-row-gap: 10px;
grid-column-gap:20px;
}
fr
and gap
Example RenderingFrom CSS Tricks
Grid Container: “The element on which display: grid is applied. It’s the direct parent of all the grid items.”
Grid Item: “The children (e.g. direct descendants) of the grid container.”
from CSS Tricks Grid Line
The dividing lines that make up the structure of the grid. Here the yellow line is an example of a column grid line.
From CSS Tricks
The space between two adjacent grid lines. You can think of them like the columns or rows of the grid. Here’s the grid track between the second and third row grid lines.
From CSS Tricks
The space between two adjacent row and two adjacent column grid lines. It’s a single “unit” of the grid. Here’s the grid cell between row grid lines 1 and 2, and column grid lines 2 and 3.
From CSS Tricks
The total space surrounded by four grid lines. A grid area may be comprised of any number of grid cells. Here’s the grid area between row grid lines 1 and 3, and column grid lines 1 and 3.
Best Detailed Guide is the CSS Tricks article
See their Grid Properties Table of Contents for quick access and explanation of all the properties you can set.
grid-template-columns
, grid-template-rows
, grid-template
grid-column-gap
, grid-row-gap
, grid-gap
grid-template-areas
property to define areas and give them namesjustify-items
From CSS Tricks
Aligns grid items along the inline (row) axis (as opposed to align-items which aligns along the block (column) axis). This value applies to all grid items inside the container.
Values: start
, end
, center
, stretch
justify-items
ExampleFrom CSS Tricks
Center
align-items
From CSS Tricks
Aligns grid items along the block (column) axis (as opposed to justify-items which aligns along the inline (row) axis). This value applies to all grid items inside the container.
Values: start
, end
, center
, stretch
align-items
ExampleFrom CSS Tricks
End
From CSS Tricks
Sometimes the total size of your grid might be less than the size of its grid container. This could happen if all of your grid items are sized with non-flexible units like px. In this case you can set the alignment of the grid within the grid container.
justify-content
align-content
start
, end
, center
, stretch
, space-around
, space-between
, space-evenly
You specify a grid position for an item on that item, not on the container.
grid-column-start
, grid-column-end
grid-row-start
, grid-row-end
span
indicator for items taking up multiple cells.grid-area
justify-self
align-self
Coarse Grained Page Layout
<body>
element my grid container<nav>
, <header>
, and <main>
File PageGridExample.html
<body>
<nav>
<ul>
<li><a href="PageGridExample.html">Home</a></li>
<li><a href="PageGridExample.html">About</a></li>
<li><a href="PageGridExample.html">Activities</a></li>
<li><a href="PageGridExample.html">Membership</a></li>
<li class="active"><a href="PageGridExample.html">Tides</a></li>
</ul>
</nav>
<header>
<h1>Berkeley Tides</h1>
</header>
<main>
<p>Information about tides in the Berkeley South sailing basin. To be furnished...</p>
</main>
<footer>
<p>🐳 © 2020 Grotto Networking! 🐬</p>
</footer>
</body>
For overall page layout I want the flexibility to reorder some of the main components.
Use the grid-area
property to give names to grid items for use in placing items in the grid.
Use the grid-template-areas
property on the container. The value field of this property indicates the placement of the items.
Naming grid items and setting justification and alignment properties, from PageGridExample.css
header {
grid-area: titleBlock;
justify-self: start;
background-color: #f86969;
}
footer {
grid-area: closing;
justify-self: center;
background-color: green;
}
nav {
grid-area: menu;
background-color: #f0cf72;
}
main {
grid-area: content;
background-color: white;
}
Assignment of items to grid areas, from PageGridExample.css
body {
display: grid;
grid-template-areas:
". titleBlock titleBlock"
"menu content content"
". content content"
"closing closing closing";
grid-template-columns: 0.5fr 1fr 1fr;
grid-template-rows: auto 30vh 40vh auto;
grid-column-gap: 10px;
background-color: gray;
margin: 2em;
}
Rendered Page, definitely needs work
Rendered Page with dev tools ready to revise
Layout out a form or control panel
<label>
, <input>
, <button>
, <select>
FormLayout.html Give container an id or class for easy reference
<section id="Application">
<label>Name:</label> <input id="name" type="text">
<label>email:</label><input id="email" type="text">
<label>Sail Number/Brand:</label><input id="sail" type="text">
<label>Level:</label>
<select id="level">
<option>Never Done It</option>
<option>Beginner</option>
<option>Intermediate</option>
<option>Foils to TI and Back</option>
<option>Racer</option>
</select>
<label>Comments:</label><textarea name="comments" rows="8" cols="20" id="comments"></textarea>
<button id="apply">Sign me up!</button>
</section>
From FormLayout.css
#Application {
display: grid;
grid-template-columns: 10rem 20rem; /* columns and size */
grid-row-gap: 0.5em;
grid-column-gap: 0.5em;
border: dashed black;
border-radius: 1rem;
padding: 1.0rem;
}
Align the labels close to their inputs
<labels>
Could use classes to identify different labels, but our form is fairly regular. From FormLayout.css.
#Application label {
font-family: sans-serif;
justify-self: end; /* right justify the labels */
font-style: italic;
}