This time I will show you how to implement animated TAB switching, what are the precautions for implementing animated TAB switching, the following is a practical case, let's take a look.
The designer gave a rendering of tab switching. Although it is a small function, front-end engineers still have many details to pay attention to when implementing it. I wrote a demo for your reference.
The final effect is as follows:
In order for the gif animation to show details, I extended the animation time to 3 seconds
Implementation ideas
Interval vertical lines, because they are not vertical, so borders cannot be used. I'm going to use pseudo-elements.
There are only 3 vertical bars, but there are 4 li s. This is simple and can be selected using the :not(:first-child) selector.
The switching background color changes, because I want to have an effect from small to large, so I can’t directly use the background color to achieve it. I am also going to use pseudo elements to achieve it.
If the size of the pseudo element is used to control it, the calculation will be more complicated, so I want to use box-shadow shadow to achieve it.
Okay, that’s it. Let’s start writing the code, as follows:
HTML code
<p> </p>
The above code structure has been written before Yes, I think it’s ok, so I won’t make any adjustments. There is no cumbersome code.
CSS code
.m { margin: 100px; } .tab { width: 400px; margin: 0 auto; border: 1px solid #ddd; height: 40px; text-align: center; line-height: 40px; background: #fff; border-radius: 10px; overflow: hidden; } .tab li { float: left; width: 100px; position: relative; overflow: hidden; } .tab li:before, .tab li:after, .tab li a { -webkit-transition: all 0.25s ease-in-out; transition: all 0.25s ease-in-out; } .tab li:before, .tab li:after { content: ""; display: block; } .tab li:not(:first-child):after { background: #ddd; height: 20px; width: 1px; left: 0; top: 10px; position: absolute; } .tab li a { display: block; position: relative; z-index: 2; color: #000; font-size: 14px; } .tab li:before { width: 0; height: 0; top: 50%; left: 50%; z-index: 1; position: absolute; } .tab li:hover a { color: #fff; } .tab li:hover:before { box-shadow: 0 0 0 100px #36bc99; } .tab li:hover + li:after, .tab li:hover:after { height: 0; top: 20px; }
Code analysis:
Animation implementation is very simple, just use the transition attribute.
To control your own pseudo-elements and the pseudo-elements of the next sibling element, just use the + selector.
Other codes are relatively clear and simple, you can analyze them yourself.
It is very simple to achieve this effect. The focus is on daily accumulation and flexible matching of various parameters. Thinking of the implementation method and finally writing the code is very fast. And there is no high point of knowledge in it.
The reason why CSS is difficult is not that you don’t know how to do it, but that you don’t know how to match it.
Actually, only 99% of the design effect is restored. One of the two lines is inside the background and the other is outside the background. What should I do if I want to put both dividing lines inside the background? What about implementation? You can think about it.
Let’s use scss. The css above is compiled. In fact, it is very convenient and fast to implement it with scss, and the code is more readable.
The demonstration is as follows:
.m { margin: 100px; } .tab { width: 400px;margin: 0 auto;border: 1px solid $cdd;height: 40px;text-align: center;line-height: 40px; background: $cff;border-radius: 10px;overflow: hidden; li { float: left;width: 100px;position: relative;overflow: hidden; &:before,&:after,a {@include dz();} &:before,&:after { content: "";display: block; } &:not(:first-child) { &:after { background: $cdd;height: 20px;width: 1px;left: 0;top: 10px;position: absolute; } } a { display: block;position: relative;z-index: 2;color: $c00;font-size: 14px; } &:before { width: 0;height: 0;top: 50%;left: 50%;z-index: 1;position: absolute; } &:hover { a {color: $cff;} &:before { box-shadow: 0 0 0 100px $cyan; } & + li:after,&:after { height: 0;top: 20px; } } } }
Of course, in this code, I used color variables and mixin to mix in the code. You cannot use it directly.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the use of pointer-events in css3
Detailed explanation of the use of focus-within
The above is the detailed content of How to implement animated TAB switching. For more information, please follow other related articles on the PHP Chinese website!

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
