← Mike Higgins Home
Custom Code Experiment •

CSS Beetle

Source Code View HTML / CSS / JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>CSS Beetle</title>
<style>
* {
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  background: linear-gradient(-45deg, #2d5016, #4a7c23, #6b9e3e, #4a7c23);
  background-size: 400% 400%;
  animation: gradient 15s ease infinite;
  position: relative;
  touch-action: none;
  display: flex;
  justify-content: center;
  align-items: center;
}

/* Subtle leaf texture overlay */
body::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0.15;
  background-image:
    radial-gradient(ellipse 40px 80px at 20% 30%, rgba(60, 90, 30, 0.3) 0%, transparent 50%),
    radial-gradient(ellipse 50px 90px at 80% 20%, rgba(70, 100, 40, 0.25) 0%, transparent 50%),
    radial-gradient(ellipse 45px 85px at 60% 70%, rgba(55, 85, 35, 0.3) 0%, transparent 50%),
    radial-gradient(ellipse 35px 70px at 30% 80%, rgba(65, 95, 38, 0.28) 0%, transparent 50%),
    radial-gradient(ellipse 55px 95px at 85% 65%, rgba(58, 88, 32, 0.26) 0%, transparent 50%),
    radial-gradient(ellipse 42px 78px at 15% 60%, rgba(62, 92, 36, 0.29) 0%, transparent 50%),
    radial-gradient(ellipse 48px 88px at 70% 25%, rgba(68, 98, 42, 0.27) 0%, transparent 50%),
    radial-gradient(ellipse 38px 75px at 45% 85%, rgba(56, 86, 34, 0.31) 0%, transparent 50%);
  pointer-events: none;
  z-index: 0;
}

/* Vein-like details */
body::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0.08;
  background-image:
    linear-gradient(135deg, transparent 48%, rgba(40, 60, 25, 0.4) 49%, rgba(40, 60, 25, 0.4) 51%, transparent 52%),
    linear-gradient(45deg, transparent 48%, rgba(45, 65, 28, 0.35) 49%, rgba(45, 65, 28, 0.35) 51%, transparent 52%);
  background-size: 120px 120px, 95px 95px;
  background-position: 0 0, 40px 40px;
  pointer-events: none;
  z-index: 0;
}

@keyframes gradient {
  0% {
    background-position: 0% 50%;
  }
  50% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}

.beetle-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  z-index: 1;
}

.beetle {
  position: relative;
  display: block;
  animation: crawl 3s infinite ease-in-out;
}

.body {
  width: 240px;
  height: 180px;
  position: relative;
  border-radius: 120px 120px 140px 140px;
  background: linear-gradient(135deg, #1a4d0a 0%, #0d2605 50%, #1a4d0a 100%);
  box-shadow:
    0 16px 40px rgba(0,0,0,0.6),
    inset 0 -10px 30px rgba(0,0,0,0.5),
    inset 0 6px 16px rgba(255,255,255,0.1);
  z-index: 2;
}

/* Wing line detail */
.body::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 4px;
  height: 70%;
  background: rgba(0,0,0,0.3);
  border-radius: 4px;
}

/* Shiny spots on shell */
.body::after {
  content: "";
  position: absolute;
  top: 20%;
  right: 25%;
  width: 30px;
  height: 30px;
  background: rgba(255,255,255,0.15);
  border-radius: 50%;
  box-shadow: -60px 20px 0 -6px rgba(255,255,255,0.1);
}

.head {
  width: 120px;
  height: 100px;
  position: absolute;
  top: -60px;
  left: 50%;
  transform: translateX(-50%);
  background: linear-gradient(180deg, #0d2605 0%, #1a4d0a 100%);
  border-radius: 60px 60px 20px 20px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.5);
  z-index: 1;
}

