[html] How to horizontally center an element

How can I horizontally center a <div> within another <div> using CSS?

<div id="outer">
  <div id="inner">Foo foo</div>
</div>

This question is related to html css alignment centering

The answer is


Flexbox Center Horizontally and Vertically Center Align an Element

_x000D_
_x000D_
.wrapper {border: 1px solid #678596; max-width: 350px; margin: 30px auto;} 
      
.parentClass { display: flex; justify-content: center; align-items: center; height: 300px;}
      
.parentClass div {margin: 5px; background: #678596; width: 50px; line-height: 50px; text-align: center; font-size: 30px; color: #fff;}
_x000D_
<h1>Flexbox Center Horizontally and Vertically Center Align an Element</h1>
<h2>justify-content: center; align-items: center;</h2>

<div class="wrapper">

<div class="parentClass">
  <div>c</div>
</div>

</div>
_x000D_
_x000D_
_x000D_


Use this code:

<div id="outer">
    <div id="inner">Foo foo</div>
</div>

#inner {
  width: 50%;
  margin: 0 auto;
  text-align: center;
}

I used Flexbox or CSS grid

  1. Flexbox

    _x000D_
    _x000D_
    #outer{_x000D_
        display: flex;_x000D_
        justify-content: center;_x000D_
    }
    _x000D_
    _x000D_
    _x000D_

  2. CSS grid

    _x000D_
    _x000D_
    #outer {_x000D_
        display: inline-grid;_x000D_
        grid-template-rows: 100px 100px 100px;_x000D_
        grid-template-columns: 100px 100px 100px;_x000D_
        grid-gap: 3px;_x000D_
    }
    _x000D_
    _x000D_
    _x000D_

You can solve the issue in many ways.


One option existed that I found:

Everybody says to use:

margin: auto 0;

But there is another option. Set this property for the parent div. It works perfectly anytime:

text-align: center;

And see, child go center.

And finally CSS for you:

#outer{
     text-align: center;
     display: block; /* Or inline-block - base on your need */
}

#inner
{
     position: relative;
     margin: 0 auto; /* It is good to be */
}

Use:

<div id="parent">
  <div class="child"></div>
</div>

Style:

#parent {
   display: flex;
   justify-content: center;
}

If you want to center it horizontally you should write as below:

#parent {
   display: flex;
   justify-content: center;
   align-items: center;
}

The way I usually do it is using absolute position:

#inner{
    left: 0;
    right: 0;
    margin-left: auto;
    margin-right: auto;
    position: absolute;
}

The outer div doesn't need any extra propertites for this to work.


CSS 3's box-align property

#outer {
    width: 100%;
    height: 100%;
    display: box;
    box-orient: horizontal;
    box-pack: center;
    box-align: center;
}

#inner {
  width: 50%;
  margin: 0 auto;
}

#inner {
    width: 50%;
    margin: 0 auto;
}

Try this:

<div id="a">
    <div id="b"></div>
</div>

CSS:

#a{
   border: 1px solid red;
   height: 120px;
   width: 400px
}

#b{
   border: 1px solid blue;
   height: 90px;
   width: 300px;
   position: relative;
   margin-left: auto;
   margin-right: auto;
}

Add text-align:center; to parent div

#outer {
    text-align: center;
}

https://jsfiddle.net/7qwxx9rs/

or

#outer > div {
    margin: auto;
    width: 100px;
}

https://jsfiddle.net/f8su1fLz/


We could use the next CSS's class which allow center vertically and horizontally any element against its parent:

.centerElement{
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

A very simple and cross-browser answer to horizontal center is to apply this rule to the parent element:

.parentBox {
    display: flex;
    justify-content: center
}

Use the below code.

HTML

<div id="outer">
  <div id="inner">Foo foo</div>
</div>

CSS

#outer {
  text-align: center;
}
#inner{
  display: inline-block;
}

The best known way which is used widely and work in many browsers including the old ones, is using margin as below:

_x000D_
_x000D_
#parent {_x000D_
  width: 100%;_x000D_
  background-color: #CCCCCC;_x000D_
}_x000D_
_x000D_
#child {_x000D_
  width: 30%; /* We need the width */_x000D_
  margin: 0 auto; /* This does the magic */_x000D_
  color: #FFFFFF;_x000D_
  background-color: #000000;_x000D_
  padding: 10px;_x000D_
  text-align: center;_x000D_
}
_x000D_
<div id="parent">_x000D_
  <div id="child">I'm the child and I'm horizontally centered! My daddy is a greyish div dude!</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Run the code to see how it works. Also, there are two important things you shouldn't forget in your CSS when you try to center this way: margin: 0 auto;. That makes it the div center as wanted. Plus don't forget width of the child, otherwise it won't get centered as expected!


Flex have more than 97% browser support coverage and might be the best way to solve these kind of problems within few lines:

#outer {
  display: flex;
  justify-content: center;
}

Give some width to the inner div and add margin:0 auto; in the CSS property.


Text-align: center

Applying text-align: center the inline contents are centered within the line box. However since the inner div has by default width: 100% you have to set a specific width or use one of the following:

_x000D_
_x000D_
#inner {
  display: inline-block;
}

#outer {
  text-align: center;
}
_x000D_
<div id="outer">
  <div id="inner">Foo foo</div>
</div>
_x000D_
_x000D_
_x000D_


Margin: 0 auto

Using margin: 0 auto is another option and it is more suitable for older browsers compatibility. It works together with display: table.

_x000D_
_x000D_
#inner {
  display: table;
  margin: 0 auto;
}
_x000D_
<div id="outer">
  <div id="inner">Foo foo</div>
</div>
_x000D_
_x000D_
_x000D_


Flexbox

display: flex behaves like a block element and lays out its content according to the flexbox model. It works with justify-content: center.

Please note: Flexbox is compatible with most of the browsers but not all. See display: flex not working on Internet Explorer for a complete and up to date list of browsers compatibility.

_x000D_
_x000D_
#inner {
  display: inline-block;
}

#outer {
  display: flex;
  justify-content: center;
}
_x000D_
<div id="outer">
  <div id="inner">Foo foo</div>
</div>
_x000D_
_x000D_
_x000D_


Transform

transform: translate lets you modify the coordinate space of the CSS visual formatting model. Using it, elements can be translated, rotated, scaled, and skewed. To center horizontally it require position: absolute and left: 50%.

_x000D_
_x000D_
#inner {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0%);
}
_x000D_
<div id="outer">
  <div id="inner">Foo foo</div>
</div>
_x000D_
_x000D_
_x000D_


<center> (Deprecated)

The tag <center> is the HTML alternative to text-align: center. It works on older browsers and most of the new ones but it is not considered a good practice since this feature is obsolete and has been removed from the Web standards.

_x000D_
_x000D_
#inner {
  display: inline-block;
}
_x000D_
<div id="outer">
  <center>
    <div id="inner">Foo foo</div>
  </center>
</div>
_x000D_
_x000D_
_x000D_


I recently found an approach:

