I am looking for a CSS element that will allow me to place an annotation above a specific word or part of a sentence. The annotation should always be visible and not disrupt the surrounding text.
It is easy to achieve this effect if the word is longer than the annotation, but I am having trouble when the annotation is longer than the word below it.
I initially thought the span
element would work best, but it seems like that might not be the case. I also tried using the table
element, but neither scenario seemed to work properly.
p { font-size: 1.5em }
.highlight {
position: relative;
display: inline-block;
margin-top: 1em;
color: red;
}
.highlight .annotation {
position: absolute;
width:100%;
left: 0;
bottom: 80%;
font-size: 0.75em;
text-align: center;
white-space: nowrap;
}
<p>short <span class=highlight>fo<span class=annotation>foo baar foo</span></span> but not centered.</p>
<p>long <span class=highlight>fooooooooooo<span class=annotation>foo baar foo</span></span> and centered.</p>
This screenshot illustrates what I am trying to accomplish: