How to horizontally center a ?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
How can I horizontally center a <div> within another <div> using CSS? 
<div id="outer">  
  <div id="inner">Foo foo</div>
</div>
html css alignment centering
add a comment |
How can I horizontally center a <div> within another <div> using CSS? 
<div id="outer">  
  <div id="inner">Foo foo</div>
</div>
html css alignment centering
 
 
 4
 
 
 
 
 
 Of those great answers, I just want to highlight that you must give "#inner" a "width", or it will be "100%", and you can't tell if it's already centered.
 
 – Jony
 Nov 7 '17 at 8:22
 
 
 
add a comment |
How can I horizontally center a <div> within another <div> using CSS? 
<div id="outer">  
  <div id="inner">Foo foo</div>
</div>
html css alignment centering
How can I horizontally center a <div> within another <div> using CSS? 
<div id="outer">  
  <div id="inner">Foo foo</div>
</div>
html css alignment centering
html css alignment centering
edited Oct 30 '18 at 12:21
community wiki
32 revs, 23 users 13%
Mosh Feu
 
 
 4
 
 
 
 
 
 Of those great answers, I just want to highlight that you must give "#inner" a "width", or it will be "100%", and you can't tell if it's already centered.
 
 – Jony
 Nov 7 '17 at 8:22
 
 
 
add a comment |
 
 
 4
 
 
 
 
 
 Of those great answers, I just want to highlight that you must give "#inner" a "width", or it will be "100%", and you can't tell if it's already centered.
 
 – Jony
 Nov 7 '17 at 8:22
 
 
 
4
4
Of those great answers, I just want to highlight that you must give "#inner" a "width", or it will be "100%", and you can't tell if it's already centered.
– Jony
Nov 7 '17 at 8:22
Of those great answers, I just want to highlight that you must give "#inner" a "width", or it will be "100%", and you can't tell if it's already centered.
– Jony
Nov 7 '17 at 8:22
add a comment |
                                96 Answers
                            96
                        
active
oldest
votes
1 2
3
4
next
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 IE8+, 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:
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer" style="width:100%">
  <div id="inner">Foo foo</div>
</div>
 
 
 10
 
 
 
 
 
 For the vertical centering I usually use "line-height" (line-height == height). This is simple and nice but it's only working with a one line content text :)
 
 – Nicolas Guillaume
 Jun 23 '10 at 12:36
 
 
 
 
 
 90
 
 
 
 
 
 You have to use the !DOCTYPE tag on your html page to make it work well on IE.
 
 – Fabio
 Jan 28 '12 at 14:23
 
 
 
 
 
 14
 
 
 
 
 
 Note that it may be necessary to add "float:none;" for the #inner.
 
 – Mert Mertce
 Sep 27 '13 at 8:30
 
 
 
 
 
 14
 
 
 
 
 
 You also set the top and bottom margins to 0, which is unrelated. Better putting- margin-left: auto; margin-right: autoI think.
 
 – Emmanuel Touzery
 Feb 8 '14 at 22:45
 
 
 
 
 
 
 
 12
 
 
 
 
 
 Not necessarily- margin:0 auto: it can be- margin: <whatever_vertical_margin_you_need> autosecond being the horizontal margin.
 
 – YakovL
 May 3 '16 at 19:07
 
 
 
 
 
|
show 14 more comments
If you don't want to set a fixed width on the inner div you could do something like this:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  display: inline-block;
}<div id="outer">  
    <div id="inner">Foo foo</div>
</div>That makes the inner div into an inline element that can be centered with text-align.
 
 
 12
 
 
 
 
 
 @SabaAhang the correct syntax for that would be- float: none;and is probably only needed because #inner has inherited a- floatof either- leftor- rightfrom somewhere else in your CSS.
 
 – Doug McLean
 Nov 12 '15 at 9:21
 
 
 
 
 
 5
 
 
 
 
 
 This is a nice solution. Just keep in mind that inner will inherit- text-alignso you may want to set inner's- text-alignto- initialor some other value.
 
 – pmoleri
 Nov 18 '16 at 21:52
 
 
 
add a comment |
The best approaches are with CSS 3.
Box model:
#outer{
    width: 100%;
    /* Firefox */
    display: -moz-box;
    -moz-box-pack: center;
    -moz-box-align: center;
    /* Safari and Chrome */
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;
    /* W3C */
    display: box;
    box-pack: center;
    box-align: center;
}
#inner{
    width: 50%;
}
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
- Link 2 
- Link 3 
- Link 4 
And this explains why the box model is the best approach:
- Why is the W3C box model considered better?
 
 
 
 
 
 
 
 it also works for me when inner div have float: left;
 
 – Tareq
 Nov 12 '12 at 6:30
 
 
 
 
 
 22
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:51
 
 
 
 
 
 4
 
 
 
 
 
 Safari, as of now, still requires- -webkitflags for flexbox (- display: -webkit-flex;and- -webkit-align-items: center;and- -webkit-justify-content: center;)
 
 – Joseph Hansen
 Jul 23 '15 at 15:59
 
 
 
add a comment |
Suppose that your div is 200px 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/
 
 
 
 
 
 
 
 This doesn't work in Safari
 
 – cesards
 Aug 8 '15 at 9:05
 
 
 
 
 
 
 
 
 
 
 I don't like this solution because when the inner element is too broad for the screen, you can't scroll over the whole element horizontally. margin: 0 auto works better.
 
 – Aloso
 Dec 30 '15 at 4:02
 
 
 
 
 
 
 
 
 
 
 why do u put margin left: -100, this will not work
 
 – Robert Limanto
 Nov 29 '16 at 23:51
 
 
 
 
 
 
 
 
 
 
 I've read that it's the only method that will work in IE6/7
 
 – Andy
 Oct 31 '17 at 7:25
 
 
 
 
 
 
 
 
 
 
 margin-left: auto; margin-right: auto; centres a block level element
 
 – crystallinering
 Nov 10 '17 at 19:15
 
 
 
|
show 1 more comment
I've created this example to show how to vertically and horizontally align.
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 re-size your screen.
 
 
 12
 
 
 
 
 
 +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.
 
 – stvnrynlds
 Dec 16 '13 at 18:27
 
 
 
 
 
 
 
 6
 
 
 
 
 
 You cannot use padding within the div, but if you want to give the illusion use a border of the same color.
 
 – Squirrl
 Jul 9 '14 at 11:45
 
 
 
 
 
 
 
 
 
 
 I think for this method to work, you need to set the with and height of inner div
 
 – Nicolas S.Xu
 Nov 29 '15 at 21:39
 
 
 
add a comment |
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 */
}
.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 */
}
* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
}
.box {
  height: 200px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  border: 2px solid tomato;
}
.box div {
  margin: 0 10px;
  width: 100px;
}
.item1 {
  height: 50px;
  background: pink;
}
.item2 {
  background: brown;
  height: 100px;
}
.item3 {
  height: 150px;
  background: orange;
}<div class="box">
  <div class="item1">A</div>
  <div class="item2">B</div>
  <div class="item3">C</div>
</div>If you need to support older browsers which use older syntax for flexbox here's a good place to look.
 
 
 
 
 
 
 
 what do you mean by "syntax is outdated", is it deprecated?
 
 – Konga Raju
 Sep 6 '13 at 10:18
 
 
 
 
 
 4
 
 
 
 
 
 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/…
 
 – cimmanon
 Oct 1 '13 at 20:33
 
 
 
 
 
 
 
 
 
 
 This worked for me in Chrome when Justin Poliey's version didn't.
 
 – Vern Jensen
 Jun 29 '16 at 2:50
 
 
 
 
 
 
 
 
 
 
 Isn't the "justify-content: center;" for the vertical alignment and the "align-items: center;" for the horizontal alignment?
 
 – Wouter Vanherck
 Mar 22 '17 at 12:04
 
 
 
 
 
 3
 
 
 
 
 
 @WouterVanherck it depends on the- flex-directionvalue. If it is 'row' (the default) - then- justify-content: center;is for the horizontal alignment (like I mentioned in the answer) If it is 'column' - then- justify-content: center;is for the vertical alignment.
 
 – Danield
 Mar 22 '17 at 12:29
 
 
 
add a comment |
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;
}
 
 
 3
 
 
 
 
 
 same requirements as display:inline-block too (quirksmode.org/css/display.html)
 
 – montrealmike
 Sep 11 '12 at 15:09
 
 
 
 
 
 
 
 
 
 
 I used this, too, but I've never encountered- display: table;before. What does it do?
 
 – Matt Cremeens
 Jul 31 '17 at 11:25
 
 
 
add a comment |
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.)
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}<div class="content">This works with any content</div>Tinker with it further on Codepen or on JSBin.
add a comment |
It cannot be centered if you don't give it a width, otherwise it will take, by default the whole horizontal space.
 
 
 44
 
 
 
 
 
 and if you don't know the width? Say because the content is dynamic?
 
 – gman
 Jun 2 '11 at 15:45
 
 
 
 
 
 
 
 
 
 
 max-width? what about that?
 
 – Will Hoskings
 Mar 17 '18 at 22:32
 
 
 
add a comment |
CSS3's box-align property
#outer {
    width:100%;
    height:100%;
    display:box;
    box-orient:horizontal;
    box-pack:center;
    box-align:center;
}
 
 
 7
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:52
 
 
 
add a comment |
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.
add a comment |
I recently had to center a "hidden" div (ie, display:none;) that had a tabled form within it that needed to be centered on the page. I wrote the following jQuery to display the hidden div & then update the CSS 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 neccessary to display.)
NOTE: I'm sharing this code because Google brought me to this Stack Overflow solution & everything would have worked except that hidden elements don't have any width & can't be resized/centered until after they are displayed.
$(function(){
  $('#inner').show().width($('#innerTable').width()).css('margin','0 auto');
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inner" style="display:none;">
  <form action="">
    <table id="innerTable">
      <tr><td>Name:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="submit"></td></tr>
    </table>
  </form>
</div>add a comment |
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.
 
 
 
 
 
 
 
 This may not work if you have other divs below the centered div.
 
 – NoChance
 Jul 26 '18 at 7:59
 
 
 
add a comment |
For Firefox and Chrome:
<div style="width:100%;">
  <div style="width: 50%; margin: 0px auto;">Text</div>
</div>For Internet Explorer, Firefox, and Chrome:
<div style="width:100%; text-align:center;">
  <div style="width: 50%; margin: 0px auto; text-align:left;">Text</div>
</div>The text-align: property is optional for modern browsers, but it is necessary in Internet Explorer Quirks Mode for legacy browsers support.
 
 
 5
 
 
 
 
 
 There is no need for text-align property. It's completely un-necessary.
 
 – Touhid Rahman
 May 23 '13 at 5:29
 
 
 
 
 
 
 
 
 
 
 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)
 
 – heytools
 Nov 4 '17 at 2:02
 
 
 
add a comment |
This is my answer.
#outerDiv {
  width: 500px;
}
#innerDiv {
  width: 200px;
  margin: 0 auto;
}<div id="outerDiv">
  <div id="innerDiv">Inner Content</div>
</div>add a comment |
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.
 
 
 2
 
 
 
 
 
 One may need vendor prefixes as well :- -webkit-transform: translate(-50%,0); -moz-transform: translate(-50%,0); -ms-transform: translate(-50%,0); -khtml-transform: translate(-50%,0); -o-transform: translate(-50%,0);
 
 – Skippy le Grand Gourou
 Sep 2 '15 at 13:48
 
 
 
 
 
add a comment |
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 then 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;
}
add a comment |
I realize I'm pretty late to the game, but this is a very popular question, and I recently found an approach I haven't seen mentioned anywhere here, so I figured I'd document it.
#outer {
    position: absolute;
    left: 50%;
}
#inner {
    position: relative;
    left: -50%;
}
EDIT: both elements must be the same width to function correctly.
 
 
 
 
 
 
 
 Just set this rule for- #inneronly:- #inner { position:relative; left:50%; transform:translateX(-50%); }. This works for any width.
 
 – Jose Rui Santos
 Nov 24 '15 at 10:30
 
 
 
 
 