#outer {
    position: absolute;
    left: 50%;
}

#inner {
    position: relative;
    left: -50%;
}

Both elements must be the same width to function correctly.


Another solution for this without having to set a width for one of the elements is using the CSS 3 transform attribute.

#outer {
  position: relative;
}

#inner {
  position: absolute;
  left: 50%;

  transform: translateX(-50%);
}

The trick is that translateX(-50%) sets the #inner element 50 percent to the left of its own width. You can use the same trick for vertical alignment.

Here's a Fiddle showing horizontal and vertical alignment.

More information is on Mozilla Developer Network.


The easiest way:

_x000D_
_x000D_
#outer {_x000D_
  width: 100%;_x000D_
  text-align: center;_x000D_
}_x000D_
#inner {_x000D_
  margin: auto;_x000D_
  width: 200px;_x000D_
}
_x000D_
<div id="outer">_x000D_
  <div id="inner">Blabla</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


You can do it by using Flexbox which is a good technique these days.

For using Flexbox you should give display: flex; and align-items: center; to your parent or #outer div element. The code should be like this:

_x000D_
_x000D_
#outer {_x000D_
  display: flex;_x000D_
  align-items: center;_x000D_
}
_x000D_
<div id="outer">_x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

This should center your child or #inner div horizontally. But you can't actually see any changes. Because our #outer div has no height or in other words, its height is set to auto, so it has the same height as all of its child elements. So after a little of visual styling, the result code should be like this:

_x000D_
_x000D_
#outer {_x000D_
  height: 500px;_x000D_
  display: flex;_x000D_
  align-items: center;_x000D_
  background-color: blue;_x000D_
}_x000D_
_x000D_
#inner {_x000D_
  height: 100px;_x000D_
  background: yellow;_x000D_
}
_x000D_
<div id="outer">_x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

You can see #inner div is now centered. Flexbox is the new method of positioning elements in horizontal or vertical stacks with CSS and it's got 96% of global browsers compatibility. So you are free to use it and if you want to find out more about Flexbox visit CSS-Tricks article. That is the best place to learn using Flexbox in my opinion.


Use the below CSS content for #inner div:

#inner {
  width: 50%;
  margin-left: 25%;
}

I mostly use this CSS content to center divs.


.outer {
    text-align: center;
    width: 100%
}

This will surely center your #inner both horizontally and vertically. This is also compatible in all browsers. I just added extra styling just to show how it is centered.

_x000D_
_x000D_
#outer {_x000D_
  background: black;_x000D_
  position: relative;_x000D_
  width:150px;_x000D_
  height:150px;_x000D_
}_x000D_
_x000D_
#inner { _x000D_
  background:white;_x000D_
  position: absolute;_x000D_
  left:50%;_x000D_
  top: 50%;_x000D_
  transform: translate(-50%,-50%);_x000D_
  -webkit-transform: translate(-50%,-50%);_x000D_
  -moz-transform: translate(-50%,-50%); _x000D_
  -o-transform: translate(-50%,-50%);_x000D_
} 
_x000D_
<div id="outer">  _x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

But of course if you only want it horizontally aligned, This may help you.

_x000D_
_x000D_
#outer {_x000D_
  background: black;_x000D_
  position: relative;_x000D_
  width:150px;_x000D_
  height:150px;_x000D_
}_x000D_
_x000D_
#inner { _x000D_
  background:white;_x000D_
  position: absolute;_x000D_
  left:50%;_x000D_
  transform: translate(-50%,0);_x000D_
  -webkit-transform: translate(-50%,0);_x000D_
  -moz-transform: translate(-50%,0); _x000D_
  -o-transform: translate(-50%,0);_x000D_
} 
_x000D_
<div id="outer">  _x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Try this:

<div style="position: absolute;left: 50%;top: 50%;-webkit-transform: translate(-50%, -50%);transform: translate(-50%, -50%);"><div>Foo foo</div></div>

<center>

I am spoiled with the most simple center known?

</center>

Just add this CSS content into your CSS file. It will automatically center the content.

Align horizontally to center in CSS:

#outer {
    display: flex;
    justify-content: center;
}

Align-vertically + horizontal to center in CSS:

#outer {
    display: flex;
    justify-content: center;
    align-items: center;
}

_x000D_
_x000D_
#outer
{
 display: grid;
 justify-content: center;

}
_x000D_
<div id="outer">
  <div id="inner">hello</div>
</div>
_x000D_
_x000D_
_x000D_ enter code here


The best approaches are with CSS 3.

Box model:

_x000D_
_x000D_
#outer {_x000D_
  width: 100%;_x000D_
  /* Firefox */_x000D_
  display: -moz-box;_x000D_
  -moz-box-pack: center;_x000D_
  -moz-box-align: center;_x000D_
  /* Safari and Chrome */_x000D_
  display: -webkit-box;_x000D_
  -webkit-box-pack: center;_x000D_
  -webkit-box-align: center;_x000D_
  /* W3C */_x000D_
  display: box;_x000D_
  box-pack: center;_x000D_
  box-align: center;_x000D_
}_x000D_
_x000D_
#inner {_x000D_
  width: 50%;_x000D_
}
_x000D_
<div id="outer">_x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

According to your usability you may also use the box-orient, box-flex, box-direction properties.

Flex:

#outer {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
}

Read more about centering the child elements

And this explains why the box model is the best approach:


For a horizontally centered DIV:

#outer {
  width: 100%;
  text-align: center;
}
#inner {
  display: inline-block;
}
<div id="outer">
  <div id="inner">Foo foo</div>
</div>

Well, I managed to find a solution that maybe will fit all situations, but uses JavaScript:

Here's the structure:

<div class="container">
  <div class="content">Your content goes here!</div>
  <div class="content">Your content goes here!</div>
  <div class="content">Your content goes here!</div>
</div>

And here's the JavaScript snippet:

$(document).ready(function() {
  $('.container .content').each( function() {
    container = $(this).closest('.container');
    content = $(this);

    containerHeight = container.height();
    contentHeight = content.height();

    margin = (containerHeight - contentHeight) / 2;
    content.css('margin-top', margin);
  })
});

If you want to use it in a responsive approach, you can add the following:

$(window).resize(function() {
  $('.container .content').each( function() {
    container = $(this).closest('.container');
    content = $(this);

    containerHeight = container.height();
    contentHeight = content.height();

    margin = (containerHeight - contentHeight) / 2;
    content.css('margin-top', margin);
  })
});

Try playing around with

margin: 0 auto;

If you want to center your text too, try using:

text-align: center;

_x000D_
_x000D_
#outer {_x000D_
  width: 160px;_x000D_
  padding: 5px;_x000D_
  border-style: solid;_x000D_
  border-width: thin;_x000D_
  display: block;_x000D_
}_x000D_
_x000D_
#inner {_x000D_
  margin: auto;_x000D_
  background-color: lightblue;_x000D_
  border-style: solid;_x000D_
  border-width: thin;_x000D_
  width: 80px;_x000D_
  padding: 10px;_x000D_
  text-align: center;_x000D_
}
_x000D_
<div id="outer">_x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


