Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Arrow to the left of the tooltip with CSS
Creating an arrow pointing to the left of a tooltip helps users understand that the tooltip relates to the element they're hovering over. This arrow is created using CSS pseudo-elements and border properties to form a triangular shape.
Syntax
.tooltip .tooltip-text::after {
content: "";
position: absolute;
border-width: size;
border-style: solid;
border-color: transparent color transparent transparent;
}
Example
The following example creates a tooltip with an arrow pointing to the left −
<!DOCTYPE html>
<html>
<head>
<style>
.mytooltip {
position: relative;
display: inline-block;
margin-left: 50px;
background-color: #007bff;
color: white;
padding: 10px 15px;
border-radius: 4px;
cursor: pointer;
}
.mytooltip .mytext {
visibility: hidden;
width: 140px;
background-color: #333;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 8px;
position: absolute;
z-index: 1;
top: -5px;
left: 110%;
}
.mytooltip .mytext::after {
content: "";
position: absolute;
top: 50%;
right: 100%;
margin-top: -5px;
border-width: 5px;
border-style: solid;
border-color: transparent #333 transparent transparent;
}
.mytooltip:hover .mytext {
visibility: visible;
}
</style>
</head>
<body>
<div class="mytooltip">Hover over me
<span class="mytext">This is my tooltip!</span>
</div>
</body>
</html>
A blue button appears with "Hover over me" text. When you hover over it, a dark tooltip with white text "This is my tooltip!" appears to the right with a left-pointing arrow connecting it to the button.
Key Points
- The arrow is created using the
::afterpseudo-element with CSS borders -
border-color: transparent #333 transparent transparentcreates a left-pointing triangle -
right: 100%positions the arrow at the left edge of the tooltip -
top: 50%; margin-top: -5pxcenters the arrow vertically
Conclusion
Creating a left-pointing arrow for tooltips enhances user experience by clearly indicating the relationship between the tooltip and its trigger element. The technique uses CSS borders and pseudo-elements to create clean, scalable arrows.
Advertisements
