DEV Community

Cover image for CSSBattle | #17 Fidget Spinner
Nazmuz Shakib Pranto
Nazmuz Shakib Pranto

Posted on

CSSBattle | #17 Fidget Spinner

Welcome to CSSBattle Challenges!

In this short article, I go through my solution for CSSBattle - #17 Fidget Spinner challenge. Please refer to the code snippet below to get a better insight into my thought processes and the implementation detail.


Challenge:

Fidget Spinner Challeng


Solution:

<div class="container">
  <div class="rectangle"></div>
  <div class="circle top"></div>
  <div class="circle bottom"></div>
  <div class="circle left"></div>
  <div class="circle right"></div>
</div>

<style>
  * {
    box-sizing: border-box;
    padding: 0;
    margin: 0;
  }
  .container {
    width: 100%;
    height: 100%;
    position: relative;
    background: #09042A;
  }
  .rectangle {
    width: 100px;
    height: 50px;
    background: #E78481;
    top: 50%;
    left: 50%;
    position: absolute;
    transform: translate(-50%, -50%);
  }
  .circle {
    width: 80px;
    height: 80px;
    border-radius: 50%;
  }
  .top, .bottom {
    border: 10px solid #09042A;
    background: #F5BB9C;
    z-index: 2;
  }
  .left, .right {
    border: 10px solid #E78481;
    background: #09042A;
  }
  .left {
    top: 50%;
    left: 50%;
    position: absolute;
    transform: translate(calc(-50% + -60px), -50%);
  }
  .right {
    top: 50%;
    left: 50%;
    position: absolute;
    transform: translate(calc(-50% + 60px), -50%);
  }
  .top {
    top: 50%;
    left: 50%;
    position: absolute;
    transform: translate(-50%, calc(-50% + -53px));
  }
  .bottom {
    top: 50%;
    left: 50%;
    position: absolute;
    transform: translate(-50%, calc(-50% + 53px));
  }
</style>
Enter fullscreen mode Exit fullscreen mode

Key Takeaway(s):

  • using transform translate to position items relative to parent container

As always, I welcome any feedback or questions regarding the implementation detail of the challenge. Otherwise, I hope this was useful!

Top comments (1)

Collapse
 
mshahanwaz profile image
Mohammad Shahanwaz

Solution to Fidget Spinner with just 2 div's

<div></div>
<div></div>
<style>
  *, *::before, *::after {
    box-sizing: border-box;
  }
  body {
    background: #09042A;
    display: flex;
    justify-content: center;
    align-items: center;
    height: calc(100vh - 16px);
  }
  div {
    width: 60px;
    height: 60px;
    background: #E78481;
    position: absolute;
  }
  div::before, div::after {
    content: "";
    background: #F5BB9C;
    width: 80px;
    height: 80px;
    border: 10px solid #09042A;
    border-radius: 100%;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    --spaceX: 10px;
    --spaceY: 3px;
  }
  div::before {
    top: calc(-100% - var(--spaceY));
  }
  div::after {
    bottom: calc(-100% - var(--spaceY));
  }
  div ~ div {
    background: unset;
  }
  div ~ div::before, div ~ div::after {
    top: 50%;
    transform: translateY(-50%);
    border-color: #E78481;
    background: #09042A;
  }
  div ~ div::before {
    left: calc(-100% - var(--spaceX));
  }
  div ~ div::after {
    left: unset;
    right: calc(-100% - var(--spaceX));
  }
</style>
Enter fullscreen mode Exit fullscreen mode