add a comment |
For example, see this link and the snippet below:
div#outer {
  height: 120px;
  background-color: red;
}
div#inner {
  width: 50%;
  height: 100%;
  background-color: green;
  margin: 0 auto;
  text-align: center; /* For text alignment to center horizontally. */
  line-height: 120px; /* For text alignment to center vertically. */
}<div id="outer" style="width:100%;">
  <div id="inner">Foo foo</div>
</div>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.
add a comment |
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:
.container {
  width: 100%;
  height: 120px;
  background: #CCC;
  text-align: center;
}
.centered-content {
  display: inline-block;
  background: #FFF;
  padding: 20px;
  border: 1px solid #000;
}<div class="container">
  <div class="centered-content">
    Center this!
  </div>
</div>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:
.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;
}<div class="outer-container">
  <div class="inner-container">
    <div class="centered-content">
      Center this!
    </div>
  </div>
</div>See also this Fiddle!
add a comment |
The easiest way:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  margin: auto;
  width: 200px;
}<div id="outer">
  <div id="inner">Blabla</div>
</div>
 
 
 1
 
 
 
 
 
 As your fiddle notes, #inner has to have a width set on it.
 
 – Michael Terry
 Feb 5 '15 at 21:06
 
 
 
add a comment |
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 1000px and 300px respectively. Proceed as follows:
- Wrap .innerinside.center-helper
 
- Make .center-helperan inline block; it becomes the same size as.innermaking it 300px wide.
- Push .center-helper50% right relative to its parent; this places its left at 500px wrt. outer.
- Push .inner50% left relative to its parent; this places its left at -150px wrt. center helper which means its left is at 500 - 150 = 350px wrt. outer.
- Set overflow on .outerto hidden to prevent horizontal scrollbar.
Demo:
body {
  font: medium sans-serif;
}
.outer {
  overflow: hidden;
  background-color: papayawhip;
}
.center-helper {
  display: inline-block;
  position: relative;
  left: 50%;
  background-color: burlywood;
}
.inner {
  display: inline-block;
  position: relative;
  left: -50%;
  background-color: wheat;
}<div class="outer">
  <div class="center-helper">
    <div class="inner">
      <h1>A div with no defined width</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
          Duis condimentum sem non turpis consectetur blandit.<br>
          Donec dictum risus id orci ornare tempor.<br>
          Proin pharetra augue a lorem elementum molestie.<br>
          Nunc nec justo sit amet nisi tempor viverra sit amet a ipsum.</p>
    </div>
  </div>
</div>.outer {
    overflow: hidden;
}
.center-helper {
    float: left;
    position: relative;
    left: 50%;
}
.inner {
    float: left;
    position: relative;
    left: -50%;
}
add a comment |
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;
add a comment |
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 % );
}
 
 
 3
 
 
 
 
 
 That centers it vertically.
 
 – Michael Terry
 Feb 6 '15 at 0:24
 
 
 
add a comment |
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:
- display: block
- display: inline
- display: inline-block
#inner {
  display: inline-block;
}
#outer {
  text-align: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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.
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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 here for a complete and up to date list of browsers compatibility.
#inner {
  display: inline-block;
}
#outer {
  display: flex;
  justify-content: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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%.
#inner {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0%);
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>
<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.
#inner {
  display: inline-block;
}<div id="outer">
  <center>
    <div id="inner">Foo foo</div>
  </center>
</div>add a comment |
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.
add a comment |
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.
add a comment |
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;
}
add a comment |
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);
  })
});
add a comment |
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 */
}
 
 
 
 
 
 
 
 text-align work for text alignment in its container not for its container to its parent.
 
 – Lalit Kumar
 Dec 4 '13 at 7:32
 
 
 
 
 
 
 
 
 
 
 i test it , i problem with set child to center , must when you have more one child , more times margin:0 auto font answer , but , text-align center , for parent make this child be center , even if they are element and not be text , test and see what happen
 
 – Pnsadeghy
 Dec 4 '13 at 8:35
 
 
 
 
 
 
 
 
 
 
 text-align center text only. You right at this time but when you write a container css which contains a child with different width and color your code does't work. Test it again!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 9:23
 
 
 
 
 
 
 
 
 
 
 See this example jsfiddle.net/uCdPK/2 and tell me what do you think about it!!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 10:03
 
 
 
add a comment |
1 2
3
4
next
                                96 Answers
                            96
                        
active
oldest
votes
                                96 Answers
                            96
                        
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
3
4
next
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 IE8+, 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:
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer" style="width:100%">
  <div id="inner">Foo foo</div>
</div>
 
 
 10
 
 
 
 
 
 For the vertical centering I usually use "line-height" (line-height == height). This is simple and nice but it's only working with a one line content text :)
 
 – Nicolas Guillaume
 Jun 23 '10 at 12:36
 
 
 
 
 
 90
 
 
 
 
 
 You have to use the !DOCTYPE tag on your html page to make it work well on IE.
 
 – Fabio
 Jan 28 '12 at 14:23
 
 
 
 
 
 14
 
 
 
 
 
 Note that it may be necessary to add "float:none;" for the #inner.
 
 – Mert Mertce
 Sep 27 '13 at 8:30
 
 
 
 
 
 14
 
 
 
 
 
 You also set the top and bottom margins to 0, which is unrelated. Better putting- margin-left: auto; margin-right: autoI think.
 
 – Emmanuel Touzery
 Feb 8 '14 at 22:45
 
 
 
 
 
 
 
 12
 
 
 
 
 
 Not necessarily- margin:0 auto: it can be- margin: <whatever_vertical_margin_you_need> autosecond being the horizontal margin.
 
 – YakovL
 May 3 '16 at 19:07
 
 
 
 
 
|
show 14 more comments
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 IE8+, 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:
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer" style="width:100%">
  <div id="inner">Foo foo</div>
</div>
 
 
 10
 
 
 
 
 
 For the vertical centering I usually use "line-height" (line-height == height). This is simple and nice but it's only working with a one line content text :)
 
 – Nicolas Guillaume
 Jun 23 '10 at 12:36
 
 
 
 
 
 90
 
 
 
 
 
 You have to use the !DOCTYPE tag on your html page to make it work well on IE.
 
 – Fabio
 Jan 28 '12 at 14:23
 
 
 
 
 
 14
 
 
 
 
 
 Note that it may be necessary to add "float:none;" for the #inner.
 
 – Mert Mertce
 Sep 27 '13 at 8:30
 
 
 
 
 
 14
 
 
 
 
 
 You also set the top and bottom margins to 0, which is unrelated. Better putting- margin-left: auto; margin-right: autoI think.
 
 – Emmanuel Touzery
 Feb 8 '14 at 22:45
 
 
 
 
 
 
 
 12
 
 
 
 
 
 Not necessarily- margin:0 auto: it can be- margin: <whatever_vertical_margin_you_need> autosecond being the horizontal margin.
 
 – YakovL
 May 3 '16 at 19:07
 
 
 
 
 
|
show 14 more comments
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 IE8+, 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:
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer" style="width:100%">
  <div id="inner">Foo foo</div>
</div>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 IE8+, 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:
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer" style="width:100%">
  <div id="inner">Foo foo</div>
</div>#inner {
  display: table;
  margin: 0 auto;
}<div id="outer" style="width:100%">
  <div id="inner">Foo foo</div>
</div>#inner {
  display: table;
  margin: 0 auto;
}<div id="outer" style="width:100%">
  <div id="inner">Foo foo</div>
</div>edited Feb 28 '17 at 13:44
community wiki
11 revs, 7 users 31%
bharadhwaj
 
 
 10
 
 
 
 
 
 For the vertical centering I usually use "line-height" (line-height == height). This is simple and nice but it's only working with a one line content text :)
 
 – Nicolas Guillaume
 Jun 23 '10 at 12:36
 
 
 
 
 
 90
 
 
 
 
 
 You have to use the !DOCTYPE tag on your html page to make it work well on IE.
 
 – Fabio
 Jan 28 '12 at 14:23
 
 
 
 
 
 14
 
 
 
 
 
 Note that it may be necessary to add "float:none;" for the #inner.
 
 – Mert Mertce
 Sep 27 '13 at 8:30
 
 
 
 
 
 14
 
 
 
 
 
 You also set the top and bottom margins to 0, which is unrelated. Better putting- margin-left: auto; margin-right: autoI think.
 
 – Emmanuel Touzery
 Feb 8 '14 at 22:45
 
 
 
 
 
 
 
 12
 
 
 
 
 
 Not necessarily- margin:0 auto: it can be- margin: <whatever_vertical_margin_you_need> autosecond being the horizontal margin.
 
 – YakovL
 May 3 '16 at 19:07
 
 
 
 
 
|
show 14 more comments
 
 
 10
 
 
 
 
 
 For the vertical centering I usually use "line-height" (line-height == height). This is simple and nice but it's only working with a one line content text :)
 
 – Nicolas Guillaume
 Jun 23 '10 at 12:36
 
 
 
 
 
 90
 
 
 
 
 
 You have to use the !DOCTYPE tag on your html page to make it work well on IE.
 
 – Fabio
 Jan 28 '12 at 14:23
 
 
 
 
 
 14
 
 
 
 
 
 Note that it may be necessary to add "float:none;" for the #inner.
 
 – Mert Mertce
 Sep 27 '13 at 8:30
 
 
 
 
 
 14
 
 
 
 
 
 You also set the top and bottom margins to 0, which is unrelated. Better putting- margin-left: auto; margin-right: autoI think.
 
 – Emmanuel Touzery
 Feb 8 '14 at 22:45
 
 
 
 
 
 
 
 12
 
 
 
 
 
 Not necessarily- margin:0 auto: it can be- margin: <whatever_vertical_margin_you_need> autosecond being the horizontal margin.
 
 – YakovL
 May 3 '16 at 19:07
 
 
 
 
 
10
10
For the vertical centering I usually use "line-height" (line-height == height). This is simple and nice but it's only working with a one line content text :)
– Nicolas Guillaume
Jun 23 '10 at 12:36
For the vertical centering I usually use "line-height" (line-height == height). This is simple and nice but it's only working with a one line content text :)
– Nicolas Guillaume
Jun 23 '10 at 12:36
90
90
You have to use the !DOCTYPE tag on your html page to make it work well on IE.
– Fabio
Jan 28 '12 at 14:23
You have to use the !DOCTYPE tag on your html page to make it work well on IE.
– Fabio
Jan 28 '12 at 14:23
14
14
Note that it may be necessary to add "float:none;" for the #inner.
– Mert Mertce
Sep 27 '13 at 8:30
Note that it may be necessary to add "float:none;" for the #inner.
– Mert Mertce
Sep 27 '13 at 8:30
14
14
You also set the top and bottom margins to 0, which is unrelated. Better putting
margin-left: auto; margin-right: auto I think.– Emmanuel Touzery
Feb 8 '14 at 22:45
You also set the top and bottom margins to 0, which is unrelated. Better putting
margin-left: auto; margin-right: auto I think.– Emmanuel Touzery
Feb 8 '14 at 22:45
12
12
Not necessarily
margin:0 auto: it can be margin: <whatever_vertical_margin_you_need> auto second being the horizontal margin.– YakovL
May 3 '16 at 19:07
Not necessarily
margin:0 auto: it can be margin: <whatever_vertical_margin_you_need> auto second being the horizontal margin.– YakovL
May 3 '16 at 19:07
|
show 14 more comments
If you don't want to set a fixed width on the inner div you could do something like this:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  display: inline-block;
}<div id="outer">  
    <div id="inner">Foo foo</div>
