.util-truncate {
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
a {
text-decoration: none;
}
a:focus {
box-shadow: 0 0 0 3px blue;
text-decoration: underline;
}
a:focus-visible {
outline: 0;
}
<div class="util-truncate">
<a href="#">This is a long link that will be truncated. This is a long link that will be truncated. This is a long link that will be truncated.</a>
</div>
Problem:
I am trying to apply a ring using box-shadow over the anchor element on focus. But because of the overflow hidden property on parent, the focus style is getting cut-off. As truncate
is a utility class I cannot update any of it styles. Is there a way we could display the desired focus style properly for anchor element with only using CSS?
Following are the approaches I tried on anchor focus state,
- box-shadow
- outline (with offset)
- box-shadow with z-index
- box-shadow with inset (this approach is a bit close to expected output but not exact)
- Using
before/after
pseudo selector
Output:
2
Answers
Increase the
line-height
and consider an insetbox-shadow
Rather than cutting off the rendering, just limit the width of the
a
element. You don’t even need a container div for that, just adisplay: inline-block
to make sure it responds tomax-width
.