Centering and Aligning Items in CSS Grid - Joomlashack
Centering and Aligning Items in CSS Grid - Joomlashack
(https://www.joomlashack.com/)
Vertically centering elements on a web page has been an issue for web designers and developers when
working with CSS.
Each developer tried their own solution so you can imagine how many possibilities and workarounds refer to
this issue. CSS Grid Layout is a bidimensional layout model.
That means it operates with two axes: the block axis, also known as the y-axis and the inline axis, also known
as the x-axis.
When dealing with the block axis (vertical), you’ll be using the align property, whereas if you want to align or
center on the horizontal axis, you should use the justify property.
<div class="container">
<div class="item item1">Item 1</div>
<div class="item item2">Item 2</div>
<div class="item item3">Item 3</div>
<div class="item item4">Item 4</div>
<div class="item item5">Item 5</div>
<div class="item item6">Item 6</div>
</div>
* {
box-sizing: border-box;
}
body {
background-color: #AAA;
margin: 50px;
}
.item {
border: 5px solid #87b5ff;
border-radius: 3px;
font-size: 2em;
font-family: sans-serif;
font-weight: bold;
background-color: #1c57b5
}
.container {
display: grid;
grid-gap: 1rem;
grid-template-columns: repeat(5, 150px);
grid-template-rows: repeat(5, 100px);
}
Now, place the items on different areas of the grid with help of the grid-column and grid-row properties and
the numbered lines in the grid.
.item2 {
grid-column: 2 / 4;
}
.item3 {
grid-row: 1 / 4;
grid-column: 4 / 5;
}
.item4 {
grid-row: 2 / -2;
grid-column: 5 / -1;
}
.item5 {
grid-row: 4 / -1;
grid-column: 2 / 4;
}
.item6 {
grid-row: 3 / -1;
}
Step #4. The align-items property
This property is used at the grid container level. As already stated, the align deals with the vertical (block)
axis.
As you can see, now all items have a height value of auto . This means they are as tall as the content inside
them.
All items are vertically centered within their respective grid area (the ones you defined by placing grid items with
line numbers).
These are some of the values you can pass to the align-items property:
start
end
stretch
.container {
display: grid;
grid-gap: 1rem;
grid-template-columns: repeat(5, 150px);
grid-template-rows: repeat(5, 100px);
align-items: start;
}
Each item is now placed at the start of its corresponding grid area.
Setting the value of the align-items property to end will place the items (vertically) to the end of their
corresponding grid areas.
The value assigned to the stretch will stretch the items across the whole area.
As you can notice, the items are now as high and as wide as the content inside of them. Moreover, they are
perfectly placed in the center of their respective grid areas.
But what if you want to target just one item, in order to align and/or center it? For those cases, you should use
the align-self and justify-self properties at the item level.
Step # 6. The align-self and justify-self
properties
These properties are used at the item level.
.item2 {
grid-column: 2 / 4;
justify-self: stretch;
}
.item3 {
grid-row: 1 / 4;
grid-column: 4 / 5;
align-self: stretch;
}
.item4 {
grid-row: 2 / -2;
grid-column: 5 / -1;
align-self: end;
}
Item 2 stretches across the whole grid area along its horizontal axis (justify).
Item 3, on the other hand, stretches vertically along the block axis and item 4 has been placed vertically to the
end of its grid area.
The other items (1, 5 and 6) are placed according to the properties declared at the grid container level.
The align and justify properties in CSS will allow you to center and align items relative to their containing
grid area in a comprehensive way with less code than trying to align the items with other layout methods.
In this short book, you are going to master the key ideas behind CSS Grid. This book is in the best traditions of
OSTraining. There are no long-dense paragraphs of theory. You pick up the book and you start writing code
immediately.
In the first chapter, we start with the basic terminology. You'll learn the difference between Grid Areas and Grid
Cells, between Grid Tracks and Grid Gaps.
Then, using a hands-on approach, you'll start building CSS Grids. There are 9 different exercises in this book.
You'll build everything from the most basic CSS Grid to a full site layout.
Write a comment
Comments (7)
Leave a comment
Quote Reply
Quote Reply
Quote Reply
You could make a grid inside a grid. The item would be vertically centered inside the
inner grid. Then, you use the align-self property of the inner grid to stretch it along
the outer grid. Take a look here at Item 1: codepen.io/jorgemb76/pen/OJPBgRp
(https://codepen.io/jorgemb76/pen/OJPBgRp)
PS: IF, I'm getting your question right....
Quote Reply
Quote Reply
Quote Reply
Quote Reply
Join Joomlashack
(/everything-club/)
Get all our extensions for one low price. It's the best deal in Joomla!
mediahelpingmedia.org (http://website.informer.com/mediahelpingmedia.org#7689_7530035)
You are welcome to translate, re-use and modify any Joomlashack post.
| archiviofotografiamediterranea.it (http://website.informer.com/archiviofotografiamediterranea.it#7689_8189919)
All we ask is that you link back to our original tutorial and that you don't use them commercially.
| cmia-viana-castelo.pt (http://website.informer.com/cmia-viana-castelo.pt#7689_8221517)
Full license details (https://creativecommons.org/licenses/by-nc/3.0/).
| lacrujiaediciones.com.ar (http://website.informer.com/lacrujiaediciones.com.ar#7689_9188713)
| writing.utoronto.ca (http://website.informer.com/writing.utoronto.ca#7689_9477471)
| basenji.org (http://website.informer.com/basenji.org#7689_11136257)
| correiocidadania.com.br (http://website.informer.com/correiocidadania.com.br#7689_13606681)
| theopedproject.org (http://website.informer.com/theopedproject.org#7689_23597547)
| dosklejania.pl (http://website.informer.com/dosklejania.pl#7689_29082257)
| srna.org (http://website.informer.com/srna.org#7689_45615237)
powered by website.informer.com
(http://website.informer.com)
Joomlashack.com is not affiliated with or endorsed by the Joomla! Project or Open Source Matters.
The Joomla! name and logo is used under a limited license granted by Open Source Matters the trademark holder in the United States and other countries.