0

How to centre this type of arrows? I tried with transform but it didn't work. Ex:

.test {
  width: 500px;
  height: 500px;
  background: pink;
  position: relative;
}

.test::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    display: inline-block;
    width: 60px;
    height: 60px;
    border-right: 2px solid red;
    border-bottom: 2px solid red;
    transform: rotate(135deg) translate(-50%, -50%);
}
<div class="test"></div>
Cloudy Skies
  • 63
  • 2
  • 8

2 Answers2

2

You can simplify the CSS for arrow::before by making arrow by using the CSS in the code snippet.

.test {
  width: 500px;
  height: 500px;
  background: pink;
  display: flex;
  align-items:center;
  justify-content: center;
  /*positiom: relative;*/
}

.test::before {
    content: '';
  /*position: absolute;*/
   /*top: 50%;*/
   /*left: 50%;
  /*display: inline-block; */
    width: 60px;
    height: 60px;
    border-right: 2px solid red;
    border-bottom: 2px solid red;
    transform: rotate(135deg) translate(-20%, -20%);
}
<div class="test"></div>

Aside from saving you a few lines of code you don't have to mess around with absolute positioning in case you want to make adjustments in the future.

Matt Croak
  • 2,788
  • 2
  • 17
  • 35
0

Translate then rotate, order is important

.test {
  width: 500px;
  height: 500px;
  background: 
   linear-gradient(black,black) center/10px 10px no-repeat,
   pink;
  position: relative;
}

.test::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    display: inline-block;
    width: 60px;
    height: 60px;
    border-right: 2px solid red;
    border-bottom: 2px solid red;
    transform: translate(-50%, -50%) rotate(135deg);
}
<div class="test"></div>

And if you want to consider only the arrow, adjust the left position:

.test {
  width: 500px;
  height: 500px;
  background: 
   linear-gradient(black,black) center/10px 10px no-repeat,
   pink;
  position: relative;
}

.test::before {
    content: '';
    position: absolute;
    top: 50%;
    left: calc(50% + 15px);
    display: inline-block;
    width: 60px;
    height: 60px;
    border-right: 2px solid red;
    border-bottom: 2px solid red;
    transform: translate(-50%, -50%) rotate(135deg);
}
<div class="test"></div>

Or the translation:

.test {
  width: 500px;
  height: 500px;
  background: 
   linear-gradient(black,black) center/10px 10px no-repeat,
   pink;
  position: relative;
}

.test::before {
    content: '';
    position: absolute;
    top: 50%;
    left: 50%;
    display: inline-block;
    width: 60px;
    height: 60px;
    border-right: 2px solid red;
    border-bottom: 2px solid red;
    transform: translate(-25%, -50%) rotate(135deg);
}
<div class="test"></div>
Temani Afif
  • 245,468
  • 26
  • 309
  • 415
  • 1
    Beat me to it. Here is the [JSFiddle demo](https://jsfiddle.net/zvLexnay/) I was working on. – Daan Oct 10 '19 at 14:59