You have two options:
JavaScript:
var container = document.querySelector('.fixo'),
w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
container.style.top = (y / 2) - (container.clientHeight / 2) + 'px';
container.style.left = (x / 2) - (container.clientWidth / 2) + 'px';
Example: link
CSS:
.fixo {
width:100px;
height:50px;
background:#000;
position:fixed;
top:50%; left: 50%;
}
Example: link
The javascript version calculates the height and width of the screen, divides by half and subtracts half the width or height of the element.
The CSS version is approximate. You can adjust if the measurements are +/- static. For modern browsers the @bfavaretto solution is ideal.
Edit:
In cases where it is necessary to support browsers before I suggest using feature-detection. That is, to detect if the Browser supports calc()
in CSS ( as @bfavaretto suggested ), and case negative positioning via JavaScript.
Example:
CSS
.fixo {
width:100px;
height:50px;
background:#000;
position:fixed;
top:0;
left: calc(50% - 50px);
}
JavaScript
// usando o feature detect do Modrnizer
var calc = (function(){
var dummy = document.createElement('div');
var props = ['calc', '-webkit-calc', '-moz-calc', '-o-calc'];
for (var i=0; i<props.length; ++i) {
var prop = props[i];
dummy.style.cssText = 'width:' + prop + '(1px);';
if (dummy.style.length)
return prop;
}
})();
if (!calc) {
var container = document.querySelector('.fixo'),
w = window,
d = document,
e = d.documentElement,
g = d.getElementsByTagName('body')[0],
x = w.innerWidth || e.clientWidth || g.clientWidth,
y = w.innerHeight|| e.clientHeight|| g.clientHeight;
container.style.top = (y / 2) - (container.clientHeight / 2) + 'px';
container.style.left = (x / 2) - (container.clientWidth / 2) + 'px';
}