css的rotate()函数中的旋转轴。

huangapple go评论73阅读模式
英文:

Axis of rotate() of css

问题

我参考了其他地方的代码,根据这个代码,它正在创建一个绿色的正方形。但我认为代码应该创建一个十字架。

如果我运行代码,它会像下面的图片一样制作一个正方形。

css的rotate()函数中的旋转轴。

看起来所有的线都有不同的旋转轴。
你能告诉我为什么它不是制作十字架而是正方形吗?

代码:

import styled from "styled-components";

export default function App() {
  const array = [0, 1, 2, 3];

  return (
    <Wrap>
      <div className="container">
        <div className="square">
          {array.map((idx) => {
            return <Line number={idx} />;
          })}
        </div>
      </div>
    </Wrap>
  );
}

const Wrap = styled.div`
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  min-height: 100vh;
  justify-content: center;
  align-items: center;
  background: #111;

  .container {
    border: 3px solid orange;
    position: relative;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .container .square {
    top: 200px;
    left: 200px;
    border: 1px solid blue;
    position: absolute;
    width: 200px;
    height: 200px;
  }
`;

const Line = styled.span`
  border: 1px solid red;
  position: absolute;
  inset: 10px;
  transform: ${(props) => `rotate(calc(90deg * ${props.number}))`};

  &::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 4px;
    background: #0f0;
  }
`;

CodeSandBox
https://codesandbox.io/s/zigzag-f2uybc?file=/src/App.js:0-1864

英文:

I refered the code elsewhere and following the code, it's making a green line square.
But I think the code has to make a cross not the square.

If I run the code it makes the squre lik the picture below

css的rotate()函数中的旋转轴。

It seems that all lines have different rotation axis.
Can you let me know why it's not making a cross but square?

code:

import styled from "styled-components";

export default function App() {
  const array = [0, 1, 2, 3];

  return (
    <Wrap>
      <div className="container">
        <div className="square">
          {array.map((idx) => {
            return <Line number={idx} />;
          })}
        </div>
      </div>
    </Wrap>
  );
}

const Wrap = styled.div`
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  min-height: 100vh;
  justify-content: center;
  align-items: center;
  background: #111;

  .container {
    border: 3px solid orange;
    position: relative;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .container .square {
    top: 200px;
    left: 200px;
    border: 1px solid blue;
    position: absolute;
    width: 200px;
    height: 200px;
  }
`;

const Line = styled.span`
  border: 1px solid red;
  position: absolute;
  inset: 10px;
  transform: ${(props) => `rotate(calc(90deg * ${props.number}))`};

  &::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 4px;
    background: #0f0;
  }
`;

CodeSandBox
https://codesandbox.io/s/zigzag-f2uybc?file=/src/App.js:0-1864

答案1

得分: 1

你的 Line 组件是一个正方形:

  position: absolute;
  inset: 10px;

因为没有明确的宽度和高度,一个绝对定位的盒子将从 leftright 属性推断宽度,从 topbottom 属性推断高度,相对于第一个具有 absoluterelativefixedsticky 定位的父元素。

-> Line 的父元素是 <div className="square"> 元素。
所以 Line 将是一个正方形。

卷轴线是 Line 的伪元素之前的部分:

    position: absolute;
    width: 100%;
    height: 4px;

并且没有定位属性 (top, bottom, left, right),它将从第一个定位父元素的左上角开始。

-> 所以每个 Line 组件是一个透明的正方形,顶部有一条绿线。
当你将正方形旋转90度时,线条会跟随,保持相对于正方形的相同位置。


如果你想要它形成一个十字:

你可以将伪元素居中在 Line 组件中,例如只需添加:

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

Line 组件的 CSS 中。

英文:

Your Line component is a square :

  position: absolute;
  inset: 10px;

Because without a explicit width and a height, a box in a absolute position with have inferred width from left and right properties, and inferred height from the top and bottom properties, so relative to the first parent with a absolute, relative, fixed or sticky positioning.

-> Line parent is the &lt;div className=&quot;square&quot;&gt; element.
So Line will be square.

The reel line is the Line before pseudo element :

    position: absolute;
    width: 100%;
    height: 4px;

And without positioning (top, bottom, left, right), it will start from the top left corner of the first positioned parent.

-> So Every Line component is a transparent square with a green line on top.
When you rotating the square by 90°, the line follows, keeping the same position relative to the square.


If you want it to form a cross :

You can center the speudo element in the Line component, for exemple just add :

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

In the CSS of the Line component.

huangapple
  • 本文由 发表于 2023年4月17日 16:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定