Table of Contents
First, I created some tags
Set background image
Style grid
Style Squares
Animation squares
Well, maybe the animation should be optional...
that's all!
Home Web Front-end CSS Tutorial Checkerboard Reveal

Checkerboard Reveal

Mar 28, 2025 am 10:12 AM

Checkerboard Reveal

I still remember when I was ten years old, my cousin came to visit my house. He is (still) a cool kid, the kind of kid who will carry a chess game disc that he programmed himself. His version of chess is as cool as he himself, because part of the board disappears after every step.

What's more cool? Every piece that disappears on the board will show a very beautiful picture.

I think the same idea can make some really cool user interfaces. It's just that maybe no user interaction is required to display the background, it can be played simply as an animation. This is my final result:

This idea is simple, and there are many other ways to achieve it, but this is the idea I followed...

First, I created some tags

Images can be processed as background in CSS on body elements, or some designed to be of a specific size<div> on the element. Therefore, this problem is not necessary for the time being. But the board is fun. This is a pattern with the words CSS Grid written on it, so I used an element as a grid container with lots of other ones inside<code><div> element. I don't know how many squares/squares/whatever there is for a legal chess board, so I just chose the number 7 out of thin air and then squared it to get 49 squares.<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;div&gt; &lt;div&gt;&lt;/div&gt; &lt;div&gt;&lt;/div&gt; &lt;/div&gt;</pre><div class="contentsignin">Copy after login</div></div> <p>Yes, write all of this<code><div> Elements are painful, and JavaScript can certainly help. But if I'm just experimenting and just need the convenience of the developer, using Haml can help:<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"> .grid - 49.times do %div</pre><div class="contentsignin">Copy after login</div></div> <p> The final result is the same. Either way, this gave me all the markings I needed to start a styling design!</p> <h2 id="Set-background-image"> Set background image</h2> <p> Again, this can happen as <code>background-image on body or other element, depending on how it is used - as long as it covers the entire space. Since I need a grid container anyway, I decided to use it.

 .checkerboard {
  background-image: url('walrus.jpg');
  background-size: cover;
  /* Other properties may be required to correctly locate the image*/
}
Copy after login

Gradients are part of the raster image file, but I can handle it cleverly with some kind of overlay on body element, using pseudo-elements, such as :after . Alas, this is exactly the common technology currently designed by CSS-Tricks.

Style grid

Yes, I used CSS Grid. Making a 7×7 grid is very easy.

 .checkerboard {
  background-image: url('walrus.jpg');
  background-size: cover;
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  grid-template-rows: repeat(7, 1fr);
}
Copy after login

I think it will be much better once we see aspect-ratio is widely supported, at least if I understand it correctly. My problem now is that the grid is not maintaining any proportions. This means that the chessboard blocks will become very squeezed at different viewport sizes. not good. If this is very important, we can do some small tricks during this period, but I decided to keep it as it is.

Style Squares

They alternate between white and dark grey, so:

 .checkerboard > div {
  background-color: #fff;
}
.checkerboard > div:nth-child(even) {
  background-color: #2f2f2f;
}
Copy after login

Believe it or not, our marks and styles are done! The rest is...

Animation squares

All the animations need to do is convert each square from opacity: 1; to opacity: 0; , and the CSS animation is perfect for this.

 @keyframes poof {
  to {
    opacity: 0;
  }
}
Copy after login

marvelous! I don't even need to set the start keyframe! All I have to do is call the animation on the square.

 .checkerboard > div {
  animation-name: poof;
  animation-duration: 0.25s;
  animation-fill-mode: forwards;
  background: #ffff;
}
Copy after login

Yes, I could have used animation abbreviation properties here, but I often find it easier to separate its constituent properties separately because...well, they are too many and it's hard to read and recognize in a single line.

If you're wondering why animation-fill-mode is needed here, it's because it prevents the animation from looping back to the beginning of the animation when set to forwards . In other words, when the animation is over, each square will remain opacity: 0; instead of reappearing.

I really wanted to do something clever to interleave animation-delay of the blocks, but I ran into some obstacles and finally decided to give up my efforts to use 100% native CSS and switch to some lightweight SCSS. This way I can iterate through all the squares and use a fairly standard function to draw the partially shifted for each square. So, sorry for switching suddenly! That's just part of the journey.

 $columns: 7;
$rows: 7;
$cells: $columns * $rows;

@for $i from 1 through $cells {
  .checkerboard > div:nth-child(#{$i}) {
    animation-delay: (random($cells) / $columns) s;
  }
}
Copy after login

Let's break it down:

  • Variables with the number of grid columns ( $columns ), the number of grid rows ( $rows ) and the total number of cells ( $cells ). The last one is the product of the first two multiplied together. If we know that we always work in a perfect square grid, then we can do some refactoring it, using an exponent to calculate the number of cells.
  • Then for each cell instance between 1 and $cells total (49 in this case), each individual square will get an animation-delay based on its :nth-child() value. So the first square is div:nth-child(1) , then div:nth-child(2) , and so on. Check out the compiled CSS in the demo and you will see how it expands all.
 /* Yes, Autoprefixer is in there... */
.checkerboard > div:nth-child(1) {}
.checkerboard > div:nth-child(2) {}
/* etc. */
Copy after login
  • Finally, animation-delay is a calculation that takes a random number between 1 and the total number of $cells , divides by the number of $columns , and appends the seconds after the value. Is this the best way? I have no idea. It comes down to playing around with something and finding something that feels “right” to you. This feels "right" to me.

I really want to get creative and customize properties with CSS instead of resorting to SCSS. I like that custom properties and values ​​can be updated on the client side, while the values ​​calculated in SCSS are compiled and left unchanged at build time. Again, this is exactly where I really want to switch to JavaScript. However, I had already prepared the bed and could only sleep on it.

If you looked at the compiled CSS earlier, you will see the calculated value:

 .checkerboard > div:nth-child(1) {
  animation-delay: 4.5714285714s;
}

.checkerboard > div:nth-child(2) {
  animation-delay: 5.2857142857s;
}

.checkerboard > div:nth-child(3) {
  animation-delay: 2.7142857143s;
}

.checkerboard > div:nth-child(4) {
  animation-delay: 1.5714285714s;
}
Copy after login

Well, maybe the animation should be optional...

Some people are sensitive to motion and movement, so it is best to change the settings so that the styling and animation of the squares are only set if the user likes it. We have a media query to achieve this!

 @media screen and (prefers-reduced-motion: no-preference) {
  .checkerboard > div {
    animation-name: poof;
    animation-duration: 0.25s;
    animation-fill-mode: forwards;
    background: #ffff;
  }
  .checkerboard > div:nth-child(even) {
    background: #2f2f2f;
  }
}
Copy after login

that's all!

The above is the detailed content of Checkerboard Reveal. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

It&#039;s out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That&#039;s like this.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...

See all articles