DEV Community

Cover image for 3 Easy Ways to Center in CSS
Hillary Nyakundi
Hillary Nyakundi

Posted on

8 2

3 Easy Ways to Center in CSS

A common task while using CSS is trying to figure out how to center text or images. Personally I have encountered this while trying to develop a project, but I found out a way of that.
I will show you the ways to get around that problem in an easy manner.

Lets get started:

  • Method 1
display: grid;
place-items: center;
Enter fullscreen mode Exit fullscreen mode

Example:

Without style:

Alt Text

After Adding code snippet:

Alt Text

  • Method 2 Center both horizontally and vertically
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
Enter fullscreen mode Exit fullscreen mode

We will and center an image:

Without style:

Alt Text

After Adding code snippet:

Alt Text

  • Method 3
display: flex;
align-items: center;
justify-content: center;
Enter fullscreen mode Exit fullscreen mode

Without style:

Alt Text

After Adding code snippet:

Alt Text
Now go forth and center all the things.

Like my work:
ko-fi

Connect With me at Twitter | Insta | YouTube | LinkedIn | GitHub

Do share your valuable opinion, I appreciate your honest feedback!

Enjoy Coding ❀

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (2)

Collapse
 
obaino82 profile image
Obaino82 β€’

Nice oneπŸ‘Œ

Collapse
 
larymak profile image
Hillary Nyakundi β€’

Thank You ;)

Eliminate Context Switching and Maximize Productivity

Pieces.app

Pieces Copilot is your personalized workflow assistant, working alongside your favorite apps. Ask questions about entire repositories, generate contextualized code, save and reuse useful snippets, and streamline your development process.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay