Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <form class="game">
  <div class="screen">
    <div class="dialog">
      <div class="dialog__content dialog__content--intro">
        <h1>MineCSSweeper</h1>
        <p>An old classic! Try and open all the cells without tripping a mine.</p>
        <label for="start-game" class="faux-button">Start Game</label>
        <input tabindex=-1 type="checkbox" id="start-game">
      </div>
      <div class="dialog__content dialog__content--lose">
        <h1>Uh Oh!</h1>
        <p>Looks like you tripped a mine!</p>
        <button tabindex=-1 class="faux-button" type="reset" title="Reset Board">Replay</button>
      </div>
      <div class="dialog__content dialog__content--win">
        <h1>Well Done!</h1>
        <div class="timer">
          <div class="digit second m m--tens">0</div>
          <div class="digit second m m--singles">0</div>
          <span>:</span>
          <div class="digit second s s--tens">0</div>
          <div class="digit second s s--singles">0</div>
          <span>:</span>
          <div class="digit digit--small ms ms--tens">0</div>
          <div class="digit digit--small ms ms--singles">0</div>
        </div>
        <button tabindex=-1 class="faux-button" type="reset" title="Reset Board">Replay</button>
      </div>
    </div>
  </div>
  <div class="board__flag-toggle toggle">
    <input type="checkbox" id="flag-toggle">
    <div class="toggle__thumb"></div>
    <label for="flag-toggle" class="toggle__control"></label>
  </div>
  <div class="board">
    <div class="board__group-controls"></div>
  </div>
  <div class="game__controls">
    <div class="flag-controls">
      <div class="flag-counter">
        <span>⛳️:</span>
        <span class="mine-count"></span>
      </div>
    </div>
    <div class="timer-container">
      <div class="timer game-timer">
        <div class="digit second m m--tens">0</div>
        <div class="digit second m m--singles">0</div>
        <span>:</span>
        <div class="digit second s s--tens">0</div>
        <div class="digit second s s--singles">0</div>
        <span>:</span>
        <div class="digit digit--small ms ms--tens">0</div>
        <div class="digit digit--small ms ms--singles">0</div>
      </div>
    </div>
    <div class="game-state-controls">
      <button class="game-reset faux-button" type="reset" title="Reset">
        <svg class="reset" viewBox="0 0 24 24">
          <path d="M17.65,6.35C16.2,4.9 14.21,4 12,4A8,8 0 0,0 4,12A8,8 0 0,0 12,20C15.73,20 18.84,17.45 19.73,14H17.65C16.83,16.33 14.61,18 12,18A6,6 0 0,1 6,12A6,6 0 0,1 12,6C13.66,6 15.14,6.69 16.22,7.78L13,11H20V4L17.65,6.35Z"></path>
        </svg>
      </button>
    </div>
  </div>
</form>
              
            
!

CSS

              
                @import "https://unpkg.com/open-props/open-props.min.css";
@import "https://unpkg.com/open-props/normalize.min.css";

* {
  box-sizing: border-box;
}
:root {
  --digit: var(--red);
  --state: paused;
  --stan-win: 0;
  --stan-star-scale: 0;
  --stan-star-rotate: 0;
  --red: #ff4d00;
  --yellow: #ebb624;
  --green: #38b24d;
  --blue: #0f73ff;
  --screen-hide: 0;
  --screen-delay: 0;
  --show-results: 0;
  --deck: hsl(210 34% 10%);
}

h1 {
	color: var(--gray-0);
}

body {
  display: grid;
  place-items: center;
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  font-family: 'Google Sans', sans-serif;
  background: var(--gradient-9);
  clip-path: inset(0 0 0 0);
}
form {
  margin: 0;
  position: relative;
}
.game-state-controls {
  justify-self: end;
}
.game__controls {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  align-items: center;
  justify-items: center;
  position: absolute;
  top: 50%;
  left: 50%;
  width: calc(var(--columns) * 5.25vmin);
  transform: translate(-50%, calc(-50% - ((var(--columns) * 0.65) * 5vmin)));
}
svg {
  fill: currentColor;
}
.board {
  background: var(--gray-6);
  border: 0.4vmin solid var(--gray-6);
  display: grid;
  grid-template-columns: repeat(var(--columns), 5vmin);
  grid-template-rows: repeat(var(--rows), 5vmin);
  grid-gap: 0.2vmin;
}
.board__cell {
  display: grid;
  place-items: center;
  color: hsl(var(--hue, 210) 100% 40% / 1);
  font-weight: bold;
  position: relative;
  font-family: sans-serif;
  font-size: 3vmin;
  background: var(--gray-2);
}
.board__cell--exploder {
  background: var(--red-8);
}
.board__group-controls {
  position: absolute;
}
.board__flag-toggle {
  position: absolute;
  top: 110%;
  right: 0;
  min-height: 5vmin;
  min-width: 5vmin;
}
.board__cell label {
  background: var(--gray-2);
  border: var(--size-2) solid var(--gray-0);
  border-right-color: var(--gray-5);
  border-bottom-color: var(--gray-5);
}
.board__cell :where(label,
span) {
  position: absolute;
  inset: 0;
  display: inline-block;
  display: grid;
  place-items: center;
}
:root:has(#flag-toggle:checked) {
  --flagging: 1;
}
:root:has(#flag-toggle:checked) .board__cell:has([id*="flag--"]:checked) [for*="flag--"],
:root:has(#flag-toggle:not(:checked)) .board__cell:has([id*="flag--"]:checked) [for*="flag--"] {
  display: block;
  z-index: 2;
}
body {
  counter-reset: flag var(--mine-count);
}
[id*=flag--]:checked {
  counter-increment: flag -1;
}
.mine-count:after {
  color: var(--text-1);
  content: counter(flag);
}
.board__cell:has(:checked:not([id*="flag"])) label {
  opacity: 0;
  z-index: -1;
}
.board__cell:has(:checked[id*="flag"]) label[for*="flag"]:after {
  content: "⛳️";
  position: absolute;
  inset: 0;
  font-size: 3vmin;
}
.board__cell:has(:checked[id*="flag"]) span {
  cursor: not-allowed;
}
label {
  opacity: calc(1 - var(--show-fail, 0));
  z-index: 1;
}
label[for*="flag"],
label[for*="defuse"] {
  z-index: calc(1 + var(--flagging, 0));
}
.timer {
  color: var(--digit);
  display: inline-flex;
  font-size: 6vmin;
  padding: 1.75vmin;
  background: #000;
}
@property --ms-tens {
  initial-value: 0;
  inherits: false;
  syntax: '<integer>';
}
@property --ms-singles {
  initial-value: 0;
  inherits: false;
  syntax: '<integer>';
}
@property --s-tens {
  initial-value: 0;
  inherits: false;
  syntax: '<integer>';
}
@property --s-singles {
  initial-value: 0;
  inherits: false;
  syntax: '<integer>';
}
@property --m-tens {
  initial-value: 0;
  inherits: false;
  syntax: '<integer>';
}
@property --m-singles {
  initial-value: 0;
  inherits: false;
  syntax: '<integer>';
}
.digit {
  position: relative;
  color: transparent;
  font-family: 'Orbitron', 'Google Sans', sans-serif;
  counter-reset: var(--counter-name) var(--counter-variable);
  -webkit-animation: var(--name, none) var(--duration, 1s) infinite steps(var(--steps)) var(--state);
          animation: var(--name, none) var(--duration, 1s) infinite steps(var(--steps)) var(--state);
}
.digit:after {
  content: counter(var(--counter-name));
  font-variant: tabular-nums;
  color: var(--digit);
  position: absolute;
  bottom: 0;
  right: 0;
}
:root:has(#start-game:checked) {
  --state: running;
}
:root:has(#start-game:checked) .ms--tens {
  --name: ms-tens;
}
:root:has(#start-game:checked) .ms--singles {
  --name: ms-singles;
}
:root:has(#start-game:checked) .s--tens {
  --name: s-tens;
}
:root:has(#start-game:checked) .s--singles {
  --name: s-singles;
}
:root:has(#start-game:checked) .m--tens {
  --name: m-tens;
}
:root:has(#start-game:checked) .m--singles {
  --name: m-singles;
}
.game-timer {
  font-size: 3vmin;
  padding: 1vmin;
}
.game-timer .ms {
  font-size: 2vmin;
}
.ms {
  font-size: 4vmin;
  transform: translate(0, -6%);
}
.ms--tens {
  --duration: 1s;
  --steps: 10;
  --counter-name: ms-tens;
  --counter-variable: var(--ms-tens);
}
.ms--singles {
  --duration: 0.1s;
  --steps: 10;
  --counter-name: ms-singles;
  --counter-variable: var(--ms-singles);
}
.s--tens {
  --duration: 60s;
  --steps: 6;
  --counter-name: s-tens;
  --counter-variable: var(--s-tens);
}
.s--singles {
  --duration: 10s;
  --steps: 10;
  --counter-name: s-singles;
  --counter-variable: var(--s-singles);
}
.m--tens {
  --duration: 3600s;
  --steps: 6;
  --counter-name: m-tens;
  --counter-variable: var(--m-tens);
}
.m--singles {
  --duration: 600s;
  --steps: 10;
  --counter-name: m-singles;
  --counter-variable: var(--m-singles);
}
@property --screen-hide {
  initial-value: 0;
  inherits: true;
  syntax: '<number>';
}
@property --screen-y {
  initial-value: 0;
  inherits: true;
  syntax: '<length>';
}
@property --screen-scale {
  initial-value: 1;
  inherits: true;
  syntax: '<number>';
}
.screen {
  height: 100vh;
  width: 100vw;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) translateY(calc(var(--screen-y, 0))) scale(var(--screen-scale, 0)) translateY(calc(var(--screen-hide, 0) * 100vh));
  z-index: 5;
  display: grid;
  place-items: center;
  transition: --screen-hide 0.2s;
}
.dialog {
  display: grid;
  grid-template-columns: auto;
  grid-template-rows: auto;
  font-family: 'Google Sans', sans-serif;
  font-size: clamp(1rem, 2vmin, 2rem);
  backdrop-filter: blur(10px) saturate(0.75);
  color: var(--gray-0);
  background: hsl(210 29% 20% / 0.8);
  position: absolute;
  box-shadow: var(--shadow-6);
  text-align: center;
  border-style: solid;
  border-top-color: var(--red);
  border-right-color: var(--green);
  border-bottom-color: var(--yellow);
  border-left-color: var(--blue);
  border-width: var(--size-2);
}
.dialog__content {
  padding: var(--size-4);
  inset: 0;
  display: grid;
  place-items: center;
  grid-column: 1;
  grid-row: 1;
}

