I"m trying to center a div within a div. At first, I thought I try to do horizontal centering but that's not working. I looked at this post for horizontal centering How to horizontally center a <div> in another <div>?
Here is my code http://codepen.io/anon/pen/VvLgrd. Here is the styling part of the code that matters
#homepage {
position: relative;
}
#homepage canvas, #console {
position: absolute;
}
#console {
background: rgb(224,168,227);
width: 75%;
margin: 0 auto;
}
For some reason, the console div does not center inside of homepage div. I'm kinda confused.
Also, I was looking up how to center both vertically and horizontally. This website https://css-tricks.com/centering-css-complete-guide/ recommends to use
.parent {
position: relative;
}
.child {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
I tried it out and had
#homepage {
position: relative;
}
#homepage canvas, #console {
position: absolute;
}
#console {
background: rgb(224,168,227);
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
But it only centered horizontally. I was also confused on what it was doing? I'm not understand percents well. So if you have top: 50%
that means "you sets the top edge position in 50% of #homepage". I don't get it. Also for transform: translate(-50%, -50%);
, wouldn't that mean that you translate to the negative numbers?