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

Detailed explanation of how to use WeChat mini program map

This article example shares the specific implemen...

Methods for optimizing Oracle database with large memory pages in Linux

Preface PC Server has developed to this day and h...

How to deploy nginx with Docker and modify the configuration file

Deploy nginx with docker, it's so simple Just...

Detailed tutorial on installing MYSQL under WINDOWS

1. Download the installation package -Choose the ...

In-depth understanding of Vue's plug-in mechanism and installation details

Preface: When we use Vue, we often use and write ...

Implementation of Docker deployment of MySQL cluster

Disadvantages of single-node database Large-scale...

MySql knowledge points: transaction, index, lock principle and usage analysis

This article uses examples to explain the princip...

Markup validation for doctype

But recently I found that using this method will c...

JavaScript to achieve product query function

This article example shares the specific code of ...

Solution for Docker Swarm external verification load balancing not taking effect

Problem Description I created three virtual machi...

Using JS to implement binary tree traversal algorithm example code

Table of contents Preface 1. Binary Tree 1.1. Tra...

MySQL query specifies that the field is not a number and comma sql

Core SQL statements MySQL query statement that do...

Detailed explanation of zabbix executing scripts or instructions on remote hosts

Scenario Requirements 1. We can use the script fu...