</div>That makes the inner div into an inline element that can be centered with text-align.
 
 
 12
 
 
 
 
 
 @SabaAhang the correct syntax for that would be- float: none;and is probably only needed because #inner has inherited a- floatof either- leftor- rightfrom somewhere else in your CSS.
 
 – Doug McLean
 Nov 12 '15 at 9:21
 
 
 
 
 
 5
 
 
 
 
 
 This is a nice solution. Just keep in mind that inner will inherit- text-alignso you may want to set inner's- text-alignto- initialor some other value.
 
 – pmoleri
 Nov 18 '16 at 21:52
 
 
 
add a comment |
If you don't want to set a fixed width on the inner div you could do something like this:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  display: inline-block;
}<div id="outer">  
    <div id="inner">Foo foo</div>
</div>That makes the inner div into an inline element that can be centered with text-align.
 
 
 12
 
 
 
 
 
 @SabaAhang the correct syntax for that would be- float: none;and is probably only needed because #inner has inherited a- floatof either- leftor- rightfrom somewhere else in your CSS.
 
 – Doug McLean
 Nov 12 '15 at 9:21
 
 
 
 
 
 5
 
 
 
 
 
 This is a nice solution. Just keep in mind that inner will inherit- text-alignso you may want to set inner's- text-alignto- initialor some other value.
 
 – pmoleri
 Nov 18 '16 at 21:52
 
 
 
add a comment |
If you don't want to set a fixed width on the inner div you could do something like this:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  display: inline-block;
}<div id="outer">  
    <div id="inner">Foo foo</div>
</div>That makes the inner div into an inline element that can be centered with text-align.
If you don't want to set a fixed width on the inner div you could do something like this:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  display: inline-block;
}<div id="outer">  
    <div id="inner">Foo foo</div>
</div>That makes the inner div into an inline element that can be centered with text-align.
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  display: inline-block;
}<div id="outer">  
    <div id="inner">Foo foo</div>
</div>#outer {
  width: 100%;
  text-align: center;
}
#inner {
  display: inline-block;
}<div id="outer">  
    <div id="inner">Foo foo</div>
</div>edited Nov 22 '17 at 11:34
community wiki
5 revs, 4 users 62%
Alfred
 
 
 12
 
 
 
 
 
 @SabaAhang the correct syntax for that would be- float: none;and is probably only needed because #inner has inherited a- floatof either- leftor- rightfrom somewhere else in your CSS.
 
 – Doug McLean
 Nov 12 '15 at 9:21
 
 
 
 
 
 5
 
 
 
 
 
 This is a nice solution. Just keep in mind that inner will inherit- text-alignso you may want to set inner's- text-alignto- initialor some other value.
 
 – pmoleri
 Nov 18 '16 at 21:52
 
 
 
add a comment |
 
 
 12
 
 
 
 
 
 @SabaAhang the correct syntax for that would be- float: none;and is probably only needed because #inner has inherited a- floatof either- leftor- rightfrom somewhere else in your CSS.
 
 – Doug McLean
 Nov 12 '15 at 9:21
 
 
 
 
 
 5
 
 
 
 
 
 This is a nice solution. Just keep in mind that inner will inherit- text-alignso you may want to set inner's- text-alignto- initialor some other value.
 
 – pmoleri
 Nov 18 '16 at 21:52
 
 
 
12
12
@SabaAhang the correct syntax for that would be
float: none; and is probably only needed because #inner has inherited a float of either left or right from somewhere else in your CSS.– Doug McLean
Nov 12 '15 at 9:21
@SabaAhang the correct syntax for that would be
float: none; and is probably only needed because #inner has inherited a float of either left or right from somewhere else in your CSS.– Doug McLean
Nov 12 '15 at 9:21
5
5
This is a nice solution. Just keep in mind that inner will inherit
text-align so you may want to set inner's text-align to initial or some other value.– pmoleri
Nov 18 '16 at 21:52
This is a nice solution. Just keep in mind that inner will inherit
text-align so you may want to set inner's text-align to initial or some other value.– pmoleri
Nov 18 '16 at 21:52
add a comment |
The best approaches are with CSS 3.
Box model:
#outer{
    width: 100%;
    /* Firefox */
    display: -moz-box;
    -moz-box-pack: center;
    -moz-box-align: center;
    /* Safari and Chrome */
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;
    /* W3C */
    display: box;
    box-pack: center;
    box-align: center;
}
#inner{
    width: 50%;
}
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
- Link 2 
- Link 3 
- Link 4 
And this explains why the box model is the best approach:
- Why is the W3C box model considered better?
 
 
 
 
 
 
 
 it also works for me when inner div have float: left;
 
 – Tareq
 Nov 12 '12 at 6:30
 
 
 
 
 
 22
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:51
 
 
 
 
 
 4
 
 
 
 
 
 Safari, as of now, still requires- -webkitflags for flexbox (- display: -webkit-flex;and- -webkit-align-items: center;and- -webkit-justify-content: center;)
 
 – Joseph Hansen
 Jul 23 '15 at 15:59
 
 
 
add a comment |
The best approaches are with CSS 3.
Box model:
#outer{
    width: 100%;
    /* Firefox */
    display: -moz-box;
    -moz-box-pack: center;
    -moz-box-align: center;
    /* Safari and Chrome */
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;
    /* W3C */
    display: box;
    box-pack: center;
    box-align: center;
}
#inner{
    width: 50%;
}
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
- Link 2 
- Link 3 
- Link 4 
And this explains why the box model is the best approach:
- Why is the W3C box model considered better?
 
 
 
 
 
 
 
 it also works for me when inner div have float: left;
 
 – Tareq
 Nov 12 '12 at 6:30
 
 
 
 
 
 22
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:51
 
 
 
 
 
 4
 
 
 
 
 
 Safari, as of now, still requires- -webkitflags for flexbox (- display: -webkit-flex;and- -webkit-align-items: center;and- -webkit-justify-content: center;)
 
 – Joseph Hansen
 Jul 23 '15 at 15:59
 
 
 
add a comment |
The best approaches are with CSS 3.
Box model:
#outer{
    width: 100%;
    /* Firefox */
    display: -moz-box;
    -moz-box-pack: center;
    -moz-box-align: center;
    /* Safari and Chrome */
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;
    /* W3C */
    display: box;
    box-pack: center;
    box-align: center;
}
#inner{
    width: 50%;
}
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
- Link 2 
- Link 3 
- Link 4 
And this explains why the box model is the best approach:
- Why is the W3C box model considered better?
The best approaches are with CSS 3.
Box model:
#outer{
    width: 100%;
    /* Firefox */
    display: -moz-box;
    -moz-box-pack: center;
    -moz-box-align: center;
    /* Safari and Chrome */
    display: -webkit-box;
    -webkit-box-pack: center;
    -webkit-box-align: center;
    /* W3C */
    display: box;
    box-pack: center;
    box-align: center;
}
#inner{
    width: 50%;
}
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
- Link 2 
- Link 3 
- Link 4 
And this explains why the box model is the best approach:
- Why is the W3C box model considered better?
edited Mar 17 '18 at 20:07
community wiki
12 revs, 7 users 58%
Konga Raju
 
 
 
 
 
 
 
 it also works for me when inner div have float: left;
 
 – Tareq
 Nov 12 '12 at 6:30
 
 
 
 
 
 22
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:51
 
 
 
 
 
 4
 
 
 
 
 
 Safari, as of now, still requires- -webkitflags for flexbox (- display: -webkit-flex;and- -webkit-align-items: center;and- -webkit-justify-content: center;)
 
 – Joseph Hansen
 Jul 23 '15 at 15:59
 
 
 
add a comment |
 
 
 
 
 
 
 
 it also works for me when inner div have float: left;
 
 – Tareq
 Nov 12 '12 at 6:30
 
 
 
 
 
 22
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:51
 
 
 
 
 
 4
 
 
 
 
 
 Safari, as of now, still requires- -webkitflags for flexbox (- display: -webkit-flex;and- -webkit-align-items: center;and- -webkit-justify-content: center;)
 
 – Joseph Hansen
 Jul 23 '15 at 15:59
 
 
 
it also works for me when inner div have float: left;
– Tareq
Nov 12 '12 at 6:30
it also works for me when inner div have float: left;
– Tareq
Nov 12 '12 at 6:30
22
22
Make sure you read this answer first before you go about implementing this solution.
– cimmanon
Apr 24 '13 at 18:51
Make sure you read this answer first before you go about implementing this solution.
– cimmanon
Apr 24 '13 at 18:51
4
4
Safari, as of now, still requires
-webkit flags for flexbox (display: -webkit-flex; and -webkit-align-items: center; and -webkit-justify-content: center;)– Joseph Hansen
Jul 23 '15 at 15:59
Safari, as of now, still requires
-webkit flags for flexbox (display: -webkit-flex; and -webkit-align-items: center; and -webkit-justify-content: center;)– Joseph Hansen
Jul 23 '15 at 15:59
add a comment |
Suppose that your div is 200px 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/
 
 
 
 
 
 
 
 This doesn't work in Safari
 
 – cesards
 Aug 8 '15 at 9:05
 
 
 
 
 
 
 
 
 
 
 I don't like this solution because when the inner element is too broad for the screen, you can't scroll over the whole element horizontally. margin: 0 auto works better.
 
 – Aloso
 Dec 30 '15 at 4:02
 
 
 
 
 
 
 
 
 
 
 why do u put margin left: -100, this will not work
 
 – Robert Limanto
 Nov 29 '16 at 23:51
 
 
 
 
 
 
 
 
 
 
 I've read that it's the only method that will work in IE6/7
 
 – Andy
 Oct 31 '17 at 7:25
 
 
 
 
 
 
 
 
 
 
 margin-left: auto; margin-right: auto; centres a block level element
 
 – crystallinering
 Nov 10 '17 at 19:15
 
 
 
|
show 1 more comment
Suppose that your div is 200px 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/
 
 
 
 
 
 
 
 This doesn't work in Safari
 
 – cesards
 Aug 8 '15 at 9:05
 
 
 
 
 
 
 
 
 
 
 I don't like this solution because when the inner element is too broad for the screen, you can't scroll over the whole element horizontally. margin: 0 auto works better.
 
 – Aloso
 Dec 30 '15 at 4:02
 
 
 
 
 
 
 
 
 
 
 why do u put margin left: -100, this will not work
 
 – Robert Limanto
 Nov 29 '16 at 23:51
 
 
 
 
 
 
 
 
 
 
 I've read that it's the only method that will work in IE6/7
 
 – Andy
 Oct 31 '17 at 7:25
 
 
 
 
 
 
 
 
 
 
 margin-left: auto; margin-right: auto; centres a block level element
 
 – crystallinering
 Nov 10 '17 at 19:15
 
 
 
|
show 1 more comment
Suppose that your div is 200px 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/
Suppose that your div is 200px 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/
edited Sep 25 '17 at 10:48
community wiki
7 revs, 7 users 48%
nuno_cruz
 
 
 
 
 
 
 
 This doesn't work in Safari
 
 – cesards
 Aug 8 '15 at 9:05
 
 
 
 
 
 
 
 
 
 
 I don't like this solution because when the inner element is too broad for the screen, you can't scroll over the whole element horizontally. margin: 0 auto works better.
 
 – Aloso
 Dec 30 '15 at 4:02
 
 
 
 
 
 
 
 
 
 
 why do u put margin left: -100, this will not work
 
 – Robert Limanto
 Nov 29 '16 at 23:51
 
 
 
 
 
 
 
 
 
 
 I've read that it's the only method that will work in IE6/7
 
 – Andy
 Oct 31 '17 at 7:25
 
 
 
 
 
 
 
 
 
 
 margin-left: auto; margin-right: auto; centres a block level element
 
 – crystallinering
 Nov 10 '17 at 19:15
 
 
 
