Support0

CSS position playground

Compare flow and containing blocks. Try fixed and sticky with real scrolling and see how logical edges change with the writing mode.

Ask AI

CSS position playground

Response generated with openai/gpt-5.4-nano. AI can make mistakes. Always review the result.
Target
Sibling

The dashed lines mark where the target box starts in normal flow.

Inset reference
The inset has no effect with static.
Space in flow
The target keeps its space.
HTML
<div class="position-example">
  <div class="position-parent">
    <div class="position-target">Target</div>
    <div class="position-sibling">Sibling</div>
  </div>
</div>
Tailwind CSS
Examples for Tailwind CSS 4.3 with the default theme. Custom values keep arbitrary classes when there is no equivalent utility. Changing the theme can change utility values.
<div class="relative [direction:ltr] [writing-mode:horizontal-tb] box-border inline-full border p-[0.75em] pbs-[calc(24px_+_2.5em)]">
  <div class="relative flex flex-col items-start border border-dashed p-[0.75em] pbs-[calc(24px_+_2.5em)]">
    <div class="box-border max-inline-full wrap-anywhere p-[0.25em_0.5em] border static top-[12px]">Target</div>
    <div class="box-border max-inline-full wrap-anywhere p-[0.25em_0.5em] border mbs-[calc(24px_+_0.5em)]">Sibling</div>
  </div>
