Introduction
In modern web development, creating interactive UI components like tooltips usually requires JavaScript libraries. However, with the introduction of CSS Anchor Positioning, developers can now build dynamic and responsive tooltips using pure CSS.
This is a big step forward for front-end development because it reduces dependency on JavaScript, improves performance, and simplifies code.
In this article, we will learn how to use CSS Anchor Positioning to create tooltips without any JavaScript libraries.
What is CSS Anchor Positioning?
CSS Anchor Positioning is a new feature in CSS that allows you to position one element relative to another element (called the anchor).
Instead of manually calculating positions using JavaScript, CSS can automatically align elements like tooltips, popups, or dropdowns.
Why It Matters
This feature is useful because:
Basic Idea
You define:
Then CSS links them together.
How CSS Anchor Positioning Works
Step-by-Step Flow
Define an anchor element
Assign it an anchor name
Create a tooltip element
Use CSS to position tooltip relative to anchor
Key Properties
anchor-name → defines the anchor
position-anchor → links element to anchor
top, left, right, bottom → control placement
Creating a Tooltip Without JavaScript
Step 1: HTML Structure
<button class="btn" style="anchor-name: --btn;">Hover me</button>
<div class="tooltip">
This is a tooltip
</div>
Step 2: Basic CSS
.tooltip {
position: absolute;
position-anchor: --btn;
top: anchor(bottom);
left: anchor(center);
transform: translateX(-50%);
background: black;
color: white;
padding: 6px 10px;
border-radius: 4px;
}
Explanation
anchor-name: --btn → defines the button as anchor
position-anchor → connects tooltip to button
top: anchor(bottom) → places tooltip below button
left: anchor(center) → centers tooltip horizontally
Adding Hover Interaction (No JavaScript)
CSS Code
.tooltip {
opacity: 0;
transition: opacity 0.3s;
}
.btn:hover + .tooltip {
opacity: 1;
}
Explanation
Styling the Tooltip for Better UI
Example Styles
.tooltip {
font-size: 14px;
box-shadow: 0 4px 10px rgba(0,0,0,0.2);
white-space: nowrap;
}
Why Styling Matters
Good styling improves:
User experience
Readability
Visual appeal
Position Variations (Top, Bottom, Left, Right)
Tooltip Above Element
top: anchor(top);
transform: translate(-50%, -100%);
Tooltip Below Element
top: anchor(bottom);
Tooltip Left Side
left: anchor(left);
transform: translate(-100%, -50%);
Tooltip Right Side
left: anchor(right);
Why This is Powerful
You can easily change tooltip position without rewriting logic.
Advantages of Using CSS Anchor Positioning
No JavaScript Required
You don’t need libraries like:
Better Performance
Less JavaScript means faster page load and smoother UI.
Cleaner Code
CSS handles layout logic, making code easier to maintain.
Modern Web Development Approach
Aligns with latest CSS features and standards.
Limitations to Consider
Browser Support
CSS Anchor Positioning is still new and may not work in all browsers.
Fallback Required
You may need fallback solutions for older browsers.
Learning Curve
Developers need to understand new CSS properties.
When Should You Use It?
Best Use Cases
Tooltips
Dropdowns
Popovers
UI hints
Avoid When
Real-World Example
Tooltip in a Form Field
Imagine a form input where you want to show help text.
Using CSS Anchor Positioning:
Anchor → input field
Tooltip → help message
This works without any JavaScript and improves performance.
Summary
CSS Anchor Positioning is a modern CSS feature that enables developers to position elements like tooltips relative to anchor elements without using JavaScript. It improves performance, reduces code complexity, and supports clean UI development. While it has some limitations like browser support, it is an excellent choice for building lightweight, fast, and responsive tooltips in modern web applications.