CSS

#inner {
  display: table;
  margin: 0 auto; 
}

HTML

<div id="outer" style="width:100%">  
  <div id="inner">Foo foo</div>
</div>

It's possible using CSS 3 Flexbox. You have two methods when using Flexbox.

  1. Set the parent display:flex; and add properties {justify-content:center; ,align-items:center;} to your parent element.

_x000D_
_x000D_
#outer {
  display: flex;
  justify-content: center;
  align-items: center;
}
_x000D_
<div id="outer" style="width:100%">
  <div id="inner">Foo foo</div>
</div>
_x000D_
_x000D_
_x000D_

  1. Set the parent display:flex and add margin:auto; to the child.

_x000D_
_x000D_
#outer {
  display: flex;
}

#inner {
  margin: auto;
}
_x000D_
<div id="outer" style="width:100%">
  <div id="inner">Foo foo</div>
</div>
_x000D_
_x000D_
_x000D_


Here is what you want in the shortest way.

JSFIDDLE

#outer {
    margin - top: 100 px;
    height: 500 px; /* you can set whatever you want */
    border: 1 px solid# ccc;
}

#inner {
    border: 1 px solid# f00;
    position: relative;
    top: 50 % ;
    transform: translateY(-50 % );
}

After reading all the answers I did not see the one I prefer. This is how you can center an element in another.

jsfiddle - http://jsfiddle.net/josephtveter/w3sksu1w/

<p>Horz Center</p>
<div class="outterDiv">
    <div class="innerDiv horzCenter"></div>
</div>
<p>Vert Center</p>
<div class="outterDiv">
    <div class="innerDiv vertCenter"></div>
</div>
<p>True Center</p>
<div class="outterDiv">
    <div class="innerDiv trueCenter"></div>
</div>
.vertCenter
{
    position: absolute;
    top:50%;
    -ms-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
}

.horzCenter
{
    position: absolute;
    left: 50%;
    -ms-transform: translateX(-50%);
    -moz-transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
}

.trueCenter
{
    position: absolute;
    left: 50%;
    top: 50%;
    -ms-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}

.outterDiv
{
    position: relative;
    background-color: blue;
    width: 10rem;
    height: 10rem;
    margin: 2rem;
}
.innerDiv
{
    background-color: red;
    width: 5rem;
    height: 5rem;
}

Use:

_x000D_
_x000D_
#outerDiv {_x000D_
  width: 500px;_x000D_
}_x000D_
_x000D_
#innerDiv {_x000D_
  width: 200px;_x000D_
  margin: 0 auto;_x000D_
}
_x000D_
<div id="outerDiv">_x000D_
  <div id="innerDiv">Inner Content</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


You can add another div which has the same size of #inner and move it to the left by -50% (half of the width of #inner) and #inner by 50%.

_x000D_
_x000D_
#inner {_x000D_
    position: absolute;_x000D_
    left: 50%;_x000D_
}_x000D_
_x000D_
#inner > div {_x000D_
    position: relative;_x000D_
    left: -50%;_x000D_
}
_x000D_
<div id="outer">_x000D_
  <div id="inner"><div>Foo foo</div></div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Instead of multiple wrappers and/or auto margins, this simple solution works for me:

<div style="top: 50%; left: 50%;
    height: 100px; width: 100px;
    margin-top: -50px; margin-left: -50px;
    background: url('lib/loading.gif') no-repeat center #fff;
    text-align: center;
    position: fixed; z-index: 9002;">Loading...</div>

It puts the div at the center of the view (vertical and horizontal), sizes and adjusts for size, centers background image (vertical and horizontal), centers text (horizontal), and keeps div in the view and on top of the content. Simply place in the HTML body and enjoy.


CSS 3:

You can use the following style on the parent container to distribute child elements evenly horizontally:

display: flex;
justify-content: space-between;  // <-- space-between or space-around

A nice DEMO regarding the different values for justify-content.

Enter image description here

CanIUse: Browser-Compatability

Try it!:

_x000D_
_x000D_
#containerdiv {_x000D_
  display: flex;_x000D_
  justify-content: space-between;_x000D_
}_x000D_
_x000D_
#containerdiv > div {_x000D_
  background-color: blue;_x000D_
  width: 50px;_x000D_
  color: white;_x000D_
  text-align: center;_x000D_
}
_x000D_
<!DOCTYPE html>_x000D_
<html>_x000D_
<head>_x000D_
  <meta charset="utf-8">_x000D_
  <meta name="viewport" content="width=device-width">_x000D_
  <title>JS Bin</title>_x000D_
</head>_x000D_
<body>_x000D_
  <div id="containerdiv">_x000D_
    <div>88</div>_x000D_
    <div>77</div>_x000D_
    <div>55</div>_x000D_
    <div>33</div>_x000D_
    <div>40</div>_x000D_
    <div>45</div>_x000D_
  </div>_x000D_
</body>_x000D_
</html>
_x000D_
_x000D_
_x000D_


In my case I needed to center(on screen) a dropdown menu(using flexbox for it's items) below a button that could have various locations vertically. None of the suggestions worked until I changed position from absolute to fixed, like this:

  margin: auto;
  left: 0;
  right: 0;
  position: fixed;

The above codes makes the dropdown to always center on the screen for devices of all sizes, no matter where the dropdown button is located vertically.


If you have a parent of some height say, body{height: 200px} or like the below has parent div#outer with height 200px, then add CSS content as below

HTML:

<div id="outer">
  <div id="centered">Foo foo</div>
</div>

CSS:

#outer{
  display: flex;
  width: 100%;
  height: 200px;
}
#centered {
  margin: auto;
}

Then child content, say div#centered content, will be vertically or horizontally middle, without using any position CSS. To remove vertically middle behavior then just modify to below CSS code:

#centered {
  margin: 0px auto;
}

or

#outer{
  display: flex;
  width: 100%;
  height: 200px;
}
#centered {
  margin: auto;
}

<div id="outer">
  <div id="centered">Foo foo</div>
</div>

Demo: https://jsfiddle.net/jinny/p3x5jb81/5/

To add only a border to show the inner div is not 100% by default:

_x000D_
_x000D_
#outer{_x000D_
  display: flex;_x000D_
  width: 100%;_x000D_
  height: 200px;_x000D_
  border: 1px solid #000000;_x000D_
}_x000D_
#centered {_x000D_
  margin: auto;_x000D_
  border: 1px solid #000000;_x000D_
}
_x000D_
<div id="outer">_x000D_
  <div id="centered">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

DEMO: http://jsfiddle.net/jinny/p3x5jb81/9


It is so simple.

Just decide what width you want to give to the inner div and use the following CSS.

CSS

.inner{
  width: 500px; /* Assumed width */
  margin: 0 auto;
}

To align a div within a div in middle -

