Table of Contents
Before you begin…
Let's get started
This is the basic code
Settings: checked state style
Use it
Home Web Front-end CSS Tutorial Conditionally Styling Selected Elements in a Grid Container

Conditionally Styling Selected Elements in a Grid Container

Mar 13, 2025 pm 12:38 PM

Conditionally Styling Selected Elements in a Grid Container

Many application scenarios require displaying selectable items in the grid, such as calendars, shopping carts, galleries, file browsers, and online libraries, and even security checks that require you to choose all pictures with zebra crossings.

This article introduces a clever way to display optional items in the grid, instead of reCAPTCHA, you can select multiple items. When selecting two or more adjacent projects, we can cleverly design them using :nth-of-type combiner, pseudo-element, and :checked pseudo-class to make them look combined.

The idea of ​​this combiner and pseudo-element to implement rounded checkboxes stems from an article I wrote earlier. That's a simple single column design:

This time, however, the rounded corner effect is applied to elements on the vertical and horizontal axes on the grid. You don't need to read my previous post on checkbox styles, because I'm going to cover everything you need to know here. However, if you are interested in a simplified version of what is done in this article, that article is worth a look.

Before you begin…

It will be very useful to note the following points. For example, for simplicity, I used static HTML and CSS in my demo. Depending on your application, you may need to dynamically generate the grid and its items within it. To focus on the effects, I omitted the actual check of the auxiliary function, but in a production environment you definitely need to consider this kind of thing.

Also, I use CSS Grid for layout. I recommend doing this, but it's just a personal preference and your experience may vary. For me, using a grid allows me to easily locate ::before and ::after pseudo-elements of the project using sibling selectors.

So, no matter what layout standards you might want to use in your application, make sure the pseudo-elements can still be positioned in CSS and make sure the layout remains the same across different browsers and screens.

Let's get started

As you might have noticed in previous demonstrations, selecting and unchecking the checkbox elements modify the box's design, depending on the selection status of other checkboxes around it. This is because I use the pseudo-element of the adjacent element instead of its own to style each box.

The following figure shows how the ::before pseudo-element of the box in each column (except the first column) overlaps with the box on their left, and how the ::after pseudo-element of the box in each row (except the first row) overlaps with the box above.

This is the basic code

Tagging is very simple:

1

<main></main>

Copy after login

There is more in the initial CSS. But first of all, the grid itself:

1

2

3

4

5

6

7

8

/* Grid*/

main {

  display: grid;

  grid: repeat(5, 60px) / repeat(4, 85px);

  align-items: center;

  justify-items: center;

  margin: 0;

}

Copy after login

This is a grid of five elements and four columns containing check boxes. I decided to clear the default appearance of the checkboxes and then give them my own light grey background and hyper-rounded borders:

1

2

3

4

5

6

7

8

9

10

11

12

/* All check boxes*/

input {

  -webkit-appearance: none;

  appearance: none;

  background: #ddd;

  border-radius: 20px;

  cursor: pointer;

  display: grid;

  height: 40px;

  width: 60px;

  margin: 0;

}

Copy after login

Also note that the checkbox itself is also a grid. This is the key to putting its ::before and ::after pseudo-elements. Having said that, let's do it now:

1

2

3

4

5

6

7

8

/* Pseudo-elements except the first column and the first row*/

input:not(:nth-of-type(4n 1))::before,

input:nth-of-type(n 5)::after {

  content: '';

  border-radius: 20px;

  grid-area: 1 / 1;

  pointer-events: none;

}

Copy after login

We only select pseudo-elements that are not in the checkbox of the first column or row of the grid. input:not(:nth-of-type(4n 1)) starts with the first check box and select ::before for every four items starting there. But please note that we are talking about :not() , so what we actually do is skip the ::before pseudo-element of each fourth checkbox starting from the first. Then, we start with the fifth checkbox and apply the style to the ::after pseudo-element of each checkbox.

Now we can style the ::before and ::after pseudo-elements for each checkbox that is not in the first column or row of the grid, so that they move left or upward respectively, hiding them by default.

1

2

3

4

5

6

7

8

9

/* Pseudo-element except the first column*/

input:not(:nth-of-type(4n 1))::before {

  transform: translatex(-85px);

}

 

/* Pseudo-element except the first line*/

input:nth-of-type(n 5)::after {

  transform: translatey(-60px);

}

Copy after login

Settings: checked state style

Now is the style when the checkbox is in the :checked state. First, let's give them a color, like a lime green background:

1

