DEV Community

Cover image for CSS Battle #2 - Carrom
Olzhas Askar
Olzhas Askar

Posted on

CSS Battle #2 - Carrom

It already happened to me during the first battle, but at least, I knew what was wrong.

This mismatch, when your result looks quite, but not exactly the same, is frustrating. The first one was simple. I thought to myself, hey, I've heard about Responsive Design, I am a Software Engineer after all, I shall not use hardcoded values! But 66vh doesn't equal to 200px out of total 300. So, an engineer as I am, I just boldly typed 200px in and it worked. This time was different.

1. The Almost Correct One

In my previous company, we had a lot of full-stack developers, who preferred to avoid frontend tasks. Fullstack Backend Developers, as one of my colleagues stated. (In this particular case, since the backend was in C#, we couldn't them Backend Frontend Developers)

Scared of CSS

As I found out, while fighting through CSS battles, floats were originally intended to place images within the text. (Just the same way tables were intended to make tables) The better way is to inline-block elements. Or even better is to use Flexbox or Grid. So I decided, screw floats, I will push inline-blocked divisions with the margins where I want to.



<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<div id="d"></div>

<style>
  body {
    margin: 0;
    background: #62374e;
  }
  div {
    width: 50px;
    height: 50px;
    background: #fdc57b;
    display: inline-block;
  }
  #a {
    margin-top: 50px;
    margin-left: 50px;
  }
  #b {
    margin-top: 50px;
    margin-left: 200px;
  }
  #c {
    margin-top: 100px;
    margin-left: 50px;
  }
  #d {
    margin-top: 100px;
    margin-left: 200px;
  }
</style>


Enter fullscreen mode Exit fullscreen mode

This is when I got a 98.7% score. Eventually, after solving the problem using another way, I learned that for inline-block elements the spaces in HTML matter. There are various ways to resolve this issue, most notably, setting font-size to zero on body element, which would lead to increased character count (sure, I'll not reach the high score with this approach anyway, but still), and just removing the spaces between the divisions. Place all four divs squeezed into one line and it would work like a charm.

2. Fixed Positions

After failing a couple of times trying the first method, I remembered that I am a respectable Software Engineer and decided to go quick and dirty all the way - giving each division a fixed position with exact top and left coordinates.



<div id="a"></div>
<div id="b"></div>
<div id="c"></div>
<div id="d"></div>

<style>
  body {
    margin: 0;
    background: #62374e;
  }
  div {
    width: 50px;
    height: 50px;
    background: #fdc57b;
    position: fixed;
  }
  #a {
    top: 50px;
    left: 50px;
  }
  #b {
    top: 50px;
    left: 300px;
  }
  #c {
    top: 200px;
    left: 50px;
  }
  #d {
    top: 200px;
    left: 300px;
  }
</style>


Enter fullscreen mode Exit fullscreen mode

3. Borders

What if we didn't need the background color for body? What if we could create a massive border around each square? Sure thing! We don't even need ids, we can use even instead (pun intended). font-size is here for educational purposes only.



<div></div>
<div></div>
<div></div>
<div></div>

<style>
  body {
    margin: 0;
    font-size: 0;
  }
  div {
    width: 50px;
    height: 50px;
    background: #fdc57b;
    border: 50px solid #62374e;
    display: inline-block;
  }
  div:nth-of-type(even){
    border-left: 150px solid #62374e;
  }
</style>


Enter fullscreen mode Exit fullscreen mode

As you may have noticed, since our canvas is not a perfect square, I am adding an exception for every even div to have a larger on the left side, non-square border.

4. Pseudo Elements

Four divisions and we are only styling them based on even and odd? This sounds like duplication to me. Can we cut it in half? Yes, if we use pseudo elements. Let's say we have two regular divs having their shapes duplicated with ::after. We first style divs and their ::afters and then lay them out separately.



<div></div>
<div></div>

<style>
  body {
    background: #62374e;
  }
  div, div::after {
    width: 50px;
    height: 50px;
    background: #fdc57b;
  }
  div:nth-of-type(odd) {
    margin: 50px 42px;
  }
  div:nth-of-type(even) {
    margin: 100px 42px;
  }
  div::after {
    content: "";
    display: inline-block;
    margin-left: 250px;
  }
</style>


Enter fullscreen mode Exit fullscreen mode

By the ways, all of these 42px appear here and there because 42 is the answer to Life, the Universe and Everything 50px - 8px, the default margin. Also, you can safely replace the body tag with * if you are not setting margin or padding.

