csscss-positionabsolute

How can I center an absolutely positioned element in a div?


I want to place a div (with position:absolute;) element in the center of the window. But I'm having problems doing so, because the width is unknown.

I tried the following CSS code, but it needs to be adjusted because the width is responsive.

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

How can I achieve this?


Solution

  • <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>