Use pure CSS to implement filter menu function
In this article, we will use pure CSS to achieve the same effect as Taobao's baby filter menu. Although the example is not as powerful as Taobao, the principle is similar. If you put some effort, you can also achieve the same effect as Taobao. Hope it helps everyone.
Content filtering is a common function on the Web, especially on e-commerce websites. It allows users to filter content and only display content that meets their requirements. A screenshot can better illustrate this function, such as Taobao:

Let’s look at an example first
Let’s look at a simple example first. When the page loads, women’s clothing, men’s clothing and children’s clothing will appear together. When you do the following During operation, there are unexpected effects: When you click the "Women's Clothing" button, "Men's Clothing" and "Children's Clothing" will be filtered outWhen you click the "Men's Clothing" button," Both "women's clothing" and "children's clothing" will be filtered outWhen you click the "children's clothing" button, both "women's clothing" and "men's clothing" will be filtered outOf course, the function of this case is not as good as Taobao It's so NB, but Haodai also implements a function similar to content filtering.
Implementation Principle
The principle of implementing this function is actually not too complicated, but everyone has not paid attention to it. I can summarize it in two aspects:
Powerful selector
To achieve this function, it mainly relies on the universal sibling selection in the powerful CSS selector Selector (E~F) and pseudo-class selector: checked. When a radio button is selected, the content of other categories is hidden:
input[type="radio"]{ &[id="men"]:checked { ~ .women, ~ .children{ .... } } }
Good, matching structure
Using only CSS to make this function requires a rigorous structure, because the disorder of the structure will directly affect the required effect. This is also its shortcoming. One of them is to use the radio button "radio" to match the label. In order to have a nice appearance, if you do not want to display , you need to control the selected "radio" through the for attribute of the label. Therefore, the id value of the input must match the for value of the label. In addition, the name values of all inputs are the same, telling the browser that they belong to a group. For example:
<input type="radio" id="men" name="clothing" /> <label for="men">男装</label> <input type="radio" id="women" name="clothing"/> <label for="women">女装</label> <input type="radio" id="children" name="clothing"/> <label for="children">童装</label> <input type="radio" id="reset" name="clothing"/> <label for="reset">重置</label>
Implementation steps
After understanding the implementation principle, it will be much simpler to complete the effect of the example at the beginning of the article.
HTML
The HTML structure is actually very simple. You only need to pay attention to the matching of input and label and the content element to be filtered and its sibling element. For example, in this example, there are three main categories of content elements: men's clothing.men, women's clothing.women and children's clothing.children.
<p class="container"> <!-- 必须保证input和label匹配 --> <input type="radio" id="men" name="clothing " /> <label for="men">男装</label> <input type="radio" id="women" name="clothing "/> <label for="women">女装</label> <input type="radio" id="children" name="clothing "/> <label for="children">童装</label> <input type="radio" id="reset" name="clothing "/> <label for="reset">重置</label> <!-- 要被过滤的内容元素需要与input元素是兄弟元素 --> <p class="tile men"> <img src="/static/imghw/default1.png" data-src="" alt=" class="lazy" alt=""> </p> <p class="tile women"> <img src="/static/imghw/default1.png" data-src="" alt=" class="lazy" alt=""> </p> <p class="tile children"> <img src="/static/imghw/default1.png" data-src="" alt=" class="lazy" alt=""> </p> <!-- 此处省略N个.men、.women和.children元素 --> </p>
It’s much simpler if you understand the principle. Let’s first look at the overall code:
body{ margin:0; text-align:center; font-family: Verdana; background:#f5f5f5; } h1 { text-align:center; } .container { width:90%; margin:0 auto; } input[type="radio"] { display:none; } label { width:23%; float:left; text-align:center; background:#ffffff; box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); color:#222222; padding:0.5%; margin:0.5%; margin-bottom:30px; cursor:pointer; } input[type="radio"]{ &[id="men"]:checked { * label { background:#6666ff; } ~ .women, ~ .children { width:0; height:0; padding:0; margin:0; opacity:0; } } &[id="women"]:checked { * label { background:#ff4466; } ~ .men, ~ .children { width:0; height:0; padding:0; margin:0; opacity:0; } } &[id="children"]:checked { * label { background:#66dd99; } ~ .men, ~ .women { width:0; height:0; padding:0; margin:0; opacity:0; } } } .tile { width:23%; float:left; transition:all 1s; margin:0.5%; padding:0.5%; background:#6666ff; img { width: 100%; } }
In order to make the page look better, first hide the :
input[type="radio"] { display:none; }
label { width:23%; float:left; text-align:center; background:#ffffff; box-shadow: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.24); color:#222222; padding:0.5%; margin:0.5%; margin-bottom:30px; cursor:pointer; }
input[type="radio"]{ &[id="men"]:checked { label { background:#6666ff; } } ... }
According to the previous principle introduction, we can know that when we choose "Men's Clothing", then "Women's Clothing" and "Children's Clothing" need to be hidden. Here they are hidden through false images, that is, input[type= "radio"][id="men"] is selected, and its similar sibling elements .women and .children are hidden:
input[type="radio"]{ &[id="men"]:checked { ... ~ .women, ~ .children { width:0; height:0; padding:0; margin:0; opacity:0; } } }
其他两个选项也是类似的,就不在做过多的阐述。
美化的样式,这里就不说了,大家都懂的。
通过这个过程下来,你就能看到前面DEMO展示的效果了。
总结
本文主要介绍了如何依赖于CSS的属性选择器、通用相邻兄弟选择器和伪类选择器来实现一个简单的Use pure CSS to implement filter menu function的功能。在整个实例当中,要把握的是严谨的HTML结构,因为相邻兄弟选择器对于结构的依赖程度非常的强。也就是说结构修改之后,你的选择器和样式都要做一定的修改。
相关推荐:
The above is the detailed content of Use pure CSS to implement filter menu function. For more information, please follow other related articles on the PHP Chinese website!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

Answer: You can use the date picker component of Bootstrap to view dates in the page. Steps: Introduce the Bootstrap framework. Create a date selector input box in HTML. Bootstrap will automatically add styles to the selector. Use JavaScript to get the selected date.