_x000D_
_x000D_
.outer{
  width: 300px; /* For example */
  height: 300px; /* For example */
  background: red;
}
.inner{
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 200px;
  height: 200px;
  background: yellow;
}
_x000D_
<body>
  <div class='outer'>
    <div class='inner'></div>
  </div>
</body>
_x000D_
_x000D_
_x000D_

This will align the internal div in the middle, both vertically and horizontally.


Some posters have mentioned the CSS 3 way to center using display:box.

This syntax is outdated and shouldn't be used anymore. [See also this post].

So just for completeness here is the latest way to center in CSS 3 using the Flexible Box Layout Module.

So if you have simple markup like:

<div class="box">
  <div class="item1">A</div>
  <div class="item2">B</div>
  <div class="item3">C</div>
</div>

...and you want to center your items within the box, here's what you need on the parent element (.box):

.box {
    display: flex;
    flex-wrap: wrap; /* Optional. only if you want the items to wrap */
    justify-content: center; /* For horizontal alignment */
    align-items: center; /* For vertical alignment */
}

_x000D_
_x000D_
.box {_x000D_
  display: flex;_x000D_
  flex-wrap: wrap;_x000D_
  /* Optional. only if you want the items to wrap */_x000D_
  justify-content: center;_x000D_
  /* For horizontal alignment */_x000D_
  align-items: center;_x000D_
  /* For vertical alignment */_x000D_
}_x000D_
* {_x000D_
  margin: 0;_x000D_
  padding: 0;_x000D_
}_x000D_
html,_x000D_
body {_x000D_
  height: 100%;_x000D_
}_x000D_
.box {_x000D_
  height: 200px;_x000D_
  display: flex;_x000D_
  flex-wrap: wrap;_x000D_
  justify-content: center;_x000D_
  align-items: center;_x000D_
  border: 2px solid tomato;_x000D_
}_x000D_
.box div {_x000D_
  margin: 0 10px;_x000D_
  width: 100px;_x000D_
}_x000D_
.item1 {_x000D_
  height: 50px;_x000D_
  background: pink;_x000D_
}_x000D_
.item2 {_x000D_
  background: brown;_x000D_
  height: 100px;_x000D_
}_x000D_
.item3 {_x000D_
  height: 150px;_x000D_
  background: orange;_x000D_
}
_x000D_
<div class="box">_x000D_
  <div class="item1">A</div>_x000D_
  <div class="item2">B</div>_x000D_
  <div class="item3">C</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

If you need to support older browsers which use older syntax for flexbox here's a good place to look.


First of all: You need to give a width to the second div:

For example:

HTML

<div id="outter">
    <div id="inner"Centered content">
    </div
</div>

CSS:

 #inner{
     width: 50%;
     margin: auto;
}

Note that if you don't give it a width, it will take the whole width of the line.


Just simply Margin:0px auto:

_x000D_
_x000D_
#inner{_x000D_
  display: block;_x000D_
  margin: 0px auto;_x000D_
  width: 100px;_x000D_
}
_x000D_
<div id="outer" style="width:100%">  _x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


With Grid

A pretty simpel and modern way is to use display: grid

_x000D_
_x000D_
div {
    border: 1px dotted grey;
}

#outer {
    display: grid;
    place-items: center;
    height: 50px; /* not necessary */
}
_x000D_
<!DOCTYPE html>
<html>
    <head>
    </head>

    <body>
        <div id="outer">
            <div>Foo foo</div>
        </div>
    </body>
</html>
_x000D_
_x000D_
_x000D_


_x000D_
_x000D_
.outer_x000D_
{_x000D_
  background-color: rgb(230,230,255);_x000D_
  width: 100%;_x000D_
  height: 50px;_x000D_
}_x000D_
.inner_x000D_
{_x000D_
  background-color: rgb(200,200,255);_x000D_
  width: 50%;_x000D_
  height: 50px;_x000D_
  margin: 0 auto;_x000D_
}
_x000D_
<div class="outer">_x000D_
  <div class="inner">_x000D_
    margin 0 auto_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


If you don't want to set a fixed width and don't want the extra margin, add display: inline-block to your element.

You can use:

#element {
    display: table;
    margin: 0 auto;
}

Use:

<style>
  #outer{
    text-align: center;
    width: 100%;
  }
  #inner{
    text-align: center;
  }
</style>

This centralizes your inner div horizontally and vertically:

#outer{
    display: flex;
}
#inner{
    margin: auto;
}

For only horizontal align, change

margin: 0 auto;

and for vertical, change

margin: auto 0;

It cannot be centered if you don't give it a width. Otherwise, it will take, by default, the whole horizontal space.


In the previous examples they used margin: 0 auto, display:table and other answers used "with transform and translate".

And what about just with a tag? Everyone knows there is a <center> tag which is just not supported by HTML5. But it works in HTML5. For instance, in my old projects.

And it is working, but now not only MDN Web Docs, but other websites are advising not to use it any more. Here in Can I use you can see notes from MDN Web Docs. But whatever, there is such a way. This is just to know. Always being noticed about something is so useful.

Enter image description here


div{
    width: 100px;
    height: 100px;
    margin: 0 auto;
}

For the normal thing if you are using div in a static way.

If you want a div to be centered when div is absolute to its parent, here is example:

.parentdiv{
    position: relative;
    height: 500px;
}

.child_div{
   position: absolute;
   height: 200px;
   width: 500px;
   left: 0;
   right: 0;
   margin: 0 auto;
}

You can just simply use Flexbox like this:

_x000D_
_x000D_
#outer {_x000D_
    display: flex;_x000D_
    justify-content: center_x000D_
}
_x000D_
<div id="outer">_x000D_
    <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

Apply Autoprefixer for all browser support:

#outer {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    width: 100%;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center
}

Or else

Use transform:

_x000D_
_x000D_
#inner {_x000D_
    position: absolute;_x000D_
    left: 50%;_x000D_
    transform: translate(-50%)_x000D_
}
_x000D_
<div id="outer">_x000D_
    <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

With Autoprefixer:

#inner {
    position: absolute;
    left: 50%;
    -webkit-transform: translate(-50%);
    -ms-transform:     translate(-50%);
    transform:         translate(-50%)
}

I recently had to center a "hidden" div (i.e., display:none;) that had a tabled form within it that needed to be centered on the page. I wrote the following jQuery code to display the hidden div and then update the CSS content to the automatic generated width of the table and change the margin to center it. (The display toggle is triggered by clicking on a link, but this code wasn't necessary to display.)

NOTE: I'm sharing this code, because Google brought me to this Stack Overflow solution and everything would have worked except that hidden elements don't have any width and can't be resized/centered until after they are displayed.

_x000D_
_x000D_
$(function(){_x000D_
  $('#inner').show().width($('#innerTable').width()).css('margin','0 auto');_x000D_
});
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div id="inner" style="display:none;">_x000D_
  <form action="">_x000D_
    <table id="innerTable">_x000D_
      <tr><td>Name:</td><td><input type="text"></td></tr>_x000D_
      <tr><td>Email:</td><td><input type="text"></td></tr>_x000D_
      <tr><td>Email:</td><td><input type="submit"></td></tr>_x000D_
    </table>_x000D_
  </form>_x000D_
</div>
_x000D_
_x000D_
_x000D_


To centre an element horizontally you can look at these 3 methods:

Method 1: Using margin: 0 auto. (margin-left and margin-right is set auto)

_x000D_
_x000D_
#outer {
  background-color: silver;
}
#inner {
  width: max-content;
  margin: 0 auto;
  background-color: #f07878;
}
_x000D_
<div id="outer">
  <div id="inner">Foo foo</div>
