Reputation: 10040
I would like to position some elements of the same class one on other. Practically, I want to have them in the same position:
<div id="container">
<div class="sheet"></div>
<div class="sheet"></div>
<div class="sheet"></div>
</div>
I also want to have these divs horizontally aligned in the center.
.container {
margin-left: auto;
margin-right: auto;
}
How would you do this?
edit: Here is what I would like to have, but in positioned to center:
Upvotes: 1
Views: 94
Reputation: 1041
My code will also work on legacy browsers (such as IE8 etc) (that is why I decided to use ids
).
If you aren't planning on supporting legacy browsers, let me know and I'll update my code or take a look at @mdesdev's answer which is compatible with all modern browsers.
HTML
<div id="container">
<div class="sheet" id='sheet1'></div>
<div class="sheet" id='sheet2'></div>
<div class="sheet" id='sheet3'></div>
</div>
CSS
#container {
width:150px;
position:relative;
left:0;
right:0;
margin:0 auto;
z-index:10;
}
.sheet{
position:absolute;
width:150px;
height:150px;
border:1px solid brown;
background:red;
}
#sheet1{
z-index:7;
top:5px;
left:5px;
background:blue;
}
#sheet2{
z-index:8;
top:10px;
left:10px;
background:green;
}
#sheet3{
z-index:9;
top:15px;
left:15px;
}
Upvotes: 1
Reputation: 3130
I recently did something just like this. In my case the "sheets" were going to slide apart on click, so I just had them expanded by default for IE8. For other browsers I did something like this:
.container {
display: flex;
}
.sheet {
position: relative;
}
.sheet:nth-child(1) {
left: 33%;
}
.sheet:nth-child(3) {
left: -33%;
}
Upvotes: 0
Reputation: 4931
Use Position Absolute:
DEMO use css property position: absolute;
to keep elements above each other.
Use z-index to arrange which element is above which:
DEMO use z-index: 3;
to defined which element should be shown above which the greater the number the above it will be from those with lesser value in z-index.
To Rotate user transform rotate
DEMO use transform: rotate(-7deg);
to rotate your divs.
Upvotes: 0
Reputation: 1975
I think I got what you want!
HTML
<div id="container">
<div class="sheet" id='sheet1'></div>
<div class="sheet" id='sheet2'></div>
</div>
CSS
.container {
width:300px;
margin:0 auto;
position:relative;
z-index:10;
}
.sheet{
top:auto;
left:auto;
vertical-align:middle;
position:absolute;
width:150px;
height:150px;
border:1px solid brown;
background:red;
}
#sheet1{
z-index:7;
transform:rotate(7deg);
-ms-transform:rotate(7deg); /* IE 9 */
-webkit-transform:rotate(7deg); /* Opera, Chrome, and Safari */
}
#sheet2{
z-index:8;
transform:rotate(-7deg);
-ms-transform:rotate(-7deg); /* IE 9 */
-webkit-transform:rotate(-7deg); /* Opera, Chrome, and Safari */
}
NOTE: edited from W.D code Thanks to him to start the code! It makes challenge!
Upvotes: 0
Reputation: 5610
Here's a FIDDLE
#container {
position: relative;
width: 450px;
height: 600px;
margin: 0 auto;
}
.sheet {
background: #fff;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
box-shadow: 0 0 3px 0 #666;
-moz-box-shadow: 0 0 3px 0 #666;
-webkit-box-shadow: 0 0 3px 0 #666;
}
.sheet:nth-child(1) {
transform: rotate(5deg);
-moz-transform: rotate(5deg);
-webkit-transform: rotate(5deg);
}
.sheet:nth-child(2) {
transform: rotate(-10deg);
-moz-transform: rotate(-10deg);
-webkit-transform: rotate(-10deg);
}
Upvotes: 2
Reputation: 1975
EDIT: Not as you need
As SUMAN said, you can use nth-child(xn).
I do not have the time to make the script, but all the informations you need can be found here: http://dannich.com/lab/css3-pseudo-classes-for-grid-with-dynamic-content/
NOTE: This is certainly not the most compatible option
Good luck!
Upvotes: 1
Reputation: 3528
go with the nth-child(n) to differentiate between those same class divs
Upvotes: 0