What is the best way to calculate the change in location caused by the rotation of a border?

After spending a significant amount of time searching today, I have yet to find a solution to my specific issue.

I modified a SASS mixin we use for generating CSS open arrows in our projects. These are typically used for faux arrows and navigation elements.

The challenge I'm facing is determining the correct positioning shift for the element when:

a) A rotation occurs

b) The width of the line/border changes.

I know that there is probably some simple math involved in solving this problem, but it has been so long since I've delved into these calculations!

Here's an example snippet of code:

p {   
    color: red;
    width: 100px;
    font-family: Arial;
    text-transform: uppercase;
    position: relative;
}
  
p::before {
    content: '';
    display: inline-block;
    position: absolute;
    top: 50%;
    right: 0rem;
    height: .125rem;
    width: .875rem;
    background: currentColor;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    transition: .25s cubic-bezier(0.39, 0.575, 0.565, 1);
    transform-style: preserve-3d;
    transform: translateX(-100%) translateY(-100%) rotate(45deg);
    z-index: 1;
}

p::after {
    content: '';
    display: inline-block;
    position: absolute;
    top: 50%;
    right: 0rem;
    height: .125rem;
    width: .875rem;
    background: currentColor;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    transition: .25s cubic-bezier(0.39, 0.575, 0.565, 1);
    transform-style: preserve-3d;
    transform: translateY(-100%) rotate(135deg);
    z-index: 1;
}
<p>Arrow</p>

This is what the mixin looks like:

