Home > Article > Web Front-end > Steps to implement the folding effect of responsive navigation bar using pure CSS
Steps to realize the folding effect of responsive navigation bar using pure CSS
Nowadays, most people browse the web through mobile devices, so responsive design has become An important part of modern web design. In responsive design, the navigation bar is an important element that can effectively display the navigation structure of the web page under different screen sizes.
This article will introduce how to use pure CSS to implement a responsive navigation bar and achieve a folding effect on smaller screen sizes. Code examples will help readers better understand the implementation approach. The specific steps are as follows:
Step 1: HTML structure
We first need to create the HTML structure of the navigation bar. In this example, we use an ordered list (<ul></ul>
) to place navigation items (<li>
) and links (<a></a>
). We also added a button (<button></button>
) that triggers the collapse effect.
<nav class="navbar"> <button class="navbar-toggle"></button> <ul class="navbar-menu"> <li><a href="#">首页</a></li> <li><a href="#">关于我们</a></li> <li><a href="#">服务</a></li> <li><a href="#">联系我们</a></li> </ul> </nav>
Step 2: CSS Style
Next, we use CSS to define the style of the navigation bar. We add a background color, fixed width, and center alignment to the navigation bar. We also add some styling to the navigation items, such as spacing and hover effects. Most importantly, we want to define a folding effect for smaller screen sizes.
.navbar { background-color: #333; width: 100%; text-align: center; } .navbar-menu { list-style-type: none; margin: 0; padding: 0; } .navbar-menu li { display: inline-block; margin: 0 10px; } .navbar-menu li a { color: #fff; text-decoration: none; padding: 10px 0; } .navbar-menu li a:hover { background-color: #555; } .navbar-toggle { display: none; }
Step 3: Responsive folding effect
Now we need to achieve the folding effect of the navigation bar in a smaller screen size. We use CSS media queries to detect the screen size and define a collapse style for the navigation bar on smaller screen sizes. We also need to use the CSS selectors :checked
and
to implement the click event of the collapse button.
@media (max-width: 768px) { .navbar-menu { display: none; } .navbar-menu.show { display: block; } .navbar-toggle { display: block; background-image: url("menu-icon.png"); background-size: cover; width: 30px; height: 30px; } .navbar-toggle:checked ~ .navbar-menu { display: block; } }
In the above code, we use media query (@media
) to detect if the screen size is less than or equal to 768 pixels, if so, the navigation bar is hidden (display : none;
). And, when the screen size is less than or equal to 768 pixels, we display the collapse button and set a background image for it. When the collapse button is clicked, the navigation bar is displayed through the selector :checked
(display: block;
).
So far, we have completed all the steps to implement the folding effect of the responsive navigation bar using pure CSS. With the above code example, we can implement a collapsible responsive navigation bar on mobile devices.
Summary
Responsive design is an important part of modern web design, and the navigation bar is an important element. By using pure CSS and media queries, we can easily implement a collapsible responsive navigation bar to provide users with a better mobile browsing experience. I hope this article can help readers better understand and master this technology.
The above is the detailed content of Steps to implement the folding effect of responsive navigation bar using pure CSS. For more information, please follow other related articles on the PHP Chinese website!