Reputation: 944
Here is my HTML code
<div id="container">
<div id="topleft"></div>
<div id="topright"></div>
<div id="bottomleft"></div>
<div id="bottomright"></div>
</div>
Here is my css code
#container{
width: 400px;
height: 400px;
background-color: red;
margin: 0 auto;
}
#topright{
width: 50px;
height: 50px;
background-color: black;
position: relative;
top:0px;
right:0px;
}
#topleft{
width: 50px;
height: 50px;
background-color: black;
position:relative;
top:0px;
left:350px;
}
#bottomright{
width: 50px;
height: 50px;
background-color: black;
position: relative;
top:250px;
right:0px;
}
#bottomleft{
width: 50px;
height: 50px;
background-color: black;
position:relative;
top:250px;
left:350px;
}
Here is output http://s23.postimg.org/dhgy9mpq3/image.png
What I need to obtain, is that all 4 black square to be positioned in the corner of the red square like in this image, what should i change or add in code? THX http://postimg.org/image/r5kv15l5v/
Upvotes: 3
Views: 104
Reputation: 15699
Add position:relative
to #container
and position:absolute
to inner divs.
You can combine common properties with comma, like
#bottomright,#bottomleft{css properties}
CSS:
#container {
position: relative;
}
#container > div {
width: 50px;
height: 50px;
background-color: black;
position: absolute;
}
#bottomright, #bottomleft {
bottom:0;
}
#topright, #topleft {
top:0;
}
#bottomleft, #topleft {
left:0;
}
#bottomright, #topright {
right:0;
}
Upvotes: 6
Reputation:
Why your css failed : For bottom aligment, use bottom:0;left:0;
instead of top
and right
....this is correct semantics which otherwise fails the concept of position
...also, make child absolute
and parents relative
!! :)
#container {
width: 400px;
height: 400px;
background-color: red;
margin: 0 auto;
position: relative;
}
#topright {
width: 50px;
height: 50px;
background-color: black;
position: absolute;
top:0px;
right:0px;
}
#topleft {
width: 50px;
height: 50px;
background-color: black;
position:absolute;
top:0px;
left:0;
}
#bottomright {
width: 50px;
height: 50px;
background-color: black;
position: absolute;
bottom:0;
right:0px;
}
#bottomleft {
width: 50px;
height: 50px;
background-color: black;
position:absolute;
bottom:0;
left:0;
}
Upvotes: 1
Reputation: 3667
the following css will work
#container {
background-color: #FF0000;
height: 400px;
margin: 0 auto;
position: relative;
width: 400px;
}
#topleft {
background-color: #000000;
height: 50px;
left: 0;
position: absolute;
top: 0;
width: 50px;
}
#topright {
background-color: #000000;
height: 50px;
position: absolute;
right: 0;
top: 0;
width: 50px;
}
#bottomleft {
background-color: #000000;
height: 50px;
left: 350px;
position: absolute;
top: 350px;
width: 50px;
}
#bottomright {
background-color: #000000;
height: 50px;
left: 0;
position: absolute;
top: 350px;
width: 50px;
}
Upvotes: 0
Reputation: 3647
Make #container position relative
. And div inside that absolute
.
full code
#container{
width: 400px;
height: 400px;
background-color: red;
margin: 0 auto;
position: relative; //change
}
#topright{
width: 50px;
height: 50px;
background-color: black;
position: absolute; //change
top:0px;
right:0px;
}
#topleft{
width: 50px;
height: 50px;
background-color: black;
position:absolute; //change
top:0px;
left:0px; //change
}
#bottomright{
width: 50px;
height: 50px;
background-color: black;
position: absolute;
bottom:0px; //change
right:0px;
}
#bottomleft{
width: 50px;
height: 50px;
background-color: black;
position:absolute; //change
bottom:0px; //change
left:0px; //change
}
Upvotes: 0
Reputation: 9738
#container{
width: 400px;
height: 400px;
background-color: red;
margin: 0 auto;
position: relative;
}
#topright{
width: 50px;
height: 50px;
background-color: black;
position: absolute;
top:0px;
right:0px;
}
#topleft{
width: 50px;
height: 50px;
background-color: black;
position:absolute;
top:0px;
left:0;
}
#bottomright{
width: 50px;
height: 50px;
background-color: black;
position: absolute;
bottom:0px;
right:0px;
}
#bottomleft{
width: 50px;
height: 50px;
background-color: black;
position:absolute;
bottom:0;
left:0;
}
Upvotes: 0