</div>
_x000D_
_x000D_
_x000D_

Method 2 Using flex box

_x000D_
_x000D_
#outer {
  display: flex;
  justify-content: center;
  background-color: silver;
}
#inner {
  background-color: #f07878;
}
_x000D_
<div id="outer">
  <div id="inner">Foo foo</div>
</div>
_x000D_
_x000D_
_x000D_

Method 3 Using center tag (Deprecated)

_x000D_
_x000D_
#outer {
  background-color: silver;
}
#inner {
  display: inline;
  background-color: #f07878;
}
_x000D_
<div id="outer">
  <center>
    <div id="inner">Foo foo</div>
  </center>
</div>
_x000D_
_x000D_
_x000D_


HTML:

<div id="outer">
  <div id="inner">
  </div>
</div>

CSS:

#outer{
  width: 500px;
  background-color: #000;
  height: 500px
}
#inner{
  background-color: #333;
  margin: 0 auto;
  width: 50%;
  height: 250px;
}

Fiddle.


Center a div in a div

_x000D_
_x000D_
.outer {_x000D_
  display: -webkit-flex;_x000D_
  display: flex;_x000D_
_x000D_
  //-webkit-justify-content: center;       _x000D_
  //justify-content: center;_x000D_
  _x000D_
  //align-items: center;_x000D_
_x000D_
  width: 100%;_x000D_
  height: 100px;_x000D_
  background-color: lightgrey;_x000D_
}_x000D_
_x000D_
.inner {_x000D_
  background-color: cornflowerblue;_x000D_
  padding: 2rem;_x000D_
  margin: auto;  _x000D_
  _x000D_
  //align-self: center;      _x000D_
}
_x000D_
<div class="outer">  _x000D_
  <div class="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Suppose that your div is 200 pixels wide:

.centered {
  position: absolute;
  left: 50%;
  margin-left: -100px;
}

Make sure the parent element is positioned, i.e., relative, fixed, absolute, or sticky.

If you don't know the width of your div, you can use transform:translateX(-50%); instead of the negative margin.

https://jsfiddle.net/gjvfxxdj/

With CSS calc(), the code can get even simpler:


.centered {
  width: 200px;
  position: absolute;
  left: calc(50% - 100px);
}

The principle is still the same; put the item in the middle and compensate for the width.


<div id="outer" style="width:100%">  
  <div id="inner" style="text-align:center">Foo foo</div>
</div>

Try out this:

#outer{
    display: inline-block;
    height: 100%;
    vertical-align: middle;
}

#outer > #inner{
    display: inline-block;
    font-size: 19px;
    margin: 20px;
    max-width: 320px;
    min-height: 20px;
    min-width: 30px;
    padding: 14px;
    vertical-align: middle;
}

For Firefox and Chrome:

_x000D_
_x000D_
<div style="width:100%;">_x000D_
  <div style="width: 50%; margin: 0px auto;">Text</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

For Internet Explorer, Firefox, and Chrome:

_x000D_
_x000D_
<div style="width:100%; text-align:center;">_x000D_
  <div style="width: 50%; margin: 0px auto; text-align:left;">Text</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

The text-align: property is optional for modern browsers, but it is necessary in Internet Explorer Quirks Mode for legacy browsers support.


You can use one line of code, just text-align:center.

Here's an example:

_x000D_
_x000D_
#inner {_x000D_
  text-align: center;_x000D_
  }
_x000D_
<div id="outer" style="width:100%">_x000D_
  <div id="inner"><button>hello</button></div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


One of the easiest ways you can do it is by using display: flex. The outer div just needs to have display flex, and the inner needs margin: 0 auto to make it centered horizontally.

To center vertically and just center a div within another div, please look at the comments of the .inner class below

_x000D_
_x000D_
.wrapper {_x000D_
  display: flex;_x000D_
  /* Adding whatever height & width we want */_x000D_
  height: 300px;_x000D_
  width: 300px;_x000D_
  /* Just so you can see it is centered */_x000D_
  background: peachpuff;_x000D_
}_x000D_
_x000D_
.inner {_x000D_
  /* center horizontally */_x000D_
  margin: 0 auto;_x000D_
  /* center vertically */_x000D_
  /* margin: auto 0; */_x000D_
  /* center */_x000D_
  /* margin: 0 auto; */_x000D_
}
_x000D_
<div class="wrapper">_x000D_
  <div class="inner">_x000D_
    I am horizontally!_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Depending on your circumstances, the simplest solution could be:

margin: 0 auto; float: none;

You can use display: flex for your outer div and to horizontally center you have to add justify-content: center

#outer{
    display: flex;
    justify-content: center;
}

or you can visit w3schools - CSS flex Property for more ideas.


The main attributes for centering the div are margin: auto and width: according to requirements:

.DivCenter{
    width: 50%;
    margin: auto;
    border: 3px solid #000;
    padding: 10px;
}

You can use the calc method. The usage is for the div you're centering. If you know its width, let's say it's 1200 pixels, go for:

.container {
    width:1200px;
    margin-left: calc(50% - 600px);
}

So basically it'll add a left margin of 50% minus half the known width.


_x000D_
_x000D_
<!DOCTYPE html>_x000D_
<html>_x000D_
  <head>_x000D_
    <title>Center</title>_x000D_
    <style>_x000D_
    .outer{_x000D_
          text-align: center;_x000D_
           }_x000D_
    .inner{_x000D_
          width: 500px;_x000D_
          margin: 0 auto;_x000D_
          background: brown;_x000D_
          color: red;_x000D_
    }_x000D_
_x000D_
</style>_x000D_
_x000D_
  </head>_x000D_
_x000D_
  <body>_x000D_
_x000D_
    <div class="outer">_x000D_
      <div class="inner">This DIV is centered</div>_x000D_
    </div>_x000D_
_x000D_
  </body>_x000D_
</html>
_x000D_
_x000D_
_x000D_

Please try this. It will work without the HTML center tag.


Make it simple!

_x000D_
_x000D_
#outer {_x000D_
  display: flex;_x000D_
  justify-content: center;_x000D_
}
_x000D_
<div id="outer">  _x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Chris Coyier who wrote an excellent post on 'Centering in the Unknown' on his blog. It's a roundup of multiple solutions. I posted one that isn't posted in this question. It has more browser support than the Flexbox solution, and you're not using display: table; which could break other things.

/* This parent can be any width and height */
.outer {
  text-align: center;
}