.dialog__content .timer {
  margin-bottom: 1rem;
}
:root:has(#start-game:checked) {
  --screen-hide: 1;
}
:root:has([id*="bomb"]:checked) body {
  --show-fail: 1;
  --screen-hide: 0;
  --state: paused;
}
:root:has([id*="bomb"]:checked) :where(.dialog__content--lose) {
  --opacity: 1;
  --z: 2;
}
:root:has([id*="bomb"]:checked) :where(.dialog__content--intro,
.dialog__content--win),
.dialog__content--win,
.dialog__content--lose {
  --opacity: 0;
  --z: -1;
}
.dialog__content {
  opacity: var(--opacity, 1);
  z-index: var(--z, 2);
}
p + * {
  margin-top: var(--size-4);
}
.faux-button {
  --bg: var(--gray-9);
  box-sizing: border-box;
  font-size: var(--font-size-fluid-1);
  padding: var(--size-4) var(--size-6);
  background: var(--bg);
  color: var(--gray-0);
  font-weight: bold;
  font-family: 'Google Sans', sans-serif;
  border: var(--size-2) solid var(--gray-0);
  display: inline-block;
  transform: translateY(calc(var(--y, 0) * 1%)) scale(var(--s));
  transition: transform 0.1s, background 0.1s;
}
.faux-button:hover {
  --bg: #000;
  --y: -5;
  --s: 1.1;
}
.faux-button:active {
  --bg: #000;
  --y: 5;
  --s: 0.9;
}
h1 {
  text-align: center;
}
p {
  margin: 0;
}
[type="checkbox"] {
  opacity: 0;
  position: fixed;
}
.board__flag-toggle {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, calc(-50% + ((var(--columns) * 0.65) * 5vmin)));
}
.toggle {
  --checked: -1;
  height: 2vmin;
  aspect-ratio: 2/1;
  border-radius: 4vmin;
  background: var(--gray-4);
}
.toggle:after {
  content: "⛳️";
  right: 0;
  transform: translate(120%, -50%);
}
.toggle:before {
  content: "👊";
  left: 0;
  transform: translate(-120%, -50%);
}
.toggle:before,
.toggle:after {
  position: absolute;
  top: 50%;
  font-size: 4vmin;
}
.toggle:has(:checked) {
  --checked: 1;
}
.toggle__thumb {
  height: 4vmin;
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--gray-2);
  position: absolute;
  left: 50%;
  top: 50%;
  border: 0.5vmin solid var(--gray-0);
  border-right-color: var(--gray-5);
  border-bottom-color: var(--gray-5);
  transition: transform 0.2s;
  transform: translate(-50%, -50%) translateX(calc(60% * var(--checked, -1)));
}
.toggle__control {
  position: absolute;
  cursor: pointer;
  inset: 0;
  z-index: 2;
  background: none;
}
.game-reset {
  background: none;
  color: var(--gray-8);
  height: 48px;
  aspect-ratio: 1;
  padding: 0;
  justify-self: flex-end;
  border: none;
}
.flag-controls {
  color: var(--gray-8);
  font-size: 4vmin;
  font-weight: bold;
  justify-self: start;
}
@keyframes ms-tens {
  to {
    --ms-tens: 10;
  }
}
@keyframes ms-singles {
  to {
    --ms-singles: 10;
  }
}
@keyframes s-tens {
  to {
    --s-tens: 6;
  }
}
@keyframes s-singles {
  to {
    --s-singles: 10;
  }
}
@keyframes m-tens {
  to {
    --m-tens: 6;
  }
}
@keyframes m-singles {
  to {
    --m-singles: 10;
  }
}
              
            
!

