2

I want a horizontally centered "floating" div, i.e. one that always is on top of the screen even if the user scrolls around.

With the following CSS I get the div centered completely on the left:

#guiBar {
margin-left: auto;
margin-right: auto;
position: fixed;
}

margin: 0 auto doesn't help either (presumably because that doesn't work together with position: fixed ?

I don't really care how the solution works so javascript or some html5 trickery would be just as fine as pure css. I already tried the solution found here from starx but that also doesn't seem to work with position: fixed. there's also this which doesn't work either (i.e. it's left centered - not sure if a number of <input class="guiButton" type="image" src="/icons/some.png" /> have a fixed size by his definition.)

Community
  • 1
  • 1
Voo
  • 29,040
  • 11
  • 82
  • 156

5 Answers5

10

If your element is a fixed width (for instance width:400px) you can use the left and top attributes along with the margin attributes:

#guiBar
{
    position:fixed;
    left:50%;
    margin-left:-200px; /*half the width*/
}

Demo: http://jsfiddle.net/VtqQD/1/show
Source: http://jsfiddle.net/VtqQD/1

EDIT: Thanks to Xander for pointing out the question was about horizontal positioning only.

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
2

html

<div id="subject">
    <div id="predicate">
    Read Me!
    </div> 
</div>

css

#subject {
    position:fixed;
    top:0px;
    left:0px;
    width:100%;
    height:100%;
}

#predicate {
    position:relative;
    top:50%;
    margin:-20px auto auto auto; /*half of height*/
    width:140px;
    height:40px;
    background:#b4da55;
}

try it out http://jsfiddle.net/RfRAb/

Tank
  • 1,006
  • 7
  • 17
1

You can use use javascript use scrollHieght and scrollWidth to find the height and width of parent div. Then used those values divided by two to get the values of the center of the div. Then offset it by the dimensions of the div you are wanting to lay on the top.

If you want the center of the entire screen/window use window.innerHeight and window.innerWidth then divide by two and offset by your div.

user982853
  • 2,470
  • 14
  • 55
  • 82
0

You would simply layer your element it with:

  z-index: 9999;

This would guarantee you keep the box on top (z axis), then you just position it wherever you want x/y axis

Reference: Adding z-index MDN

Jakub
  • 20,418
  • 8
  • 65
  • 92
  • I already use z-indizes to make sure a click/touch event gets to the right element, but I don't see how this helps me center the floating div? – Voo Jan 05 '12 at 19:28
  • you define a width/height to your div, right now you have no dimensions (at least from your example) – Jakub Jan 05 '12 at 19:29
  • By using my javascript below it doesn't matter the size of your divs or window. It can be used for both fixed and fluid layouts because javascript is finding the size of the window/div after it has loaded. – user982853 Jan 05 '12 at 19:34
0

I made a tiny example:

<html>
<head>
    <style type="text/css">
        body { margin: 0; }
        #wrapper { width: 500px; margin: 0 auto; } 
        #top { margin: 0 auto; width: inherit; background: red; position: fixed; }
    </style>
</head>
<body>
<div id="wrapper">
    <div id="top">
        foo
    </div>
</div>
</body>
</html>

I hope it will help you!

Remy Strijker
  • 261
  • 3
  • 19