ARTICLE AD BOX
Asked 17 years, 5 months ago
Viewed 5.0m times
How can I horizontally center a <div> within another <div> using CSS?
<div id="outer"> <div id="inner">Foo foo</div> </div>With flexbox it is very easy to style the div horizontally and vertically centered.
To align the div vertically centered, use the property align-items: center.
Other Solutions
You can apply this CSS to the inner <div>:
#inner { width: 50%; margin: 0 auto; }Of course, you don't have to set the width to 50%. Any width less than the containing <div> will work. The margin: 0 auto is what does the actual centering.
If you are targeting Internet Explorer 8 (and later), it might be better to have this instead:
#inner { display: table; margin: 0 auto; }It will make the inner element center horizontally and it works without setting a specific width.
Working example here:
7 Comments
If you don't want to set a fixed width on the inner div you could do something like this:
That makes the inner div into an inline element that can be centered with text-align.
2 Comments
The modern box model with Flexbox
#outer { display: flex; flex-direction: row; flex-wrap: wrap; justify-content: center; align-items: center; }The old box model (deprecated)
display: box and its properties box-pack, box-align, box-orient, box-direction etc. have been replaced by flexbox. While they may still work, they are not recommended to be used in production.
According to your usability you may also use the box-orient, box-flex, box-direction properties.
Read more about centering the child elements
And this explains why the box model is the best approach:
Why is the W3C box model considered better?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.
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.
3 Comments
The default width for most block level elements is auto, which fills the available area on screen. Just being centered places it in the same position as left alignment. If you wish it to be visually centered you should set a width (or a max-width although Internet Explorer 6 and earlier do not support this, and IE 7 only supports it in standards mode).
2017-11-10T19:16:10.13Z+00:00
I've created this example to show how to vertically and horizontally align.
The code is basically this:
And it will stay in the center even when you resize your screen.
1 Comment
+1 for this method, I was about to answer with it. Note that you must declare a width on the element you wish to center horizontally (or height if centering vertically). Here's a comprehensive explanation: codepen.io/shshaw/full/gEiDt. One of the more versatile and widely-supported methods of centering elements vertically and/or horizontally.
2013-12-16T18:27:12.27Z+00:00
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 */ }If you need to support older browsers which use older syntax for flexbox here's a good place to look.
4 Comments
The Flexbox specification has gone through 3 major revisions. The most recent draft is from Sept 2012, which officially deprecates all previous drafts. However, browser support is spotty (particularly old Android browsers): stackoverflow.com/questions/15662578/…
2013-10-01T20:33:33.383Z+00:00
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:
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.)
2 Comments
It's a nice trick, but there is a little caveat. If the element has inline content that's wider than 50% of the parent's width, then the extra 50% offset from the left will extrapolate the parent's width, breaking the content to the next lines to avoid overflow. But it's possible to keep the content inline by setting in the centered element the white-space attribute to nowrap. Try that in this JSFiddle.
2020-12-29T12:47:07.12Z+00:00
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.
It cannot be centered if you don't give it a width. Otherwise, it will take, by default, the whole horizontal space.
The way I usually do it is using absolute position:
The outer div doesn't need any extra properties for this to work.
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.
For Firefox and Chrome:
For Internet Explorer, Firefox, and Chrome:
The text-align: property is optional for modern browsers, but it is necessary in Internet Explorer Quirks Mode for legacy browsers support.
1 Comment
text-align is actually necessary for it to work in IE quicks mode, so if you don't mind adding a little expression to support older browsers keep it there. (IE8 with IE8 rules and IE7 rules both work without text-align, so may be it's only IE6 and older that are concerned)
2017-11-04T02:02:51.997Z+00:00
Use:
Another solution for this without having to set a width for one of the elements is using the CSS 3 transform attribute.
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.
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.
I recently found an approach:
Both elements must be the same width to function correctly.
For example, see this link and the snippet below:
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.
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:
See also this Fiddle!
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:
See also this Fiddle!
This method also works just fine:
For the inner <div>, the only condition is that its height and width must not be larger than the ones of its container.
The easiest way:
Flex have more than 97% browser support coverage and might be the best way to solve these kind of problems within few lines:
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:
Wrap .inner inside .center-helper Make .center-helper an inline block; it becomes the same size as .inner making it 300 pixels wide. Push .center-helper 50% right relative to its parent; this places its left at 500 pixels wrt. outer. 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. Set overflow on .outer to hidden to prevent horizontal scrollbar.Demo:
You can do something like this
#container { display: table; height: /* height of your container */; width: /* width of your container */; } #inner { display: table-cell; margin: 0 auto; text-align: center; vertical-align: middle; width: /* width of your center div */; }This will also align the #inner vertically. If you don't want to, remove the display and vertical-align properties;
Here is what you want in the shortest way.
#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 % ); }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.
You can just simply use Flexbox like this:
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 }Use transform:
With Autoprefixer:
#inner { position: absolute; left: 50%; -webkit-transform: translate(-50%); -ms-transform: translate(-50%); transform: translate(-50%) }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, replace $(document).ready by $(window).resize in the previous example.
Explore related questions
See similar questions with these tags.













