<div class="not-supported"> Your browser doesn't support this demo. It is only supported in Chrome 125+</div>

<main dir="ltr">
  <div class="anchor">anchor</div>
  <div class="target">target</div>
  <div class="SPACE">
    <div>
</main>

<form onsubmit="return applyStyles(event)">
  <header>
    <h2>Edit Styles</h2>
    <button class="toggleDirection" type="button" onclick="toggleDirection()">LTR</button>
    <span class="toggleDirectionTooltip">Toggle Direction</span>
  </header>
  <div class="targetStyle">
    <span class="className">.target {</span>

    <div class="property">
      <span class="propertyName">position-area: </span>
      <input class="styleInput" id="positionAreaInput" value="top right" placeholder="styles here..." />
    </div>

    <div class="property">
      <span class="propertyName">position-try: </span>
      <input class="styleInput" id="positionTryInput" value="" placeholder="styles here..." />
    </div>

    <span>}</span>
  </div>
  <button type="submit">Apply Styles</button>
</form>
.anchor {
  anchor-name: --my-anchor;
}

.target {
  position: absolute;
  position-anchor: --my-anchor;

  position-area: top right;
}

/* Aesthetic Changes */

:root {
  --anchor-size: clamp(60px, 20vw, 80px);
}

* {
  margin: 0px;
  padding: 0px;
  box-sizing: border-box;
}

body {
  display: flex;
  flex-flow: row-reverse wrap;
  justify-content: center;
  align-items: center;

  gap: 20px 40px;

  padding: 20px;

  height: 100vh;

  font-family: monospace;
}

main {
  display: flex;
  align-items: center;
  justify-content: center;

  border: 2px solid black;
  border-radius: 10px;

  height: calc(var(--anchor-size) * 4);
  aspect-ratio: 1;

  overflow: scroll;
  position: relative;
  scrollbar-width: thin;
}

.SPACE {
  position: absolute;
  height: calc(var(--anchor-size) * 9);
  aspect-ratio: 1;
}

.anchor {
  display: flex;
  align-items: center;
  justify-content: center;

  border-radius: 10px;

  width: var(--anchor-size);
  aspect-ratio: 1;

  background-color: #ffbd59;
}

.target {
  display: flex;
  align-items: center;
  justify-content: center;

  width: var(--anchor-size);
  aspect-ratio: 1;

  border-radius: 10px;

  background-color: #cb6ce6;
}

header {
  display: flex;
  justify-content: space-between;
  aling-items: center;
}

form {
  flex: 1 1 250px;
  max-width: 600px;

  display: flex;
  flex-flow: column;
  gap: 10px;
}

form input {
  all: unset;

  flex: 1 1 0;

  border: 1px solid grey;
  border-radius: 5px;
  padding: 5px;
}

form button {
  all: unset;

  display: inline-block;

  border-radius: 4px;
  padding: 0 16px;

  height: 36px;
  min-width: 64px;

  cursor: pointer;
  font-weight: 500;

  line-height: 1.15;
  word-spacing: 0px;
  font-size: 14px;
  text-transform: uppercase;
  text-align: center;

  color: #000;
  background: #ffbd59;
  box-shadow: 0px 3px 1px -2px rgb(0 0 0 / 20%),
    0px 2px 2px 0px rgb(0 0 0 / 14%), 0px 1px 5px 0px rgb(0 0 0 / 12%);
  transition: box-shadow 280ms cubic-bezier(0.4, 0, 0.2, 1);

  &:hover {
    box-shadow: 0px 2px 4px -1px rgb(0 0 0 / 20%),
      0px 4px 5px 0px rgb(0 0 0 / 14%), 0px 1px 10px 0px rgb(0 0 0 / 12%);
  }
}

.targetStyle {
  display: flex;
  flex-flow: column;
  gap: 10px;

  width: 100%;

  padding: 15px;
  border-radius: 10px;

  font-weight: 600;

  background-color: #1d1e23;
  color: #fff;
}

.className {
  color: #d3c47a;
}

.property {
  display: flex;
  align-items: center;
  gap: 10px;
}

.propertyName {
  color: #887573;
}

.toggleDirection {
  anchor-name: --toggle-direction;
}

.toggleDirection:hover + .toggleDirectionTooltip {
  pointer-events: initial;
  visibility: visible;
  opacity: 1;
}

.toggleDirectionTooltip {
  pointer-events: none;
  visibility: none;
  opacity: 0;

  position: absolute;
  position-anchor: --toggle-direction;
  inset-area: top;

  padding: 10px;
  border-radius: 10px;
  margin-bottom: 10px;

  font-size: 10px;

  background-color: #1d1e23d0;
  color: #fff;
  transition: all 50ms ease-in;
}

.not-supported {
  position: fixed;
  top: 0;
  left: 0;
  z-index: 2;

  display: flex;
  align-items: center;
  justify-content: center;

  width: 100vw;
  height: 40px;

  font-weight: 600;

  background-color: #ffbd59aa;
}

@supports (inset-area: top right) {
  .target {
    inset-area: top right;
  }
}

@supports (position-area: top) or (inset-area: top) {
  .not-supported {
    display: none;
  }
}
function toggleDirection() {
  const toggleDirectionButton = document.querySelector(".toggleDirection");
  const mainContainer = document.querySelector("main");
  const nextDirection =
    mainContainer.getAttribute("dir") === "ltr" ? "rtl" : "ltr";
  mainContainer.setAttribute("dir", nextDirection);
  toggleDirectionButton.innerText = nextDirection;
}

function applyStyles(event) {
  event.preventDefault();

  const positionAreaInput = document.querySelector("#positionAreaInput").value;
  const positionTryInput = document.querySelector("#positionTryInput").value;

  const target = document.querySelector(".target");

  // Create a new style element
  const styleElement = document.createElement("style");
  styleElement.textContent = `.target { 
    inset-area: ${positionAreaInput};  
    position-area: ${positionAreaInput};
    position-try: ${positionTryInput}; }`;

  // Remove any previous style elements for the .target class
  document.querySelectorAll("style[data-target]").forEach((el) => el.remove());

  // Append the new style element
  styleElement.setAttribute("data-target", "true");
  document.head.appendChild(styleElement);
}
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.