Use CSS content attr to achieve mouse hover prompt (tooltip) effect

Use CSS content attr to achieve mouse hover prompt (tooltip) effect

Why do we achieve this effect? ​​In fact, this effect is also derived from the title prompt, but because the original one is ugly, there are many derived plug-ins like this, such as jquery-ui's tooltip, Bootstrap's tooltips, etc. There are many plug-in libraries.

Sometimes we don’t need such a large plugin library. In fact, we only need to make some tooltips in one or two places, so we can use the CSS content property with the :before and :after pseudo-elements to insert generated content.

Check the effect as follows

The html code is as follows

<a class="dui-tips" data-tooltip="I am a 3cbest.com tips">w3cbest.com</a>

"data-" is a custom attribute, such as the custom prompt data-tooltip="I am a 3cbest.com prompt", and use the attr of content to call the custom prompt in conjunction with before and after, content: attr(data-tooltip);

content: attr is easy to understand. As long as you know jq's .attr(), you will know what it means. In this example, content: attr is used to get the value in data-tooltip.

CSS Code

.dui-tips {
position: relative;
display: inline-block;
cursor: pointer;
}
 
.dui-tips[data-tooltip]:after,
.dui-tips[data-tooltip]:before {
visibility: hidden;
position: absolute;
top: 50%;
left: 100%;
transition: all .3s;
}
 
.dui-tips[data-tooltip]:after {
 
content: attr(data-tooltip);
transform: translate(-5px, -50%);
white-space: pre;
padding: 5px 10px;
background-color: rgba(0, 0, 0, 0);
color: rgba(255, 255, 255, 0);
}
 
.dui-tips[data-tooltip]:before {
content: '';
height: 0;
width: 0;
transform: translate(-10px, -50%);
border-width: 5px 5px 5px 0;
border-style: solid;
border-color: transparent rgba(0, 0, 0, 0) transparent transparent;
}
.dui-tips:hover:after,.dui-tips:hover:before {
transition: all .3s;
visibility: visible;
 
}
.dui-tips:hover:after {
color: rgba(255, 255, 255, 1);
background-color: rgba(0, 0, 0, 0.8);
transform: translate(5px, -50%);
}
 
.dui-tips:hover:before {
border-color: transparent rgba(0, 0, 0, 0.8) transparent transparent;
transform: translate(0px, -50%);
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  Summary of MySQL InnoDB locks

>>:  Detailed graphic tutorial on installing centos7 virtual machine in Virtualbox

Recommend

About the usage and precautions of promise in javascript (recommended)

1. Promise description Promise is a standard buil...

MySQL 5.7.18 Installer installation download graphic tutorial

This article records the detailed installation tu...

CSS3 mobile vw+rem method to achieve responsive layout without relying on JS

1. Introduction (1) Introduction to vw/vh Before ...

How to use nginx to simulate blue-green deployment

This article introduces blue-green deployment and...

Introduction to the common API usage of Vue3

Table of contents Changes in the life cycle react...

JavaScript event loop case study

Event loop in js Because JavaScript is single-thr...

Two ways to implement text stroke in CSS3 (summary)

question Recently I encountered a requirement to ...

Two ways to build Docker images

Table of contents Update the image from an existi...

Implementation of JavaScript downloading linked images and uploading them

Since we are going to upload pictures, the first ...

Docker builds python Flask+ nginx+uwsgi container

Install Nginx First pull the centos image docker ...

base target="" specifies the target of the base link to open the frame

<base target=_blank> changes the target fram...

Linux lossless expansion method

Overview The cloud platform customer's server...