DEV Community

Cover image for CSS Battle: #5 - Acid Rain
Jatin Sharma
Jatin Sharma

Posted on • Originally published at j471n.in

CSS Battle: #5 - Acid Rain

In this article, I will solve a Acid Rain CSS Challenge on CSS Battle. Let's look at the problem first.

Problem

We need to create the following container by using CSS Properties only:
Acid Rain

Solution

So now look at the Solution and how we are going to achieve this.

HTML

<div class="one"></div>
<div class="two"></div>
<div class="three"></div>
Enter fullscreen mode Exit fullscreen mode

There are three divs, each one represents a single raindrop.

CSS

Now let's style the Body first.

body {
  margin: 0;
  background: #0b2429;
}
Enter fullscreen mode Exit fullscreen mode

After doing that, we can style the raindrops.

.one, .two, .three {
  position: absolute;
  width: 120;
  height: 120;
  background: #998235;
  border-radius: 50% 0 50% 50%;  
}
.one, .three {
  background: #f3ac3c; 
}
Enter fullscreen mode Exit fullscreen mode

In the above code, we are initializing the height and width of the raindrops to 120px and applying the border-radius property to make the perfect raindrop shape.

Now I'll define positions for all the raindrops individually with z-index.

The z-index property specifies the stack order of an element. An element with greater stack order is always in front of an element with a lower stack order.

.one {
  left: 20%;
  top: 50%;
  z-index: 11;
}
.two {
  z-index: 10;
  left: 35%;
  top: 30%;
}
.three {
  transform: rotate(180deg);
  left: 50%;
  top: 10%;
  z-index: 9;
}
Enter fullscreen mode Exit fullscreen mode

This is all you need to solve this CSS Problem.

In CSS Battle you can use 100 instead of 100px. You don't need to define px in CSS. However, if you are using rem or % then you need to pass them separately. That's why in the above CSS code there are no units mostly. For more info visit here

Codepen

Alternate Solution

There could be many Alternative Solutions I've used this one because of the fewer characters and simplicity.

HTML

<p t></p>
<p m></p>
<p b></p>
Enter fullscreen mode Exit fullscreen mode

Here I am using the <p> tag and inside them t stands for a top, m stands for middle and b stands for the bottom raindrop.

CSS

* {
  margin: 0;
}
body {
  background: #0b2429;
}
p[t],
p[m],
p[b] {
  position: absolute;
  width: 120px;
  height: 120px;
  background: #998235;
  border-radius: 50% 0 50% 50%;
}
p[t],
p[b] {
  background: #f3ac3c;
}
p[t] {
  transform: rotate(180deg);
  left: 50%;
  top: 10%;
  z-index: 9;
}
p[b] {
  left: 20%;
  top: 50%;
  z-index: 11;
}
p[m] {
  z-index: 10;
  left: 35%;
  top: 30%;
}
Enter fullscreen mode Exit fullscreen mode

Minify the code or CSS by using any CSS Minifier. It helps you to reduce the characters in the code which will increase the score.

Wrapping up

If you like this then don't forget to ❤️ it. And I'll see you in the next article. See you soon.

Top comments (7)

Collapse
 
supportic profile image
Supportic • Edited
  body{background:#0B2429;display:grid;place-items:center;}
  div {
    width: 120px; height: 120px;
    background: #998235;
    border-radius: 50% 0 50% 50%;
    position: relative;
  }
  div::before,
  div::after{
    content: '';
    width: 120px; height: 120px;
    background: #F3AC3C;
    position: absolute;
  }
  div::before{
    z-index: 1;
    inset: 50% 0 0 -50%;
    border-radius: 50% 0 50% 50%;
  } 
  div::after{
    z-index: -1;
    inset: -50% 0 0 50%;
    border-radius: 50% 50% 50% 50%;
  }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
gass profile image
Gass

I didn't know about the place-items:center property. Very useful, thanks.

Collapse
 
j471n profile image
Jatin Sharma

It is very useful when you need to center a child container vertically and horizontally.

If you wish to learn more about how to center divs then check the the following article it showcase the 6 different ways you can center a child container-

Thread Thread
 
gass profile image
Gass

thanks ;)

Collapse
 
j471n profile image
Jatin Sharma

That's also a good solution. Thanks for the solution. Just add the respective codepen :)

Collapse
 
gass profile image
Gass • Edited

Good battle, nice solution.

My approach is quite different. I use a 16 square grid :

jsfiddle.net/Gass_snippets/ky2jv08...

<body>
<section>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</section>
</body>
Enter fullscreen mode Exit fullscreen mode
body{
  height:100vh;
  display:grid;
  place-items:center;
  background:#0b2429;
}

section{
  height:200px; 
  width:200px;
  display:grid;
  grid-template-columns: auto auto auto auto;
}

div:nth-child(3),
div:nth-child(4),
div:nth-child(8),
div:nth-child(9),
div:nth-child(10),
div:nth-child(13),
div:nth-child(14){
  background-color:gold;
}

div:nth-child(6),
div:nth-child(7),
div:nth-child(11){
  background-color:darkOliveGreen;
}

div:nth-child(3),
div:nth-child(6),
div:nth-child(9){
  border-radius:100% 0 0 0;
}

div:nth-child(4){
  border-radius:0 100% 0 0;
}
div:nth-child(8),
div:nth-child(14),
div:nth-child(11){
  border-radius:0 0 100% 0;
}

div:nth-child(13){
  border-radius:0 0 0 100%;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
j471n profile image
Jatin Sharma

That's an intresting solutions. In CSS battle we try to keep the minimum characters possible. Well this works too ✨