ish1301 ish1301 - 6 months ago 20
CSS Question

How to center absolute element in div?

I need to place a

div
(with
position:absolute;
) element in the center of my window. But I am having problems doing so, because the width is unknown

I tried this. But it needs to be adjusted as the width is responsive.

.center {
left: 50%;
bottom:5px;
}


Any ideas?

Answer
<body>
    <div style="position: absolute; left: 50%;">
        <div style="position: relative; left: -50%; border: dotted red 1px;">
            I am some centered shrink-to-fit content! <br />
            tum te tum
        </div>
    </div>
</body>