How to use CSS attribute value regular matching selector (tips)

How to use CSS attribute value regular matching selector (tips)

There are three types of attribute value regular matching selectors:

  • [attr^="val"]
  • [attr$="val"]
  • [attr*="val"]

These three attribute selectors match characters, not words. The angle symbol ^ , dollar sign $ , and asterisk * are special identifiers in regular expressions, representing preceding match, following match, and any match, respectively.

Using these selectors, pure CSS can create very cool functions.
<!-- more -->

Displays a small icon for the hyperlink and a graphic of the file type

The [attr^="val"] front-matching selector can be used to determine the link address type of the <a> element to display the corresponding small icon. The styles of small icons showing hyperlinks are as follows:

[href] {padding-left: 18px;}
/* Link address*/
[href^="https"],
[href^="//"] {
    background: url("./images/link.png") no-repeat left;
}
/* Anchor link in web page */
[href^="#"]
    background: url("./images/anchor.png") no-repeat left;
}
/* Mobile phone and email */
[href^="tel:"]
    background: url("./images/tel.png") no-repeat left;
}
[href^="mailto:"]
    background: url("./images/e-mail.png") no-repeat left;
}

Effect

By using [attr$="val"] and then matching the selector, you can display the small icon of the file type. The CSS is as follows:

/* Point to the PDF file */
[href$=".pdf"]
    background: url("./images/pdf.png") no-repeat left;
}
/* Download the zip file */
[href$=".zip"] {
    background: url("./images/zip.png") no-repeat left;
}
/* Image link*/
[href$=".png"],
[href$=".gif"],
[href$=".jpg"],
[href$=".jpeg"],
[href$=".webp"]
    background: url("./images/image.png") no-repeat left;
}

The effect is as follows

CSS attribute selector search filtering technology

We can use attribute selectors to help us achieve search filtering effects, such as address books and city lists. This has high performance and requires less code.

The HTML structure is as follows:

<input type="search" id="input" placeholder="Enter the city name or pinyin" />
<ul>
    <li data-search="Chongqing Citychongqing">Chongqing City</li>
    <li data-search="Harbin Cityhaerbin">Harbin City</li>
    <li data-search="Changchun Citychangchun">Changchun City</li>
    <li data-search="Changsha Citychangsha">Changsha City</li>
    <li data-search="沪shanghai">Shanghai</li>
    <li data-search="Hangzhou Cityhangzhou">Hangzhou City</li>
</ul>

At this point, when we enter content in the input box, we can achieve the search and matching effect by dynamically creating a CSS code based on the input content, without having to write code for matching verification ourselves.

var eleStyle = document.createElement('style');
document.head.appendChild(eleStyle);
// Text input box input.addEventListener('input', function() {
    var value = this.value.trim();
    eleStyle.innerHTML = value ? '[data-search]:not([data-search*="' + value +'"]) { display: none; } ' : '';
});

This concludes this article on how to cleverly use CSS attribute value regular matching selectors (tips). For more relevant CSS attribute value regular matching content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope that everyone will support 123WORDPRESS.COM in the future!

<<:  Getting Started with MySQL - Concepts

>>:  HTML+css to create a simple progress bar

Recommend

Analysis of the pros and cons of fixed, fluid, and flexible web page layouts

There is a question that has troubled web designe...

How to implement the jQuery carousel function

This article shares the implementation code of jQ...

MySQL Series Database Design Three Paradigm Tutorial Examples

Table of contents 1. Knowledge description of the...

Detailed explanation of querying JSON format fields in MySQL

During the work development process, a requiremen...

How to obtain and use time in Linux system

There are two types of Linux system time. (1) Cal...

Detailed explanation of using INS and DEL to mark document changes

ins and del were introduced in HTML 4.0 to help au...

MySQL 8.0.20 installation and configuration tutorial under Win10

MySQL 8.0.20 installation and configuration super...

Gradient slide effect implemented by CSS3

Achieve results Code html <div class="css...

A Brief Analysis of the Differences between “:=” and “=” in MySQL

= Only when setting and updating does it have the...

Vue project packaging and optimization implementation steps

Table of contents Packaging, launching and optimi...

Install mysql5.7.17 using RPM under Linux

The installation method of MySQL5.7 rpm under Lin...

Two ways to completely delete users under Linux

Linux Operation Experimental environment: Centos7...

How to implement animation transition effect on the front end

Table of contents Introduction Traditional transi...