DEV Community

Cover image for CSS Battle: #6 - Missing Slice
Jatin Sharma
Jatin Sharma

Posted on • Originally published at j471n.in

CSS Battle: #6 - Missing Slice

In this article, I will solve a Missing Slice 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:
Missing Slice

Solution

So now look at the Solution and how we will achieve this.

HTML

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

I took three divs to create the shape. Each one of them represents a slice.

CSS

Now let's style the body container first.

body {
  margin: 0;
  background: #e3516e;
  display: grid;
  grid-template-columns: 1fr 1fr;
}
Enter fullscreen mode Exit fullscreen mode

I have used a grid to layout the children. grid-template-columns: 1fr 1fr property defines that there should be two columns with 1fr. Here fr represents a fraction of the available space in the grid container.

Now Let's style the individual slices

.one, .two, .three {
  width: 100;
  height: 100;
}
.one {
  background: #51b5a9;
  border-radius: 100% 0 0 0; 
  place-self: end;
}
.two {
  background: #fade8b;
  border-radius: 0 100% 0 0;
  place-self: end start;
}
.three {
  background: #f7f3d7;
  border-radius: 0 0 0 100%;      /* top-left , top-right, bottom-right, bottom-left */
  place-self: start end;
}
Enter fullscreen mode Exit fullscreen mode

In the above code, you can see that I have used place-self in all of the slices. The place-self CSS shorthand property allows you to align an individual item in both the block and inline directions at once (i.e. the align-self and justify-self properties).

Note: 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 %, 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 f></p>
<p s></p>
<p t></p>
Enter fullscreen mode Exit fullscreen mode

Here f stands for the First slice, s stands for the Second slice, and t stands for the third slice.

CSS

* { margin:0 }  
body {
  background: #e3516e;
  display: grid;
  grid-template-columns: 1fr 1fr;
}
p[f], p[s], p[t] {
  width: 100;
  height: 100;
}
p[f] {
  background: #51b5a9;
  border-radius: 100% 0 0 0;
  place-self: end;
}
p[s] {
  background: #fade8b;
  border-radius: 0 100% 0 0;
  place-self: end start;
}
p[t] {
  background: #f7f3d7;
  border-radius: 0 0 0 100%;
  place-self: start end;
}
Enter fullscreen mode Exit fullscreen mode

Tip

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 (5)

Collapse
 
gass profile image
Gass • Edited

Good one. My solution is similar. It's easy with grid

jsfiddle.net/Gass_snippets/4scoqym...

<body>
  <section>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
  </section>
</body>
Enter fullscreen mode Exit fullscreen mode
body{
  background:deepPink;
  height:100vh;
  display:grid;
  place-items:center;
}
body > *{aspect-ratio:1}
section{
  height: 200px;
  display:grid;
  grid-template-columns: auto auto;
}
section > *{
  box-sizing:border-box;
  height:100px; 
}
div:nth-child(1){
  background:darkCyan;
  border-radius:100% 0 0 0;
}
div:nth-child(2){
  background:khaki;
  border-radius:0 100% 0 0;
}
div:nth-child(3){
  background:AntiqueWhite;
  border-radius:0 0 0 100%;
}
Enter fullscreen mode Exit fullscreen mode

Do you know why is not necessary to use grid-template-rows: auto auto; ??

Collapse
 
j471n profile image
Jatin Sharma

Yes, because grid automatically wrap the elements when it overflows from columns. For example you have 6 elements and you define the grid of two columns. In this case it will automatically have 3 rows with two columns. So we don't need to worry about rows here as in your code you only have 4 divs.

Collapse
 
gass profile image
Gass

Amazing explanation. Thanks!

Collapse
 
ghostclonelol2000 profile image
<}:-{~ .A.K.a. DOOM

De_nuke

Collapse
 
j471n profile image
Jatin Sharma

What does that mean?