Search code examples
htmlcssfrontendweb-frontend

How do i stack elements without them interfering with each other, without using "position: absolute"?


How would i convert this to be able to use dynamic scaling? My main problem is that i don't know how i would go about the fan(the meter-scale's) without having the individual meter-scale's position be absolute. This all comes down to the question of: how do i stack elements without them interfering with each other, without using position: absolute?

body {
  background-color: #151515;
}

.meter {
  height: 400px;
}

.meter-pointer {
  width: 2px;
  height: 200px;
  background: #2c3e50;
  transform: rotate(45deg);
  transform-origin: bottom;
  transition: transform 0.5s;
  position: absolute;
  margin-left: 50%;
}

.meter-dot {
  width: 10px;
  height: 10px;
  background: #2c3e50;
  border-radius: 50%;
  position: absolute;
  margin-top: calc(200px - 5px);
  margin-left: calc(50% - 5px);
}

.meter-scale {
  color: #cfcfcf;
  width: 1px;
  height: 200px;
  transform-origin: bottom;
  transition: transform 0.2s;
  box-sizing: border-box;
  border-top: 10px solid;
  position: absolute;
  margin-left: 50%;
}

.meter-scale-strong {
  width: 2px;
  border-top-width: 20px;
}
<div class="meter">
  <div class="meter-dot"></div>
  <div class="meter-scale meter-scale-strong" style="transform: rotate(-45deg);"></div>
  <div class="meter-scale" style="transform: rotate(-36deg);"></div>
  <div class="meter-scale" style="transform: rotate(-27deg);"></div>
  <div class="meter-scale" style="transform: rotate(-18deg);"></div>
  <div class="meter-scale" style="transform: rotate(-9deg);"></div>
  <div class="meter-scale meter-scale-strong" style="transform: rotate(0deg);"></div>
  <div class="meter-scale" style="transform: rotate(9deg);"></div>
  <div class="meter-scale" style="transform: rotate(18deg);"></div>
  <div class="meter-scale" style="transform: rotate(27deg);"></div>
  <div class="meter-scale" style="transform: rotate(36deg);"></div>
  <div class="meter-scale meter-scale-strong" style="transform: rotate(45deg);"></div>
  <div class="meter-pointer" style="transform: rotate(12deg);"></div>
</div>


Solution

  • My idea using gradient, mask and clip-path where you don't need a lot of html code

    .meter {
      width:300px; /* it's responsive, simply change the width */
      display:grid;
      margin:auto;
    }
    .meter::before,
    .meter::after,
    .meter i{
      content:"";
      grid-area:1/1;
      padding-top:50%;
    }
    .meter::before,
    .meter i{
      border-radius:400px 400px 0 0;
      clip-path:polygon(-10% 0,110% 0,50% 100%);
      background:repeating-conic-gradient(from -.5deg at bottom,red 0 1deg,#0000 0 9deg);
      -webkit-mask:radial-gradient(farthest-side at bottom,#0000 calc(100% - 15px),#000 0); 
    }
    .meter i {
      background:repeating-conic-gradient(from -.5deg at bottom,red 0 1deg,#0000 0 45deg);
      -webkit-mask:radial-gradient(farthest-side at bottom,#0000 calc(100% - 30px),#000 0); 
    }
    .meter::after {
      width:20px;
      z-index:1;
      margin:0 auto -10px;
      background:
        linear-gradient(blue 0 0) top/3px calc(100% - 10px),
        radial-gradient(farthest-side,blue 97%,#0000) bottom/20px 20px;
      background-repeat:no-repeat;
      transform-origin:50% calc(100% - 10px);
      transform:rotate(10deg); /* adjust this */
    }
    <div class="meter"><i></i></div>