|
show 1 more comment
 
 
 
 
 
 
 
 This doesn't work in Safari
 
 – cesards
 Aug 8 '15 at 9:05
 
 
 
 
 
 
 
 
 
 
 I don't like this solution because when the inner element is too broad for the screen, you can't scroll over the whole element horizontally. margin: 0 auto works better.
 
 – Aloso
 Dec 30 '15 at 4:02
 
 
 
 
 
 
 
 
 
 
 why do u put margin left: -100, this will not work
 
 – Robert Limanto
 Nov 29 '16 at 23:51
 
 
 
 
 
 
 
 
 
 
 I've read that it's the only method that will work in IE6/7
 
 – Andy
 Oct 31 '17 at 7:25
 
 
 
 
 
 
 
 
 
 
 margin-left: auto; margin-right: auto; centres a block level element
 
 – crystallinering
 Nov 10 '17 at 19:15
 
 
 
This doesn't work in Safari
– cesards
Aug 8 '15 at 9:05
This doesn't work in Safari
– cesards
Aug 8 '15 at 9:05
I don't like this solution because when the inner element is too broad for the screen, you can't scroll over the whole element horizontally. margin: 0 auto works better.
– Aloso
Dec 30 '15 at 4:02
I don't like this solution because when the inner element is too broad for the screen, you can't scroll over the whole element horizontally. margin: 0 auto works better.
– Aloso
Dec 30 '15 at 4:02
why do u put margin left: -100, this will not work
– Robert Limanto
Nov 29 '16 at 23:51
why do u put margin left: -100, this will not work
– Robert Limanto
Nov 29 '16 at 23:51
I've read that it's the only method that will work in IE6/7
– Andy
Oct 31 '17 at 7:25
I've read that it's the only method that will work in IE6/7
– Andy
Oct 31 '17 at 7:25
margin-left: auto; margin-right: auto; centres a block level element
– crystallinering
Nov 10 '17 at 19:15
margin-left: auto; margin-right: auto; centres a block level element
– crystallinering
Nov 10 '17 at 19:15
|
show 1 more comment
I've created this example to show how to vertically and horizontally align.
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 re-size your screen.
 
 
 12
 
 
 
 
 
 +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.
 
 – stvnrynlds
 Dec 16 '13 at 18:27
 
 
 
 
 
 
 
 6
 
 
 
 
 
 You cannot use padding within the div, but if you want to give the illusion use a border of the same color.
 
 – Squirrl
 Jul 9 '14 at 11:45
 
 
 
 
 
 
 
 
 
 
 I think for this method to work, you need to set the with and height of inner div
 
 – Nicolas S.Xu
 Nov 29 '15 at 21:39
 
 
 
add a comment |
I've created this example to show how to vertically and horizontally align.
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 re-size your screen.
 
 
 12
 
 
 
 
 
 +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.
 
 – stvnrynlds
 Dec 16 '13 at 18:27
 
 
 
 
 
 
 
 6
 
 
 
 
 
 You cannot use padding within the div, but if you want to give the illusion use a border of the same color.
 
 – Squirrl
 Jul 9 '14 at 11:45
 
 
 
 
 
 
 
 
 
 
 I think for this method to work, you need to set the with and height of inner div
 
 – Nicolas S.Xu
 Nov 29 '15 at 21:39
 
 
 
add a comment |
I've created this example to show how to vertically and horizontally align.
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 re-size your screen.
I've created this example to show how to vertically and horizontally align.
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 re-size your screen.
edited Aug 18 '17 at 10:35
community wiki
4 revs, 4 users 60%
Tom Maton
 
 
 12
 
 
 
 
 
 +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.
 
 – stvnrynlds
 Dec 16 '13 at 18:27
 
 
 
 
 
 
 
 6
 
 
 
 
 
 You cannot use padding within the div, but if you want to give the illusion use a border of the same color.
 
 – Squirrl
 Jul 9 '14 at 11:45
 
 
 
 
 
 
 
 
 
 
 I think for this method to work, you need to set the with and height of inner div
 
 – Nicolas S.Xu
 Nov 29 '15 at 21:39
 
 
 
add a comment |
 
 
 12
 
 
 
 
 
 +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.
 
 – stvnrynlds
 Dec 16 '13 at 18:27
 
 
 
 
 
 
 
 6
 
 
 
 
 
 You cannot use padding within the div, but if you want to give the illusion use a border of the same color.
 
 – Squirrl
 Jul 9 '14 at 11:45
 
 
 
 
 
 
 
 
 
 
 I think for this method to work, you need to set the with and height of inner div
 
 – Nicolas S.Xu
 Nov 29 '15 at 21:39
 
 
 
12
12
+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.
– stvnrynlds
Dec 16 '13 at 18:27
+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.
– stvnrynlds
Dec 16 '13 at 18:27
6
6
You cannot use padding within the div, but if you want to give the illusion use a border of the same color.
– Squirrl
Jul 9 '14 at 11:45
You cannot use padding within the div, but if you want to give the illusion use a border of the same color.
– Squirrl
Jul 9 '14 at 11:45
I think for this method to work, you need to set the with and height of inner div
– Nicolas S.Xu
Nov 29 '15 at 21:39
I think for this method to work, you need to set the with and height of inner div
– Nicolas S.Xu
Nov 29 '15 at 21:39
add a comment |
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 */
}
.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 */
}
* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
}
.box {
  height: 200px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  border: 2px solid tomato;
}
.box div {
  margin: 0 10px;
  width: 100px;
}
.item1 {
  height: 50px;
  background: pink;
}
.item2 {
  background: brown;
  height: 100px;
}
.item3 {
  height: 150px;
  background: orange;
}<div class="box">
  <div class="item1">A</div>
  <div class="item2">B</div>
  <div class="item3">C</div>
</div>If you need to support older browsers which use older syntax for flexbox here's a good place to look.
 
 
 
 
 
 
 
 what do you mean by "syntax is outdated", is it deprecated?
 
 – Konga Raju
 Sep 6 '13 at 10:18
 
 
 
 
 
 4
 
 
 
 
 
 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/…
 
 – cimmanon
 Oct 1 '13 at 20:33
 
 
 
 
 
 
 
 
 
 
 This worked for me in Chrome when Justin Poliey's version didn't.
 
 – Vern Jensen
 Jun 29 '16 at 2:50
 
 
 
 
 
 
 
 
 
 
 Isn't the "justify-content: center;" for the vertical alignment and the "align-items: center;" for the horizontal alignment?
 
 – Wouter Vanherck
 Mar 22 '17 at 12:04
 
 
 
 
 
 3
 
 
 
 
 
 @WouterVanherck it depends on the- flex-directionvalue. If it is 'row' (the default) - then- justify-content: center;is for the horizontal alignment (like I mentioned in the answer) If it is 'column' - then- justify-content: center;is for the vertical alignment.
 
 – Danield
 Mar 22 '17 at 12:29
 
 
 
add a comment |
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 */
}
.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 */
}
* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
}
.box {
  height: 200px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  border: 2px solid tomato;
}
.box div {
  margin: 0 10px;
  width: 100px;
}
.item1 {
  height: 50px;
  background: pink;
}
.item2 {
  background: brown;
  height: 100px;
}
.item3 {
  height: 150px;
  background: orange;
}<div class="box">
  <div class="item1">A</div>
  <div class="item2">B</div>
  <div class="item3">C</div>
</div>If you need to support older browsers which use older syntax for flexbox here's a good place to look.
 
 
 
 
 
 
 
 what do you mean by "syntax is outdated", is it deprecated?
 
 – Konga Raju
 Sep 6 '13 at 10:18
 
 
 
 
 
 4
 
 
 
 
 
 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/…
 
 – cimmanon
 Oct 1 '13 at 20:33
 
 
 
 
 
 
 
 
 
 
 This worked for me in Chrome when Justin Poliey's version didn't.
 
 – Vern Jensen
 Jun 29 '16 at 2:50
 
 
 
 
 
 
 
 
 
 
 Isn't the "justify-content: center;" for the vertical alignment and the "align-items: center;" for the horizontal alignment?
 
 – Wouter Vanherck
 Mar 22 '17 at 12:04
 
 
 
 
 
 3
 
 
 
 
 
 @WouterVanherck it depends on the- flex-directionvalue. If it is 'row' (the default) - then- justify-content: center;is for the horizontal alignment (like I mentioned in the answer) If it is 'column' - then- justify-content: center;is for the vertical alignment.
 
 – Danield
 Mar 22 '17 at 12:29
 
 
 
add a comment |
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 */
}
.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 */
}
* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
}
.box {
  height: 200px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  border: 2px solid tomato;
}
.box div {
  margin: 0 10px;
  width: 100px;
}
.item1 {
  height: 50px;
  background: pink;
}
.item2 {
  background: brown;
  height: 100px;
}
.item3 {
  height: 150px;
  background: orange;
}<div class="box">
  <div class="item1">A</div>
  <div class="item2">B</div>
  <div class="item3">C</div>
</div>If you need to support older browsers which use older syntax for flexbox here's a good place to look.
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 */
}
.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 */
}
* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
}
.box {
  height: 200px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  border: 2px solid tomato;
}
.box div {
  margin: 0 10px;
  width: 100px;
}
.item1 {
  height: 50px;
  background: pink;
}
.item2 {
  background: brown;
  height: 100px;
}
.item3 {
  height: 150px;
  background: orange;
}<div class="box">
  <div class="item1">A</div>
  <div class="item2">B</div>
  <div class="item3">C</div>
</div>If you need to support older browsers which use older syntax for flexbox here's a good place to look.
.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 */
}
* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
}
.box {
  height: 200px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  border: 2px solid tomato;
}
.box div {
  margin: 0 10px;
  width: 100px;
}
.item1 {
  height: 50px;
  background: pink;
}
.item2 {
  background: brown;
  height: 100px;
}
.item3 {
  height: 150px;
  background: orange;
}<div class="box">
  <div class="item1">A</div>
  <div class="item2">B</div>
  <div class="item3">C</div>
</div>.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 */
}
* {
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
}
.box {
  height: 200px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  border: 2px solid tomato;
}
.box div {
  margin: 0 10px;
  width: 100px;
}
.item1 {
  height: 50px;
  background: pink;
}
.item2 {
  background: brown;
  height: 100px;
}
.item3 {
  height: 150px;
  background: orange;
}<div class="box">
  <div class="item1">A</div>
  <div class="item2">B</div>
  <div class="item3">C</div>
</div>edited Mar 17 '18 at 20:14
community wiki
8 revs, 4 users 86%
Danield
 
 
 
 
 
 
 
 what do you mean by "syntax is outdated", is it deprecated?
 
 – Konga Raju
 Sep 6 '13 at 10:18
 
 
 
 
 
 4
 
 
 
 
 
 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/…
 
 – cimmanon
 Oct 1 '13 at 20:33
 
 
 
 
 
 
 
 
 
 
 This worked for me in Chrome when Justin Poliey's version didn't.
 
 – Vern Jensen
 Jun 29 '16 at 2:50
 
 
 
 
 
 
 
 
 
 
 Isn't the "justify-content: center;" for the vertical alignment and the "align-items: center;" for the horizontal alignment?
 
 – Wouter Vanherck
 Mar 22 '17 at 12:04
 
 
 
 
 
 3
 
 
 
 
 
 @WouterVanherck it depends on the- flex-directionvalue. If it is 'row' (the default) - then- justify-content: center;is for the horizontal alignment (like I mentioned in the answer) If it is 'column' - then- justify-content: center;is for the vertical alignment.
 
 – Danield
 Mar 22 '17 at 12:29
 
 
 
