Home > database >  How to close the gap between two rotated rectangles forming a V?
How to close the gap between two rotated rectangles forming a V?

Time:06-08

So there are these two rotated rectangles forming a V:

div {
  position: relative;
  left: 100px;
  display: inline-block;
  margin-left: 100px;
}
div::before {
  content: '';
  position: absolute;
  transform: rotate(-45deg);
  transform-origin: bottom right;
  width: 20px;
  height: 100px;
  background: red;
}
div::after {
  content: '';
  position: absolute;
  transform: rotate(45deg);
  transform-origin: bottom left;
  width: 20px;
  height: 100px;
  background: red;
}
<div></div>

Now how to close the gap between them? Frist I thought the key would be transform-origin. But no matter what is set there, it can't result in a isosceles V without a gap.

CodePudding user response:

If you're already using transforms, you could add a translate to each and just offset them till they cross over

html {
  font-size: 16px;
}

div {
  position: relative;
  left: 100px;
  display: inline-block;
  margin-left: 100px;
}

div::before {
  content: '';
  position: absolute;
  transform: translateX(0.523em) rotate(-45deg);
  transform-origin: bottom;
  width: 1.25rem;
  height: 6.25rem;
  background: blue;
  border: 2px solid blue;
}

div::after {
  content: '';
  position: absolute;
  transform: translateX(-0.53rem) rotate(45deg);
  transform-origin: bottom;
  width: 1.25rem;
  height: 6.25rem;
  background: red;
  border: 2px solid red;
}
<div></div>

CodePudding user response:

You have to update transform-origin like below. You don't need to consider bottom but a slightly upper than bottom (100% - 10px). 10px is half the width

div {
  position: relative;
  left: 100px;
  display: inline-block;
  margin-left: 100px;
  width: 20px;
  height: 100px;
}
div::before,
div::after{
  content: '';
  position: absolute;
  inset: 0;
  transform: rotate(-45deg);
  transform-origin: 50% calc(100% - 10px);
  background: red;
}

div::after {
  transform: rotate(45deg);
  background: blue;
}
<div></div>

  • Related