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:
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>
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)
Solution to Fidget Spinner with just 2
div
's