Align a div to center
Asked Answered
H

14

52

I want to float a div to center. Is it possible? text-align: center is not working in IE.

Hutner answered 16/11, 2009 at 7:19 Comment(0)
B
82

There is no float to center per se. If you want to center a block element inside another do this:

<div id="outer">
  <div id="inner">Stuff to center</div>
</div>

with:

#outer { width: 600px; }
#inner { width: 250px; margin: 0 auto; }

Now that won't make the text wrap around it (like it would with a float left or right) but like I said: there is no float center.

Brecciate answered 16/11, 2009 at 7:20 Comment(3)
For those frustrated after doing this and it still isn't working, put 'display:inline-block;' in your "#outer" argumentsScholar
I know this is an old question. Now we can simply use <div align="center">Suspend
Also, to override float set it to noneFaulkner
H
26

This has always worked for me.

Provided you set a fixed width for your DIV, and the proper DOCTYPE, try this

div {        
  margin-left:auto;
  margin-right:auto;
}

Hope this helps.

Hollywood answered 28/9, 2012 at 18:3 Comment(1)
This works great with a DIV that isn't fixed width as well, just add display: table;Razzia
H
12

The usual technique for this is margin:auto

However, old IE doesn't grok this so one usually adds text-align: center to an outer containing element. You wouldn't think that would work but the same IE's that ignore auto also incorrectly apply the text align center to block level inner elements so things work out.

And this doesn't actually do a real float.

Habitat answered 16/11, 2009 at 7:33 Comment(4)
text align center not working in IE ... Ross cab give me any other ansHutner
Note that 'old IE' means IE 5.x and earlier, which most people don't concern themselves with these days.Zoa
Don't you even have to do text-align: center on IE6?Iatry
Only in Quirks Mode (and you should not be using quirks mode except under truly exceptional circumstances)Zoa
E
6

floating divs to center "works" with the combination of display:inline-block and text-align:center.

Try changing width of the outer div by resizing the window of this jsfiddle

<div class="outer">
    <div class="block">one</div>
    <div class="block">two</div>
    <div class="block">three</div>
    <div class="block">four</div>
    <div class="block">five</div>
</div>

and the css:

.outer {
    text-align:center;
    width: 50%;
    background-color:lightgray;
}

.block {
    width: 50px;
    height: 50px;
    border: 1px solid lime;
    display: inline-block;
    margin: .2rem;
    background-color: white;
}
Eldin answered 8/4, 2013 at 15:42 Comment(0)
I
6

Following solution worked for me.

  .algncenterdiv {
    display: block;
    margin-left: auto;
    margin-right: auto;
    }
Interest answered 7/10, 2013 at 7:2 Comment(0)
Y
4

One of my websites involves a div whose size is variable and you won't know it ahead of time. it is an outer div with 2 nested divs, the outer div is the same width as the first nested div, which is the content, and the second nested div right below the content is the caption, which must be centered. Because the width is not known, I use jQuery to adjust accordingly.

so my html is this

<div id='outer-container'>
<div id='inner-container'></div>
<div id='captions'></div>
</div>

and then I center the captions in jQuery like this

captionWidth=$("#captions").css("width");
outerWidth=$("#outer-container").css("width");
marginIndent=(outerWidth-captionWidth)/2;
$("#captions").css("margin","0px "+marginIndent+"px");
Yelena answered 18/2, 2013 at 2:0 Comment(0)
S
3

Use "spacer" divs to surround the div you want to center. Works best with a fluid design. Be sure to give the spacers height, or else they will not work.

<style>
div.row{width=100%;}
dvi.row div{float=left;}
#content{width=80%;}
div.spacer{width=10%; height=10px;}
</style>

<div class="row">
<div class="spacer"></div>
<div id="content">...</div>
<div class="spacer"></div>
</div>
Sigmon answered 11/7, 2010 at 17:17 Comment(0)
N
3

This worked for me..

div.className {
display: inline-block;
margin: auto;
}
Niel answered 5/8, 2015 at 20:24 Comment(1)
I was trying to float dynamically created divs in an outer div.. I can have 1 or 2 divs.. if there was 1 div then it should float in center and if there were 2 divs then float side by side.. i hope its clear :)Niel
E
1

this could help you..:D

div#outer {
  width:200px;
  height:200px;
  float:left;
  position:fixed;
  border:solid 5px red;
}
div#inner {
  border:solid 5px green;
}
<div id="outer">
  <center>
    <div id="inner">Stuff to center</div>
  </center>
</div>
Ellord answered 25/5, 2015 at 9:22 Comment(0)
Z
0

No, it isn't.

You can either have content bubble up to the right of an element (float: left) or to the left of an element (float: right), there is no provision for having content bubble up on both sides.

Zoa answered 16/11, 2009 at 7:46 Comment(0)
B
0
<div id="outer" style="z-index:10000;width:99%;height:200px;margin-top:300px;margin-left:auto;margin-right:auto;float:left;position:absolute;opacity:0.9;">

<div id="inner" style="opacity:1;background-color:White;width:300px;height:200px;margin-left:auto;margin-right:auto;">Inner</div></div>

Float the div in the background to the max width, set a div inside that that's not transparent and center it using margin auto.

Broker answered 2/9, 2012 at 20:55 Comment(0)
A
0

this works nicely

width:40%; // the width of the content div
right:0;
margin-right:30%; // 1/2 the remaining space

This resizes nicely with adaptive layouts also..

CSS example would be:

.centered-div {
   position:fixed;
   background-color:#fff;
   text-align:center;
   width:40%;
   right:0;
   margin-right:30%;
}
Absurdity answered 3/12, 2014 at 23:28 Comment(0)
F
0

This worked for me.

I included an unordered list on my page twice. One div class="menu" id="vertical" the other to be centered was div class="menu" id="horizontal". Since the list was floated left, I needed an inner div to center it. See below.

<div class=menu id="horizontal">
<div class="fix">
  Centered stuff
</div>
</div>

.menu#horizontal { display: block;  float: left; margin: 0px; padding: 0 10px; position: relative; left: 50%; }
#fix { float: right; position: relative; left: -50%; margin: 0px auto; }
Fiendish answered 10/2, 2015 at 22:46 Comment(0)
F
-1

Try this, it helped me: wrap the div in tags, the problem is that it will center the content of the div also (if not coded otherwise). Hope that helps :)

Fourlegged answered 23/2, 2014 at 0:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.