Position an element relative to its container with CSS

Use position: relative on a container when an absolutely positioned child should be anchored to it. Leave ordinary text in normal flow so the container still grows with its content.

This browser layout rule applies regardless of whether the HTML comes from Web Forms, Razor or a static page. Old Visual Studio designer positioning settings are not required.

Create a positioned badge

Save this complete document as position-demo.html and open it in a browser:

HTML
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Positioning example</title>
<style>
  .card { position: relative; max-width: 28rem; padding: 3rem 1rem 1rem;
          border: 1px solid #aaa; border-radius: .5rem; }
  .badge { position: absolute; top: .75rem; right: .75rem;
           background: #def; padding: .25rem .5rem; }
</style>
<article class="card">
  <span class="badge">Draft</span>
  <h1>Reference note</h1>
  <p>This content stays in normal document flow.</p>
</article>
</html>

The Draft badge sits near the card's top-right corner. After adding several paragraphs, the card becomes taller while the badge remains anchored to its corner.

Choose flow before coordinates

An absolutely positioned child does not contribute to its parent's normal content height. The top padding reserves room so the badge does not overlap the heading. Test narrow widths and enlarged text. Fixed coordinates can collide with translated or wrapped content.

Use Grid or Flexbox when the goal is arranging peer controls in rows or columns. Relative positioning by itself keeps an element's original space in flow. It is not the same as absolutely positioning all children. Avoid reconstructing a whole responsive form with pixel coordinates.

Official references

position CSS property - CSS | MDN
The position CSS property sets how an element is positioned in a document. The top, right, bottom, and left physical properties and the inset-block-start, inset-block-end, inset-inline-start, and inset-inline-end flow-relative logical properties can be used to determine the final location of positioned elements.
https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/position
CSS position

Author: Herman Schoenfeld