add a comment |
 
 
 
 
 
 
 
 what do you mean by "syntax is outdated", is it deprecated?
 
 – Konga Raju
 Sep 6 '13 at 10:18
 
 
 
 
 
 4
 
 
 
 
 
 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/…
 
 – cimmanon
 Oct 1 '13 at 20:33
 
 
 
 
 
 
 
 
 
 
 This worked for me in Chrome when Justin Poliey's version didn't.
 
 – Vern Jensen
 Jun 29 '16 at 2:50
 
 
 
 
 
 
 
 
 
 
 Isn't the "justify-content: center;" for the vertical alignment and the "align-items: center;" for the horizontal alignment?
 
 – Wouter Vanherck
 Mar 22 '17 at 12:04
 
 
 
 
 
 3
 
 
 
 
 
 @WouterVanherck it depends on the- flex-directionvalue. If it is 'row' (the default) - then- justify-content: center;is for the horizontal alignment (like I mentioned in the answer) If it is 'column' - then- justify-content: center;is for the vertical alignment.
 
 – Danield
 Mar 22 '17 at 12:29
 
 
 
what do you mean by "syntax is outdated", is it deprecated?
– Konga Raju
Sep 6 '13 at 10:18
what do you mean by "syntax is outdated", is it deprecated?
– Konga Raju
Sep 6 '13 at 10:18
4
4
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/…
– cimmanon
Oct 1 '13 at 20:33
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/…
– cimmanon
Oct 1 '13 at 20:33
This worked for me in Chrome when Justin Poliey's version didn't.
– Vern Jensen
Jun 29 '16 at 2:50
This worked for me in Chrome when Justin Poliey's version didn't.
– Vern Jensen
Jun 29 '16 at 2:50
Isn't the "justify-content: center;" for the vertical alignment and the "align-items: center;" for the horizontal alignment?
– Wouter Vanherck
Mar 22 '17 at 12:04
Isn't the "justify-content: center;" for the vertical alignment and the "align-items: center;" for the horizontal alignment?
– Wouter Vanherck
Mar 22 '17 at 12:04
3
3
@WouterVanherck it depends on the
flex-direction value. If it is 'row' (the default) - then justify-content: center;  is for the horizontal alignment (like I mentioned in the answer) If it is 'column' - then justify-content: center; is for the vertical alignment.– Danield
Mar 22 '17 at 12:29
@WouterVanherck it depends on the
flex-direction value. If it is 'row' (the default) - then justify-content: center;  is for the horizontal alignment (like I mentioned in the answer) If it is 'column' - then justify-content: center; is for the vertical alignment.– Danield
Mar 22 '17 at 12:29
add a comment |
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;
}
 
 
 3
 
 
 
 
 
 same requirements as display:inline-block too (quirksmode.org/css/display.html)
 
 – montrealmike
 Sep 11 '12 at 15:09
 
 
 
 
 
 
 
 
 
 
 I used this, too, but I've never encountered- display: table;before. What does it do?
 
 – Matt Cremeens
 Jul 31 '17 at 11:25
 
 
 
add a comment |
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;
}
 
 
 3
 
 
 
 
 
 same requirements as display:inline-block too (quirksmode.org/css/display.html)
 
 – montrealmike
 Sep 11 '12 at 15:09
 
 
 
 
 
 
 
 
 
 
 I used this, too, but I've never encountered- display: table;before. What does it do?
 
 – Matt Cremeens
 Jul 31 '17 at 11:25
 
 
 
add a comment |
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;
}
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;
}
edited Oct 29 '13 at 17:23
community wiki
Salman Abbas
 
 
 3
 
 
 
 
 
 same requirements as display:inline-block too (quirksmode.org/css/display.html)
 
 – montrealmike
 Sep 11 '12 at 15:09
 
 
 
 
 
 
 
 
 
 
 I used this, too, but I've never encountered- display: table;before. What does it do?
 
 – Matt Cremeens
 Jul 31 '17 at 11:25
 
 
 
add a comment |
 
 
 3
 
 
 
 
 
 same requirements as display:inline-block too (quirksmode.org/css/display.html)
 
 – montrealmike
 Sep 11 '12 at 15:09
 
 
 
 
 
 
 
 
 
 
 I used this, too, but I've never encountered- display: table;before. What does it do?
 
 – Matt Cremeens
 Jul 31 '17 at 11:25
 
 
 
3
3
same requirements as display:inline-block too (quirksmode.org/css/display.html)
– montrealmike
Sep 11 '12 at 15:09
same requirements as display:inline-block too (quirksmode.org/css/display.html)
– montrealmike
Sep 11 '12 at 15:09
I used this, too, but I've never encountered
display: table; before. What does it do?– Matt Cremeens
Jul 31 '17 at 11:25
I used this, too, but I've never encountered
display: table; before. What does it do?– Matt Cremeens
Jul 31 '17 at 11:25
add a comment |
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.)
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}<div class="content">This works with any content</div>Tinker with it further on Codepen or on JSBin.
add a comment |
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.)
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}<div class="content">This works with any content</div>Tinker with it further on Codepen or on JSBin.
add a comment |
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.)
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}<div class="content">This works with any content</div>Tinker with it further on Codepen or on JSBin.
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.)
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}<div class="content">This works with any content</div>Tinker with it further on Codepen or on JSBin.
.content {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}<div class="content">This works with any content</div>.content {
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}<div class="content">This works with any content</div>edited Mar 17 '18 at 18:49
community wiki
6 revs, 4 users 81%
iamnotsam
add a comment |
add a comment |
It cannot be centered if you don't give it a width, otherwise it will take, by default the whole horizontal space.
 
 
 44
 
 
 
 
 
 and if you don't know the width? Say because the content is dynamic?
 
 – gman
 Jun 2 '11 at 15:45
 
 
 
 
 
 
 
 
 
 
 max-width? what about that?
 
 – Will Hoskings
 Mar 17 '18 at 22:32
 
 
 
add a comment |
It cannot be centered if you don't give it a width, otherwise it will take, by default the whole horizontal space.
 
 
 44
 
 
 
 
 
 and if you don't know the width? Say because the content is dynamic?
 
 – gman
 Jun 2 '11 at 15:45
 
 
 
 
 
 
 
 
 
 
 max-width? what about that?
 
 – Will Hoskings
 Mar 17 '18 at 22:32
 
 
 
add a comment |
It cannot be centered if you don't give it a width, otherwise it will take, by default the whole horizontal space.
It cannot be centered if you don't give it a width, otherwise it will take, by default the whole horizontal space.
edited May 9 '11 at 17:23
community wiki
gizmo
 
 
 44
 
 
 
 
 
 and if you don't know the width? Say because the content is dynamic?
 
 – gman
 Jun 2 '11 at 15:45
 
 
 
 
 
 
 
 
 
 
 max-width? what about that?
 
 – Will Hoskings
 Mar 17 '18 at 22:32
 
 
 
add a comment |
 
 
 44
 
 
 
 
 
 and if you don't know the width? Say because the content is dynamic?
 
 – gman
 Jun 2 '11 at 15:45
 
 
 
 
 
 
 
 
 
 
 max-width? what about that?
 
 – Will Hoskings
 Mar 17 '18 at 22:32
 
 
 
44
44
and if you don't know the width? Say because the content is dynamic?
– gman
Jun 2 '11 at 15:45
and if you don't know the width? Say because the content is dynamic?
– gman
Jun 2 '11 at 15:45
max-width? what about that?
– Will Hoskings
Mar 17 '18 at 22:32
max-width? what about that?
– Will Hoskings
Mar 17 '18 at 22:32
add a comment |
CSS3's box-align property
#outer {
    width:100%;
    height:100%;
    display:box;
    box-orient:horizontal;
    box-pack:center;
    box-align:center;
}
 
 
 7
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:52
 
 
 
add a comment |
CSS3's box-align property
#outer {
    width:100%;
    height:100%;
    display:box;
    box-orient:horizontal;
    box-pack:center;
    box-align:center;
}
 
 
 7
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:52
 
 
 
add a comment |
CSS3's box-align property
#outer {
    width:100%;
    height:100%;
    display:box;
    box-orient:horizontal;
    box-pack:center;
    box-align:center;
}
CSS3's box-align property
#outer {
    width:100%;
    height:100%;
    display:box;
    box-orient:horizontal;
    box-pack:center;
    box-align:center;
}
edited Oct 29 '13 at 17:23
community wiki
neoneye
 
 
 7
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:52
 
 
 
add a comment |
 
 
 7
 
 
 
 
 
 Make sure you read this answer first before you go about implementing this solution.
 
 – cimmanon
 Apr 24 '13 at 18:52
 
 
 
