Reputation: 821
I have a base site right now that has a grey background with text on top, moving/animated with external javascript. I want another div to be positioned on top of this background div, but I don't want to sacrifice page centering by using absolute positioning. Any ideas?
<body>
<div id="background">
<p id="backgroundText">
</p>
<div id="mainBody">
</div>
</div>
</body>
* {
margin: 0;
padding: 0;
border: 0;
font-family: monospace;
}
head {
display: none;
}
body {
background-color: #333;
overflow: hidden;
}
div#background {
z-index: 0;
height: 100%;
width: 100%;
overflow: hidden;
position: absolute;
top: 0;
left: 0;
}
p#backgroundText {
overflow: hidden;
color: #494949;
font-size: 13px;
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}
div#mainBody {
width: 500px;
height: 500px;
margin: 0 auto;
z-index: 1;
position: absolute;
top: 0;
left: 50%;
background-color: white;
}
Upvotes: 1
Views: 4107
Reputation: 21882
You can use relative positioning.
div#mainBody {
width: 500px;
height: 500px;
margin: 0 auto;
z-index: 1;
position: relative;
background-color: white;
}
In order to utilize z-index properties you must use a position property of some kind.
Upvotes: 4