input:checked { background: limegreen; }

Copy after login

A selected box should be able to restyle all adjacent selected boxes. In other words, if we select the 11th check box in the grid, we should also be able to style the boxes above, below, left and right of it.

This is done by positioning the correct pseudo-elements. How do we do this? Well, it depends on the actual number of columns in the grid. If two adjacent boxes are selected in a 5×4 grid, the CSS is as follows:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

/* The right boundary of the selected box (if the element to its right is selected)*/

input:not(:nth-of-type(4n)):checked input:checked::before {

  border-top-right-radius: 0;

  border-bottom-right-radius: 0;

  background: limegreen;

}

/* The lower boundary of the selected box (if the following element is selected)*/

input:nth-last-of-type(n 5):checked * * * input:checked::after {

  border-bottom-right-radius: 0;

  border-bottom-left-radius: 0;

  background: limegreen;

}

/* The left boundary of the selected box adjacent (right) selected box*/

input:not(:nth-of-type(4n)):checked input:checked input::before {

  border-top-left-radius: 0;

  border-bottom-left-radius: 0;

  background: limegreen;

}

/* The upper boundary of the selected box (below) selected box*/

input:not(:nth-of-type(4n)):checked * * * input:checked input::before {

  border-top-left-radius: 0;

  border-top-right-radius: 0;

  background: limegreen;

}

Copy after login

If you want, you can generate the above code dynamically. However, for a typical grid (such as a picture library), the number of columns will be small and may be a fixed number of items, while rows may continue to increase. Especially if designed for mobile screens. That's why this approach is still an effective approach. If for some reason your application happens to have finite rows and extended columns, consider rotating the grid to sides, because for a series of projects, the CSS Grid arranges them from left to right, top to bottom (i.e. row by row).

We also need to add styles to the last checkbox in the grid - they are not all overlaid by pseudo-elements because they are the last item in each axis.

1

2

3

4

5

6

7

8

9

10

/* The left boundary of the selected box (last column)*/

input:nth-of-type(4n-1):checked input:checked {

  border-top-left-radius: 0;

  border-bottom-left-radius: 0;

}

/* The upper boundary of the selected box (last column) adjacent (lower) checked box*/

input:nth-of-type(4n):checked * * * input:checked {

  border-top-left-radius: 0;

  border-top-right-radius: 0;

}

Copy after login

These selectors are tricky! The first one...

1

input:nth-of-type(4n-1):checked input:checked

Copy after login

...It basically says this:

In the penultimate column, a selected element is next to a selected element.

The calculation method of nth-of-type is as follows:

1

<code>4(0) - 1 = 无匹配项4(1) - 1 = 第3 个项目4(2) - 1 = 第7 个项目4(3) - 1 = 第11 个项目等等。</code>

Copy after login

So, we start with the third checkbox and select each fourth checkbox from there. If the check boxes in the sequence are selected, then if they are also selected, we also style the adjacent check boxes.

And this line:

1

input:nth-of-type(4n):checked * * * input:checked

Copy after login

It's said:

An element, provided that it is selected, directly adjacent to one element, the element is directly adjacent to another element, the element is directly adjacent to another element, and the element is directly adjacent to an element in the selected state.

This means we are selecting each fourth checkbox that is selected. If the check box in the sequence is selected, then if it is also selected, we will style the next fourth check box starting from the check box.

Use it

What we just saw was the general principles and logic behind design. Again, its practicality in your application will depend on the mesh design.

I used rounded borders, but you can try other shapes and even try background effects (Temani gives you ideas). Now that you know how formulas work, the rest depends entirely on your imagination.

Here is what it might look like in a simple calendar:

Again, this is just a rough prototype using static tags. Moreover, there are many auxiliary functions that need to be considered in the calendar function.

That's it! Very clever, right? I mean, nothing completely "new" about what happened. But it is a good example of choosing things in CSS. If we master more advanced selection techniques using combiners and pseudo-elements, our styling capabilities can go beyond styles that only set one item—as we can conditionally style items based on the state of another element.

The above is the detailed content of Conditionally Styling Selected Elements in a Grid Container. 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)

Hot Topics

Java Tutorial
1655
14
PHP Tutorial
1254
29
C# Tutorial
1228
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

How to select a child element with the first class name item through CSS? How to select a child element with the first class name item through CSS? Apr 05, 2025 pm 11:24 PM

When the number of elements is not fixed, how to select the first child element of the specified class name through CSS. When processing HTML structure, you often encounter different elements...

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...

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

See all articles