7
7
Make sure you read this answer first before you go about implementing this solution.
– cimmanon
Apr 24 '13 at 18:52
Make sure you read this answer first before you go about implementing this solution.
– cimmanon
Apr 24 '13 at 18:52
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 17 '18 at 19:11
community wiki
4 revs, 4 users 33%
Sneakyness
add a comment |
add a comment |
I recently had to center a "hidden" div (ie, display:none;) that had a tabled form within it that needed to be centered on the page. I wrote the following jQuery to display the hidden div & then update the CSS 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 neccessary to display.)
NOTE: I'm sharing this code because Google brought me to this Stack Overflow solution & everything would have worked except that hidden elements don't have any width & can't be resized/centered until after they are displayed.
$(function(){
  $('#inner').show().width($('#innerTable').width()).css('margin','0 auto');
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inner" style="display:none;">
  <form action="">
    <table id="innerTable">
      <tr><td>Name:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="submit"></td></tr>
    </table>
  </form>
</div>add a comment |
I recently had to center a "hidden" div (ie, display:none;) that had a tabled form within it that needed to be centered on the page. I wrote the following jQuery to display the hidden div & then update the CSS 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 neccessary to display.)
NOTE: I'm sharing this code because Google brought me to this Stack Overflow solution & everything would have worked except that hidden elements don't have any width & can't be resized/centered until after they are displayed.
$(function(){
  $('#inner').show().width($('#innerTable').width()).css('margin','0 auto');
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inner" style="display:none;">
  <form action="">
    <table id="innerTable">
      <tr><td>Name:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="submit"></td></tr>
    </table>
  </form>
</div>add a comment |
I recently had to center a "hidden" div (ie, display:none;) that had a tabled form within it that needed to be centered on the page. I wrote the following jQuery to display the hidden div & then update the CSS 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 neccessary to display.)
NOTE: I'm sharing this code because Google brought me to this Stack Overflow solution & everything would have worked except that hidden elements don't have any width & can't be resized/centered until after they are displayed.
$(function(){
  $('#inner').show().width($('#innerTable').width()).css('margin','0 auto');
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inner" style="display:none;">
  <form action="">
    <table id="innerTable">
      <tr><td>Name:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="submit"></td></tr>
    </table>
  </form>
</div>I recently had to center a "hidden" div (ie, display:none;) that had a tabled form within it that needed to be centered on the page. I wrote the following jQuery to display the hidden div & then update the CSS 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 neccessary to display.)
NOTE: I'm sharing this code because Google brought me to this Stack Overflow solution & everything would have worked except that hidden elements don't have any width & can't be resized/centered until after they are displayed.
$(function(){
  $('#inner').show().width($('#innerTable').width()).css('margin','0 auto');
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inner" style="display:none;">
  <form action="">
    <table id="innerTable">
      <tr><td>Name:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="submit"></td></tr>
    </table>
  </form>
</div>$(function(){
  $('#inner').show().width($('#innerTable').width()).css('margin','0 auto');
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inner" style="display:none;">
  <form action="">
    <table id="innerTable">
      <tr><td>Name:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="submit"></td></tr>
    </table>
  </form>
</div>$(function(){
  $('#inner').show().width($('#innerTable').width()).css('margin','0 auto');
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="inner" style="display:none;">
  <form action="">
    <table id="innerTable">
      <tr><td>Name:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="text"></td></tr>
      <tr><td>Email:</td><td><input type="submit"></td></tr>
    </table>
  </form>
</div>edited Aug 4 '16 at 13:09
community wiki
2 revs, 2 users 65%
James Moberg
add a comment |
add a comment |
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.
 
 
 
 
 
 
 
 This may not work if you have other divs below the centered div.
 
 – NoChance
 Jul 26 '18 at 7:59
 
 
 
add a comment |
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.
 
 
 
 
 
 
 
 This may not work if you have other divs below the centered div.
 
 – NoChance
 Jul 26 '18 at 7:59
 
 
 
add a comment |
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.
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.
edited May 31 '16 at 13:19
community wiki
3 revs, 2 users 95%
william44isme
 
 
 
 
 
 
 
 This may not work if you have other divs below the centered div.
 
 – NoChance
 Jul 26 '18 at 7:59
 
 
 
add a comment |
 
 
 
 
 
 
 
 This may not work if you have other divs below the centered div.
 
 – NoChance
 Jul 26 '18 at 7:59
 
 
 
This may not work if you have other divs below the centered div.
– NoChance
Jul 26 '18 at 7:59
This may not work if you have other divs below the centered div.
– NoChance
Jul 26 '18 at 7:59
add a comment |
For Firefox and Chrome:
<div style="width:100%;">
  <div style="width: 50%; margin: 0px auto;">Text</div>
</div>For Internet Explorer, Firefox, and Chrome:
<div style="width:100%; text-align:center;">
  <div style="width: 50%; margin: 0px auto; text-align:left;">Text</div>
</div>The text-align: property is optional for modern browsers, but it is necessary in Internet Explorer Quirks Mode for legacy browsers support.
 
 
 5
 
 
 
 
 
 There is no need for text-align property. It's completely un-necessary.
 
 – Touhid Rahman
 May 23 '13 at 5:29
 
 
 
 
 
 
 
 
 
 
 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)
 
 – heytools
 Nov 4 '17 at 2:02
 
 
 
add a comment |
For Firefox and Chrome:
<div style="width:100%;">
  <div style="width: 50%; margin: 0px auto;">Text</div>
</div>For Internet Explorer, Firefox, and Chrome:
<div style="width:100%; text-align:center;">
  <div style="width: 50%; margin: 0px auto; text-align:left;">Text</div>
</div>The text-align: property is optional for modern browsers, but it is necessary in Internet Explorer Quirks Mode for legacy browsers support.
 
 
 5
 
 
 
 
 
 There is no need for text-align property. It's completely un-necessary.
 
 – Touhid Rahman
 May 23 '13 at 5:29
 
 
 
 
 
 
 
 
 
 
 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)
 
 – heytools
 Nov 4 '17 at 2:02
 
 
 
add a comment |
For Firefox and Chrome:
<div style="width:100%;">
  <div style="width: 50%; margin: 0px auto;">Text</div>
</div>For Internet Explorer, Firefox, and Chrome:
<div style="width:100%; text-align:center;">
  <div style="width: 50%; margin: 0px auto; text-align:left;">Text</div>
</div>The text-align: property is optional for modern browsers, but it is necessary in Internet Explorer Quirks Mode for legacy browsers support.
For Firefox and Chrome:
<div style="width:100%;">
  <div style="width: 50%; margin: 0px auto;">Text</div>
</div>For Internet Explorer, Firefox, and Chrome:
<div style="width:100%; text-align:center;">
  <div style="width: 50%; margin: 0px auto; text-align:left;">Text</div>
</div>The text-align: property is optional for modern browsers, but it is necessary in Internet Explorer Quirks Mode for legacy browsers support.
<div style="width:100%;">
  <div style="width: 50%; margin: 0px auto;">Text</div>
</div><div style="width:100%;">
  <div style="width: 50%; margin: 0px auto;">Text</div>
</div><div style="width:100%; text-align:center;">
  <div style="width: 50%; margin: 0px auto; text-align:left;">Text</div>
</div><div style="width:100%; text-align:center;">
  <div style="width: 50%; margin: 0px auto; text-align:left;">Text</div>
</div>edited Mar 17 '18 at 19:13
community wiki
7 revs, 7 users 47%
ch2o
 
 
 5
 
 
 
 
 
 There is no need for text-align property. It's completely un-necessary.
 
 – Touhid Rahman
 May 23 '13 at 5:29
 
 
 
 
 
 
 
 
 
 
 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)
 
 – heytools
 Nov 4 '17 at 2:02
 
 
 
add a comment |
 
 
 5
 
 
 
 
 
 There is no need for text-align property. It's completely un-necessary.
 
 – Touhid Rahman
 May 23 '13 at 5:29
 
 
 
 
 
 
 
 
 
 
 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)
 
 – heytools
 Nov 4 '17 at 2:02
 
 
 
5
5
There is no need for text-align property. It's completely un-necessary.
– Touhid Rahman
May 23 '13 at 5:29
There is no need for text-align property. It's completely un-necessary.
– Touhid Rahman
May 23 '13 at 5:29
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)
– heytools
Nov 4 '17 at 2:02
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)
– heytools
Nov 4 '17 at 2:02
add a comment |
This is my answer.
#outerDiv {
  width: 500px;
}
#innerDiv {
  width: 200px;
  margin: 0 auto;
}<div id="outerDiv">
  <div id="innerDiv">Inner Content</div>
</div>add a comment |
This is my answer.
#outerDiv {
  width: 500px;
}
#innerDiv {
  width: 200px;
  margin: 0 auto;
}<div id="outerDiv">
  <div id="innerDiv">Inner Content</div>
</div>add a comment |
This is my answer.
#outerDiv {
  width: 500px;
}
#innerDiv {
  width: 200px;
  margin: 0 auto;
}<div id="outerDiv">
  <div id="innerDiv">Inner Content</div>
</div>This is my answer.
#outerDiv {
  width: 500px;
}
#innerDiv {
  width: 200px;
  margin: 0 auto;
}<div id="outerDiv">
  <div id="innerDiv">Inner Content</div>
</div>#outerDiv {
  width: 500px;
}
#innerDiv {
  width: 200px;
  margin: 0 auto;
}<div id="outerDiv">
  <div id="innerDiv">Inner Content</div>
</div>#outerDiv {
  width: 500px;
}
#innerDiv {
  width: 200px;
  margin: 0 auto;
}<div id="outerDiv">
  <div id="innerDiv">Inner Content</div>
</div>edited Feb 24 '17 at 8:37
community wiki
4 revs, 4 users 39%
Ankit Jain
add a comment |
add a comment |
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.
 
 
 2
 
 
 
 
 
 One may need vendor prefixes as well :- -webkit-transform: translate(-50%,0); -moz-transform: translate(-50%,0); -ms-transform: translate(-50%,0); -khtml-transform: translate(-50%,0); -o-transform: translate(-50%,0);
 
 – Skippy le Grand Gourou
 Sep 2 '15 at 13:48
 
 
 
 
 
add a comment |
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.
 
 
 2
 
 
 
 
 
 One may need vendor prefixes as well :- -webkit-transform: translate(-50%,0); -moz-transform: translate(-50%,0); -ms-transform: translate(-50%,0); -khtml-transform: translate(-50%,0); -o-transform: translate(-50%,0);
 
 – Skippy le Grand Gourou
 Sep 2 '15 at 13:48
 
 
 
 
 
add a comment |
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.
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.
edited Mar 17 '18 at 19:08
community wiki
2 revs, 2 users 89%
Kilian Stinson
 
 
 2
 
 
 
 
 
 One may need vendor prefixes as well :- -webkit-transform: translate(-50%,0); -moz-transform: translate(-50%,0); -ms-transform: translate(-50%,0); -khtml-transform: translate(-50%,0); -o-transform: translate(-50%,0);
 
 – Skippy le Grand Gourou
 Sep 2 '15 at 13:48
 
 
 
 
 
add a comment |
 
 
 2
 
 
 
 
 
 One may need vendor prefixes as well :- -webkit-transform: translate(-50%,0); -moz-transform: translate(-50%,0); -ms-transform: translate(-50%,0); -khtml-transform: translate(-50%,0); -o-transform: translate(-50%,0);
 
 – Skippy le Grand Gourou
 Sep 2 '15 at 13:48
 
 
 
 
 
2
2
One may need vendor prefixes as well :
-webkit-transform: translate(-50%,0); 	-moz-transform: translate(-50%,0); 	-ms-transform: translate(-50%,0); 	-khtml-transform: translate(-50%,0); 	-o-transform: translate(-50%,0);– Skippy le Grand Gourou
Sep 2 '15 at 13:48
One may need vendor prefixes as well :
-webkit-transform: translate(-50%,0); 	-moz-transform: translate(-50%,0); 	-ms-transform: translate(-50%,0); 	-khtml-transform: translate(-50%,0); 	-o-transform: translate(-50%,0);– Skippy le Grand Gourou
Sep 2 '15 at 13:48
add a comment |
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 then 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;
}
add a comment |
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 then 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;
}
add a comment |
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 then 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;
}
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 then 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;
}
edited Mar 10 '16 at 16:50
community wiki
3 revs, 3 users 93%
Willem de Wit
add a comment |
add a comment |
I realize I'm pretty late to the game, but this is a very popular question, and I recently found an approach I haven't seen mentioned anywhere here, so I figured I'd document it.
#outer {
    position: absolute;
    left: 50%;
}
#inner {
    position: relative;
    left: -50%;
}
EDIT: both elements must be the same width to function correctly.
 
 
 
 
 
 
 
 Just set this rule for- #inneronly:- #inner { position:relative; left:50%; transform:translateX(-50%); }. This works for any width.
 
 – Jose Rui Santos
 Nov 24 '15 at 10:30
 
 
 
 
 
add a comment |
I realize I'm pretty late to the game, but this is a very popular question, and I recently found an approach I haven't seen mentioned anywhere here, so I figured I'd document it.
#outer {
    position: absolute;
    left: 50%;
}
#inner {
    position: relative;
    left: -50%;
}
EDIT: both elements must be the same width to function correctly.
 
 
 
 
 
 
 
 Just set this rule for- #inneronly:- #inner { position:relative; left:50%; transform:translateX(-50%); }. This works for any width.
 
 – Jose Rui Santos
 Nov 24 '15 at 10:30
 
 
 
 
 
add a comment |
I realize I'm pretty late to the game, but this is a very popular question, and I recently found an approach I haven't seen mentioned anywhere here, so I figured I'd document it.
#outer {
    position: absolute;
    left: 50%;
}
#inner {
    position: relative;
    left: -50%;
}
EDIT: both elements must be the same width to function correctly.
I realize I'm pretty late to the game, but this is a very popular question, and I recently found an approach I haven't seen mentioned anywhere here, so I figured I'd document it.
#outer {
    position: absolute;
    left: 50%;
}
#inner {
    position: relative;
    left: -50%;
}
EDIT: both elements must be the same width to function correctly.
edited Jul 19 '13 at 14:16
community wiki
BenjaminRH
 
 
 
 
 
 
 
 Just set this rule for- #inneronly:- #inner { position:relative; left:50%; transform:translateX(-50%); }. This works for any width.
 
 – Jose Rui Santos
 Nov 24 '15 at 10:30
 
 
 
 
 
add a comment |
 
 
 
 
 
 
 
 Just set this rule for- #inneronly:- #inner { position:relative; left:50%; transform:translateX(-50%); }. This works for any width.
 
 – Jose Rui Santos
 Nov 24 '15 at 10:30
 
 
 
 
 
Just set this rule for
#inner only: #inner { position:relative; left:50%; transform:translateX(-50%); }. This works for any width.– Jose Rui Santos
Nov 24 '15 at 10:30
Just set this rule for
#inner only: #inner { position:relative; left:50%; transform:translateX(-50%); }. This works for any width.– Jose Rui Santos
Nov 24 '15 at 10:30
add a comment |
For example, see this link and the snippet below:
div#outer {
  height: 120px;
  background-color: red;
}
div#inner {
  width: 50%;
  height: 100%;
  background-color: green;
  margin: 0 auto;
  text-align: center; /* For text alignment to center horizontally. */
  line-height: 120px; /* For text alignment to center vertically. */
}<div id="outer" style="width:100%;">
  <div id="inner">Foo foo</div>
