- How to Center in CSS with CSS Grid
- Web Component Essentials
- Web Component Essentials
- CSS Grid most easy center vertical and horizontal
- CSS Grid center horizontally permalink
- CSS Grid center vertically permalink
- Thank you for reading, and let’s connect! permalink
- All the Ways You Can Vertically Center Content in CSS
How to Center in CSS with CSS Grid
Using CSS Grid, we can layout content in a two-dimensional grid with columns and rows. CSS grid is a fantastic CSS API to layout content on the web. Often we need to center content within a page or container. We can accomplish this in many different ways in CSS. In a previous post, we learned how to center using Flexbox; in this post, we will learn how to center content using CSS Grid.
In our example, we have two elements: a parent element and the child element. We want to center the child element within the width of the parent element container.
section
width: 200px;
border: 1px solid #2d2d2d;
>
div
color: #fff;
background: #2d2d2d;
padding: 12px;
display: inline-block;
>
To center our item, we need to set the parent element to render as a CSS Grid.
section
width: 200px;
border: 1px solid #2d2d2d;
display: grid;
justify-content: center;
>
First, we set the section to have configured to display: grid . This CSS property sets the element to render using CSS Grid. Now each direct child element will be a grid item placed in a column. To align the item horizontally within the grid, we use the justify-content property and set it to center .
With justify-content we can align the columns start , end , stretch or center .
Centering Vertically
Now that the element is centered horizontally, we can center the element vertically. Let’s increase our parent container to be 200px high.
Similar to Flexbox, Grid columns will fill the space where they are defined. To center the item vertically we set align-content: center; .
section
display: grid;
justify-content: center;
align-content: center;
>
To center multiple items, we need to add a couple more CSS properties to the parent grid element.
section>
div>1div>
div>2div>
div>3div>
section>
section
display: grid;
justify-content: center;
align-content: center;
gap: 4px;
grid-auto-flow: column;
>
The first property gap allows us to define space between grid items. The second property grid-auto-flow , will enable us to define the direction we want items to layout, such as from left to right or vertically stacked as rows.
When using justify-content and align-content , we are aligning the columns relative to the parent Grid container. In our next example, we will see how we can align items relative to the column instead of the parent grid container.
Centering Items within a CSS Grid Column
CSS Grid is very powerful and gives us excellent grain control of our layouts. We learned how to center columns in a grid but now lets center items within a column.
For our example, we will create a grid layout with three columns and three items.
.item-center
display: grid;
grid-auto-flow: column;
gap: 4px;
align-items: center;
justify-items: center;
>
To center the items within a column, we use align-items and justify-items instead of the align-content and justify-content in our previous example. Using the item properties, we can align our items just like we did with our columns.
If we use the dev tools and inspect the grid, we can see the outlined columns and our items centered relative to the assigned column.
CSS Grid is a fantastic tool to use for layouts in our Web pages and Web Applications. Check out the full working demo below!
Web Component Essentials
Save development time, improve product consistency and ship everywhere. With this new Course and E-Book learn how to build UI components that work in any JavaScript framework such as Angular, Vue, React, and more!
Web Component Essentials
Reusable UI Components for all your Web Applications
CSS Grid most easy center vertical and horizontal
As a follow up on the CSS Flex center article, which I’ve used in about almost all me articles that include a demo, it is now time to give you a view of the same principle. In this tutorial, we’ll use CSS Grid to center horizontally and vertically.
Like Flexbox, it’s super easy to center an HTML element using CSS Grid.
To entirely center an item with CSS grid, all the CSS code we need is:
.container display: grid; place-items: center; min-height: 100vh; >
The min-height property is optional. In this case, it’s needed to give the HTML canvas a vertical height.
The above code to align an item horizontally and vertically with CSS grid will result in the following CodePen example:
CSS Grid center horizontally permalink
If for instance, you only want to center an element horizontally you can use the following CSS grid code:
.container display: grid; justify-content: center; min-height: 100vh; >
We can use the justify-content property and pass the center value to make it horizontally centered.
This code results in the following Codepen:
CSS Grid center vertically permalink
On the other hand, maybe you are looking to only center an item vertically.
In CSS grid, you can use the following code to achieve this:
.container display: grid; align-items: center; min-height: 100vh; >
We use the align-items with a value of center to get the vertical alignment on the item.
Note: This is as well the same functionality as we’ve seen in CSS Flex
See the example code in the following Codepen:
There you go! We now learned another super-easy way to center elements horizontally, vertically, or both using CSS Grid .
Thank you for reading, and let’s connect! permalink
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter
All the Ways You Can Vertically Center Content in CSS
Источник