ToolTips are the little yellow text boxes that pop up in some browsers when you hover over elements with title tags. several developers have created their own custom, stylized tooltipsusing a combination of JavaScript and CSS. however, it is possible to create pure CSS tooltips by using CSS positioning techniques. this technique requires a modern, standardscomplian browser like Firefox to work properly. as such, it is not a technique you wocould add to your day-to-day Arsenal. however, it does demonstrate the power of advanced CSS and gives you a hint of what will be possible when CSS is better supported. as with all of the examples in this book, you need to start with well-structured ured and meaningful (x) HTML: 1 <p>
2 <a href = "http://www.andybudd.com/" class = "tooltip">
3 Andy Budd <span> (this website rocks) </span> </a>
4 is a web developer based in Brighton England.
5 </P> I have given the link a class of tooltip to differentiate it from other links. inside the link I have added the text I wish to display as the link text, followed by the tooltip text enclosed in a span. I have wrapped my tooltip text in brackets so that the sentence still makes sense with styles turned off. the first thing you need to do is set the position property of the anchor to relative. this allows you to position the contents of the span absolutely, relative to the position of its parent anchor. you do not want the tooltip text to display initially, So you shoshould set its display property to none: 1 A. tooltip {
2 position: relative;
3}
4 A. tooltip span {
5 display: none;
6} when the anchor is hovered over, you want the contents of the span to appear. this is done by setting the display property of the span to block, but only when the link is hovered over. if you were to test the code now, hovering over the link wocould simply make the span text appear next to the link. to position the contents of the span below and to the right of the anchor, you need to set the position property of the span to absolute and position it 1em from the top of the anchor and 2ems from the left. 1 A. tooltip: hover span {
2 display: block;
3 position: absolute;
4 top: 1em;
5 left: 2em;
6} Remember, an absolutely positioned element is positioned in relation to its nearest positioned ancestor, or failing that, the root element. in this example, we have positioned the anchor, so the span is positioned in relation to that. and that's the bulk of the technique. all that is left is to add some styling to make the span look more like a tooltip. you can do this by giving the span some padding, a border, and a background color: 1 A. tooltip: hover span {
2 display: block;
3 position: absolute;
4 top: 1em;
5 left: 2em;
6 padding: 0.2em 0.6em;
7 Border: 1px solid #996633;
8 Background-color: # ffff66;
9 color: #000;
10} Unfortunately, this technique does not work properly in IE 5.x on Windows as it stands. it wowould seem that IE has problems styling elements inside anchor links using a dynamic pseudo-class. however, there is a fix: 1 A. tooltip: hover {
2 font-size: 100%;/* Fixes bug in ie5.x/win */
3} setting the font size as 100% on the hovered anchor somehow triggers Internet Explorer on windows into correctly styling the contained span. strange I know, but that's ie for you. sadly this technique breaks in Safari, and I have not managed to find a fix as I 've done for Internet Explorer on Windows.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service