/* The ghost, nudged to maintain perfect centering */
.outer:before {
  content: '.';
  display: inline-block;
  height: 100%;
  vertical-align: middle;
  width: 0;
  overflow: hidden;
}

/* The element to be centered, can
   also be of any width and height */
.inner {
  display: inline-block;
  vertical-align: middle;
  width: 300px;
}

Centering: Auto-width Margins

This box is horizontally centered by setting its right and left margin widths to "auto". This is the preferred way to accomplish horizontal centering with CSS and works very well in most browsers with CSS 2 support. Unfortunately, Internet Explorer 5/Windows does not respond to this method - a shortcoming of that browser, not the technique.

There is a simple workaround. (A pause while you fight back the nausea induced by that word.) Ready? Internet Explorer 5/Windows incorrectly applies the CSS "text-align" attribute to block-level elements. Declaring "text-align:center" for the containing block-level element (often the BODY element) horizontally centers the box in Internet Explorer 5/Windows.

There is a side effect of this workaround: the CSS "text-align" attribute is inherited, centering inline content. It is often necessary to explicitly set the "text-align" attribute for the centered box, counteracting the effects of the Internet Explorer 5/Windows workaround. The relevant CSS follows.

body {
    margin: 50px 0px;
    padding: 0px;
    text-align: center;
}

#Content {
    width: 500px;
    margin: 0px auto;
    text-align: left;
    padding: 15px;
    border: 1px dashed #333;
    background-color: #EEE;
}

http://bluerobot.com/web/css/center1.html


I'm sorry but this baby from the 1990s just worked for me:

<div id="outer">  
  <center>Foo foo</center>
</div>

Am I going to hell for this sin?


_x000D_
_x000D_
<div id="outer" style="width:100%;margin: 0 auto; text-align: center;">  _x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


With Sass (SCSS syntax) you can do this with a mixin:

With translate

// Center horizontal mixin
@mixin center-horizontally {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
}

// Center horizontal class
.center-horizontally {
    @include center-horizontally;
}

In an HTML tag:

<div class="center-horizontally">
    I'm centered!
</div>

Remember to add position: relative; to the parent HTML element.


With Flexbox

Using flex, you can do this:

@mixin center-horizontally {
  display: flex;
  justify-content: center;
}

// Center horizontal class
.center-horizontally {
    @include center-horizontally;
}

In an HTML tag:

<div class="center-horizontally">
    <div>I'm centered!</div>
</div>

Try this CodePen!


It can also be centered horizontally and vertically using absolute positioning, like this:

#outer{
    position: relative;
}

#inner{
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%)
}

This worked for me:

#inner {
    position: absolute;
    margin: 0 auto;
    left: 0;
    width: 7%;
    right: 0;
}

In this code, you determine the width of the element.


You can do it in a different way. See the below examples:

1. First Method
#outer {
   text-align: center;
   width: 100%;
}
#inner {
   display: inline-block;
}


2. Second method
#outer {
  position: relative;
  overflow: hidden;
}
.centered {
   position: absolute;
   left: 50%;
}

I know I'm a bit late to answering this question, and I haven't bothered to read every single answer so this may be a duplicate. Here's my take:

inner { width: 50%; background-color: Khaki; margin: 0 auto; }

One of the easiest ways...

<!DOCTYPE html>
<html>
    <head>
        <style>
            #outer-div {
                width: 100%;
                text-align: center;
                background-color: #000
            }
            #inner-div {
                display: inline-block;
                margin: 0 auto;
                padding: 3px;
                background-color: #888
            }
        </style>
    </head>

    <body>
       <div id ="outer-div" width="100%">
           <div id ="inner-div"> I am a easy horizontally centered div.</div>
       <div>
    </body>
</html>

I am sharing this answer for the designers who want to align their content both horizontally and vertically. Or you may say in the center of the web page. Please go to this site to download the file.

CSS Code

.ver-hor-div {
    display: table-cell;
    vertical-align: middle;
    text-align: center;
}

I have applied the inline style to the inner div. Use this one:

<div id="outer" style="width:100%">  
    <div id="inner" style="display:table;margin:0 auto;">Foo foo</div>
</div>

CSS justify-content property

It aligns the Flexbox items at the center of the container:

#outer {
    display: flex;
    justify-content: center;
}

I just use the simplest solution, but it works in all browsers:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <title>center a div within a div?</title>
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }

            #outer{
                width: 80%;
                height: 500px;
                background-color: #003;
                margin: 0 auto;
            }

            #outer p{
                color: #FFF;
                text-align: center;
            }

            #inner{
                background-color: #901;
                width: 50%;
                height: 100px;
                margin: 0 auto;

            }

            #inner p{
                color: #FFF;
                text-align: center;
            }
        </style>
    </head>

    <body>
        <div id="outer"><p>this is the outer div</p>
            <div id="inner">
                <p>this is the inner div</p>
            </div>
        </div>
    </body>
</html>

Add CSS to your inner div. Set margin: 0 auto and set its width less than 100%, which is the width of the outer div.

<div id="outer" style="width:100%"> 
    <div id="inner" style="margin:0 auto;width:50%">Foo foo</div> 
</div>

This will give the desired result.


We can use Flexbox to achieve this really easily:

<div id="outer">
    <div id="inner">Foo foo</div>
</div>

Center a div inside a div horizontally:

#outer {
   display: flex;
   justify-content: center;
}

Enter image description here

Center a div inside a div vertically:

#outer {
    display: flex;
    align-items: center;
}

Enter image description here

And, to completely middle the div vertically and horizontally:

#outer{
    display: flex;
    justify-content: center;
    align-items: center;
}

Enter image description here


#outer {postion: relative}
#inner {
    width: 100px; 
    height: 40px; 
    position: absolute;
    top: 50%;
    margin-top: -20px; /* Half of your height */
}

If you don't want to set a fixed width on the inner div you could do something like this:

_x000D_
_x000D_
#outer {_x000D_
  width: 100%;_x000D_
  text-align: center;_x000D_
}_x000D_
_x000D_
#inner {_x000D_
  display: inline-block;_x000D_
}
_x000D_
<div id="outer">  _x000D_
    <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

That makes the inner div into an inline element that can be centered with text-align.


I found a similar way with margin-left, but it can be left as well.

#inner {
    width: 100%;
    max-width: 65px; /* To adapt to screen width. It can be whatever you want. */
    left: 65px; /* This has to be approximately the same as the max-width. */
}

Set the width and set margin-left and margin-right to auto. That's for horizontal only, though. If you want both ways, you'd just do it both ways. Don't be afraid to experiment; it's not like you'll break anything.


This method also works just fine:

div.container {
   display: flex;
   justify-content: center; /* For horizontal alignment */
   align-items: center;     /* For vertical alignment   */
}

For the inner <div>, the only condition is that its height and width must not be larger than the ones of its container.


I've created this example to show how to vertically and horizontally align.

The code is basically this:

#outer {
  position: relative;
}

and...

#inner {
  margin: auto;
  position: absolute;
  left:0;
  right: 0;
  top: 0;
  bottom: 0;
}

