Home > Web Front-end > Front-end Q&A > css implementation of tab

css implementation of tab

王林
Release: 2023-05-21 09:10:36
Original
986 people have browsed it

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>
Copy after login

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;
}
Copy after login

Here we are .tab -nav aAdded :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;
}
Copy after login

Here we will .tab-content’s default state opacitySet 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;
}
Copy after login

Here we are.tab-contentAdd 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>
Copy after login
.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;
}
Copy after login

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template