Home Web Front-end CSS Tutorial Use pure CSS to implement filter menu function

Use pure CSS to implement filter menu function

Dec 13, 2017 pm 01:38 PM
css 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:


##

Use pure CSS to implement filter menu function



As shown in the picture above, the user wants to buy a dress. In the entire product list, there are too many products displayed. Customers don't know how to choose the dress they want. A category is provided at the top. Customers select the options they need, and the products that do not meet the conditions will be filtered out. In the picture above, I chose an "elegant" skirt. Of course, you can choose more conditions. I won’t say much more here.

Having said that, in the past, JavaScript (or jquery-based plug-in) was needed to achieve such an effect. For example, CodyHouse’s content filtering example. But for a student who doesn't understand JavaScript (not understanding is not a reason), it is indeed difficult to implement a similar function (sometimes I have personal experience and feel like crying without tears, who said I didn't study hard at the beginning).

Fortunately, puppets can now be implemented without JavaScript. In other words, it is not difficult to implement a content filter with simpler functions using pure CSS. This is what I want to share with you today.

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 out

When you click the "Men's Clothing" button," Both "women's clothing" and "children's clothing" will be filtered out

When you click the "children's clothing" button, both "women's clothing" and "men's clothing" will be filtered out

Of 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{
            ....
        }
    }
}
Copy after login

To control the corresponding .women and .children elements through the above style, you must ensure that the elements are consistent with the input element It’s the brotherly element. This is also the second key point to be discussed below.



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

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

SCSS


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

Simple Analyze this style code.


In order to make the page look better, first hide the :


input[type="radio"] {
  display:none;
}
Copy after login

We use the for attribute of the label to control whether the radio is selected. Beautify the label style:



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

The next style code is also the most critical part. When the input is selected, the label style:



input[type="radio"]{
  &[id="men"]:checked {
      label {
      background:#6666ff;
    }
  }
  ...
}
Copy after login

Above The code represents when selected: checked, its adjacent label changes the background color.


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

其他两个选项也是类似的,就不在做过多的阐述。

美化的样式,这里就不说了,大家都懂的。

通过这个过程下来,你就能看到前面DEMO展示的效果了。
总结

本文主要介绍了如何依赖于CSS的属性选择器、通用相邻兄弟选择器和伪类选择器来实现一个简单的Use pure CSS to implement filter menu function的功能。在整个实例当中,要把握的是严谨的HTML结构,因为相邻兄弟选择器对于结构的依赖程度非常的强。也就是说结构修改之后,你的选择器和样式都要做一定的修改。
相关推荐:

最简洁的CSS学习笔记

全面总结css中属性值继承知识

css和js实现弹出登录居中界面教程

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

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.

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

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.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

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-

The Roles of HTML, CSS, and JavaScript: Core Responsibilities The Roles of HTML, CSS, and JavaScript: Core Responsibilities Apr 08, 2025 pm 07:05 PM

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.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

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.

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

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 bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

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

How to view the date of bootstrap How to view the date of bootstrap Apr 07, 2025 pm 03:03 PM

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.

See all articles