Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trig functions in css transform?

Tags:

css

sass

I'm trying to put some circles around a larger circle at specific angles using transform: translate and cos and sin functions. It doesn't seem to work, and I'm not sure why.

.ctr_btn {
  background-color: red;
  color: white;
  display: block;
  display: relative;
  border-radius: 50%;
  width: 4rem;
  height: 4rem;
}
.out_btn {
  background-color: yellowgreen;
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  color: black;
  position: absolute;

  //this is the part I'm concerned about.
  transform: translateX(calc(2rem * cos(var(--angle)))) translateY(calc(2rem * sin(var(--angle))));
}
  <div class="Nav">
    <div class="ctr_btn">
      maincircle
      <div class="out_btn" style="--angle:15deg">1</div>
      <div class="out_btn" style="--angle:30deg">2</div>
      <div class="out_btn" style="--angle:45deg">3</div>
      <div class="out_btn" style="--angle:60deg">4</div>
      <div class="out_btn" style="--angle:75deg">5</div>
    </div>
  </div>

See this snippet:

https://codepen.io/spencerbug/pen/PvwaWQ

Thank you!

like image 889
spencerbug Avatar asked Jul 15 '26 14:07

spencerbug


2 Answers

You can get the same result with a little trick. Just rotate the angle that you want, translate it by the radius length, and counter rotate by the same angle. This way, the result is the same, and you don't need trig functions.

.ctr_btn {
  background-color: red;
  color: white;
  display: block;
  display: relative;
  border-radius: 50%;
  width: 4rem;
  height: 4rem;
  margin: 100px;
}
.out_btn {
  background-color: yellowgreen;
  width: 1rem;
  height: 1rem;
  padding: 1rem;
  border-radius: 50%;
  color: black;
  position: absolute;

 
  transform:  rotate(var(--angle)) translate(10em) rotate(calc(-1 * var(--angle)));
}
<div class="Nav">
    <div class="ctr_btn">
      maincircle
      <div class="out_btn" style="--angle:15deg">1</div>
      <div class="out_btn" style="--angle:30deg">2</div>
      <div class="out_btn" style="--angle:45deg">3</div>
      <div class="out_btn" style="--angle:60deg">4</div>
      <div class="out_btn" style="--angle:75deg">5</div>
    </div>
  </div>
like image 173
vals Avatar answered Jul 17 '26 03:07

vals


Beginning with Firefox 108 and Safari 15.4 (and experimentally in Chrome 111 and Edge 111), the trigonometric cos() and sin() CSS functions are now supported (at < 18% browser coverage for either function, pls see this reference for example).

For the full list of trigonometric functions, please see the W3C documentation.

like image 38
CSSBurner Avatar answered Jul 17 '26 04:07

CSSBurner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!