</div>
CSS
.position-example {
  position: relative;
  direction: ltr;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  border: 1px solid;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example .position-parent {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border: 1px dashed;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example :is(.position-target, .position-sibling) {
  box-sizing: border-box;
  max-inline-size: 100%;
  overflow-wrap: anywhere;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

.position-example .position-target {
  position: static;
  top: 12px;
}

.position-example .position-sibling {
  margin-block-start: calc(24px + 0.5em);
}

Educational guide to position and inset

Find the reference that moves each box

Track the space a box keeps in flow, identify the edge each inset is measured from, and see what changes as you scroll.

Mental model

Before setting an inset, identify the positioning mode, its reference, and the axis you want to control.

  1. Space in flow

    Do the neighboring elements still account for the space occupied by the box?

  2. Reference

    Does the box use its original position, a containing block, or a scroll region?

  3. Edges

    Does the offset follow a physical edge or the start or end of a logical axis?

Use these questions to read the comparisons. The examples position their boxes with native CSS.

Flow and the containing block

The first four examples keep top: 12px. The last two compare right: 12px. The dashed border marks the inner frame.

For absolute, look for the nearest ancestor that establishes the containing block. A block ancestor with position: relative establishes it at its padding edge. When the inner frame becomes static in this scene, the outer frame takes over. Other properties, such as transform, can also establish it.

In Flow and reference, change the direction or writing mode to move the origin of the flow. The inset stays tied to the physical edge you selected. Try logical properties in the Insets scene.

top
With relative, a positive value moves the box down from its original position. With absolute, it leaves that distance from the top edge of the containing block.
right
With relative, a positive value moves the box left. With absolute, it leaves that distance from the right edge of the containing block.
bottom
With relative, a positive value moves the box up. With absolute, it leaves that distance from the bottom edge of the containing block.
left
With relative, a positive value moves the box right. With absolute, it leaves that distance from the left edge of the containing block.

A negative value reverses the relative offset. With absolute, it lets the box extend past the chosen edge. Insets leave a static box in its normal position.

With ltr, the inline axis starts at the top in vertical-lr and at the bottom in sideways-lr. Browsers without support for the sideways values keep the horizontal writing mode.

static

Target
Sibling
The box sits in its usual place. The inset has no effect and the sibling stays in place.
static
<div class="position-example">
  <div class="position-parent">
    <div class="position-target">Target</div>
    <div class="position-sibling">Sibling</div>
  </div>
</div>
.position-example {
  position: relative;
  direction: ltr;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  border: 1px solid;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example .position-parent {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border: 1px dashed;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example :is(.position-target, .position-sibling) {
  box-sizing: border-box;
  max-inline-size: 100%;
  overflow-wrap: anywhere;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

.position-example .position-target {
  position: static;
  top: 12px;
}

.position-example .position-sibling {
  margin-block-start: calc(24px + 0.5em);
}

relative

Target
Sibling
The box moves 12 px down from its normal position. The sibling stays where it was with static.
relative
<div class="position-example">
  <div class="position-parent">
    <div class="position-target">Target</div>
    <div class="position-sibling">Sibling</div>
  </div>
</div>
.position-example {
  position: relative;
  direction: ltr;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  border: 1px solid;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example .position-parent {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border: 1px dashed;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example :is(.position-target, .position-sibling) {
  box-sizing: border-box;
  max-inline-size: 100%;
  overflow-wrap: anywhere;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

.position-example .position-target {
  position: relative;
  top: 12px;
}

.position-example .position-sibling {
  margin-block-start: calc(24px + 0.5em);
}

absolute with the inner frame

Target
Sibling
The box sits 12 px from the inner edge of the dashed frame. The sibling moves up when the box stops reserving space.
absolute with the inner frame
<div class="position-example">
  <div class="position-parent">
    <div class="position-target">Target</div>
    <div class="position-sibling">Sibling</div>
  </div>
</div>
.position-example {
  position: relative;
  direction: ltr;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  border: 1px solid;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example .position-parent {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border: 1px dashed;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example :is(.position-target, .position-sibling) {
  box-sizing: border-box;
  max-inline-size: 100%;
  overflow-wrap: anywhere;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

.position-example .position-target {
  position: absolute;
  top: 12px;
}

.position-example .position-sibling {
  margin-block-start: calc(24px + 0.5em);
}

absolute with the outer frame

Target
Sibling
The dashed frame becomes static. The 12 px offset is measured from the outer frame.
absolute with the outer frame
<div class="position-example">
  <div class="position-parent">
    <div class="position-target">Target</div>
    <div class="position-sibling">Sibling</div>
  </div>
</div>
.position-example {
  position: relative;
  direction: ltr;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  border: 1px solid;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example .position-parent {
  position: static;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border: 1px dashed;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example :is(.position-target, .position-sibling) {
  box-sizing: border-box;
  max-inline-size: 100%;
  overflow-wrap: anywhere;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

.position-example .position-target {
  position: absolute;
  top: 12px;
}

.position-example .position-sibling {
  margin-block-start: calc(24px + 0.5em);
}

relative with right

Target
Sibling
The target moves 12 px left and its sibling stays in place.
relative with right
<div class="position-example">
  <div class="position-parent">
    <div class="position-target">Target</div>
    <div class="position-sibling">Sibling</div>
  </div>
</div>
.position-example {
  position: relative;
  direction: ltr;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  border: 1px solid;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example .position-parent {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border: 1px dashed;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example :is(.position-target, .position-sibling) {
  box-sizing: border-box;
  max-inline-size: 100%;
  overflow-wrap: anywhere;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

.position-example .position-target {
  position: relative;
  right: 12px;
}

.position-example .position-sibling {
  margin-block-start: calc(24px + 0.5em);
}

absolute with right

Target
Sibling
The target sits 12 px from the right edge of the dashed frame and leaves the flow.
absolute with right
<div class="position-example">
  <div class="position-parent">
    <div class="position-target">Target</div>
    <div class="position-sibling">Sibling</div>
  </div>
</div>
.position-example {
  position: relative;
  direction: ltr;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  border: 1px solid;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example .position-parent {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  border: 1px dashed;
  padding: 0.75em;
  padding-block-start: calc(24px + 2.5em);
}

.position-example :is(.position-target, .position-sibling) {
  box-sizing: border-box;
  max-inline-size: 100%;
  overflow-wrap: anywhere;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

.position-example .position-target {
  position: absolute;
  right: 12px;
}

.position-example .position-sibling {
  margin-block-start: calc(24px + 0.5em);
}

What sets the reference for fixed

Scroll each viewer. The ancestor uses position: relative in both. The only declaration that changes is transform.

In this screen example, fixed takes the box out of flow. With transform: none, it measures insets from the viewer document’s viewport. The identity transform translate(0) establishes a containing block for fixed even though the ancestor stays in the same visual position.

fixed with transform: none

As the viewer document scrolls, the box stays 12 px from the top and left of the viewer viewport.
fixed with transform: none
<div class="position-fixed-example">
  <div class="position-before">Start of the document</div>
  <div class="position-ancestor">
    Box ancestor
    <div class="position-fixed-target">Fixed box</div>
  </div>
  <div class="position-after">After the ancestor</div>
</div>
.position-fixed-example {
  padding-inline: 0.75rem;
  overflow-wrap: anywhere;
}

.position-fixed-example .position-before {
  box-sizing: border-box;
  min-block-size: 6rem;
  padding-block: 3.25rem 0.75rem;
}

.position-fixed-example .position-ancestor {
  box-sizing: border-box;
  position: relative;
  transform: none;
  min-block-size: 17rem;
  border: 1px dashed;
  padding: 0.75rem;
  padding-block-start: 3.25rem;
}

.position-fixed-example .position-fixed-target {
  box-sizing: border-box;
  position: fixed;
  inset-block-start: 12px;
  inset-inline-start: 12px;
  max-inline-size: calc(100% - 24px);
  background: Canvas;
  color: CanvasText;
  border: 1px solid;
  padding: 0.375rem 0.5rem;
}

.position-fixed-example .position-after {
  min-block-size: 20rem;
  padding-block-start: 0.75rem;
}

fixed with translate(0)

The box moves with the ancestor during scrolling. It stays 12 px from the inner edges of the dashed frame.
fixed with translate(0)
<div class="position-fixed-example">
  <div class="position-before">Start of the document</div>
  <div class="position-ancestor">
    Box ancestor
    <div class="position-fixed-target">Fixed box</div>
  </div>
  <div class="position-after">After the ancestor</div>
</div>
.position-fixed-example {
  padding-inline: 0.75rem;
  overflow-wrap: anywhere;
}

.position-fixed-example .position-before {
  box-sizing: border-box;
  min-block-size: 6rem;
  padding-block: 3.25rem 0.75rem;
}

.position-fixed-example .position-ancestor {
  box-sizing: border-box;
  position: relative;
  transform: translate(0);
  min-block-size: 17rem;
  border: 1px dashed;
  padding: 0.75rem;
  padding-block-start: 3.25rem;
}

.position-fixed-example .position-fixed-target {
  box-sizing: border-box;
  position: fixed;
  inset-block-start: 12px;
  inset-inline-start: 12px;
  max-inline-size: calc(100% - 24px);
  background: Canvas;
  color: CanvasText;
  border: 1px solid;
  padding: 0.375rem 0.5rem;
}

.position-fixed-example .position-after {
  min-block-size: 20rem;
  padding-block-start: 0.75rem;
}

When sticky sticks

Focus or scroll the region in each viewer. The same dashed section contains the header and limits its travel in both.

The header keeps its space in flow. Once its top edge reaches the 12 px threshold in the scroll region it uses as a reference, it can stay there while it fits within the section. When it reaches the lower boundary, scrolling farther carries the header away with the section.

sticky with overflow: hidden

The section becomes the scroll reference. Scrolling the outer region carries the header away from the edge of the viewer along with the section.
sticky with overflow: hidden
<div class="position-sticky-example" role="region" aria-label="Outer scroll region" tabindex="0">
  <div class="position-before">Before the section</div>
  <section class="position-section">
    <p>Section that limits travel</p>
    <h2 class="position-sticky-target">Sticky header</h2>
  </section>
  <div class="position-after">After the section</div>
</div>
.position-sticky-example {
  block-size: min(100dvb, 16rem);
  overflow: auto;
  overscroll-behavior: contain;
  overflow-wrap: anywhere;
}

.position-sticky-example:focus-visible {
  outline: 2px solid CanvasText;
  outline-offset: -2px;
}

.position-sticky-example .position-before {
  box-sizing: border-box;
  min-block-size: 4rem;
  padding: 0.75rem;
}

.position-sticky-example .position-section {
  box-sizing: border-box;
  min-block-size: 15rem;
  outline: 1px dashed;
  outline-offset: -1px;
  overflow: hidden;
}

.position-sticky-example .position-section p {
  margin: 0;
  padding: 0.25rem 0.75rem;
}

.position-sticky-example .position-sticky-target {
  position: sticky;
  inset-block-start: 12px;
  inset-block-end: auto;
  margin: 0;
  font: inherit;
  font-weight: bold;
  background: Canvas;
  color: CanvasText;
  border-block: 1px solid;
  padding: 0.375rem 0.75rem;
}

.position-sticky-example .position-after {
  min-block-size: 21rem;
  padding: 0.75rem;
}

sticky with overflow: clip

The outer region remains the reference. The header reaches the 12 px threshold, sticks, then moves away with the lower boundary of the section.
sticky with overflow: clip
<div class="position-sticky-example" role="region" aria-label="Outer scroll region" tabindex="0">
  <div class="position-before">Before the section</div>
  <section class="position-section">
    <p>Section that limits travel</p>
    <h2 class="position-sticky-target">Sticky header</h2>
  </section>
  <div class="position-after">After the section</div>
</div>
.position-sticky-example {
  block-size: min(100dvb, 16rem);
  overflow: auto;
  overscroll-behavior: contain;
  overflow-wrap: anywhere;
}

.position-sticky-example:focus-visible {
  outline: 2px solid CanvasText;
  outline-offset: -2px;
}

.position-sticky-example .position-before {
  box-sizing: border-box;
  min-block-size: 4rem;
  padding: 0.75rem;
}

.position-sticky-example .position-section {
  box-sizing: border-box;
  min-block-size: 15rem;
  outline: 1px dashed;
  outline-offset: -1px;
  overflow: clip;
}

.position-sticky-example .position-section p {
  margin: 0;
  padding: 0.25rem 0.75rem;
}

.position-sticky-example .position-sticky-target {
  position: sticky;
  inset-block-start: 12px;
  inset-block-end: auto;
  margin: 0;
  font: inherit;
  font-weight: bold;
  background: Canvas;
  color: CanvasText;
  border-block: 1px solid;
  padding: 0.375rem 0.75rem;
}

.position-sticky-example .position-after {
  min-block-size: 21rem;
  padding: 0.75rem;
}

With both insets on the axis set to auto, the box follows its normal flow position on that axis. Try that preset in the Scrolling scene.

The header uses position: sticky. This card shows support for overflow: clip, the declaration that lets the outer scroll region remain the reference in the second viewer.

Physical and logical edges

Compare the first two boxes with horizontal-tb and rtl. Then rotate the axes with vertical-rl.

inset groups top, right, bottom, and left in that physical order. With two values, the first repeats at the bottom and the second at the left. With three, the second repeats at the left.

inset: 12px
All four edges get 12px.
inset: 12px 24px
Top and bottom get 12px. Right and left get 24px.
inset: 12px 24px 10%
Top gets 12px, bottom gets 10%, and both sides get 24px.
inset: 12px 24px 10% auto
Top gets 12px, right 24px, bottom 10%, and left auto.

inset-block and inset-inline assign one or two values to the start and end of each axis. A single value applies to both ends. In horizontal-tb, the block axis starts at the top and direction changes the inline start from left to right. In vertical-rl, the block axis starts at the right and the inline axis starts at the top with ltr and at the bottom with rtl.

Physical inset with rtl

A
A sits 12 px from the top and left. Text direction leaves those physical edges in place.
Physical inset with rtl
<div class="position-inset-example">
  <div class="position-inset-target">A</div>
</div>
.position-inset-example {
  position: relative;
  direction: rtl;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  block-size: 10rem;
  border: 1px solid;
}

.position-inset-example .position-inset-target {
  position: absolute;
  inset: 12px auto auto 12px;
  inline-size: auto;
  block-size: auto;
  margin: 0;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

Logical start with rtl

A
The block axis still starts at the top. The inline start moves to the right.
Logical start with rtl
<div class="position-inset-example">
  <div class="position-inset-target">A</div>
</div>
.position-inset-example {
  position: relative;
  direction: rtl;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  block-size: 10rem;
  border: 1px solid;
}

.position-inset-example .position-inset-target {
  position: absolute;
  inset-block-start: 12px;
  inset-block-end: auto;
  inset-inline-start: 12px;
  inset-inline-end: auto;
  inline-size: auto;
  block-size: auto;
  margin: 0;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

Logical axes in vertical-rl

A
With ltr, the 12 px offsets are measured from the right on the block axis and from the top on the inline axis.
Logical axes in vertical-rl
<div class="position-inset-example">
  <div class="position-inset-target">A</div>
</div>
.position-inset-example {
  position: relative;
  direction: ltr;
  writing-mode: vertical-rl;
  box-sizing: border-box;
  inline-size: 10rem;
  block-size: 100%;
  border: 1px solid;
}

.position-inset-example .position-inset-target {
  position: absolute;
  inset-block: 12px auto;
  inset-inline: 12px auto;
  inline-size: auto;
  block-size: auto;
  margin: 0;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

Percentages on opposite edges

A
The 10% left inset and 25% right inset leave 65% of the frame width for A. The 10% top inset uses the height.
Percentages on opposite edges
<div class="position-inset-example">
  <div class="position-inset-target">A</div>
</div>
.position-inset-example {
  position: relative;
  direction: ltr;
  writing-mode: horizontal-tb;
  box-sizing: border-box;
  inline-size: 100%;
  block-size: 10rem;
  border: 1px solid;
}

.position-inset-example .position-inset-target {
  position: absolute;
  top: 10%;
  bottom: auto;
  left: 10%;
  right: 25%;
  inline-size: auto;
  block-size: auto;
  margin: 0;
  padding: 0.25em 0.5em;
  border: 1px solid;
}

For the absolute box in the example, top and bottom percentages use the height of the containing block. Left and right use its width. With two opposite edges set and an automatic width, the box stretches between them. Set one edge back to auto and the content determines how much width the box takes.

Common pitfalls

Start with the result you expected and check which rule or ancestor determines it.

The inset does not move the box

Expected result
Move the box 12 px from its original position and keep its space.
Cause
With position: static, the insets have no effect.
Solution
Use position: relative and inset-block-start: 12px. Check that the sibling stays in place.

The fixed box scrolls with the page

Expected result
Keep the same distance from the viewport.
Cause
An ancestor with a transform value other than none establishes the containing block for fixed. Even translate(0) does so.
Solution
Inspect the ancestors. If the box should follow the viewport, remove the transform that contains it or move the box outside that ancestor.

The sticky header does not stick

Expected result
Keep the header 12 px from the start after it reaches the threshold.
Cause
Both insets on the axis may be auto, an ancestor with overflow: hidden may change the scrollport, or the section may end before the header has room to travel.
Solution
Set inset-block-start: 12px, check which region scrolls, and leave room before the section ends. If you only need to clip the ancestor, try overflow: clip and check whether you relied on its formatting context.

The edge is opposite the reading start

Expected result
Follow the inline start when switching from LTR to RTL.
Cause
The left property and the physical inset shorthand still measure from the left edge.
Solution
Use inset-inline-start to follow the inline axis and check direction and writing-mode.