@mixin arrow($width: 1, $length: 6, $position: 0, $placement: right, $rotate: 0) {

    position: relative;

      &:before,
      &:after {
          content: '';
          display: inline-block;
          position: absolute;
          top: 50%;
          @if ($placement==right) {
            right: rem($position);
          } @else {
            left: rem($position);
          }
          height: rem($width);
          width: rem($length);
          background: currentColor;
          backface-visibility: hidden;
          transition: $transition-speed $transition-easeInOutSine;
          transform-style: preserve-3d;
          transform: translateZ(0) rotate(#{$rotate}deg);

      }
      &:before {
          transform: translateX(-100%) translateY(-100%) rotate(#{$rotate + 45}deg);
      }
      &:after {
          transform: translateY(-100%) rotate(#{$rotate + 135}deg);
      }

  .is-active &,
      .is-open & {
          &:before {
              transform: translateX(100%) translateY(-100%) rotate(-#{$rotate + 45}deg);
          }
          &:after {
              transform: translateY(-100%) rotate(#{$rotate + 225}deg);
          }
      }

}

Here are some examples:

https://i.sstatic.net/MBCLO.png

https://i.sstatic.net/f6thE.png

I hope I have explained my issue clearly. Essentially, I am trying to figure out how to close the gap based on the given numbers!

Thank you for your help in advance!

Please overlook any other imperfections, as this is still a work in progress.

Answer №1

My suggestion is to forget about attempting to line up 2 distinct elements and instead fashion your arrow using a rotated square.

.arrow{
  position: relative;
  padding-right: 18px;
}

.arrow::after{
  position: absolute;
  right: 0;
  top: 0;
  width:10px;
  height:10px;
  content: "";
  transform: rotate(45deg);
  display: block;
  border-width: 2px;
  border-color: red;
  border-style: solid;
  border-top: none;
  border-left: none;
 }
<span class="arrow">Arrow</span>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determining if an object is visible on the screen (with no reliance on scrollbars)

Is there a way to optimize rendering on a website by only displaying elements on the screen and rendering others when they are in view? Similar to 3D culling, but for a website ^.^ [update] Is it not feasible to achieve this effect? [update2] I experim ...

Menu with option to delete next to each selection item

My task is to create a drop-down menu with a delete 'X' option next to each item. This functionality should work even when the drop-down is dynamically populated, without resorting to using a list as an alternative. UPDATE: The icons next to eac ...

Animating changes on a webpage using CSS transitions in conjunction with the

Can the border color of a simple line div be changed from top to bottom using GreenSock? .line { position: relative; border: 1px solid #c3ced8; width: 0; height: 50px; display: block; content: ''; z-index: 1; top: -19px; marg ...

What is the best way to update the text within a Span element as the user moves through the Jquery slider?

Can I utilize jQquery to dynamically alter the text content of my "video_box_label" span element based on the active slide in my "flexslider" slideshow? For instance, when the slideshow transitions to the second slide, I want the text to change from "Mee ...

Ways to increase the spacing between content boxes using Bootstrap

I currently have a row of three Bootstrap boxes structured like this: <div class="row"> <div class="col-lg-4 pathBoxMain"> <h2>Content</h2> </div> <div class="col-lg-4 pathBoxMain"> <h2>Content</h2> </di ...

What is the best way to align a button within a Jumbotron?

Struggling to find the right CSS placement for a button within a jumbotron. I attempted using classes like text-left or pull-left, but nothing seemed to work as I hoped. I believe a simple CSS solution exists, but it's eluding me at the moment. Ideall ...

The display of the AngularJS {{variable}} is not supported within a script tag

<script type="text/template"> <div class="abcd"> <div class="efg" data-price="{{::displayPrice}}"></div> </div> </script> I am facing an issue with a template that contains a div element with the da ...

Creating geometric designs on an image and storing them using PHP

I am attempting to retrieve an image from a specific location on my server, draw lines on it, and then save the modified image back to the same location. Below is the code I am using: function drawShapes($src_path, $json) { echo "---inside dra ...

Is there a way to prevent hyphens from appearing in Outlook email messages?

I have encountered an issue while developing an email template. When viewed in Outlook emails, the words break into two lines with hyphens. I have attempted various CSS properties on the 'td' tag, but to no avail. Is there no way to control this ...

What is the best way to make a panel width adjust automatically to fit the screen width?

I have developed a React/Material UI application that includes a Portal which showcases a Panel and a Widget. Main App: Show Portal and Set Background Color to Blue export default function App() { return ( <div style={{ backgroundC ...

What could be causing my CSS navigation toggle button to malfunction?

My attempt at creating a toggle button for tablets and phones seems to be failing. Despite the javascript class being triggered when I click the toggle button, it is not functioning as expected... https://i.stack.imgur.com/j5BN8.png https://i.stack.imgur. ...

Implementing a sticky sidebar that overlaps content on mobile with Bootstrap 4

I've been working on designing a website with Bootstrap 4, and I wanted to incorporate a sticky sidebar on the right side. My idea was to have two boxes - one for content and the other for the sidebar. Everything is displaying correctly on most devic ...

What is the most effective method for creating a central file containing multiple subdomain pages?

I am working with a file named master.php, containing a code snippet that retrieves the city and state from the subdomain and inserts it into the body. $url = "//$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $parsedUrl = parse_url($url); $host = explode(&apo ...

Thr3e.js - Illuminating Circle: A Unique Light Effect

Has anyone experimented with achieving a flashlight effect using THREE.SpotLight? // Line 110 spotLight = new THREE.SpotLight(0xffffff, 0.5, 150); spotLight.power = 6000; spotLight.angle = 0.5; spotLight.decay = 2; spotLight.penumbra = 0.1; spotLight.dist ...

Utilizing htmx for interactive elements throughout the website

When I make an htmx-request on my page, a loading spinner is displayed like this: <div class="col-4 px-4dot5 py-3 bg-secondary-light"> <label for="stellenangebotLink" class="form-label fs-5">Link</label> ...

Is the form data in AngularJS not submitting correctly?

Why is my HTML form data not being submitted using POST method? HTML View Page:- <div class="col-sm-12"> <div class="card-box"> <form class="form-inline" name="addstock" ng-submit="saveStockPurchaseInvoice()"> <h ...

Maintain the original order of rows in the table while shuffling the columns they appear in randomly

I need help with my table setup. The left column contains words in English, while the right column has the same words translated into Korean. Is there a way to keep the rows intact while randomizing the order of the columns? Here's an example: <ta ...

Toggle the menu using jQuery on unordered list items

Below is the HTML code I am using : <ul class="main-block"> <li class="firstLevel"> <a href="#category">EXAMPLE CATEGORY 1</a> <ul class="dijete"> <li class="child"> <a href="some-sub-categ ...

Two distinct styles showcasing contrasting hues

Can an element have two different styles with different colors? In simpler terms, imagine I have the following HTML code: <table class='my_table'> <th class="style1 style2">LastName</th> ... </table> and this CSS: .my_ ...

Utilizing a loop for setting variable values in JavaScript

Using both JavaScript and JQuery. Let's imagine there is an array called ListArray with various sentences inside. Sounds easy enough. Is there a way to achieve this? var List = for (var i = 0; i < 10; i++) { //iterate over an array here to c ...