DEV Community

Cover image for CSS MediaQuery for making a board game responsive
Luciano Graziani
Luciano Graziani

Posted on

3 2

CSS MediaQuery for making a board game responsive

Cover from https://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2015/04/colortheory.jpg

Today I was working in a little board game I started with a friend and tried to solve something I was avoiding for some time now: how to make the board responsive.

I used CSS Grid for making a 9x9 board with as much space as possible, given the height/width of the window. I also tried using the vw unit, but when the window's height was less than the width, the page size would overflow vertically. The same did happened with vh but with a horizontal overflow.

Solution

So, how is it possible to adapt a fixed css grid with 9x9 (or whatever) squares? With Media Queries! (or at least is what I found.) Using the orientation condition we can change the unit we're using. This specific snippet is what I used for the board:

.board {
  display: grid;
  grid-template-columns: repeat(9, 10vh);
  grid-template-rows: repeat(9, 10vh);
  gap: 0.2em;
}

@media screen and (orientation: portrait) {
  .board {
    /* no more overflow with this! yay! */
    grid-template-columns: repeat(9, 10vw);
    grid-template-rows: repeat(9, 10vw);
  }
}

Demo

If you know something better than this approach, feel free to respond! Thank you 😄!

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay