I have a div called #text
, inside another div #box
. Right now I've tried to center #text
by doing this:
#box {
width: 50%;
#text {position: relative; margin-left: 48%;}
}
This code puts #text
approximately in the center of #box
, but when I resize the screen, the size of #text
changes relative to the size of the screen, so the size of its margin-right
changes, and it is no longer exactly in the center.
I've heard there is a "hack" for this involving a few wrapper divs, but I don't want to overcomplicate my css or html. Is there a simple way to horizontally position a div exactly in the center of its parent?