</div>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.
add a comment |
For example, see this link and the snippet below:
div#outer {
  height: 120px;
  background-color: red;
}
div#inner {
  width: 50%;
  height: 100%;
  background-color: green;
  margin: 0 auto;
  text-align: center; /* For text alignment to center horizontally. */
  line-height: 120px; /* For text alignment to center vertically. */
}<div id="outer" style="width:100%;">
  <div id="inner">Foo foo</div>
</div>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.
add a comment |
For example, see this link and the snippet below:
div#outer {
  height: 120px;
  background-color: red;
}
div#inner {
  width: 50%;
  height: 100%;
  background-color: green;
  margin: 0 auto;
  text-align: center; /* For text alignment to center horizontally. */
  line-height: 120px; /* For text alignment to center vertically. */
}<div id="outer" style="width:100%;">
  <div id="inner">Foo foo</div>
</div>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.
For example, see this link and the snippet below:
div#outer {
  height: 120px;
  background-color: red;
}
div#inner {
  width: 50%;
  height: 100%;
  background-color: green;
  margin: 0 auto;
  text-align: center; /* For text alignment to center horizontally. */
  line-height: 120px; /* For text alignment to center vertically. */
}<div id="outer" style="width:100%;">
  <div id="inner">Foo foo</div>
</div>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.
div#outer {
  height: 120px;
  background-color: red;
}
div#inner {
  width: 50%;
  height: 100%;
  background-color: green;
  margin: 0 auto;
  text-align: center; /* For text alignment to center horizontally. */
  line-height: 120px; /* For text alignment to center vertically. */
}<div id="outer" style="width:100%;">
  <div id="inner">Foo foo</div>
</div>div#outer {
  height: 120px;
  background-color: red;
}
div#inner {
  width: 50%;
  height: 100%;
  background-color: green;
  margin: 0 auto;
  text-align: center; /* For text alignment to center horizontally. */
  line-height: 120px; /* For text alignment to center vertically. */
}<div id="outer" style="width:100%;">
  <div id="inner">Foo foo</div>
</div>edited Mar 17 '18 at 18:44
community wiki
4 revs, 3 users 63%
Lalit Kumar
add a comment |
add a comment |
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:
.container {
  width: 100%;
  height: 120px;
  background: #CCC;
  text-align: center;
}
.centered-content {
  display: inline-block;
  background: #FFF;
  padding: 20px;
  border: 1px solid #000;
}<div class="container">
  <div class="centered-content">
    Center this!
  </div>
</div>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:
.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;
}<div class="outer-container">
  <div class="inner-container">
    <div class="centered-content">
      Center this!
    </div>
  </div>
</div>See also this Fiddle!
add a comment |
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:
.container {
  width: 100%;
  height: 120px;
  background: #CCC;
  text-align: center;
}
.centered-content {
  display: inline-block;
  background: #FFF;
  padding: 20px;
  border: 1px solid #000;
}<div class="container">
  <div class="centered-content">
    Center this!
  </div>
</div>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:
.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;
}<div class="outer-container">
  <div class="inner-container">
    <div class="centered-content">
      Center this!
    </div>
  </div>
</div>See also this Fiddle!
add a comment |
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:
.container {
  width: 100%;
  height: 120px;
  background: #CCC;
  text-align: center;
}
.centered-content {
  display: inline-block;
  background: #FFF;
  padding: 20px;
  border: 1px solid #000;
}<div class="container">
  <div class="centered-content">
    Center this!
  </div>
</div>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:
.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;
}<div class="outer-container">
  <div class="inner-container">
    <div class="centered-content">
      Center this!
    </div>
  </div>
</div>See also this Fiddle!
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:
.container {
  width: 100%;
  height: 120px;
  background: #CCC;
  text-align: center;
}
.centered-content {
  display: inline-block;
  background: #FFF;
  padding: 20px;
  border: 1px solid #000;
}<div class="container">
  <div class="centered-content">
    Center this!
  </div>
</div>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:
.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;
}<div class="outer-container">
  <div class="inner-container">
    <div class="centered-content">
      Center this!
    </div>
  </div>
</div>See also this Fiddle!
.container {
  width: 100%;
  height: 120px;
  background: #CCC;
  text-align: center;
}
.centered-content {
  display: inline-block;
  background: #FFF;
  padding: 20px;
  border: 1px solid #000;
}<div class="container">
  <div class="centered-content">
    Center this!
  </div>
</div>.container {
  width: 100%;
  height: 120px;
  background: #CCC;
  text-align: center;
}
.centered-content {
  display: inline-block;
  background: #FFF;
  padding: 20px;
  border: 1px solid #000;
}<div class="container">
  <div class="centered-content">
    Center this!
  </div>
</div>.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;
}<div class="outer-container">
  <div class="inner-container">
    <div class="centered-content">
      Center this!
    </div>
  </div>
</div>.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;
}<div class="outer-container">
  <div class="inner-container">
    <div class="centered-content">
      Center this!
    </div>
  </div>
</div>edited Mar 17 '18 at 15:41
community wiki
4 revs, 4 users 75%
John Slegers
add a comment |
add a comment |
The easiest way:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  margin: auto;
  width: 200px;
}<div id="outer">
  <div id="inner">Blabla</div>
</div>
 
 
 1
 
 
 
 
 
 As your fiddle notes, #inner has to have a width set on it.
 
 – Michael Terry
 Feb 5 '15 at 21:06
 
 
 
add a comment |
The easiest way:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  margin: auto;
  width: 200px;
}<div id="outer">
  <div id="inner">Blabla</div>
</div>
 
 
 1
 
 
 
 
 
 As your fiddle notes, #inner has to have a width set on it.
 
 – Michael Terry
 Feb 5 '15 at 21:06
 
 
 
add a comment |
The easiest way:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  margin: auto;
  width: 200px;
}<div id="outer">
  <div id="inner">Blabla</div>
</div>The easiest way:
#outer {
  width: 100%;
  text-align: center;
}
#inner {
  margin: auto;
  width: 200px;
}<div id="outer">
  <div id="inner">Blabla</div>
</div>#outer {
  width: 100%;
  text-align: center;
}
#inner {
  margin: auto;
  width: 200px;
}<div id="outer">
  <div id="inner">Blabla</div>
</div>#outer {
  width: 100%;
  text-align: center;
}
#inner {
  margin: auto;
  width: 200px;
}<div id="outer">
  <div id="inner">Blabla</div>
</div>edited Mar 17 '18 at 18:51
community wiki
4 revs, 3 users 57%
joan16v
 
 
 1
 
 
 
 
 
 As your fiddle notes, #inner has to have a width set on it.
 
 – Michael Terry
 Feb 5 '15 at 21:06
 
 
 
add a comment |
 
 
 1
 
 
 
 
 
 As your fiddle notes, #inner has to have a width set on it.
 
 – Michael Terry
 Feb 5 '15 at 21:06
 
 
 
1
1
As your fiddle notes, #inner has to have a width set on it.
– Michael Terry
Feb 5 '15 at 21:06
As your fiddle notes, #inner has to have a width set on it.
– Michael Terry
Feb 5 '15 at 21:06
add a comment |
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 1000px and 300px respectively. Proceed as follows:
- Wrap .innerinside.center-helper
 
- Make .center-helperan inline block; it becomes the same size as.innermaking it 300px wide.
- Push .center-helper50% right relative to its parent; this places its left at 500px wrt. outer.
- Push .inner50% left relative to its parent; this places its left at -150px wrt. center helper which means its left is at 500 - 150 = 350px wrt. outer.
- Set overflow on .outerto hidden to prevent horizontal scrollbar.
Demo:
body {
  font: medium sans-serif;
}
.outer {
  overflow: hidden;
  background-color: papayawhip;
}
.center-helper {
  display: inline-block;
  position: relative;
  left: 50%;
  background-color: burlywood;
}
.inner {
  display: inline-block;
  position: relative;
  left: -50%;
  background-color: wheat;
}<div class="outer">
  <div class="center-helper">
    <div class="inner">
      <h1>A div with no defined width</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
          Duis condimentum sem non turpis consectetur blandit.<br>
          Donec dictum risus id orci ornare tempor.<br>
          Proin pharetra augue a lorem elementum molestie.<br>
          Nunc nec justo sit amet nisi tempor viverra sit amet a ipsum.</p>
    </div>
  </div>
</div>.outer {
    overflow: hidden;
}
.center-helper {
    float: left;
    position: relative;
    left: 50%;
}
.inner {
    float: left;
    position: relative;
    left: -50%;
}
add a comment |
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 1000px and 300px respectively. Proceed as follows:
- Wrap .innerinside.center-helper
 
- Make .center-helperan inline block; it becomes the same size as.innermaking it 300px wide.
- Push .center-helper50% right relative to its parent; this places its left at 500px wrt. outer.
- Push .inner50% left relative to its parent; this places its left at -150px wrt. center helper which means its left is at 500 - 150 = 350px wrt. outer.
- Set overflow on .outerto hidden to prevent horizontal scrollbar.
Demo:
body {
  font: medium sans-serif;
}
.outer {
  overflow: hidden;
  background-color: papayawhip;
}
.center-helper {
  display: inline-block;
  position: relative;
  left: 50%;
  background-color: burlywood;
}
.inner {
  display: inline-block;
  position: relative;
  left: -50%;
  background-color: wheat;
}<div class="outer">
  <div class="center-helper">
    <div class="inner">
      <h1>A div with no defined width</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
          Duis condimentum sem non turpis consectetur blandit.<br>
          Donec dictum risus id orci ornare tempor.<br>
          Proin pharetra augue a lorem elementum molestie.<br>
          Nunc nec justo sit amet nisi tempor viverra sit amet a ipsum.</p>
    </div>
  </div>
</div>.outer {
    overflow: hidden;
}
.center-helper {
    float: left;
    position: relative;
    left: 50%;
}
.inner {
    float: left;
    position: relative;
    left: -50%;
}
add a comment |
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 1000px and 300px respectively. Proceed as follows:
- Wrap .innerinside.center-helper
 
- Make .center-helperan inline block; it becomes the same size as.innermaking it 300px wide.
- Push .center-helper50% right relative to its parent; this places its left at 500px wrt. outer.
- Push .inner50% left relative to its parent; this places its left at -150px wrt. center helper which means its left is at 500 - 150 = 350px wrt. outer.
- Set overflow on .outerto hidden to prevent horizontal scrollbar.
Demo:
body {
  font: medium sans-serif;
}
.outer {
  overflow: hidden;
  background-color: papayawhip;
}
.center-helper {
  display: inline-block;
  position: relative;
  left: 50%;
  background-color: burlywood;
}
.inner {
  display: inline-block;
  position: relative;
  left: -50%;
  background-color: wheat;
}<div class="outer">
  <div class="center-helper">
    <div class="inner">
      <h1>A div with no defined width</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
          Duis condimentum sem non turpis consectetur blandit.<br>
          Donec dictum risus id orci ornare tempor.<br>
          Proin pharetra augue a lorem elementum molestie.<br>
          Nunc nec justo sit amet nisi tempor viverra sit amet a ipsum.</p>
    </div>
  </div>
</div>.outer {
    overflow: hidden;
}
.center-helper {
    float: left;
    position: relative;
    left: 50%;
}
.inner {
    float: left;
    position: relative;
    left: -50%;
}
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 1000px and 300px respectively. Proceed as follows:
- Wrap .innerinside.center-helper
 
