<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="/rss/styles.xsl" type="text/xsl"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>David Aji&apos;s Blog</title><description>Frontend engineering, design engineering, creative coding, and all things web development.</description><link>https://davidaji.com/</link><language>en-us</language><item><title>CSS 101: Centering Elements</title><link>https://davidaji.com/css-101-centering-elements/</link><guid isPermaLink="true">https://davidaji.com/css-101-centering-elements/</guid><description>A comprehensive guide to different methods for centering elements in CSS, including text-align, margin auto, transforms, Grid, and Flexbox approaches.</description><pubDate>Fri, 27 Mar 2020 00:00:00 GMT</pubDate><content:encoded>*This blog post was originally published [here](https://ajiva.hashnode.dev/css-101-centering-elements) on March 27th 2020*

I keep seeing developers complain about continuously googling how to center elements each time they try to achieve this. Well, I&apos;m here to solve your problem. I&apos;m going to show you different ways to center elements in css and when to use them.

Banter from designers like this must end.

![Designer complaining about centering elements](https://cdn.hashnode.com/res/hashnode/image/upload/v1590498021321/1-hsGPk6U.png)

Let&apos;s get into it.

# 1\. Good old &quot;Text-Align: center&quot;

This method is really popular and though it&apos;s normally used for text, it works well for any element. Basically you give the parent element of what you want to center a

```css
text-align: center;
```

Note: The child element has to be an **inline** or **inline-block** element for this to work.

&lt;iframe height=&quot;300&quot; style=&quot;width: 100%;&quot; scrolling=&quot;no&quot; title=&quot;CSS Centering - Text Align&quot; src=&quot;https://codepen.io/Ajiva/embed/zYvXVEY?default-tab=html%2Cresult&quot; frameborder=&quot;no&quot; loading=&quot;lazy&quot; allowtransparency=&quot;true&quot; allowfullscreen=&quot;true&quot;&gt;
  See the Pen &lt;a href=&quot;https://codepen.io/Ajiva/pen/zYvXVEY&quot;&gt;CSS Centering - Text Align&lt;/a&gt; by David Aji (&lt;a href=&quot;https://codepen.io/Ajiva&quot;&gt;@Ajiva&lt;/a&gt;) on &lt;a href=&quot;https://codepen.io&quot;&gt;CodePen&lt;/a&gt;.
&lt;/iframe&gt;

As you can see from the above example, the **p** element inherits its text-align from the &quot;grand-parent element&quot;

# 2\. Using Margin: auto

All you have to do is give an element a specific width and set it&apos;s the horizontal margin to auto and the element will be centered horizontally.

&lt;iframe height=&quot;300&quot; style=&quot;width: 100%;&quot; scrolling=&quot;no&quot; title=&quot;CSS Centering - Margin Auto&quot; src=&quot;https://codepen.io/Ajiva/embed/LYpvwzQ?default-tab=html%2Cresult&quot; frameborder=&quot;no&quot; loading=&quot;lazy&quot; allowtransparency=&quot;true&quot; allowfullscreen=&quot;true&quot;&gt;
  See the Pen &lt;a href=&quot;https://codepen.io/Ajiva/pen/LYpvwzQ&quot;&gt;CSS Centering - Margin Auto&lt;/a&gt; by David Aji (&lt;a href=&quot;https://codepen.io/Ajiva&quot;&gt;@Ajiva&lt;/a&gt;) on &lt;a href=&quot;https://codepen.io&quot;&gt;CodePen&lt;/a&gt;.
&lt;/iframe&gt;

This method could come in handy when creating signup/login forms that need to be centered 😉

# 3\. Transform/Translate method

Now, we&apos;ve all been in positions where we want to place an element in a particular position where we need

```css
position: absolute; or position: relative;
```

But at the same time, this element needs to be positioned centrally either horizontally or vertically. Well, I got you. Here&apos;s a simple way to get this done.

&lt;iframe height=&quot;300&quot; style=&quot;width: 100%;&quot; scrolling=&quot;no&quot; title=&quot;CSS Centering - Transform/Translate&quot; src=&quot;https://codepen.io/Ajiva/embed/PoPvbmq?default-tab=html%2Cresult&quot; frameborder=&quot;no&quot; loading=&quot;lazy&quot; allowtransparency=&quot;true&quot; allowfullscreen=&quot;true&quot;&gt;
  See the Pen &lt;a href=&quot;https://codepen.io/Ajiva/pen/PoPvbmq&quot;&gt;CSS Centering - Transform/Translate&lt;/a&gt; by David Aji (&lt;a href=&quot;https://codepen.io/Ajiva&quot;&gt;@Ajiva&lt;/a&gt;) on &lt;a href=&quot;https://codepen.io&quot;&gt;CodePen&lt;/a&gt;.
&lt;/iframe&gt;

From the above, you can see that the **relative-box** is horizontally positioned. This is achieved by giving the element a

```css
left:50%;
```

and using transform to offset the x axis with

```css
transform: translateX(-50%);
```

achieves **centralization**🤯

This is the same for the **absolute-box**, just reversed to centralize vertically this time; Note: you can achieve both horizontal and vertical centralization on one element like this

```css
     left:50%;
     top:50%;
     transform:translate(-50% , -50%);
```

# 4\. The Grid method

Yet another wonderful way to centralize elements is using css grid. it&apos;s simple and straight to the point.

&lt;iframe height=&quot;300&quot; style=&quot;width: 100%;&quot; scrolling=&quot;no&quot; title=&quot;CSS Centering - Grid&quot; src=&quot;https://codepen.io/Ajiva/embed/pojmNVd?default-tab=html%2Cresult&quot; frameborder=&quot;no&quot; loading=&quot;lazy&quot; allowtransparency=&quot;true&quot; allowfullscreen=&quot;true&quot;&gt;
  See the Pen &lt;a href=&quot;https://codepen.io/Ajiva/pen/pojmNVd&quot;&gt;CSS Centering - Grid&lt;/a&gt; by David Aji (&lt;a href=&quot;https://codepen.io/Ajiva&quot;&gt;@Ajiva&lt;/a&gt;) on &lt;a href=&quot;https://codepen.io&quot;&gt;CodePen&lt;/a&gt;.
&lt;/iframe&gt;

A flaw with this method is when you need to centralize vertically that&apos;s

```css
align-items: center;
```

You need to specify something like

Which may be overkill. But that&apos;s why the next method is perfect for you.

# 5\. The Magic of Flexbox method

They say, &quot;Save the best for last&quot; right? This is the best of the bunch.

I&apos;m sure you can tell from my enthusiasm that this is definitely my favorite method. The flex method is so simple, clean, effective, and very versatile. You can make responsive changes by changing just one or two properties smoothly.

&lt;iframe height=&quot;300&quot; style=&quot;width: 100%;&quot; scrolling=&quot;no&quot; title=&quot;CSS Centering - Flexbox&quot; src=&quot;https://codepen.io/Ajiva/embed/LYpobXK?default-tab=html%2Cresult&quot; frameborder=&quot;no&quot; loading=&quot;lazy&quot; allowtransparency=&quot;true&quot; allowfullscreen=&quot;true&quot;&gt;
  See the Pen &lt;a href=&quot;https://codepen.io/Ajiva/pen/LYpobXK&quot;&gt;CSS Centering - Flexbox&lt;/a&gt; by David Aji (&lt;a href=&quot;https://codepen.io/Ajiva&quot;&gt;@Ajiva&lt;/a&gt;) on &lt;a href=&quot;https://codepen.io&quot;&gt;CodePen&lt;/a&gt;.
&lt;/iframe&gt;

All the methods are great but I&apos;ll recommend using the **flex method** most of the time, and also switch to others when necessary, like the **transform/translate method** when you&apos;re dealing with **positiioned elements**

I hope I&apos;ve been of help, and I&apos;ve forever solved the great mystery of centering elements in css.</content:encoded><category>css</category><category>web-development</category><category>frontend</category><category>tutorial</category><author>David Aji</author></item></channel></rss>