And it will stay in the center even when you resize your screen.


You can do something like this

#container {
   display: table;
   width: <width of your container>;
   height: <height of your container>;
}

#inner {
   width: <width of your center div>;
   display: table-cell;
   margin: 0 auto;
   text-align: center;
   vertical-align: middle;
}

This will also align the #inner vertically. If you don't want to, remove the display and vertical-align properties;


_x000D_
_x000D_
#inner {_x000D_
  display: table;_x000D_
  margin: 0 auto;_x000D_
}
_x000D_
<div id="outer" style="width:100%">_x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


Centering a div of unknown height and width

Horizontally and vertically. It works with reasonably modern browsers (Firefox, Safari/WebKit, Chrome, Internet & Explorer & 10, Opera, etc.)

_x000D_
_x000D_
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
_x000D_
<div class="content">This works with any content</div>
_x000D_
_x000D_
_x000D_

Tinker with it further on Codepen or on JSBin.


You can use CSS Flexbox.

#inner {
    display: flex;
    justify-content: center;
}

You can learn more about it on this link: https://css-tricks.com/snippets/css/a-guide-to-flexbox/


For example, see this link and the snippet below:

_x000D_
_x000D_
div#outer {_x000D_
  height: 120px;_x000D_
  background-color: red;_x000D_
}_x000D_
_x000D_
div#inner {_x000D_
  width: 50%;_x000D_
  height: 100%;_x000D_
  background-color: green;_x000D_
  margin: 0 auto;_x000D_
  text-align: center; /* For text alignment to center horizontally. */_x000D_
  line-height: 120px; /* For text alignment to center vertically. */_x000D_
}
_x000D_
<div id="outer" style="width:100%;">_x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

If you have a lot of children under a parent, so your CSS content must be like this example on fiddle.

The HTML content look likes this:

<div id="outer" style="width:100%;">
    <div class="inner"> Foo Text </div>
    <div class="inner"> Foo Text </div>
    <div class="inner"> Foo Text </div>
    <div class="inner"> </div>
    <div class="inner"> </div>
    <div class="inner"> </div>
    <div class="inner"> </div>
    <div class="inner"> </div>
    <div class="inner"> Foo Text </div>
</div>

Then see this example on fiddle.


The best I have used in my various projects is

<div class="outer">
    <div class="inner"></div>
</div>
.outer{
  width: 500px;
  height: 500px;
  position: relative;
  background: yellow;
}
.inner{
  width: 100px;
  height: 100px;
  background:red;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

fiddle link


Yes, this is short and clean code for horizontal align.

.classname {
   display: box;
   margin: 0 auto;
   width: 500px /* Width set as per your requirement. */;
}

The easiest answer: Add margin:auto; to inner.

<div class="outer">
  <div class="inner">
    Foo foo
  </div>
</div>

CSS code

.outer{
    width: 100%;
    height: 300px;
    background: yellow;
}

.inner{
    width: 30%;
    height: 200px;
    margin: auto;
    background: red;
    text-align: center
}

Check my CodePen link: http://codepen.io/feizel/pen/QdJJrK

Enter image description here


Centering only horizontally

In my experience, the best way to center a box horizontally is to apply the following properties:

The container:

  • should have text-align: center;

The content box:

  • should have display: inline-block;

Demo:

_x000D_
_x000D_
.container {
  width: 100%;
  height: 120px;
  background: #CCC;
  text-align: center;
}

.centered-content {
  display: inline-block;
  background: #FFF;
  padding: 20px;
  border: 1px solid #000;
}
_x000D_
<div class="container">
  <div class="centered-content">
    Center this!
  </div>
</div>
_x000D_
_x000D_
_x000D_

See also this Fiddle!


Centering both horizontally & vertically

In my experience, the best way to center a box both vertically and horizontally is to use an additional container and apply the following properties:

The outer container:

  • should have display: table;

The inner container:

  • should have display: table-cell;
  • should have vertical-align: middle;
  • should have text-align: center;

The content box:

  • should have display: inline-block;

Demo:

_x000D_
_x000D_
.outer-container {
  display: table;
  width: 100%;
  height: 120px;
  background: #CCC;
}

.inner-container {
  display: table-cell;
  vertical-align: middle;
  text-align: center;
}

.centered-content {
  display: inline-block;
  background: #FFF;
  padding: 20px;
  border: 1px solid #000;
}
_x000D_
<div class="outer-container">
  <div class="inner-container">
    <div class="centered-content">
      Center this!
    </div>
  </div>
</div>
_x000D_
_x000D_
_x000D_

See also this Fiddle!


This is the best example to horizontally center a <div>

_x000D_
_x000D_
#outer {_x000D_
    display: flex;_x000D_
    align-items: center;_x000D_
    justify-content: center;_x000D_
}
_x000D_
<!DOCTYPE html>_x000D_
<html>_x000D_
    <head>_x000D_
_x000D_
    </head>_x000D_
_x000D_
    <body>_x000D_
        <div id="outer">_x000D_
            <div id="inner">Foo foo</div>_x000D_
        </div>_x000D_
    </body>_x000D_
</html>
_x000D_
_x000D_
_x000D_


If anyone would like a jQuery solution for center align these divs:

$(window).bind("load", function() {
    var wwidth = $("#outer").width();
    var width = $('#inner').width();
    $('#inner').attr("style", "padding-left: " + wwidth / 2 + "px; margin-left: -" + width / 2 + "px;");
});

Here is another way to center horizontally using Flexbox and without specifying any width to the inner container. The idea is to use pseudo elements that will push the inner content from the right and the left.

Using flex:1 on pseudo element will make them fill the remaining spaces and take equal size and the inner container will get centered.

_x000D_
_x000D_
.container {_x000D_
  display: flex;_x000D_
  border: 1px solid;_x000D_
}_x000D_
_x000D_
.container:before,_x000D_
.container:after {_x000D_
  content: "";_x000D_
  flex: 1;_x000D_
}_x000D_
_x000D_
.inner {_x000D_
  border: 1px solid red;_x000D_
  padding: 5px;_x000D_
}
_x000D_
<div class="container">_x000D_
  <div class="inner">_x000D_
    Foo content_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_

We can also consider the same situation for vertical alignment by simply changing the direction of flex to column:

_x000D_
_x000D_
.container {_x000D_
  display: flex;_x000D_
  flex-direction: column;_x000D_
  border: 1px solid;_x000D_
  min-height: 200px;_x000D_
}_x000D_
_x000D_
.container:before,_x000D_
.container:after {_x000D_
  content: "";_x000D_
  flex: 1;_x000D_
}_x000D_
_x000D_
.inner {_x000D_
  border: 1px solid red;_x000D_
  padding: 5px;_x000D_
}
_x000D_
<div class="container">_x000D_
  <div class="inner">_x000D_
    Foo content_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


The best way is using table-cell display (inner) that come exactly after a div with the display table (outer) and set vertical align for the inner div (with table-cell display) and every tag you use in the inner div placed in the center of div or page.