- Make .center-helperan inline block; it becomes the same size as.innermaking it 300px wide.
- Push .center-helper50% right relative to its parent; this places its left at 500px wrt. outer.
- Push .inner50% left relative to its parent; this places its left at -150px wrt. center helper which means its left is at 500 - 150 = 350px wrt. outer.
- Set overflow on .outerto hidden to prevent horizontal scrollbar.
Demo:
body {
  font: medium sans-serif;
}
.outer {
  overflow: hidden;
  background-color: papayawhip;
}
.center-helper {
  display: inline-block;
  position: relative;
  left: 50%;
  background-color: burlywood;
}
.inner {
  display: inline-block;
  position: relative;
  left: -50%;
  background-color: wheat;
}<div class="outer">
  <div class="center-helper">
    <div class="inner">
      <h1>A div with no defined width</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
          Duis condimentum sem non turpis consectetur blandit.<br>
          Donec dictum risus id orci ornare tempor.<br>
          Proin pharetra augue a lorem elementum molestie.<br>
          Nunc nec justo sit amet nisi tempor viverra sit amet a ipsum.</p>
    </div>
  </div>
</div>.outer {
    overflow: hidden;
}
.center-helper {
    float: left;
    position: relative;
    left: 50%;
}
.inner {
    float: left;
    position: relative;
    left: -50%;
}
body {
  font: medium sans-serif;
}
.outer {
  overflow: hidden;
  background-color: papayawhip;
}
.center-helper {
  display: inline-block;
  position: relative;
  left: 50%;
  background-color: burlywood;
}
.inner {
  display: inline-block;
  position: relative;
  left: -50%;
  background-color: wheat;
}<div class="outer">
  <div class="center-helper">
    <div class="inner">
      <h1>A div with no defined width</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
          Duis condimentum sem non turpis consectetur blandit.<br>
          Donec dictum risus id orci ornare tempor.<br>
          Proin pharetra augue a lorem elementum molestie.<br>
          Nunc nec justo sit amet nisi tempor viverra sit amet a ipsum.</p>
    </div>
  </div>
</div>body {
  font: medium sans-serif;
}
.outer {
  overflow: hidden;
  background-color: papayawhip;
}
.center-helper {
  display: inline-block;
  position: relative;
  left: 50%;
  background-color: burlywood;
}
.inner {
  display: inline-block;
  position: relative;
  left: -50%;
  background-color: wheat;
}<div class="outer">
  <div class="center-helper">
    <div class="inner">
      <h1>A div with no defined width</h1>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>
          Duis condimentum sem non turpis consectetur blandit.<br>
          Donec dictum risus id orci ornare tempor.<br>
          Proin pharetra augue a lorem elementum molestie.<br>
          Nunc nec justo sit amet nisi tempor viverra sit amet a ipsum.</p>
    </div>
  </div>
</div>edited Mar 17 '18 at 21:12
community wiki
3 revs, 2 users 92%
Salman A
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
edited Oct 16 '13 at 17:08
community wiki
Kenneth Palaganas
add a comment |
add a comment |
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 % );
}
 
 
 3
 
 
 
 
 
 That centers it vertically.
 
 – Michael Terry
 Feb 6 '15 at 0:24
 
 
 
add a comment |
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 % );
}
 
 
 3
 
 
 
 
 
 That centers it vertically.
 
 – Michael Terry
 Feb 6 '15 at 0:24
 
 
 
add a comment |
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 % );
}
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 % );
}
edited Nov 9 '15 at 3:35
community wiki
2 revs, 2 users 71%
caniz
 
 
 3
 
 
 
 
 
 That centers it vertically.
 
 – Michael Terry
 Feb 6 '15 at 0:24
 
 
 
add a comment |
 
 
 3
 
 
 
 
 
 That centers it vertically.
 
 – Michael Terry
 Feb 6 '15 at 0:24
 
 
 
3
3
That centers it vertically.
– Michael Terry
Feb 6 '15 at 0:24
That centers it vertically.
– Michael Terry
Feb 6 '15 at 0:24
add a comment |
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:
- display: block
- display: inline
- display: inline-block
#inner {
  display: inline-block;
}
#outer {
  text-align: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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.
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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 here for a complete and up to date list of browsers compatibility.
#inner {
  display: inline-block;
}
#outer {
  display: flex;
  justify-content: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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%.
#inner {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0%);
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>
<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.
#inner {
  display: inline-block;
}<div id="outer">
  <center>
    <div id="inner">Foo foo</div>
  </center>
</div>add a comment |
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:
- display: block
- display: inline
- display: inline-block
#inner {
  display: inline-block;
}
#outer {
  text-align: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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.
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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 here for a complete and up to date list of browsers compatibility.
#inner {
  display: inline-block;
}
#outer {
  display: flex;
  justify-content: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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%.
#inner {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0%);
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>
<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.
#inner {
  display: inline-block;
}<div id="outer">
  <center>
    <div id="inner">Foo foo</div>
  </center>
</div>add a comment |
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:
- display: block
- display: inline
- display: inline-block
#inner {
  display: inline-block;
}
#outer {
  text-align: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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.
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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 here for a complete and up to date list of browsers compatibility.
#inner {
  display: inline-block;
}
#outer {
  display: flex;
  justify-content: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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%.
#inner {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0%);
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>
<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.
#inner {
  display: inline-block;
}<div id="outer">
  <center>
    <div id="inner">Foo foo</div>
  </center>
</div>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:
- display: block
- display: inline
- display: inline-block
#inner {
  display: inline-block;
}
#outer {
  text-align: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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.
#inner {
  display: table;
  margin: 0 auto;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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 here for a complete and up to date list of browsers compatibility.
#inner {
  display: inline-block;
}
#outer {
  display: flex;
  justify-content: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>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%.
#inner {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0%);
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>
<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.
#inner {
  display: inline-block;
}<div id="outer">
  <center>
    <div id="inner">Foo foo</div>
  </center>
</div>#inner {
  display: inline-block;
}
#outer {
  text-align: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>#inner {
  display: inline-block;
}
#outer {
  text-align: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>#inner {
  display: table;
  margin: 0 auto;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>#inner {
  display: table;
  margin: 0 auto;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>#inner {
  display: inline-block;
}
#outer {
  display: flex;
  justify-content: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>#inner {
  display: inline-block;
}
#outer {
  display: flex;
  justify-content: center;
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>#inner {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0%);
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>#inner {
  position: absolute;
  left: 50%;
  transform: translate(-50%, 0%);
}<div id="outer">
  <div id="inner">Foo foo</div>
</div>#inner {
  display: inline-block;
}<div id="outer">
  <center>
    <div id="inner">Foo foo</div>
  </center>
</div>#inner {
  display: inline-block;
}<div id="outer">
  <center>
    <div id="inner">Foo foo</div>
  </center>
</div>edited Aug 25 '17 at 7:13
answered Jun 9 '17 at 8:34
Paolo ForgiaPaolo Forgia
4,73762850
4,73762850
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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 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.
edited Aug 20 '18 at 12:57
community wiki
2 revs, 2 users 75%
Milan Panigrahi
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Sep 23 '16 at 6:49
community wiki
Billal Begueradj
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
edited Mar 17 '18 at 18:34
community wiki
2 revs, 2 users 83%
Sandesh Damkondwar
add a comment |
add a comment |
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);
  })
});
add a comment |
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);
  })
});
add a comment |
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);
  })
});
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);
  })
});
edited Mar 17 '18 at 18:47
community wiki
2 revs, 2 users 91%
Miguel Leite
add a comment |
add a comment |
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 */
}
 
 
 
 
 
 
 
 text-align work for text alignment in its container not for its container to its parent.
 
 – Lalit Kumar
 Dec 4 '13 at 7:32
 
 
 
 
 
 
 
 
 
 
 i test it , i problem with set child to center , must when you have more one child , more times margin:0 auto font answer , but , text-align center , for parent make this child be center , even if they are element and not be text , test and see what happen
 
 – Pnsadeghy
 Dec 4 '13 at 8:35
 
 
 
 
 
 
 
 
 
 
 text-align center text only. You right at this time but when you write a container css which contains a child with different width and color your code does't work. Test it again!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 9:23
 
 
 
 
 
 
 
 
 
 
 See this example jsfiddle.net/uCdPK/2 and tell me what do you think about it!!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 10:03
 
 
 
add a comment |
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 */
}
 
 
 
 
 
 
 
 text-align work for text alignment in its container not for its container to its parent.
 
 – Lalit Kumar
 Dec 4 '13 at 7:32
 
 
 
 
 
 
 
 
 
 
 i test it , i problem with set child to center , must when you have more one child , more times margin:0 auto font answer , but , text-align center , for parent make this child be center , even if they are element and not be text , test and see what happen
 
 – Pnsadeghy
 Dec 4 '13 at 8:35
 
 
 
 
 
 
 
 
 
 
 text-align center text only. You right at this time but when you write a container css which contains a child with different width and color your code does't work. Test it again!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 9:23
 
 
 
 
 
 
 
 
 
 
 See this example jsfiddle.net/uCdPK/2 and tell me what do you think about it!!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 10:03
 
 
 
add a comment |
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 */
}
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 */
}
edited Mar 17 '18 at 18:38
community wiki
3 revs, 2 users 73%
Pnsadeghy
 
 
 
 
 
 
 
 text-align work for text alignment in its container not for its container to its parent.
 
 – Lalit Kumar
 Dec 4 '13 at 7:32
 
 
 
 
 
 
 
 
 
 
 i test it , i problem with set child to center , must when you have more one child , more times margin:0 auto font answer , but , text-align center , for parent make this child be center , even if they are element and not be text , test and see what happen
 
 – Pnsadeghy
 Dec 4 '13 at 8:35
 
 
 
 
 
 
 
 
 
 
 text-align center text only. You right at this time but when you write a container css which contains a child with different width and color your code does't work. Test it again!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 9:23
 
 
 
 
 
 
 
 
 
 
 See this example jsfiddle.net/uCdPK/2 and tell me what do you think about it!!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 10:03
 
 
 
add a comment |
 
 
 
 
 
 
 
 text-align work for text alignment in its container not for its container to its parent.
 
 – Lalit Kumar
 Dec 4 '13 at 7:32
 
 
 
 
 
 
 
 
 
 
 i test it , i problem with set child to center , must when you have more one child , more times margin:0 auto font answer , but , text-align center , for parent make this child be center , even if they are element and not be text , test and see what happen
 
 – Pnsadeghy
 Dec 4 '13 at 8:35
 
 
 
 
 
 
 
 
 
 
 text-align center text only. You right at this time but when you write a container css which contains a child with different width and color your code does't work. Test it again!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 9:23
 
 
 
 
 
 
 
 
 
 
 See this example jsfiddle.net/uCdPK/2 and tell me what do you think about it!!!!!
 
 – Lalit Kumar
 Dec 4 '13 at 10:03
 
 
 
text-align work for text alignment in its container not for its container to its parent.
– Lalit Kumar
Dec 4 '13 at 7:32
text-align work for text alignment in its container not for its container to its parent.
– Lalit Kumar
Dec 4 '13 at 7:32
i test it , i problem with set child to center , must when you have more one child , more times margin:0 auto font answer , but , text-align center , for parent make this child be center , even if they are element and not be text , test and see what happen
– Pnsadeghy
Dec 4 '13 at 8:35
i test it , i problem with set child to center , must when you have more one child , more times margin:0 auto font answer , but , text-align center , for parent make this child be center , even if they are element and not be text , test and see what happen
– Pnsadeghy
Dec 4 '13 at 8:35
text-align center text only. You right at this time but when you write a container css which contains a child with different width and color your code does't work. Test it again!!!!
– Lalit Kumar
Dec 4 '13 at 9:23
text-align center text only. You right at this time but when you write a container css which contains a child with different width and color your code does't work. Test it again!!!!
– Lalit Kumar
Dec 4 '13 at 9:23
See this example jsfiddle.net/uCdPK/2 and tell me what do you think about it!!!!!
– Lalit Kumar
Dec 4 '13 at 10:03
See this example jsfiddle.net/uCdPK/2 and tell me what do you think about it!!!!!
– Lalit Kumar
Dec 4 '13 at 10:03
add a comment |
1 2
3
4
next
4
Of those great answers, I just want to highlight that you must give "#inner" a "width", or it will be "100%", and you can't tell if it's already centered.
– Jony
Nov 7 '17 at 8:22