This article is a follow-up/reformulation of the less-specific formulated question Is it possible to have a hyperlink inside {content:"..."}?.
User Naeem Shaikh, to whom many thanks, friendly and successfully helped me having a hyperlink on mouse-hover, after an HTML tag with a specific id (here called "HPV"
), using jQuery. Full credit for the JS help goes to him.
When I adapted this to my specific content (a term description), I get the following (formerly: JSFiddle 1):
HTML
<br><br>
<a class="term" id="HPV">HPV</a>
CSS
a.term{text-decoration:underline; text-decoration-style:dotted; -moz-text-decoration-style:dotted}
a.term:hover{text-decoration:none; color:#aaaaaa}
a.term:hover:after{position:relative; padding: 1px; top:-0.9em; left:-5px; border:1px dotted #aaaaaa; color:black; background-color:white}
a.term#HPV:hover:after{content:"Human papillomavirus."}
JS (requires jQuery)
$(function(){
$('#HPV').hover(function(e){
$(this).append('<a href="http://en.wikipedia.org/wiki/Human_papillomavirus"> Wikipedia.</a>');
},function(){
$(this).find('a').remove();
});
});
Now my next question is how to get the hyperlink "inside/after" the content of the after-selector. Instead of just after the content of the original HTML tag itself.
In my example: after "Human papillomavirus.", as such:
Human papillomavirus. Wikipedia.
HPV
Instead of how it is now:
Human papillomavirus.
HPV Wikipedia.
I guess this calls for an ingenious way to have these links positioned inside the content
of the :after-selector
: especially when more than 1 link should be inserted, with extra text/content in between.
---
A small remark/glitch: as it is now, you can't put the "dot" .
after Wikipedia on the right-side of the </a>
in the JS. Or rather: you can, but then the CSS of the original tag (in CSS: a.term:hover
) would unfortunately be adopted (cf. the gray color of the .
then).
The same behavior can be seen now, since, the hyperlink adopts the gray color of the "parent" tag.
---
Another way to generally deal with the whole question (to have hyperlinks inside such a "on-hover term description"), is not to use the CSS :after
-selector, and manually have an extra e.g. div
, right after the original HTML tag, to account for a "pop-up description", as such (formerly: JSFiddle 2):
HTML
<br>
<a id="term">HPV</a><div>Human papillomavirus. <a href="http://en.wikipedia.org/wiki/Human_papillomavirus">Wikipedia</a>.</div>
CSS
a#term:hover + div{display:inline} a#term + div{display:none; border:1px dotted #aaaaaa; padding:1px; top:-1em; position:relative}
Of course this is not ideal, because this is a trade-off with loss of ease on data-handling (especially when a term occurs multiple times in a document) and HTML readability.
Without the :after
-tag also, could be to have a more jQuery centered approach, and to have the full content (regular text and links) of the reference inside the jQuery. One could then use some additional CSS on the jQuery, I suppose?