This is a fantastic challenge! Simulating "life" with only HTML and CSS requires a bit of creative abstraction, as we can't implement complex logic like cellular automata or physics engines. Instead, we'll focus on creating the impression of organic, evolving, and interacting entities within a contained environment using clever animations, gradients, and pseudo-elements.
We'll create a "microscopic" or "cellular" life simulation, mimicking simple organisms moving, growing, and subtly changing.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS Life Simulation</title>
<style>
:root {
--bg-dark: #0a0a1a;
--cell-color-1: #3498db; /* Blue */
--cell-color-2: #2ecc71; /* Green */
--cell-color-3: #e74c3c; /* Red */
--cell-color-4: #f1c40f; /* Yellow */
--glow-color: #00ffff;
--container-size: 500px;
}
body {
margin: 0;
padding: 0;
height: 100vh;
background: linear-gradient(135deg, #000000 0%, #0d0d1a 50%, #000000 100%);
display: flex;
justify-content: center;
align-items: center;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
overflow: hidden; /* Prevent scrollbars from cells moving out */
color: white;
}
.simulation-container {
position: relative;
width: var(--container-size);
height: var(--container-size);
background: radial-gradient(circle at center, #1a1a3a 0%, #0a0a20 70%, #000000 100%);
border-radius: 50%; /* Petri dish look */
box-shadow: 0 0 50px rgba(0, 255, 255, 0.3),
inset 0 0 30px rgba(0, 255, 255, 0.1);
overflow: hidden; /* Keep cells inside */
animation: container-glow 5s ease-in-out infinite alternate;
}
/* General Cell Styling */
.cell {
position: absolute;
border-radius: 50%;
opacity: 0; /* Start hidden for initial animation */
animation-iteration-count: infinite;
animation-timing-function: ease-in-out;
will-change: transform, opacity, background-color; /* Optimize animations */
z-index: 1; /* Above background particles */
}
/* Cell core/nucleus */
.cell::before {
content: '';
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border-radius: 50%;
background: rgba(255, 255, 255, 0.4);
box-shadow: 0 0 10px rgba(255, 255, 255, 0.5);
animation: nucleus-pulse 3s infinite ease-in-out;
}
/* ----- Cell Type 1: Blue "Amoeba" ----- */
.cell.type-1 {
width: 40px;
height: 40px;
background: radial-gradient(circle, var(--cell-color-1) 0%, rgba(52, 152, 219, 0.7) 70%);
box-shadow: 0 0 15px rgba(52, 152, 219, 0.5);
animation:
cell-fade-in 1s forwards 0s,
cell-move-1 15s infinite 1s,
cell-pulse 3s infinite 0.5s;
left: 10%; top: 20%;
}
.cell.type-1::before {
width: 50%; height: 50%;
}
/* ----- Cell Type 2: Green "Dividing" Cell ----- */
.cell.type-2 {
width: 50px;
height: 50px;
background: radial-gradient(circle, var(--cell-color-2) 0%, rgba(46, 204, 113, 0.7) 70%);
box-shadow: 0 0 15px rgba(46, 204, 113, 0.5);
animation:
cell-fade-in 1.5s forwards 0.5s,
cell-move-2 18s infinite 1.5s,
cell-divide 4s infinite 1s; /* Simulates division */
left: 70%; top: 50%;
}
.cell.type-2::before {
width: 40%; height: 40%;
background: rgba(255, 255, 255, 0.6);
}
/* ----- Cell Type 3: Red "Aggressive" Cell ----- */
.cell.type-3 {
width: 35px;
height: 35px;
background: radial-gradient(circle, var(--cell-color-3) 0%, rgba(231, 76, 60, 0.7) 70%);
box-shadow: 0 0 15px rgba(231, 76, 60, 0.5);
animation:
cell-fade-in 1.2s forwards 0.8s,
cell-move-3 12s infinite 1.2s,
cell-jiggle 0.8s infinite alternate 0.2s; /* More erratic */
left: 30%; top: 80%;
}
.cell.type-3::before {
width: 60%; height: 60%;
background: rgba(255, 255, 255, 0.3);
}
/* ----- Cell Type 4: Yellow "Spore" Cell ----- */
.cell.type-4 {
width: 25px;
height: 25px;
background: radial-gradient(circle, var(--cell-color-4) 0%, rgba(241, 196, 15, 0.7) 70%);
box-shadow: 0 0 10px rgba(241, 196, 15, 0.5);
animation:
cell-fade-in 0.8s forwards 1.5s,
cell-move-4 10s infinite 2s,
cell-flicker 2s infinite step-end; /* Subtle flickering */
left: 50%; top: 10%;
}
.cell.type-4::before {
width: 30%; height: 30%;
}
/* Create more cells with different delays and positions for variety */
.cell.type-1:nth-child(2) { left: 80%; top: 15%; animation-delay: 2s, 16s, 1s; }
.cell.type-2:nth-child(2) { left: 15%; top: 60%; animation-delay: 2.5s, 17s, 2s; }
.cell.type-3:nth-child(2) { left: 60%; top: 25%; animation-delay: 3s, 13s, 0.5s; }
.cell.type-4:nth-child(2) { left: 40%; top: 70%; animation-delay: 3.5s, 11s, 2.5s; }
.cell.type-1:nth-child(3) { left: 5%; top: 45%; animation-delay: 4s, 14s, 1.5s; }
.cell.type-2:nth-child(3) { left: 75%; top: 80%; animation-delay: 4.5s, 19s, 3s; }
.cell.type-3:nth-child(3) { left: 20%; top: 5%; animation-delay: 5s, 10s, 0.8s; }
.cell.type-4:nth-child(3) { left: 90%; top: 30%; animation-delay: 5.5s, 12s, 1.8s; }
/* Background particles/nutrients */
.particle {
position: absolute;
background: rgba(0, 255, 255, 0.3);
border-radius: 50%;
animation-iteration-count: infinite;
animation-timing-function: linear;
z-index: 0; /* Below cells */
}
.particle:nth-child(odd) {
width: 3px; height: 3px;
animation: particle-move-1 20s infinite;
}
.particle:nth-child(even) {
width: 2px; height: 2px;
animation: particle-move-2 25s infinite;
}
.particle:nth-child(1) { left: 5%; top: 10%; animation-delay: 0s; }
.particle:nth-child(2) { left: 20%; top: 80%; animation-delay: 2s; }
.particle:nth-child(3) { left: 70%; top: 30%; animation-delay: 4s; }
.particle:nth-child(4) { left: 10%; top: 60%; animation-delay: 6s; }
.particle:nth-child(5) { left: 85%; top: 40%; animation-delay: 8s; }
.particle:nth-child(6) { left: 40%; top: 5%; animation-delay: 10s; }
.particle:nth-child(7) { left: 50%; top: 90%; animation-delay: 12s; }
.particle:nth-child(8) { left: 95%; top: 70%; animation-delay: 14s; }
.particle:nth-child(9) { left: 30%; top: 25%; animation-delay: 16s; }
.particle:nth-child(10) { left: 60%; top: 8%; animation-delay: 18s; }
/* ----- Keyframe Animations ----- */
@keyframes container-glow {
0% { box-shadow: 0 0 50px rgba(0, 255, 255, 0.3), inset 0 0 30px rgba(0, 255, 255, 0.1); }
50% { box-shadow: 0 0 70px rgba(0, 255, 255, 0.5), inset 0 0 40px rgba(0, 255, 255, 0.2); }
100% { box-shadow: 0 0 50px rgba(0, 255, 255, 0.3), inset 0 0 30px rgba(0, 255, 255, 0.1); }
}
@keyframes cell-fade-in {
0% { opacity: 0; transform: scale(0.1); }
100% { opacity: 1; transform: scale(1); }
}
@keyframes cell-pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
@keyframes nucleus-pulse {
0%, 100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
50% { transform: translate(-50%, -50%) scale(0.9); opacity: 0.8; }
}
@keyframes cell-divide {
0% { transform: scale(1); opacity: 1; }
40% { transform: scale(1.2); opacity: 1; }
50% { transform: scale(0.1); opacity: 0; } /* Cell "divides" and disappears */
51% { transform: scale(0.1); opacity: 0; } /* Hold hidden */
60% { transform: scale(0.1); opacity: 0.5; } /* Start to reappear (new cell) */
100% { transform: scale(1); opacity: 1; } /* Fully visible again */
}
/* Note: This simulates one cell disappearing and a "new" one appearing in its place.
For true division, you'd need JS to create new elements. */
@keyframes cell-jiggle {
0%, 100% { transform: rotate(0deg); }
25% { transform: rotate(5deg); }
50% { transform: rotate(-5deg); }
75% { transform: rotate(3deg); }
}
@keyframes cell-flicker {
0%, 100% { opacity: 1; }
20% { opacity: 0.8; }
40% { opacity: 1; }
60% { opacity: 0.9; }
80% { opacity: 1; }
}
/* Movement paths - using translate for smooth animation */
@keyframes cell-move-1 {
0% { transform: translate(0px, 0px); }
25% { transform: translate(100px, 50px); }
50% { transform: translate(-50px, 150px); }
75% { transform: translate(70px, 0px); }
100% { transform: translate(0px, 0px); }
}
@keyframes cell-move-2 {
0% { transform: translate(0px, 0px); }
20% { transform: translate(-80px, 30px); }
40% { transform: translate(120px, -70px); }
60% { transform: translate(50px, 100px); }
80% { transform: translate(-100px, -50px); }
100% { transform: translate(0px, 0px); }
}
@keyframes cell-move-3 {
0% { transform: translate(0px, 0px); }
30% { transform: translate(60px, -100px); }
60% { transform: translate(-90px, -20px); }
100% { transform: translate(0px, 0px); }
}
@keyframes cell-move-4 {
0% { transform: translate(0px, 0px); }
40% { transform: translate(-50px, 80px); }
80% { transform: translate(100px, -30px); }
100% { transform: translate(0px, 0px); }
}
/* Particle movement */
@keyframes particle-move-1 {
0% { transform: translate(0px, 0px); opacity: 0.5; }
25% { transform: translate(calc(var(--container-size) * 0.1), calc(var(--container-size) * 0.2)); opacity: 0.8; }
50% { transform: translate(calc(var(--container-size) * 0.05), calc(var(--container-size) * -0.1)); opacity: 0.5; }
75% { transform: translate(calc(var(--container-size) * -0.05), calc(var(--container-size) * 0.1)); opacity: 0.7; }
100% { transform: translate(0px, 0px); opacity: 0.5; }
}
@keyframes particle-move-2 {
0% { transform: translate(0px, 0px); opacity: 0.3; }
30% { transform: translate(calc(var(--container-size) * -0.05), calc(var(--container-size) * 0.15)); opacity: 0.6; }
60% { transform: translate(calc(var(--container-size) * 0.1), calc(var(--container-size) * -0.05)); opacity: 0.3; }
100% { transform: translate(0px, 0px); opacity: 0.3; }
}
/* Optional: Add a subtle overall background flicker/noise */
body::before {
content: '';
position: fixed;
top: 0; left: 0; width: 100%; height: 100%;
background: url('data:image/svg+xml;base64,PHN2ZyB2aWR0aD0iNTAwIiBoZWlnaHQ9IjUwMCIgdmlld0JveD0iMCAwIDUwMCA1MDAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PHJlY3Qgd2lkdGg9IjUwMCIgaGVpZ2h0PSI1MDAiIGZpbGw9InJnYmEoMCwgMCwgMCwgMCkiLz48Y2lyY2xlIGN4PSIyNTAiIGN5PSIyNTAiIHI9IjIzMCIgZmlsbD0idXJsKCNncmFkaWVudCkiLz48ZGVmcz48cmFkaWFsR3JhZGllbnQgaWQ9ImdyYWRpZW50IiBjZm9ybWF0PSJzUkdCIj48c3RvcCBvZmZzZXQ9IjAlIiBzdG9wLWNvbG9yPSIjZmZmZmZmIiBzdG9wLW9wYWNpdHk9IjAuMDIiLz48c3RvcCBvZmZzZXQ9IjEwMCUiIHN0b3AtY29sb3I9IiMwMDAwMDAiIHN0b3Atb3BhY2l0eT0iMC4wMiIvPjwvcmFkaWFsR3JhZGllbnQ+PC9kZWZzPjwvc3ZnPg==') repeat;
filter: blur(1px); /* Soften the noise */
animation: noise 10s steps(10) infinite;
pointer-events: none; /* Allow interaction with elements beneath */
opacity: 0.05;
}
@keyframes noise {
0%, 100% { transform: translate(0, 0); }
10% { transform: translate(-5%, 5%); }
20% { transform: translate(-10%, -5%); }
30% { transform: translate(5%, 10%); }
40% { transform: translate(-5%, -10%); }
50% { transform: translate(10%, 5%); }
60% { transform: translate(-10%, -5%); }
70% { transform: translate(5%, 10%); }
80% { transform: translate(-5%, -10%); }
90% { transform: translate(10%, 5%); }
}
.info-text {
position: absolute;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
font-size: 0.8em;
color: rgba(255, 255, 255, 0.4);
letter-spacing: 1px;
text-align: center;
}
</style>
</head>
<body>
<div class="simulation-container">
<!-- Cells -->
<div class="cell type-1"></div>
<div class="cell type-1"></div>
<div class="cell type-1"></div>
<div class="cell type-2"></div>
<div class="cell type-2"></div>
<div class="cell type-2"></div>
<div class="cell type-3"></div>
<div class="cell type-3"></div>
<div class="cell type-3"></div>
<div class="cell type-4"></div>
<div class="cell type-4"></div>
<div class="cell type-4"></div>
<!-- Background particles -->
<div class="particle"></div>
<div class="particle"></div>
<div class="particle"></div>
<div class="particle"></div>
<div class="particle"></div>
<div class="particle"></div>
<div class="particle"></div>
<div class="particle"></div>
<div class="particle"></div>
<div class="particle"></div>
</div>
<div class="info-text">
CSS Life Simulation: Observe evolving patterns.
</div>
</body>
</html>
Explanation of the "Life Simulation" in HTML & CSS:
Since true "life simulation" (like Conway's Game of Life or boids) requires logic and state changes, which are JavaScript's domain, we're using CSS to create the visual impression of life.
-
The Environment (.simulation-container):
- Acts as a "petri dish" or microscopic view.
- Uses
position: relative to contain the cells.
- A
radial-gradient for the background gives it depth.
border-radius: 50% makes it circular.
box-shadow and animation: container-glow give it a subtle, ambient glow, making the environment itself feel alive.
overflow: hidden ensures cells don't animate outside the container.
-
The Organisms (.cell):
- Each
div with the class cell represents an individual organism/cell.
position: absolute allows them to move freely within the container.
border-radius: 50% makes them circular.
- Colors and Gradients: Different
type-* classes apply distinct background colors (radial gradients for depth) and box-shadow for a subtle glow.
- Inner Nucleus (
.cell::before): Each cell has a pseudo-element ::before acting as a "nucleus" or core, which also has a nucleus-pulse animation for more organic movement.
- Initial Appearance (
cell-fade-in): Cells start opacity: 0 and scale(0.1) and animate in, giving the impression of birth or emergence.
- Movement (
cell-move-*): Each type-* has its own unique animation path defined by transform: translate() keyframes, making them float around at different speeds and patterns. This simulates movement.
- Organic Behaviors:
cell-pulse: A subtle scaling animation to mimic breathing or metabolic activity.
cell-divide: For type-2 cells, this animation makes the cell grow slightly, then shrink rapidly and disappear (opacity: 0), only to reappear and grow back to full size. This visually suggests division, even though it's the same HTML element animating its appearance.
cell-jiggle: For type-3 cells, a slight rotation animation for more erratic or "aggressive" movement.
- : For cells, a subtle opacity flicker to give a "spore-like" or more fragile appearance.
This combination of independent, looping animations, strategic use of pseudo-elements, and careful styling creates a compelling illusion of a dynamic, living system using only HTML and CSS.