How to horizontally center a div using CSS ? Last Updated : 14 Mar, 2024 Comments Improve Suggest changes Like Article Like Report To make an HTML div center horizontally using CSS is quite easy. We can horizontally center any element using the below-mentioned approaches. Before centering a div horizontally we need to create a fixed-size div. We have given a border to the div so the centering of that div can be visible. Set the main container width to 100% so the div can be moved around into that container. HTML <!DOCTYPE html> <html> <head> <title>Centerin div Horizontally in CSS</title> <style> div { width: 300px; border: 2px solid black; } </style> </head> <body> <article class="container"> <div> <h1>Geeksforgeeks</h1> <p>Horizontally Center Div</p> </div> </article> </body> </html> Centering div with margin auto PropertyThis approach lets the browser calculate the available space and place the div in the center horizontally. In thise scenario you have to keep the div width less than the viewport width. Example: HTML <!DOCTYPE html> <html> <head> <title>Centerin div Horizontally in CSS</title> <style> div { width: 300px; margin: auto; border: 2px solid black; } </style> </head> <body> <article class="container"> <div> <h1>Geeksforgeeks</h1> <p>Horizontally Center Div</p> </div> </article> </body> </html> Output: Centering div with margin auto Output Explanation: In this example we utilized the browser capability by using the CSS margin property, we set the margin value to auto so the browser can adjust with the available space and place the div into the center of the viewport. Note: For Microsoft Edge & Internet Explorer, The auto value is not supported in quirks mode. Comment More infoAdvertise with us M manaschhabra2 Follow Improve Article Tags : Web Technologies CSS CSS-Misc CSS-Questions Similar Reads How to style a dropdown using CSS? We will learn how to style the dropdown list using CSS and explore its implementation through examples. Dropdown menus are commonly used to allow users to select an option from a predefined list. Styling them properly can enhance your website's user experience and visual appeal.What is a <select 3 min read Remove border from IFrame using CSS The <iframe> tag is used to embed another web page within a webpage. To remove the border from an <iframe>, you can use the frameBorder attribute in the <iframe> tag, setting its value to "0".Syntax:<iframe frameBorder="value"></iframe>Note:The frameBorder attribute is 2 min read How to Hide Scrollbar with CSS Hiding the scrollbar with CSS can enhance the look of your webpage by removing visible scrollbars while still allowing users to scroll through content. This can create a cleaner, more streamlined user interface without sacrificing usability. In this article, weâll explore different methods for hidin 3 min read How to apply !important in CSS? The !important rule in CSS is used to add more importance to a property/value than normal. It forces a style to override any other declarations, ensuring the specified property value is applied, regardless of specificity. It helps resolve conflicts but should be used sparingly to avoid complicating 2 min read How to use a not:first-child Selector in CSS? The :not(:first-child) selector in CSS targets all elements that are not the first child within their parent. It allows you to style elements except the first one, making it useful for applying styles to subsequent items in a list or layout.Syntax:not( element ) { // CSS property } Example: In this 2 min read How to Auto-Resize an Image to Fit a Div Container? To resize an image or video to fit inside a div container, use the object-fit property for precise control over how the content fits.object-fit maintains the aspect ratio or stretches the content to fill the container.Common values include cover (fills the container, may crop) and contain (fits with 3 min read How to Disable Resizable Property of Textarea using CSS? The resize property is used to disable the resizeable property of Textarea using CSS, by setting the resize property to none.Note: The resize property in CSS controls whether an element, like a textarea, can be resized by the user.Disable Resizing of TextareaThe resize property in CSS controls an el 1 min read Create an Unordered List Without Bullets using CSS To create an unordered list without bullet points, the CSS list-style-type property is used. This removes the default bullet points from the list items, and making the list appear plain.Table of ContentUsing list-style-type property Remove margin and padding after removing bullet pointsUnordered Lis 1 min read Set cellpadding and cellspacing in CSS Cell Padding The cell padding is used to define the spaces between the cells and its border. If cell padding property is not applied then it will be set as the default value. Example: html <!DOCTYPE html> <html> <head> <title>cell padding</title> <style> table, th 1 min read How to overlay one div over another div using CSS Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look. It looks sophisticated and is simple 2 min read Like