DEV Community

Discussion on: Binding CSS and JavaScript with HTML data-attributes

Collapse
manuthecoder profile image
ManuTheCoder

Data attributes can also be used for pure CSS tooltips!

<a href="#" data-tooltip="Test tooltip!">Hover me!</a>
Enter fullscreen mode Exit fullscreen mode
[data-tooltip]:hover {
    position: relative;
}
[data-tooltip]:hover::before {
    all: initial;
    font-family: Arial, Helvetica, sans-serif;
    display: inline-block;
    border-radius: 5px;
    padding: 10px;
    background-color: #1a1a1a;
    content: attr(data-tooltip);
    color: #f9f9f9;
    position: absolute;
    bottom: 100%;
    width: 100px;
    left: 50%;
    transform: translate(-50%, 0);
    margin-bottom: 15px;
    text-align: center;
    font-size: 14px;
}
Enter fullscreen mode Exit fullscreen mode
Collapse
crinklesio profile image
Kevin Pennekamp Author

add ::focus in the mix and it gets a lot more accessible!