CSS - transform-box 属性



CSS 属性transform-box 确定变换属性相关的布局框,例如transformtranslatescalerotatetransform-origin

可能的值

CSS 属性transform-box 可以包含以下关键字值之一:

  • content-box:这是默认值。变换相对于元素的内容框应用。

  • border-box:变换相对于元素的边框框应用。

  • fill-box:变换相对于元素的绘制区域应用,不包括任何笔触。

  • stroke-box:变换相对于笔触边界框应用,包括笔触区域及其占据的空间。

  • view-box:变换相对于最近的 SVG 视窗应用。

应用于

所有可变换的元素。

语法

transform-box = content-box | border-box | fill-box | stroke-box | view-box;

CSS transform-box - 示例

通过并排比较盒子,您应该能够观察到如下所示的盒子变换。

<html>
<head>
<style>
   svg {
      width: 120vh;
      border: 1px solid #d9d9d9;
      position: relative;
      margin: auto;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
   }

   #box {
      transform-origin: 50% 50%; /* anything other than `0 0` to see the effect */
      transform-box: content-box;
      animation: rotateBox 3s linear infinite;
   }
   #box1 {
      transform-origin: 50% 50%; /* anything other than `0 0` to see the effect */
      transform-box: border-box;
      animation: rotateBox 3s linear infinite;
   }
   #box2 {
      transform-origin: 50% 50%; /* anything other than `0 0` to see the effect */
      transform-box: view-box;
      animation: rotateBox 3s linear infinite;
   }
   #box3 {
      transform-origin: 50% 50%; /* anything other than `0 0` to see the effect */
      transform-box: fill-box;
      animation: rotateBox 3s linear infinite;
   }
   #box4 {
      transform-origin: 50% 50%; /* anything other than `0 0` to see the effect */
      transform-box: stroke-box;
      animation: rotateBox 3s linear infinite;
   }

   @keyframes rotateBox {
      to {
         transform: rotate(360deg);
      }
   }
</style>
</head>
<body>
   <svg id="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
      <text x="0" y="5" fill="red" font-size="5px">content-box</text>
      <g stroke-width="1">
         <circle id="center" fill="red" r="1" transform="translate(25 25)" />
         <circle id="boxcenter" fill="blue" r=".5" transform="translate(15 15)" />
         <rect
            id="box"
            x="10"
            y="10"
            width="10"
            height="10"
            rx="1"
            ry="1"
            stroke="black"
            fill="none" />
      </g>
      </svg>
      <br/>
      <svg id="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
      <text x="0" y="5" fill="red" font-size="5px">border-box</text>
      <g>
         <circle id="center" fill="red" r="1" transform="translate(25 25)" />
         <circle id="boxcenter" fill="blue" r=".5" transform="translate(15 15)" />
         <rect
            id="box1"
            x="10"
            y="10"
            width="10"
            height="10"
            rx="1"
            ry="1"
            stroke="black"
            fill="none" />
      </g>
      </svg>
      <br/>
      <svg id="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
      <text x="0" y="5" fill="red" font-size="5px">view-box</text>
      <g>
         <circle id="center" fill="red" r="1" transform="translate(25 25)" />
         <circle id="boxcenter" fill="blue" r=".5" transform="translate(15 15)" />
         <rect
            id="box2"
            x="10"
            y="10"
            width="10"
            height="10"
            rx="1"
            ry="1"
            stroke="black"
            fill="none" />
      </g>
      </svg>
      <br/>
      <svg id="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
      <text x="0" y="5" fill="red" font-size="5px">fill-box</text>
      <g>
         <circle id="center" fill="red" r="1" transform="translate(25 25)" />
         <circle id="boxcenter" fill="blue" r=".5" transform="translate(15 15)" />
         <rect
            id="box3"
            x="10"
            y="10"
            width="10"
            height="10"
            rx="1"
            ry="1"
            stroke="black"
            fill="none" />
      </g>
      </svg>
      <br/>
      <svg id="svg" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 50 50">
      <text x="0" y="5" fill="red" font-size="5px">stroke-box</text>
      <g>
         <circle id="center" fill="red" r="1" transform="translate(25 25)" />
         <circle id="boxcenter" fill="blue" r=".5" transform="translate(15 15)" />
         <rect
            id="box4"
            x="10"
            y="10"
            width="10"
            height="10"
            rx="1"
            ry="1"
            stroke="black"
            fill="none" />
      </g>
      </svg>
</body>
</html>
广告