---
title: "How can you center an element horizontally in CSS?"  
description: "How can you center an element horizontally in CSS?"  
author: "Ashutosh Patel"  
published: 2024-10-16  
updated: 2024-10-16  
canonical: https://www.mindstick.com/interview/33975/how-can-you-center-an-element-horizontally-in-css  
category: "css-css3"  
tags: ["css-css3", "css", "css selector"]  
reading_time: 1 minute  

---

# How can you center an element horizontally in CSS?

## Horizontal an Element-

Use `margin: 0 auto;` on a block-level element with a defined width, or use `display: flex; justify-content: center;` in a flex container.

## Answers

### Answer by Ashutosh Patel

## Horizontal an Element-

Use `margin: 0 auto;` on a block-level element with a defined width, or use `display: flex; justify-content: center;` in a flex container.


---

Original Source: https://www.mindstick.com/interview/33975/how-can-you-center-an-element-horizontally-in-css

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
