In my coding project, I've encountered an issue with centering a #text
div inside another #box
div. My current attempt at centering it involves CSS code like this:
#box {
width: 50%;
#text {position: relative; margin-left: 48%;}
}
While this code does place the #text
relatively in the center of the #box
, resizing the screen causes the text to shift due to its margin changing size. It's no longer perfectly centered when the screen size changes.
I've heard there are methods involving multiple wrapper divs as a "hack" to solve this issue, but I prefer not to complicate my CSS or HTML too much. Is there a straightforward way to horizontally align a div exactly in the center of its parent?