JS

              
                /**
 * Building the DOM although you could snapshot this with Pug or whatever.
 *
 * */

const GET_RANDOM = (min, max) =>
  Math.floor(
    Math.random() * (Math.floor(max) - Math.ceil(min) + 1) + Math.ceil(min)
  )

/**
 * This is a bunch of boilerplate that generates the game map (?)
 * */
const COLUMNS = 9
const ROWS = 9
const CELL_COUNT = COLUMNS * ROWS
const MINE_COUNT = 8
const MINE_INDEXES = []
const ZEROS = []
const ZERO_GROUPS = []
let highestCount = 1

const FORM = document.querySelector('form')
FORM.style.setProperty('--columns', COLUMNS)
FORM.style.setProperty('--rows', ROWS)
document.documentElement.style.setProperty('--mine-count', MINE_COUNT)

const ADD_INDEX = () => {
  const INDEX = GET_RANDOM(0, CELL_COUNT)
  if (MINE_INDEXES.indexOf(INDEX) === -1) {
    return INDEX
  } else return ADD_INDEX()
}

for (let m = 0; m < MINE_COUNT; m++) {
  MINE_INDEXES[m] = ADD_INDEX()
}

const GET_SURROUNDINGS = (INDEX) => {
  const COLUMN = INDEX % COLUMNS
  const TL = INDEX - (COLUMNS + 1)
  const TC = INDEX - COLUMNS
  const TR = INDEX - (COLUMNS - 1)
  const L = INDEX - 1
  const R = INDEX + 1
  const BL = INDEX + (COLUMNS - 1)
  const BC = INDEX + COLUMNS
  const BR = INDEX + (COLUMNS + 1)
  let SURROUNDINGS = [TC, BC]

  if (COLUMN !== 0) SURROUNDINGS.push(TL, L, BL)
  if (COLUMN !== COLUMNS - 1) SURROUNDINGS.push(TR, R, BR)

  return SURROUNDINGS.filter((s) => s >= 0)
}