5. Box shadow

Is there something more elegant? What if we didn't need four divisions? Can't we try to replicate one three times? Do you remember paper staples? box-shadow? Actually, we can specify multiple shadows separated by commas. Here is how it looks like.



<div></div>

<style>
  body {
    background: #62374e;
  }
  div {
    margin: 50px 42px;
    width: 50px;
    height: 50px;
    background: #fdc57b;
    box-shadow: 250px 0 #fdc57b, 250px 150px #fdc57b, 0 150px #fdc57b;
  }
</style>


Enter fullscreen mode Exit fullscreen mode

Do you see what I see? Does anything bother you? #fdc57b. Four freaking times! Shouldn't there be something like box-shadow-color? Unfortunately, no. But! Box shadow defaults to color value. So here we go again.



<div></div>

<style>
  body {
    background: #62374e;
  }
  div {
    margin: 50px 42px;
    width: 50px;
    height: 50px;
    background: #fdc57b;
    color: #fdc57b;
    box-shadow: 250px 0, 250px 150px, 0 150px;
  }
</style>


Enter fullscreen mode Exit fullscreen mode

Did I miss anything significant?

Latest comments (21)

Collapse
 
harindradev profile image
Harindra Masna • Edited

My 103 chars solution

<style>body{background:#62374e;margin:50;border:50px solid;border-image:linear-gradient(#fdc57b,#fdc57b
Enter fullscreen mode Exit fullscreen mode
Collapse
 
yashbro profile image
Yash Joglekar • Edited

Here's my 115-character solution:

<d style=color:#fdc57b;box-shadow:71q+71q+0+25px,71q+230q+0+25px,335q+71q+0+25px,335q+230q+0+25px,0+0+0+5in#62374e>
Enter fullscreen mode Exit fullscreen mode
Collapse
 
facihabdo profile image
Abdo Facih

new score in my career 104char

<body bgcolor=#62374e style="border:50px solid;margin:50;border-image:linear-gradient(#fdc57b,#fdc57b)">
Enter fullscreen mode Exit fullscreen mode
Collapse
 
facihabdo profile image
Abdo Facih

125char maximum what i can do ,i'm thinking how can possible to achieve it with 66 char wtf

<body bgcolor=#62374e style="width:50%;height:100;border:50px solid;margin:50;border-image:linear-gradient(#fdc57b,#fdc57b)">
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tdyh profile image
Tdyh

42 is the answer ofo Life, the Universe and Everything hahahahahah you are funny bro
I get a lot from this , thanks

Collapse
 
paglaparatha profile image
paglaparatha • Edited


<b style=box-shadow:67px+67px+0+0.26in#fdc57b,67px+217px+0+0.26in#fdc57b,317px+217px+0+0.26in#fdc57b,317px+67px+0+0.26in#fdc57b,0+0+0+5in#62374e></b>

Collapse
 
theredkorsar profile image
TheRedKorsar

This is my no block solution xD (using clip-path)

<style>html{background:#62374e}body{background:#fdc57b;margin:50;clip-path:polygon(0 0,0 25%,50px 25%,50px 0,100% 0,100% 25%,100% 100%,50px 100%,50px 75%,0% 75%,0 100%,250px 100%,250px 75%,100% 75%,100% 25%,250px 25%,250px 0)}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tarishahmed profile image
Tarish Ahmed • Edited

html and body can be replaced with:
html -> *
body -> * > *

Collapse
 
e_xo1738 profile image
Enrique_xO

went to 200 char without closing tags, spaces etc XD
it looks weird but it is what it is (got 100%)
gonna try something more short

.X{float:left;width:50;height:50;background:#fdc57b}

.a,.c{margin:50 100 50 50}

.b,.d{margin:50 50 50 100}

*{margin:0;background:#62374e

Collapse
 
vsnegovik profile image
Vladimir Kashutin • Edited

My 214 character solution

<style>html{background-size:250px 250px;background-image:linear-gradient(to right,#62374e 50px,transparent 0 100px,#62374e 100px),linear-gradient(to bottom,#62374e 50px,#fdc57b 0 100px,#62374e 0 200px,#fdc57b 50px)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
tomek24 profile image
tOmek24

This is my 140 character solution.

<body bgcolor=#62374e><img i><img><img i><img><style>img{border:25px solid #fdc57b;margin:42;float:right}img[i]{float:none;margin-bottom:58}