/* Antennae */
.antenna {
  width: 6px;
  height: 70px;
  position: absolute;
  top: 10px;
  background: linear-gradient(to top, #1a4d0a 0%, #0d2605 100%);
  border-radius: 6px;
  transform-origin: bottom center;
}

.antenna::after {
  content: "";
  position: absolute;
  top: -8px;
  left: 50%;
  transform: translateX(-50%);
  width: 12px;
  height: 12px;
  background: #0d2605;
  border-radius: 50%;
}

.antenna.left {
  left: 30px;
  transform: rotate(-25deg);
  animation: wiggle-left 2s infinite ease-in-out;
}

.antenna.right {
  right: 30px;
  transform: rotate(25deg);
  animation: wiggle-right 2s infinite ease-in-out;
}

.eye {
  width: 24px;
  height: 24px;
  position: absolute;
  top: 50px;
  background-color: rgba(255,255,255,0.9);
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(255,255,255,0.5);
}

.eye:after {
  width: 12px;
  height: 12px;
  content: "";
  display: block;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #000;
  border-radius: 50%;
}

.eye.left {
  left: 24px;
}

.eye.right {
  right: 24px;
}



@keyframes crawl {
  0% {
    transform: translateX(0) rotate(0deg);
  }
  25% {
    transform: translateX(-3px) rotate(-1deg);
  }
  50% {
    transform: translateX(0) rotate(0deg);
  }
  75% {
    transform: translateX(3px) rotate(1deg);
  }
  100% {
    transform: translateX(0) rotate(0deg);
  }
}

/* Legs */
.legs-container {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 1;
}

.leg {
  position: absolute;
  width: 80px;
  height: 12px;
  background: linear-gradient(90deg, #0d2605 0%, #1a4d0a 100%);
  border-radius: 6px;
  transform-origin: left center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.4);
}

.leg::after {
  content: "";
  position: absolute;
  right: -60px;
  top: 50%;
  width: 60px;
  height: 10px;
  background: linear-gradient(90deg, #1a4d0a 0%, #0d2605 100%);
  border-radius: 5px;
  transform: translateY(-50%) rotate(-45deg);
  transform-origin: left center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.4);
}

/* Left legs */
.leg.left {
  left: 0px;
  transform-origin: right center;
}

.leg.left::after {
  right: auto;
  left: -60px;
  transform: translateY(-50%) rotate(45deg);
  transform-origin: right center;
}

.leg.left-front {
  top: 40px;
  transform: rotate(-30deg);
}

.leg.left-middle {
  top: 80px;
  transform: rotate(-45deg);
}

.leg.left-back {
  top: 120px;
  transform: rotate(-60deg);
}

/* Right legs */
.leg.right {
  right: 0px;
  left: auto;
  transform-origin: left center;
}

.leg.right::after {
  right: -60px;
  left: auto;
  transform: translateY(-50%) rotate(-45deg);
  transform-origin: left center;
}

.leg.right-front {
  top: 40px;
  transform: rotate(30deg);
}

.leg.right-middle {
  top: 80px;
  transform: rotate(45deg);
}

.leg.right-back {
  top: 120px;
  transform: rotate(60deg);
}

@keyframes wiggle-left {
  0%, 100% {
    transform: rotate(-25deg);
  }
  50% {
    transform: rotate(-35deg);
  }
}

@keyframes wiggle-right {
  0%, 100% {
    transform: rotate(25deg);
  }
  50% {
    transform: rotate(35deg);
  }
}

@media (max-width: 768px) {
  .body {
    width: 192px;
    height: 144px;
    border-radius: 96px 96px 112px 112px;
  }

  .body::before {
    width: 3px;
  }

  .body::after {
    width: 24px;
    height: 24px;
    box-shadow: -48px 16px 0 -5px rgba(255,255,255,0.1);
  }

  .head {
    width: 96px;
    height: 80px;
    top: -48px;
    border-radius: 48px 48px 16px 16px;
  }

  .eye {
    width: 20px;
    height: 20px;
    top: 40px;
  }

  .eye:after {
    width: 10px;
    height: 10px;
  }

  .eye.left {
    left: 20px;
  }

  .eye.right {
    right: 20px;
  }

  .antenna {
    width: 5px;
    height: 56px;
    top: 8px;
  }

  .antenna::after {
    top: -6px;
    width: 10px;
    height: 10px;
  }

  .antenna.left {
    left: 24px;
  }

  .antenna.right {
    right: 24px;
  }

  .leg {
    width: 64px;
    height: 10px;
  }

  .leg::after {
    width: 48px;
    height: 8px;
  }

  .leg.left {
    left: 0px;
  }

  .leg.left::after {
    left: -48px;
  }

  .leg.left-front {
    top: 32px;
  }

  .leg.left-middle {
    top: 64px;
  }

  .leg.left-back {
    top: 96px;
  }

  .leg.right {
    right: 0px;
  }

  .leg.right::after {
    right: -48px;
  }

  .leg.right-front {
    top: 32px;
  }

  .leg.right-middle {
    top: 64px;
  }

  .leg.right-back {
    top: 96px;
  }
}
</style>
</head>
<body>
<div class="beetle-container">
  <div class="beetle">
    <div class="body">
      <div class="legs-container">
        <!-- Left legs -->
        <div class="leg left left-front"></div>
        <div class="leg left left-middle"></div>
        <div class="leg left left-back"></div>
        <!-- Right legs -->
        <div class="leg right right-front"></div>
        <div class="leg right right-middle"></div>
        <div class="leg right right-back"></div>
      </div>
      <div class="head">
        <div class="antenna left"></div>
        <div class="antenna right"></div>
        <div class="eye left"></div>
        <div class="eye right"></div>
      </div>
    </div>
  </div>
</div>

<script>
document.addEventListener('visibilitychange', function() {
  if (!document.hidden) {
    const beetle = document.querySelector('.beetle');
    const antennae = document.querySelectorAll('.antenna');

    beetle.style.animation = 'none';
    antennae.forEach(antenna => antenna.style.animation = 'none');

    void beetle.offsetWidth;

    beetle.style.animation = '';
    antennae.forEach(antenna => antenna.style.animation = '');
  }
});

window.addEventListener('load', function() {
  const beetle = document.querySelector('.beetle');
  beetle.style.animation = 'crawl 3s infinite ease-in-out';

  setTimeout(() => {
    void beetle.offsetWidth;
  }, 10);
});

window.addEventListener('pageshow', function(event) {
  if (event.persisted) {
    const beetle = document.querySelector('.beetle');
    const antennae = document.querySelectorAll('.antenna');

    beetle.style.animation = 'none';
    antennae.forEach(antenna => antenna.style.animation = 'none');

    setTimeout(() => {
      beetle.style.animation = '';
      antennae.forEach(antenna => antenna.style.animation = '');
    }, 50);
  }
});
</script>
</body>
</html>