[CSS] CSS anchor positioning

Browser support: https://caniuse.com/?search=anchor%20positioning

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #div1 {
        width: 100px;
        height: 100px;
        background-color: red;
        resize: both;
        overflow: auto;
        anchor-name: --my-anchor;
      }
      #div2 {
        width: 100px;
        height: 100px;
        background-color: blue;
        position: absolute;
        position-anchor: --my-anchor;
        left: anchor(right);
        top: anchor(bottom);
      }
    </style>
  </head>
  <body>
    <h2>CSS anchor positioning (CSS锚点定位)</h2>
    <div id="div1"></div>
    <div id="div2"></div>
  </body>
</html>

 

If want to put #div2 inside #dev1, we can do:

      #div2 {
        width: 40px;
        height: 40px;
        background-color: blue;
        position: absolute;
        position-anchor: --my-anchor;
        left: anchor(right);
        top: anchor(bottom);

        transform: translate(-200%, -200%);
      }

posted @ 2025-05-11 15:13  Zhentiw  阅读(17)  评论(0)    收藏  举报