<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/easy-ways-to-center-a-div-dmmzkhyb2" -->

---
title: Easy ways to center a div | daily.dev
description: Using CSS Grid and Flexbox, we can center a .div using margin:auto in our flex item. Using the position property requires a few more lines of code, but don't...
canonical: https://daily.dev/posts/easy-ways-to-center-a-div-dmmzkhyb2
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Easy ways to center a div | daily.dev
og:description: Using CSS Grid and Flexbox, we can center a .div using margin:auto in our flex item. Using the position property requires a few more lines of code, but don't...
og:url: https://daily.dev/posts/easy-ways-to-center-a-div-dmmzkhyb2
og:image: https://api.daily.dev/og/posts/DmMZKHYB2.png
og:image:alt: Easy ways to center a div
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Easy ways to center a div

**[Hashnode](https://daily.dev/sources/hashnode)** · 2 min read · 37 upvotes · 2 comments

## Summary

Using CSS Grid and Flexbox, we can center a .div using margin:auto in our flex item. Using the position property requires a few more lines of code, but don't worry! It's still an easy way. We first need to set the position of our parent element to relative, followed by our child element position to absolute.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://yuricodesbot.hashnode.dev/4-easy-ways-to-center-a-div>

## Community discussion

Top comments from developers on daily.dev.

**@adityakrcodes** · 2 upvotes

> Everyone stucks here in there dev life! 😂

**@elanthony** · 0 upvotes

> the definitive guide

---

Tags: [#css](https://daily.dev/tags/css)

[View this post on daily.dev](https://daily.dev/posts/easy-ways-to-center-a-div-dmmzkhyb2)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Easy ways to center a div","url":"https://daily.dev/posts/easy-ways-to-center-a-div-dmmzkhyb2","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/easy-ways-to-center-a-div-dmmzkhyb2"},"datePublished":"2021-12-06T06:10:11.496Z","dateModified":"2021-12-06T06:10:11.497Z","description":"Using CSS Grid and Flexbox, we can center a .div using margin:auto in our flex item. Using the position property requires a few more lines of code, but don't...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c7dc5597772fb5376d8dc485323c51d2","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/c7dc5597772fb5376d8dc485323c51d2","isAccessibleForFree":true,"articleSection":"Hashnode","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Hashnode","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/4a287b2e7cb5499bae863f8e7137cdb4","url":"https://daily.dev/sources/hashnode"},"commentCount":2,"discussionUrl":"https://daily.dev/posts/easy-ways-to-center-a-div-dmmzkhyb2","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":37},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":2}],"keywords":"css","timeRequired":"PT2M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Hashnode","item":"https://daily.dev/sources/hashnode"},{"@type":"ListItem","position":3,"name":"Easy ways to center a div"}]}
{"@context":"https://schema.org","@type":"WebPage","@id":"https://daily.dev/posts/easy-ways-to-center-a-div-dmmzkhyb2","comment":[{"@type":"Comment","text":"Everyone stucks here in there dev life! 😂","datePublished":"2021-12-07T14:40:28.893Z","url":"https://daily.dev/posts/DmMZKHYB2#c-OwAr9io1B","author":{"@type":"Person","name":"Aditya Kumar","url":"https://daily.dev/adityakrcodes","image":"https://media.daily.dev/image/upload/s--jD8DmFVC--/f_auto/v1772506491/avatars/avatar_yLi65k7rrnVtM3YT1L9Kn?_a=BAMAMiiu0"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2}},{"@type":"Comment","text":"the definitive guide","datePublished":"2021-12-07T15:06:25.133Z","url":"https://daily.dev/posts/DmMZKHYB2#c-91o5zvaLH","author":{"@type":"Person","name":"Antonio Arias","url":"https://daily.dev/elanthony","image":"https://lh3.googleusercontent.com/a/AATXAJzuF8F_43XcBAxbqI2Gvuk7aiBn2MMcB6ltK-Sf=s100"}}]}
```

