way's to center divs in css
Tags: css, html
Published: 2022-10-19 19:09:06





![divs centered](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/serpfgpam9jrds9rceuh.jpg)
<p>As a web developer, sometimes centering a div or text may feel like one of the toughest jobs on planet Earth.in this article we'll learn: </p>
<ol>

<li>What divs are and why center them.</li>

<li>How to center text: <ul><li>horizontally</li>  <li>vertically</li></li>
<li>How to center divs: <ul><li>horizontally</li> <li>vertically</li></li
</ol>
 <h1>What are divs?</bh1>
 <p>Divs - short for content division elements — they are HTML elements that can divide your web page into sections so you can target them with unique CSS properties. </p>
 <p> Understanding how to center divs and text in divs, in particular, is a valuable skill for beginner programmers. It can help prevent content from stretching out to the edges of its container, overlapping with other elements on the page, or other issues that arise when creating web page layouts.</p>

<h2>How to center text.horizontally,vertically.</h2>

 <p>There are three ways to center text in a div vertically. They're slightly more complicated than the methods for centering text in a div horizontally.</p>

Let's take a look at examples of two methods below. 
<ul>
 <li><b>Using the Padding Property.</b></li>
   There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom <strong> padding:</strong>
![center divs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/di0q7c30n9bkxa1k9wyq.jpg)
 ![css](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mcsa2y2ynxkwr12if9tv.jpg)
 <Center>The result </center>

![result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ntlxp4e17u7dwoo5r2e.jpg)
<li> <b>Using the text-align method.</b></li>
</ul>
<p>To center text or links horizontally, just use the text-align property with the value center:</p>

![vertically](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tre9l9w3963qibmwk1ve.jpg)
![also vertically](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qtk279cqdum41tu941h1.jpg)
<center> see the result</center>
![a text centered horizontally](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgz1p7bu3xu9b2qgqq8e.jpg)
<h3> How to center divs horizontally and vertically. </h3>
<p>there are few different ways of setting a div center in css.While it’s easy to center a div horizontally on a page, it’s trickier to do so vertically (but still manageable!). but we'll be at it together .</p>
<ul><li>
<b>Using flexbox to center a div horizontally </b> </li>
</ul>
<p>Flexbox is the most modern way to center things on the page, and makes designing responsive layouts much easier than it used to be. However, it's not fully supported in some  browsers like Internet Explorer.</p>
<p>  To center an element horizontally with Flexbox, just apply display: flex and justify-content: center to the parent element,see the visual example below :</p>

![divs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4x8gxw9d8cph5aapy187.jpg)

![css flexbox](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d5ctad6r1r097sluk7ql.jpg)
 <center>the result</center>

![display:flex](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dkmvr1xa51iwdlwakhm.jpg)
<ul><li> <b> Center a Div Vertically with CSS using absolute positioning and negative margin </b> </li>
</ul>
<p> To center the div element using the child element,you have to set the margin-top property to (negative of half the child element's height)and also using the absolute positioning feature:</p>

![div child](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emcyq487rlfixbnebibs.jpg)

![absolute positioning and negative margin](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0tnvy7ttdmihncfzsnl4.jpg)
<center><p> check the result </p></center>


![result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mp2an8kny84c23x4s3ms.jpg)
<h4>conclusion </h4>
<p>  Thanks for reading this article! I hope you liked what you read on some methods to center a div , I'll also share other ways on how to center divs and text in my article some other time,I hope that they come handy to you in the future.</p>

<p>Do consider sharing this article with your friends – I'd really appreciate that. Stay tuned for more amazing content. Peace out! 🖖</p>
   <p> you can practice more and learn from W3Schools
https://www.w3schools.com </p>