const GET_BOMB_COUNT = (index) => {
  let count = 0
  for (const SURROUNDING of GET_SURROUNDINGS(index)) {
    if (MINE_INDEXES.indexOf(SURROUNDING) !== -1) count++
  }
  if (count > highestCount) highestCount = count
  return count
}

const GAME_BOARD = new Array(CELL_COUNT).fill(0).map((_, i) => {
  const COUNT = MINE_INDEXES.indexOf(i) !== -1 ? 'X' : GET_BOMB_COUNT(i)
  if (COUNT === 0) ZEROS.push(i)
  return COUNT
})

const PUSH_TO_GROUP = (INDEX, GROUP, ZEROS) => {
  GROUP.push(INDEX)
  for (const SURROUNDING of GET_SURROUNDINGS(INDEX)) {
    if (ZEROS.indexOf(SURROUNDING) !== -1 && GROUP.indexOf(SURROUNDING) === -1)
      PUSH_TO_GROUP(SURROUNDING, GROUP, ZEROS)
  }
}

const PAD_GROUPS = (GROUPS) => {
  for (const GROUP of GROUPS) {
    for (const POS of [...GROUP]) {
      for (const SURROUNDING of GET_SURROUNDINGS(POS)) {
        if (
          GROUP.indexOf(SURROUNDING) === -1 &&
          GAME_BOARD[SURROUNDING] !== 'X'
        )
          GROUP.push(SURROUNDING)
      }
    }
  }
}

const GROUP_ZEROS = () => {
  for (let o = 0; o < ZEROS.length; o++) {
    if (
      ZERO_GROUPS.filter((group) => group.indexOf(ZEROS[o]) !== -1).length === 0
    ) {
      const ZERO_GROUP = []
      PUSH_TO_GROUP(ZEROS[o], ZERO_GROUP, ZEROS)
      ZERO_GROUPS.push(ZERO_GROUP)
    }
  }
  PAD_GROUPS(ZERO_GROUPS)
}

GROUP_ZEROS()


// Here is where we generate the DOM and styles that are needed for it.

let defusers = []
let created = []
let prompted = []