Note: you must set a specified height to outer

It is the best way you know without position relative or absolute, and you can use it in every browser as same.

_x000D_
_x000D_
#outer{_x000D_
  display: table;_x000D_
  height: 100vh;_x000D_
  width: 100%;_x000D_
}_x000D_
_x000D_
_x000D_
#inner{_x000D_
  display: table-cell;_x000D_
  vertical-align: middle;_x000D_
  text-align: center;_x000D_
}
_x000D_
<div id="outer">_x000D_
    <div id="inner">_x000D_
      <h1>_x000D_
          set content center_x000D_
      </h1>_x000D_
      <div>_x000D_
        hi this is the best way to align your items center_x000D_
      </div>_x000D_
    </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


You can attain this using the CSS Flexbox. You just need to apply 3 properties to the parent element to get everything working.

#outer {
  display: flex;
  align-content: center;
  justify-content: center;
}

Have a look at the code below this will make you understand the properties much better.

Get to know more about CSS Flexbox

_x000D_
_x000D_
#outer {_x000D_
  display: flex;_x000D_
  align-items: center;_x000D_
  justify-content: center;_x000D_
  border: 1px solid #ddd;_x000D_
  width: 100%;_x000D_
  height: 200px;_x000D_
 }
_x000D_
<div id="outer">  _x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


If width of the content is unknown you can use the following method. Suppose we have these two elements:

  • .outer -- full width
  • .inner -- no width set (but a max-width could be specified)

Suppose the computed width of the elements are 1000 pixels and 300 pixels respectively. Proceed as follows:

  1. Wrap .inner inside .center-helper
  2. Make .center-helper an inline block; it becomes the same size as .inner making it 300 pixels wide.
  3. Push .center-helper 50% right relative to its parent; this places its left at 500 pixels wrt. outer.
  4. Push .inner 50% left relative to its parent; this places its left at -150 pixels wrt. center helper which means its left is at 500 - 150 = 350 pixels wrt. outer.
  5. Set overflow on .outer to hidden to prevent horizontal scrollbar.

Demo:

_x000D_
_x000D_
body {_x000D_
  font: medium sans-serif;_x000D_
}_x000D_
_x000D_
.outer {_x000D_
  overflow: hidden;_x000D_
  background-color: papayawhip;_x000D_
}_x000D_
_x000D_
.center-helper {_x000D_
  display: inline-block;_x000D_
  position: relative;_x000D_
  left: 50%;_x000D_
  background-color: burlywood;_x000D_
}_x000D_
_x000D_
.inner {_x000D_
  display: inline-block;_x000D_
  position: relative;_x000D_
  left: -50%;_x000D_
  background-color: wheat;_x000D_
}
_x000D_
<div class="outer">_x000D_
  <div class="center-helper">_x000D_
    <div class="inner">_x000D_
      <h1>A div with no defined width</h1>_x000D_
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>_x000D_
          Duis condimentum sem non turpis consectetur blandit.<br>_x000D_
          Donec dictum risus id orci ornare tempor.<br>_x000D_
          Proin pharetra augue a lorem elementum molestie.<br>_x000D_
          Nunc nec justo sit amet nisi tempor viverra sit amet a ipsum.</p>_x000D_
    </div>_x000D_
  </div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


You can add this code:

_x000D_
_x000D_
#inner {_x000D_
  width: 90%;_x000D_
  margin: 0 auto;_x000D_
  text-align:center;_x000D_
}
_x000D_
<div id="outer">  _x000D_
  <div id="inner">Foo foo</div>_x000D_
</div>
_x000D_
_x000D_
_x000D_


A nice thing I recently found, mixing the use of line-height+vertical-align and the 50% left trick, you can center a dynamically sized box inside another dynamically sized box, on both the horizontal and vertical using pure CSS.

Note you must use spans (and inline-block), tested in modern browsers + Internet Explorer 8. HTML:

<h1>Center dynamic box using only css test</h1>
<div class="container">
  <div class="center">
    <div class="center-container">
      <span class="dyn-box">
        <div class="dyn-head">This is a head</div>
        <div class="dyn-body">
          This is a body<br />
          Content<br />
          Content<br />
          Content<br />
          Content<br />
        </div>
      </span>
    </div>
  </div>
</div>

CSS:

.container {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  overflow: hidden;
}

.center {
  position: absolute;
  left: 50%;
  top: 50%;
}

.center-container {
  position: absolute;
  left: -2500px;
  top: -2500px;
  width: 5000px;
  height: 5000px;
  line-height: 5000px;
  text-align: center;
  overflow: hidden;
}

.dyn-box {
  display: inline-block;
  vertical-align: middle;
  line-height: 100%;
  /* Purely asthetic below this point */
  background: #808080;
  padding: 13px;
  border-radius: 11px;
  font-family: arial;
}

.dyn-head {
  background: red;
  color: white;
  min-width: 300px;
  padding: 20px;
  font-size: 23px;
}

.dyn-body {
  padding: 10px;
  background: white;
  color: red;
}

See example here.


You can use the link https://plnkr.co/edit/MQD5QHJe5oUVKEvHCz8p?p=preview

.outer{
      display: table;
      width: 100%;
      height: 100%;
}
.inner {
    vertical-align: middle;
}

Refer to https://v4-alpha.getbootstrap.com/examples/cover/


Examples related to html

Embed ruby within URL : Middleman Blog Please help me convert this script to a simple image slider Generating a list of pages (not posts) without the index file Why there is this "clear" class before footer? Is it possible to change the content HTML5 alert messages? Getting all files in directory with ajax DevTools failed to load SourceMap: Could not load content for chrome-extension How to set width of mat-table column in angular? How to open a link in new tab using angular? ERROR Error: Uncaught (in promise), Cannot match any routes. URL Segment

Examples related to css

need to add a class to an element Using Lato fonts in my css (@font-face) Please help me convert this script to a simple image slider Why there is this "clear" class before footer? How to set width of mat-table column in angular? Center content vertically on Vuetify bootstrap 4 file input doesn't show the file name Bootstrap 4: responsive sidebar menu to top navbar Stylesheet not loaded because of MIME-type Force flex item to span full row width

Examples related to alignment

How do I center text vertically and horizontally in Flutter? CSS align one item right with flexbox What's the difference between align-content and align-items? align images side by side in html How to align iframe always in the center How to make popup look at the centre of the screen? Responsive image align center bootstrap 3 How to align title at center of ActionBar in default theme(Theme.Holo.Light) Center align a column in twitter bootstrap How do I position an image at the bottom of div?

Examples related to centering

Flutter: Trying to bottom-center an item in a Column, but it keeps left-aligning Centering in CSS Grid Bootstrap 4 Center Vertical and Horizontal Alignment Center a column using Twitter Bootstrap 3 How to horizontally align ul to center of div? How to center a <p> element inside a <div> container? Using margin:auto to vertically-align a div How to center body on a page? How to center a "position: absolute" element How to center a button within a div?