In web design, achieving Tab switching effect is a common requirement. This effect can be easily achieved using CSS. Let’s explain in detail how to use CSS to achieve Tab switching.
First, we need to prepare the HTML code. Tab switching is often composed of a set of content areas and corresponding navigation buttons, as shown below:
<div class="tabs"> <div class="tab-nav"> <a href="#" class="active">Tab 1</a> <a href="#">Tab 2</a> <a href="#">Tab 3</a> </div> <div class="tab-content active"> <p>这是Tab 1的内容</p> </div> <div class="tab-content"> <p>这是Tab 2的内容</p> </div> <div class="tab-content"> <p>这是Tab 3的内容</p> </div> </div>
In this example, we use a .tabs
container to load Tab-related elements, Includes navigation buttons and content area. .tab-nav
is used to place the Tab navigation button, .tab-content
is used to place the Tab content, .active
indicates the default state of the navigation button or content area .
Next, we need to use CSS to achieve the Tab switching effect. We can use the pseudo-class :hover
or :focus
to achieve the hover or focus effect of the navigation button, for example:
.tab-nav a:hover, .tab-nav a:focus { background-color: #ccc; }
Here we are .tab -nav a
Added :hover
and :focus
pseudo-classes. When the mouse is hovering or the navigation button is focused, the background color will appear#ccc
Effect. In this way, the user can perceive the selected Tab more intuitively.
Next, we need to add an animation effect for Tab switching. We can achieve this effect by using the transition
attribute of CSS3, for example:
.tab-content { opacity: 0; transition: opacity .3s ease-in-out; } .tab-content.active { opacity: 1; }
Here we will .tab-content
’s default state opacity
Set to 0, indicating that no content is displayed. Then we added a transition
attribute to .tab-content
, which means that a fade-in and fade-out effect will be produced within 0.3 seconds. When .tab-content
is in the .active
state, opacity
is set to 1 to display the specific content.
Finally, we need to realize the linkage between the Tab navigation button and the content area. Here we can use the :target
pseudo-class of CSS, for example:
.tab-content:target { display: block; } .tab-content { display: none; }
Here we are.tab-content
Add the :target
pseudo-class, which means that the display
of the element will be set to block only when the element is positioned by the anchor target.
, display specific content. When it is not located by the anchor target, set the display
of .tab-content
to none
to hide it.
Through the above steps, we have completed the implementation of the Tab switching effect. The complete code is as follows:
<div class="tabs"> <div class="tab-nav"> <a href="#tab1" class="active">Tab 1</a> <a href="#tab2">Tab 2</a> <a href="#tab3">Tab 3</a> </div> <div class="tab-content active" id="tab1"> <p>这是Tab 1的内容</p> </div> <div class="tab-content" id="tab2"> <p>这是Tab 2的内容</p> </div> <div class="tab-content" id="tab3"> <p>这是Tab 3的内容</p> </div> </div>
.tab-nav a:hover, .tab-nav a:focus { background-color: #ccc; } .tab-content { opacity: 0; transition: opacity .3s ease-in-out; display: none; } .tab-content.active { opacity: 1; display: block; } .tab-content:target { display: block; } .tab-content { display: none; }
Through the above steps, you can now use CSS to easily achieve the tab switching effect in your web pages.
The above is the detailed content of css implementation of tab. For more information, please follow other related articles on the PHP Chinese website!

The article discusses useEffect in React, a hook for managing side effects like data fetching and DOM manipulation in functional components. It explains usage, common side effects, and cleanup to prevent issues like memory leaks.

Lazy loading delays loading of content until needed, improving web performance and user experience by reducing initial load times and server load.

Higher-order functions in JavaScript enhance code conciseness, reusability, modularity, and performance through abstraction, common patterns, and optimization techniques.

The article discusses currying in JavaScript, a technique transforming multi-argument functions into single-argument function sequences. It explores currying's implementation, benefits like partial application, and practical uses, enhancing code read

The article explains React's reconciliation algorithm, which efficiently updates the DOM by comparing Virtual DOM trees. It discusses performance benefits, optimization techniques, and impacts on user experience.Character count: 159

Article discusses preventing default behavior in event handlers using preventDefault() method, its benefits like enhanced user experience, and potential issues like accessibility concerns.

The article explains useContext in React, which simplifies state management by avoiding prop drilling. It discusses benefits like centralized state and performance improvements through reduced re-renders.

The article discusses the advantages and disadvantages of controlled and uncontrolled components in React, focusing on aspects like predictability, performance, and use cases. It advises on factors to consider when choosing between them.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

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