tags:

views:

25

answers:

3
<head>
<title>Overlay test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
#overlay {
    position: absolute;
    background-color: #ccffcc;
    display: none;
    height: 200px;
    margin: 0 auto;
    width: 200px;
}
</style>
<script type="text/javascript">
//<![CDATA[
function hide() {
    document.getElementById("overlay").style.display = "none";
}

function show() {
    document.getElementById("overlay").style.display = "block";
}
//]]>
</script>

so when the user clicks it runs show() which places the css box on top. However i want it to be centered in the browser. I've set the margin: 0 auto; which should be doing the trick shouldnt it?

I'm just trying to create an overlay function without using jquery because it seems to be incompatible with my schools cms templates.

Thanks

+4  A: 

Margin: 0 auto won't work on position absolute elements, they exist in their own little world, outside of normal flow. So in order to pull this off, you need to do an extra step. The CSS dead centre technique will work here.

danp
+1 Or just don't use absolute.
sworoc
Nothing wrong with absolute, it's the only way to pull off some designs, but have to respect that it's not normal flow.
danp
thanks works great... anyone know if my technique will easily incorporate swfobject to place flash inside?
Adam
Make sure you use wmode: transparent, or opaque, so the flash will support z-indexing and it should be fine.
danp
thanks got it to work
Adam
A: 

Try setting the top and left attributes on your overlay.

Teja Kantamneni
A: 

Use % to set top and left position. Set css attribute top:10%; Left 40%;

Gil Duzanski