Table of Contents
Pure CSS realizes the N%3 cycle effect of element color
Home Web Front-end CSS Tutorial How to use pure CSS to achieve the effect of alternating element colors by n%3?

How to use pure CSS to achieve the effect of alternating element colors by n%3?

Apr 05, 2025 pm 07:00 PM
css ai

How to use pure CSS to achieve the effect of alternating element colors by n%3?

Pure CSS realizes the N%3 cycle effect of element color

In front-end development, it is often necessary to dynamically allocate colors according to element positions, such as realizing the effect of color cycles by N%3. This article will explain how to achieve this effect using CSS only.

Assume that the HTML structure is as follows:

<div class="container">
  <p></p>
<p></p>
<p></p>
<p></p>
<p></p>
<p></p>
</div>

The goal is to make the p element color cyclically change according to its position (N%3 result): N%3 == 0 is color 1, N%3 == 1 is color 2, and N%3 == 2 is color 3.

First, define the CSS variable to store the color value:

 .container {
  --color1: #fff;
  --color2: #da3838;
  --color3: rgb(49, 196, 23);
}

Then, use nth-child pseudo-class and the calc() var() to dynamically apply the color:

 .container p:nth-child(3n 1) {
  background-color: var(--color1);
}

.container p:nth-child(3n 2) {
  background-color: var(--color2);
}

.container p:nth-child(3n 3) {
  background-color: var(--color3);
}

In the code, 3n 1 , 3n 2 , 3n 3 select elements that satisfy N%3 == 0, N%3 == 1, N%3 == 2, respectively, and apply predefined colors through the var() function. In this way, the background color of each p element will automatically cyclize according to its position in .container , and the N%3 color loop effect of pure CSS can be achieved without JavaScript.

The above is the detailed content of How to use pure CSS to achieve the effect of alternating element colors by n%3?. 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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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

PHP Tutorial
1596
276
Cryptocurrency IDO platform top5 Cryptocurrency IDO platform top5 Aug 21, 2025 pm 07:33 PM

The best IDO platforms in 2025 are pump.fun, Bounce, Coin Terminal, Avalaunch and Gate Launchpad, which are suitable for Meme coin speculation, community-driven auctions, high-return pursuits, Avalanche ecological investment and fair participation of novices. The choice needs to combine investment goals, risk tolerance and project preferences, and focus on platform review and security.

What is Polkadot (DOT currency)? Future development and price forecast of DOT What is Polkadot (DOT currency)? Future development and price forecast of DOT Aug 21, 2025 pm 07:30 PM

What is the directory DOT (Poker Coin)? The origin of Polkadot DOT (Polkadot) The operating principle of Polkadot has 5 major features, aiming to establish the Polkadot ecosystem (Ecosystem) 1. Interoperability 2. Scalability 3. Community Autonomy 4. No Fork Upgrade 5. NPOS Consensus Protocol Polkadot Key Features DOT Ecosystem Polkadot Vision: Connecting Everything Polkadot's Future Development Polkadot Price Forecast Polkadot 2025 Price Forecast Polkadot 2026-203

What websites are there for cryptocurrency airdrop information aggregation? What websites are there for cryptocurrency airdrop information aggregation? Aug 21, 2025 pm 07:36 PM

Cryptocurrency airdrop information aggregation websites include Airdrop Alert, One Click Airdrop Tracker, Free Airdrop.io and CoinMarketCap airdrop sectors. These platforms integrate full-network airdrop projects and provide functions such as classification screening, task guidance and participation progress tracking to help users efficiently obtain free tokens.

How much does a wlfie token cost How much does a wlfie token cost Aug 21, 2025 am 09:15 AM

WLFI is a governance token for the LendFlare platform, built on Convex Finance to optimize revenue farming on Curve and Convex. Its price is affected by the overall market conditions of the crypto market, platform TVL, governance and pledge mechanism, supply and demand relationship and competitive environment. Investors can query real-time prices through platforms such as CoinGecko, CoinMarketCap or Uniswap, and then purchase USDT through mainstream exchanges such as Binance, OKX, and Huobi, and withdraw cash to a platform that supports WLFI transactions for redemption. Pay attention to network consistency and address accuracy during operation to avoid asset losses.

Have wlfi tokens been on the exchange Have wlfi tokens been on the exchange Aug 21, 2025 am 09:18 AM

WLFI tokens have not yet been confirmed to be launched on mainstream centralized exchanges. Investors need to verify their listing status through official channels or platforms such as CoinMarketCap and CoinGecko. If they are not launched, they may only trade on decentralized exchanges (DEXs) such as Uniswap and PancakeSwap. Users can connect to DEXs through Web3 storage such as MetaMask and enter the contract address obtained by the official to trade. Pay attention to slippage tolerance and security risks when operating; whether WLFI can be launched on a centralized exchange in the future depends on factors such as project fundamentals, community activity, liquidity and compliance. Project parties need to actively connect with the exchange and meet the review requirements, while Binance, OKX, Huobi

How to create a glassmorphism effect with CSS How to create a glassmorphism effect with CSS Aug 22, 2025 am 07:54 AM

To create a glass mimicry effect of CSS, you need to use backdrop-filter to achieve background blur, set a translucent background such as rgba(255,255,255,0.1), add subtle borders and shadows to enhance the sense of hierarchy, and ensure that there is enough visual content behind the elements; 1. Use backdrop-filter:blur(10px) to blur the background content; 2. Use rgba or hsla to define the transparent background to control the degree of transparency; 3. Add 1pxsolidrgba(255,255,255,0.3) borders and box-shadow to enhance the three-dimensionality; 4. Ensure that the container has rich backgrounds such as pictures or textures to present a blurred penetration effect; 5. It is compatible with old browsers

Ranking of mainstream Bitcoin trading apps, list of eight global users Ranking of mainstream Bitcoin trading apps, list of eight global users Aug 21, 2025 am 08:54 AM

The crypto asset trading platform is a key hub connecting users and digital currencies. The article introduces mainstream global platforms such as Binance, OKX, gate.io, Huobi, KuCoin, Kraken, BITFINEX and Bitstamp. These platforms have performed outstandingly in terms of user volume, transaction volume, security, liquidity and service diversity, covering a variety of businesses such as spot, derivatives, DeFi, NFT, etc., meeting the needs of different users, and promoting the popularization and development of digital assets on a global scale.

The difference between fundamentals of cryptocurrencies and capital flows and how to choose The difference between fundamentals of cryptocurrencies and capital flows and how to choose Aug 21, 2025 pm 07:39 PM

Cryptocurrency investment needs to combine fundamentals and capital flows: long-term investors should pay attention to fundamental factors such as project technology and teams to evaluate intrinsic value, while short-term traders can rely on capital flow data such as trading volume and capital flow to grasp market opportunities. The two are used complementary and refer to authoritative data sources such as CoinMarketCap and Glassnode, which can more effectively reduce risks and improve decision-making quality.

See all articles