58

How to horizontally center a floating element of a variable width?

Edit: I already have this working using a containing div for the floating element and specifying a width for the container (then use margin: 0 auto; for the container). I just wanted to know whether it can be done without using a containing element or at least without having to specify a width for the containing element.

Barney
  • 16,181
  • 5
  • 62
  • 76
Waleed Eissa
  • 10,283
  • 16
  • 60
  • 82

8 Answers8

91

Assuming the element which is floated and will be centered is a div with an id="content" ...

<body>
<div id="wrap">
   <div id="content">
   This will be centered
   </div>
</div>
</body>

And apply the following CSS:

#wrap {
    float: left;
    position: relative;
    left: 50%;
}

#content {
    float: left;
    position: relative;
    left: -50%;
}

Here is a good reference regarding that.

Leyu
  • 2,687
  • 2
  • 23
  • 27
  • You can also try `margin-left` instead of `left` and `position: relative`. Margin-left worked for me on the -50% element. – Muhd Dec 01 '11 at 00:13
  • yea - but what is the added value @muhd? – marcusklaas Dec 27 '11 at 22:58
  • @marcusklaas, the added value is that it may work in cases where the posted solution does not, or may be easier to implement. I don't remember much about this since it was a month ago. – Muhd Dec 29 '11 at 00:46
  • I dig the solution, but you might need overflow-x: hidden to prevent horizontal scrollbars. That took a bit to figure out. – gtd Apr 03 '12 at 12:22
  • Link to "reference" appears dead. – Pang Feb 28 '19 at 02:51
75
.center {
  display: table;
  margin: auto;
}
7

You can use fit-content value for width.

#wrap {
  width: -moz-fit-content;
  width: -webkit-fit-content;
  width: fit-content;
  margin: auto;   
}

Note: It works only in latest browsers.

Mark
  • 6,762
  • 1
  • 33
  • 50
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
5

This works better when the id = container (which is the outer div) and id = contained (which is the inner div). The problem with the highly recommended solution is that it results in some cases into an horizontal scrolling bar when the browser is trying to cater for the left: -50% attribute. There is a good reference for this solution

        #container {
            text-align: center;
        }
        #contained {
            text-align: left;
            display: inline-block;
        }
4

Say you have a DIV you want centred horizontally:

 <div id="foo">Lorem ipsum</div>

In the CSS you'd style it with this:

#foo
{
  margin:0 auto; 
  width:30%;
}

Which states that you have a top and bottom margin of zero pixels, and on either left or right, automatically work out how much is needed to be even.

Doesn't really matter what you put in for the width, as long as it's there and isn't 100%. Otherwise you wouldn't be setting the centre on anything.

But if you float it, left or right, then the bets are off since that pulls it out of the normal flow of elements on the page and the auto margin setting won't work.

random
  • 9,774
  • 10
  • 66
  • 83
  • 1
    The only issue here is that you have to explicitly set the width; `width: auto;` doesn't work (AFAIK). – You Aug 05 '09 at 09:37
  • Correct, you do have to set something as the width. But the question was asking about variable width, hopefully that's not another word for auto and instead some fluctuating value or relative amount, like em's or % – random Aug 05 '09 at 09:43
  • Thanks for your answer, actually I don't have a problem with centering elements, what I'm trying to do is center a "floating element". I know this is not possible in CSS but was hoping somebody might have a hack that can work for this, I even tried to use a table but that didn't work too. Unfortunately in my scenario there's no way to make the element not floating, I need this for specific reasons. – Waleed Eissa Aug 05 '09 at 09:44
  • This solution helped me out on a project. Thanks. – Julian Oct 29 '10 at 03:50
4

The popular answer here does work sometimes, but other times it creates horizontal scroll bars that are tough to deal with - especially when dealing with wide horizontal navigations and large pull down menus. Here is an even lighter-weight version that helps avoid those edge cases:

#wrap {
    float: right;
    position: relative;
    left: -50%;
}
#content {
    left: 50%;
    position: relative;
}

Proof that it is working!

To more specifically answer your question, it is probably not possible to do without setting up some containing element, however it is very possible to do without specifying a width value. Hope that saves someone out there some headaches!

fohryan
  • 155
  • 2
  • 8
3

Can't you just use display: inline block and align to center?

Example.

atiquratik
  • 1,296
  • 3
  • 27
  • 34
Kai
  • 451
  • 4
  • 5
0

for 50% element

width: 50%;
display: block;
float: right;
margin-right: 25%;
lior ben yosef
  • 144
  • 1
  • 6