开发者

how to display an element always at top and centered?

I was just wondering why my following code doesn't work:

EDIT: The following code works in firefox 3.5, i can't test on other browsers, does开发者_StackOverflow it work universally, or is there some problems with it?

<html>
<head>
<style type="text/css">
#test{
    position:fixed;
    left:50%;
}
</style>
</head>
<body>
<div id="test">Center</div>
<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />asdf<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />asfd<br />
</body>
</html>

It always displays the element at top even if scrolled, but the text-align:center doesn't work at all.

Anyone has any idea how to move this element to the center of the screen?

Thanks a lot!


When you change an element's position to fixed, it will no longer fill horizontally like a normal block level element (div, p, etc). Without a specified width parameter, it will only be as wide as it needs to be. Since your block is only as wide as the text, changing the text alignment won't change the appearance, since it's already flush to the left and right, if you get my meaning...

One way you can get around this is to specify the width, however this might be problematic, since you don't know the width of the browser window. A much neater way (which might not work cross-browser, I'm not sure), is to also specify the right:

position: fixed;
left: 5px;
right: 5px;
text-align: center;

This will make the block fill the window horizontally leaving 5px on either side. Text alignment should work then.

Edit: I just tested and this works in Firefox and Internet Explorer 7. Try it out here: http://jsbin.com/ihofa


To center non-text objects you must use

margin:0px auto; display: block

Forgetting display:block is a common problem with centering objects that default to display:inline, such as images.


margin:0px auto

or you can use Javascript/jQuery to evaluate and center it each time the user resizes the window.


.position-fixed-center {
    position:fixed !important;
    top: 50%;
}


Depending on if your site is using a fixed size as a resolution or not would be the solution to the issue.

If you are using a fixed size then use the left property to center it since you are fixing the position.

If you expand your site depending on user's resolution you will need to use javascript to get the position of the screen. Using an x and y offset. Below is some sample code to get dead center you would need to alter the y offset to the top of the screen to get the effect you want but it's a good starting point and I'm sure you can build on it to get what you want.

function showdeadcenterdiv(Xwidth,Yheight,divid) {
// First, determine how much the visitor has scrolled

var scrolledX, scrolledY;
if( self.pageYoffset ) {
   scrolledX = self.pageXoffset;
   scrolledY = self.pageYoffset;
} else if( document.documentElement && document.documentElement.scrollTop ) {
   scrolledX = document.documentElement.scrollLeft;
   scrolledY = document.documentElement.scrollTop;
} else if( document.body ) {
   scrolledX = document.body.scrollLeft;
   scrolledY = document.body.scrollTop;
}

// Next, determine the coordinates of the center of browser's window

var centerX, centerY;
if( self.innerHeight ) {
  centerX = self.innerWidth;
  centerY = self.innerHeight;
} else if( document.documentElement && document.documentElement.clientHeight ) {
  centerX = document.documentElement.clientWidth;
  centerY = document.documentElement.clientHeight;
} else if( document.body ) {
  centerX = document.body.clientWidth;
  centerY = document.body.clientHeight;
}

// Xwidth is the width of the div, Yheight is the height of the
// div passed as arguments to the function:
var leftoffset = scrolledX + (centerX - Xwidth) / 2;
var topoffset = scrolledY + (centerY - Yheight) / 2;
// The initial width and height of the div can be set in the
// style sheet with display:none; divid is passed as an argument to // the function
var o=document.getElementById(divid);
var r=o.style;
r.position='absolute';
r.top = topoffset + 'px';
r.left = leftoffset + 'px';
r.display = "block";
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