Css flex align items center height 100

How to Center Anything in CSS Using Flexbox and Grid ✨

Joy Shaheb

Joy Shaheb

How to Center Anything in CSS Using Flexbox and Grid ✨

Today I’m gonna show you how you can center and align your content with CSS. Along the way, we’ll look at various alignment techniques. So, let’s get started! 🥇

Читайте также:  Stderr to file php

Table of Contents ->

Frame-73

  • How to Use Flexbox to
    • center anything horizontally
    • center anything vertically
    • center both horizontally & Vertically
  • How to Use Grid to
    • center anything horizontally
    • center anything vertically
    • center both horizontally & Vertically
  • The Transform & position property
  • The Margin Property
  • Additional resources
  • Conclusion

You can watch this tutorial on YouTube as well if you like:

Frame-35--3-

First of all, let’s understand:

What is the Main Axis in CSS?

The line from left to right is the Main-Axis.

Frame-71

What is the Cross Axis in CSS?

The line from top to bottom is the Cross-Axis.

Frame-72

Project Setup

Frame-54

To experiment with all the properties and values, write the following code in your code editor.

HTML

Write this code inside the body tag:

CSS

Clear the default browser styles so that we can work more accurately:

Select the .container class and set it to 100vh. Otherwise, we can’t see our result on the Vertical Axis:

Style the .box-1 class like this:

We’re all set, now let’s start coding!

Frame-3--5-

How to use Flexbox to center anything

Thumbnail-hashnode

We can use Flexbox to align our content div both along the X and Y Axis. To do that, we need to write the display: flex; property inside the .container class:

We’ll experiment with these 2 properties:

How to center anything horizontally using Flexbox

We can use the justify-content property to do this using these values 👇

Justify-content-1

To experiment with the values, write the following code👇

The result will look like this👇

Frame-6--2-

How to center anything vertically using Flexbox

We can use the align-items property to do this using these values 👇

align-items-1

To experiment with the values, write the following code👇

The result looks like this👇

Frame-7--4-

How to center a div horizontally & vertically using Flexbox

Here, we can combine both the justify-content and align-items properties to align a div both horizontally and vertically.

Write the following codes👇

The result looks like this 👇

Frame-8--1-

You can check out this cheatsheet to learn more about various Flexbox properties.

How to use CSS Grid to center anything

Frame-70

We can use grid to align our content div both along the X and Y Axis. To do that, we need to write the display: grid; property inside the .container class:

We’ll experiment with these 2 properties:

Alternatively, you can use these 2 properties:

How to center anything horizontally using CSS Grid

We can use the justify-content property to do this using these values 👇

justify-content-1--1-

The result looks like this👇

Frame-6--2--1

How to center anything vertically using CSS Grid

We can use the align-content property to do this using these values 👇

align-content-1

The result will look like this👇

Frame-7--4--1

How to center a div horizontally & vertically using CSS Grid

Here, we can combine both the justify-content and align-content properties to align a div both horizontally and vertically.

The result looks like this👇

Frame-8--1--1

Alternative way

You can also use the justify-items and align-items properties to duplicate the same results:

The place-content Property in CSS Grid

This is the shorthand of 2 properties of CSS Grid->

Frame-8--1--2

Check out this cheatsheet to find out the difference between various Grid properties.

Take a break!

So far so good – take a break.

Frame-67--1-

How to use the CSS Position Property to center anything

Frame-12-1

This is a combination of these properties ->

First. Understand the center point of a div

By default, this is the center point of a div 👇

Frame-9

That’s why we see this odd behavior 👇

Frame-8--2-

Notice that the box is not at the exact center in the image above. 👆

Frame-10--2-

Frame-11--1-

What is the Translate property in CSS?

Translate is the shorthand of 3 properties ->

How to center a div horizontally using CSS Position property

We’re gonna use the left property inside the `.box- ` class. Follow along 👇

Frame-6--2--2

How to center a div vertically using CSS Position property

We’re gonna use the top property inside the `box- ` class. Follow along 👇

Frame-7--4--2

How to center a div horizontally & vertically using CSS position property

To achieve this result, we’re gonna combine these properties together ->

Frame-8--1--3

How to use the margin Property to center anything

Frame-73--2-

The margin property is the shorthand of 4 properties

Write this code to set it up 👇

How to center a div horizontally using CSS margin property

We’re gonna use the margin property inside the .box-1 class. Write the following code 👇

The result looks like this👇

Frame-6--2--3

How to center a div vertically using CSS margin property

We’re gonna use the margin property inside the .box-1 class. Write the following code 👇

The result looks like this 👇

Frame-7--4--3

How to center a div horizontally & vertically using CSS margin property

We’re gonna use the margin property inside the `.box- ` class. Write the following code 👇

The result looks like this👇

Frame-8--1--4

Additional Resources

Credits

Conclusion

Now, you can confidently align or center your content using any of these four methods in CSS.

Here’s your medal for reading until the end ❤️

Suggestions & Criticisms Are Highly Appreciated ❤️

Alt Text

Источник

Оцените статью