CSS Battle Tips & Code Golf

Every character counts. Master these tricks to dominate CSS Battle and code golf competitions.

1. CSS Battle Fundamentals

How CSS Battle Works

/* Canvas: 400px x 300px
   Scoring:
   - Match score (0-100): pixel-perfect matching
   - Character score: fewer chars = higher score
   - Total score = match% * 6 + (1 - chars/1000) * 4

   Default browser styles you NEED to know:
   - body has 8px margin by default
   - Elements have default padding, margin, font-size
   - Default background is white (#fff)

   The output is rendered in a 400x300 iframe */

2. HTML Shortening Tricks

Shortest HTML Tags

TagChars (open+close)Notes
<p>3 (no close needed!)Block element, shortest
<a>3 (no close needed!)Inline element
<b>3Inline element
<i>3Inline element
<s>3Inline element
<u>3Inline element
<div>5Block element, DON'T use
Key insight: <p> doesn't need a closing tag in HTML5. So <p style="..."> saves 4 chars over <div style="..."></div>

Tag Comparison

LONG (26 chars)
<div style="color:red"></div>
SHORT (22 chars)
<p style="color:red">
LONG - Multiple elements
<div class="a"></div>
<div class="b"></div>
SHORT - p tags, no closing
<p class="a">
<p class="b">
/* Even shorter: use body itself as a shape */
<style>body{background:#62306D;margin:0}</style>

/* Use <body> tag with inline style as FIRST element */
<body bgcolor=red>   /* Old HTML attr, still works! */

HTML Attributes Shortcuts

/* bgcolor attribute (saves chars vs CSS background) */
<body bgcolor=#62306D>      /* No quotes needed if no spaces */
<table bgcolor=red>

/* No quotes on attribute values (if no spaces/special chars) */
<p style=color:red>         /* Works! */
<p style=background:red>    /* Works! */
<p class=a>                 /* Works! */

/* Use style tag vs inline: when is each shorter? */
/* 1 element  -> inline style is shorter */
/* 2+ elements with same style -> <style> tag is shorter */

/* Single element */
<p style=background:red>

/* Multiple elements: use style block */
<style>p{background:red;width:100px;height:100px}</style>
<p><p><p>

/* No doctype, head, or html tags needed */
/* Just start writing HTML directly */

3. CSS Shortening Tricks

Remove Unnecessary Characters

/* Remove last semicolon in block */
p{color:red;margin:0}     /* NOT color:red;margin:0; */

/* Remove spaces around : and ; */
p{color:red;margin:0}     /* NOT color: red; margin: 0 */

/* Remove leading zero */
opacity:.5                 /* NOT opacity:0.5 */
margin:.5em                /* NOT margin:0.5em */

/* Remove units on zero */
margin:0                   /* NOT margin:0px */
padding:0                  /* NOT padding:0rem */
/* EXCEPTION: 0% is needed in gradients/animations */

/* Use * instead of body/html */
*{margin:0}               /* instead of body{margin:0} */
/* * is 1 char, body is 4, html is 4 */

/* No space after comma in values */
rgb(0,0,0)                /* NOT rgb(0, 0, 0) */
translate(0,50px)          /* NOT translate(0, 50px) */

Color Shortcuts

/* Use shortest color representation */

/* 3-char named colors (SHORTEST!) */
red        /* 3 chars = #FF0000 */
tan        /* 3 chars = #D2B48C */

/* 4-char named colors */
gold cyan blue gray lime navy pink peru plum teal aqua snow

/* 3-char hex (shorter than 6-char) */
#fff       /* instead of #ffffff or white (5 chars) */
#000       /* instead of #000000 or black (5 chars) */
#f00       /* instead of red (same length, 3 chars) */
#0f0       /* instead of lime (same length) */

/* Comparison table: */
/* red    = 3  | #f00 = 4  | Use: red */
/* cyan   = 4  | #0ff = 4  | Same length */
/* white  = 5  | #fff = 4  | Use: #fff */
/* black  = 5  | #000 = 4  | Use: #000 */
/* orange = 6  | #ffa500=7 | Use: orange */
/* tomato = 6  | #ff6347=7 | Use: tomato */

/* HEX shorthand trick */
#aabbcc -> #abc   /* 4 chars saved */
#112233 -> #123   /* 4 chars saved */

Complete Short Named Colors Table

3 chars4 chars5 chars6 chars
red
tan
aqua gold gray blue cyan lime navy peru pink plum snow teal azure beige black coral green ivory khaki linen olive wheat white bisque indigo maroon orange orchid purple salmon sienna silver tomato violet yellow
Rule of thumb: If the named color is shorter than its hex value, use the name. If hex is shorter (like #fff vs white), use hex.

Property Value Shortcuts

/* background shorthand */
background:red          /* NOT background-color:red */
/* Saves 6 chars! */

/* border shortcuts */
border:1px solid red    /* Full shorthand */
border:0                /* NOT border:none */
outline:0               /* NOT outline:none */

/* margin/padding shortcuts */
margin:0 auto           /* Center horizontally */
margin:5px 0            /* Top/bottom only */
padding:0               /* NOT padding:0 0 0 0 */

/* font-weight */
font-weight:700         /* NOT font-weight:bold */
/* Actually same chars! But 400 vs normal saves 2 */

/* position shortcuts */
inset:0                 /* = top:0;right:0;bottom:0;left:0 */
/* Saves ~24 chars! */

/* place-items (grid centering) */
place-items:center      /* = align-items:center;justify-items:center */

Selector Tricks

/* Tag selector saves chars over class */
p{...}           /* 1 char selector */
.a{...}          /* 2 char selector (need class= attr too) */

/* Use tag+nth-child instead of classes */
p:nth-child(2)   /* Select 2nd p without class */

/* * selects everything (reset margin) */
*{margin:0}

/* Adjacent sibling trick */
p+p{margin-left:100px}  /* Every p after first p */

/* Use body as a shape */
body{background:#62306D;margin:50 100}
/* Numbers without units work for margin in some cases */

/* Style HTML element directly */
html{background:red}
/* This fills entire viewport as background */

4. Shape-Making Techniques

Circles

/* Perfect circle */
p{
  width:100;
  height:100;
  background:red;
  border-radius:50%
}

/* Semicircle (top half) */
p{
  width:200;
  height:100;
  background:red;
  border-radius:100px 100px 0 0
}

/* Quarter circle */
p{
  width:100;
  height:100;
  background:red;
  border-radius:100% 0 0
}

/* Ring (hollow circle) */
p{
  width:80;
  height:80;
  border:10px solid red;
  border-radius:50%
}

/* Pill shape */
p{
  width:200;
  height:80;
  background:red;
  border-radius:40px
}

Triangles & Arrows

/* Triangle (border trick) */

/* Triangle pointing UP */
p{
  width:0;
  height:0;
  border:50px solid transparent;
  border-bottom-color:red
}

/* Triangle pointing DOWN */
p{
  width:0;
  height:0;
  border:50px solid transparent;
  border-top-color:red
}

/* Triangle pointing RIGHT */
p{
  width:0;
  height:0;
  border:50px solid transparent;
  border-left-color:red
}

/* Shorter triangle with clip-path */
p{
  width:100;
  height:100;
  background:red;
  clip-path:polygon(50% 0,0 100%,100% 100%)
}

Complex Shapes

/* Trapezoid */
p{
  width:100;
  height:0;
  border-bottom:80px solid red;
  border-left:30px solid transparent;
  border-right:30px solid transparent
}

/* Parallelogram */
p{
  width:150;
  height:80;
  background:red;
  transform:skewX(-20deg)
}

/* Heart */
p{
  width:100;
  height:100;
  background:red;
  transform:rotate(45deg);
  position:relative
}
/* + ::before and ::after circles */

/* Diamond */
p{
  width:100;
  height:100;
  background:red;
  clip-path:polygon(50% 0,100% 50%,50% 100%,0 50%)
}

/* Cross / Plus */
p{
  width:30;
  height:100;
  background:red;
  box-shadow:35px 0,0 -35px;
  /* Creative shadow placement */
}

Oval & Egg Shapes

/* Oval (horizontal) */
p{
  width:200;
  height:100;
  background:red;
  border-radius:50%
}

/* Egg shape */
p{
  width:100;
  height:130;
  background:red;
  border-radius:50% 50% 50% 50% / 60% 60% 40% 40%
}

/* Leaf shape */
p{
  width:100;
  height:100;
  background:green;
  border-radius:0 50%
}

5. Centering (Shortest Methods)

Centering Techniques by Character Count

MethodCodeCharsNotes
Griddisplay:grid;place-items:center31Apply to parent
Margin automargin:auto (+ fixed size)11+Needs width/height
Fixed positionposition:fixed;inset:0;margin:auto35Needs width/height
Absolute centerposition:absolute;inset:0;margin:auto38Needs positioned parent
Flexboxdisplay:flex;justify-content:center;align-items:center53Most verbose
SHORTEST: Grid centering on body (parent)
<style>
  body{display:grid;place-items:center;height:100vh;margin:0;background:#62306D}
  p{width:200;height:200;background:#F7EC7D;border-radius:50%}
</style>
<p>
SHORTEST: Fixed centering (element)
<p style="position:fixed;inset:0;margin:auto;width:200;height:200;background:#F7EC7D;border-radius:50%">
<style>body{background:#62306D;margin:0}</style>

6. box-shadow Duplication Hacks

Cloning Shapes with Shadows

/* box-shadow: x y blur spread color */
/* With blur:0 and spread:0, shadow = exact copy */

/* Clone a circle to the right */
p{
  width:50;
  height:50;
  background:red;
  border-radius:50%;
  box-shadow:100px 0 red
  /* Creates identical circle 100px right */
}

/* Multiple clones! */
p{
  box-shadow:
    100px 0 red,
    200px 0 blue,
    0 100px green,
    100px 100px yellow
  /* Creates a 2x2 grid of circles */
}

/* Scale copies with spread */
p{
  width:20;
  height:20;
  border-radius:50%;
  box-shadow:0 0 0 20px red
  /* Spread adds 20px to each side = 60px circle */
}

Shadow Patterns

/* Grid pattern */
p{
  width:50;height:50;
  background:red;
  box-shadow:
    60px 0 red, 120px 0 red,
    0 60px red, 60px 60px red, 120px 60px red,
    0 120px red, 60px 120px red, 120px 120px red
}

/* Shadow color inherits text color! */
p{
  color:red;
  box-shadow:100px 0
  /* Shadow is red (from color property) */
  /* Saves repeating color in shadow! */
}

/* Inset shadow for inner shapes */
p{
  width:200;
  height:200;
  background:#fff;
  box-shadow:inset 0 0 0 50px red
  /* 50px red border inside the element */
}

7. Background & Gradient Hacks

Gradient Shapes

/* Horizontal stripes */
background:repeating-linear-gradient(
  red 0 50px,#fff 0 100px
);

/* Vertical stripes */
background:repeating-linear-gradient(
  90deg,red 0 50px,#fff 0 100px
);

/* Checkerboard */
background:
  conic-gradient(red 25%,#fff 0 50%,red 0 75%,#fff 0)
;
background-size:100px 100px;

/* Diagonal stripes */
background:repeating-linear-gradient(
  45deg,red 0 25px,#fff 0 50px
);

Gradient Circles & More

/* Circle with radial-gradient (no element!) */
background:radial-gradient(
  circle 50px,red 100%,#fff 0
) center/100%;

/* Multiple circles */
background:
  radial-gradient(circle 30px,red 100%,transparent 0) 50px 50px,
  radial-gradient(circle 30px,blue 100%,transparent 0) 150px 50px
;
background-repeat:no-repeat;

/* Hard-stop gradient (two-color split) */
background:linear-gradient(red 50%,blue 0);
/* Top half red, bottom half blue. No transition. */

/* Left-right split */
background:linear-gradient(90deg,red 50%,blue 0);

/* Pie chart */
background:conic-gradient(red 30%,blue 0 60%,green 0);
border-radius:50%;

/* Concentric circles */
background:radial-gradient(
  red 20%,blue 0 40%,red 0 60%,blue 0
);

8. Common Battle Patterns (Shortest Code)

Centered Circle on Background

/* Pattern: Circle centered on solid bg */
/* SHORTEST (92 chars) */
<p><style>
*{
  background:#62306D
}
p{
  position:fixed;
  inset:0;
  margin:auto;
  width:200;
  height:200;
  background:#F7EC7D;
  border-radius:50%
}
</style>

Horizontal & Vertical Bars

/* Horizontal bars */
<style>
*{background:#fff}
p{
  height:60;
  background:red
}
</style>
<p><p><p>

/* Flag pattern (3 horizontal stripes) */
<style>
body{
  background:linear-gradient(
    red 33%,#fff 0 66%,blue 0
  );
  margin:0
}
</style>

/* Vertical stripes */
<style>
body{
  background:linear-gradient(
    90deg,red 33%,#fff 0 66%,blue 0
  );
  margin:0
}
</style>

Concentric Circles

/* Using radial-gradient (no elements needed!) */
<style>
body{
  margin:0;
  background:radial-gradient(
    circle,
    red 50px,#fff 0 80px,red 0 110px,#fff 0
  ) center/400px 300px
}
</style>

/* Using outline + border + box-shadow */
<p><style>
*{background:#fff}
p{
  position:fixed;
  inset:0;
  margin:auto;
  width:50;
  height:50;
  background:red;
  border-radius:50%;
  border:20px solid #fff;
  outline:20px solid red;
  box-shadow:0 0 0 40px #fff,0 0 0 60px red
}
</style>

Grid of Dots

/* Repeating radial gradient */
<style>
body{
  margin:0;
  background:radial-gradient(
    circle 15px,red 100%,#fff 0
  );
  background-size:80px 75px
}
</style>

/* Using box-shadow */
<p><style>
*{background:#fff}
p{
  width:30;
  height:30;
  background:red;
  border-radius:50%;
  margin:35 0 0 35;
  box-shadow:80px 0 red,160px 0 red,240px 0 red,
    0 75px red,80px 75px red,160px 75px red,240px 75px red,
    0 150px red,80px 150px red,160px 150px red,240px 150px red
}
</style>

9. Character Count Reference

Property:Value Pairs Sorted by Length

CharsProperty:ValueWhat it does
7color:redRed text
8margin:0Remove margins
8inset:0top:0;right:0;bottom:0;left:0
8float:leftFloat element left
9padding:0Remove padding
9opacity:.550% transparent
10border:0Remove borders
10width:100100px wide
11height:100100px tall
12display:flexFlexbox
12display:gridGrid
14background:redRed background
14position:fixedFixed positioning
16border-radius:50%Circle
18place-items:centerCenter grid items

10. Starter Templates

Minimal Template (Inline Style)

<!-- For single-element solutions -->
<p style="background:#62306D;position:fixed;inset:0;margin:auto;width:200;height:200">
<style>*{background:#fff}</style>

Minimal Template (Style Block)

<!-- For multi-element solutions -->
<style>
*{background:#62306D;margin:0}
p{
  width:200;
  height:200;
  background:#F7EC7D
}
</style>
<p><p>

Body-Only Template (No Elements)

<!-- Use gradients to draw everything -->
<style>
body{
  margin:0;
  background:
    radial-gradient(circle 50px at 200px 150px,
      #F7EC7D 100%,transparent 0),
    #62306D
}
</style>

Grid Centering Template

<style>
body{
  display:grid;
  place-items:center;
  height:100vh;
  margin:0;
  background:#62306D
}
p{
  width:200;
  height:200;
  background:#F7EC7D;
  border-radius:50%
}
</style>
<p>

Battle Strategy Checklist

/* STEP-BY-STEP:
   1. Analyze the target image
   2. Identify shapes: rectangles, circles, triangles, lines
   3. Pick strategy:
      - Can you do it with ONLY gradients on body? (shortest!)
      - Need 1 element? Use <p> with inline style
      - Need multiple elements? Use <style> block + p tags
   4. Pick centering method (if needed)
   5. Write the shortest version
   6. Minify: Remove spaces, semicolons, quotes
   7. Check: Can box-shadow replace extra elements?
   8. Check: Can outline/border add extra shapes?
   9. Count characters and optimize

   PRIORITIES:
   - body bg gradient > multiple elements
   - box-shadow cloning > extra p tags
   - outline > extra border elements
   - inline style (1 element) > style block
   - style block > inline (2+ elements)
   - Named colors if shorter > hex values
   - No closing tags on p/a/li
   - No quotes on attribute values
*/

Advanced: outline + border + box-shadow Layering

/* You can get 4+ visual layers from ONE element: */
p{
  width:100;
  height:100;
  background:red;            /* Layer 1: content */
  border:20px solid blue;    /* Layer 2: border */
  outline:20px solid green;  /* Layer 3: outline */
  box-shadow:
    0 0 0 60px yellow,        /* Layer 4: shadow 1 */
    0 0 0 80px purple        /* Layer 5: shadow 2 */
}

/* Add ::before and ::after for 2 MORE elements = 7+ layers */
p::before{
  content:"";
  position:absolute;
  /* ... more shapes */
}

/* ORDER (back to front):
   box-shadow (outer) -> outline -> border -> background
   -> content -> ::before/::after */