const BOARD_GROUP_CONTROLS = document.querySelector('.board__group-controls')
const genBoardGroupControls = () => {
  let c = 0
  while (c < CELL_COUNT) {
    const COUNT = GAME_BOARD[c]
    let forId
    if (ZEROS.indexOf(c) !== -1) {
      let idx = 0
      for (let z = 0; z < ZERO_GROUPS.length; z++) {
        if (ZERO_GROUPS[z].indexOf(c) !== -1) idx = z
      }
      forId = `group--${idx}`
    }
    if(forId && created.indexOf(forId) === -1) {
      const CHECK = Object.assign(document.createElement('input'), {
        type: 'checkbox',
        id: forId
      })
      BOARD_GROUP_CONTROLS.appendChild(CHECK)
      created.push(forId)
    }
    c++
  }
  let d = 0
  while(d < CELL_COUNT) {
    const COUNT = GAME_BOARD[d]
    let forId
    if (COUNT !== 'X' && COUNT !== 0 && ZERO_GROUPS.filter(g => g.indexOf(d) !== -1).length === 0) {
      forId = `prompt--${d}`
    }
    if (forId && (COUNT !== 'X' && COUNT !== 0)) {
      const CHECK = Object.assign(document.createElement('input'), {
        type: 'checkbox',
        id: forId
      })
      BOARD_GROUP_CONTROLS.appendChild(CHECK)
      prompted.push(forId)
    }
    d++
  }
}
genBoardGroupControls()
const BOARD = document.querySelector('.board')
// Generate the labels and things :D
const genGameControls = () => {
  let i = 0
  while (i < CELL_COUNT) {
    const COUNT = GAME_BOARD[i]
    let hue = 0
    let forId
    let CLASS_NAME = `board__cell board__cell--${COUNT}`
    if (COUNT === 'X') {
      CLASS_NAME += ' board__cell--exploder'
    }
    if (COUNT !== 'X' && COUNT !== 0 && ZERO_GROUPS.filter(g => g.indexOf(i) !== -1).length === 0) {
      forId = `prompt--${i}`
    }
    let group
    for (let g = 0; g < ZERO_GROUPS.length; g++) {
      const GROUP = ZERO_GROUPS[g]
      if (GROUP.indexOf(i) !== -1) {
        hue = (180 / highestCount) * COUNT
        CLASS_NAME += ` board__cell-group--${g}`
        if (COUNT === 0) forId = `group--${g}`
      }
    }
    const CELL = Object.assign(document.createElement('div'), {
      className: CLASS_NAME,
      style: `--hue: ${hue}`,
    })
    BOARD.appendChild(CELL)

    let innerHTML = ``
    let content = COUNT === 0 ? '' : COUNT
    if (COUNT === 'X') {
      content = '💣'
    }
    innerHTML += `<span>${content}</span>`
    innerHTML += `<input type=checkbox id=flag--${i}><label for=flag--${i}></label>`

    console.info({ COUNT, forId })

    if (COUNT !== 'X' && forId) {
      innerHTML += `<label for=${forId}></label>`
    }
    if (!forId) {
      innerHTML += `<input type=checkbox id=placeholder--${i}><label for=placeholder--${i}></label>`
    }
    if (COUNT === 'X') {
      innerHTML += `<input type=checkbox id=bomb--${i}><label for=bomb--${i}></label>`
    }
    CELL.innerHTML = innerHTML
    i++
  }
}
genGameControls()

const genRequiredStyles = () => {
  let GROUPINGS = ''
  for (let g = 0; g < ZERO_GROUPS.length; g++) {
    GROUPINGS += `:root:has(#group--${g}:checked) .board__cell-group--${g} label,`
  }
  for (let p = 0; p < prompted.length; p++) {
    GROUPINGS += `:root:has(#${prompted[p]}:checked) .board__cell:has([for=${prompted[p]}]) label ${p === prompted.length - 1 ? ' {' : ','}`
  }
  // GROUPINGS += 'z-index: -1; opacity: 0; }'
  GROUPINGS += 'display: none; }'
  let VICTORY_DANCE = ':root:has('
  for (let a = 0; a < ZERO_GROUPS.length; a++) {
    VICTORY_DANCE += `#${created[a]}:checked ~`
  }
  for (let b = 0; b < prompted.length; b++) {
    VICTORY_DANCE += `#${prompted[b]}:checked ${b === prompted.length - 1 ? '' : '~'}`
  }
  VICTORY_DANCE += ') body ' 
  const COMBINATION_STYLE = '{ --screen-hide: 0; --show-win: 1; --state: paused; }'
  const COMBINATION_WIN_HIDE = `
    :where(.dialog__content--intro, .dialog__content--lose) {
      --z: -1;
      --opacity: 0;
    }
  `
  const COMBINATION_WIN_SHOW = `
    :where(.dialog__content--win) {
      --z: 2;
      opacity: 1;
    }
  `
  document.body.appendChild(Object.assign(document.createElement('style'), {
    innerHTML: `${GROUPINGS} ${VICTORY_DANCE}${COMBINATION_STYLE} ${VICTORY_DANCE}${COMBINATION_WIN_HIDE} ${VICTORY_DANCE}${COMBINATION_WIN_SHOW}`
  }))
}

genRequiredStyles()
              
            
!
999px

Console