{"id":246,"date":"2008-02-28T01:22:23","date_gmt":"2008-02-27T23:22:23","guid":{"rendered":"http:\/\/www.css3.info\/tooltips-with-css3\/"},"modified":"2008-02-28T18:55:27","modified_gmt":"2008-02-28T16:55:27","slug":"tooltips-with-css3","status":"publish","type":"post","link":"https:\/\/www.css3.info\/tooltips-with-css3\/","title":{"rendered":"Tooltips with CSS3"},"content":{"rendered":"<div style=\"float: right; margin-left: 32px; margin-top: 14px; margin-bottom: 10px;\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-via=\"css3\" data-count=\"vertical\" data-url=\"https:\/\/www.css3.info\/tooltips-with-css3\/\">Tweet<\/a><\/div>\n<p>As this is my first post, I&#8217;d like to introduce myself. My names James and I&#8217;m currently working as a senior User Interface Developer for Teachers TV. Like many of you guys, I&#8217;m fanatical about web standards and about creating the leanest, most semantic markup possible. I too am excited about the new possibilities with the arrival of CSS3, and am keen to contribute as much as I can to this great site. My first post is regarding a new method of creating Tooltips that I devised while coming up with a solution for a recent project.<\/p>\n<p>Up until now, there were a few options in existence; ugly Javascript-based methods, or solutions that use blank anchor tags (for IE6 compatibility). Another possibility is to ignore IE6 altogether and make use of the hover pseudo class in conjunction with the parent element that contains the tooltip text. I had to think out the box a bit on this one, but have come up with a completely new CSS3-based solution.<\/p>\n<p>My solution degrades gracefully in browsers that don&#8217;t support the CSS3 elements I&#8217;ve used and is a lot more elegant than former methods since it utilises an element&#8217;s title attribute, rather than creating additional markup within a parent element.<\/p>\n<p>Let&#8217;s start with the HTML (Note: the following example is based on use of icon background images for the divs, hence the fixed dimensions):-<\/p>\n<pre>&lt;div title=\"Tooltip text for first div\"&gt;&lt;\/div&gt;\r\n\r\n&lt;div title=\"Tooltip text for second div\"&gt;&lt;\/div&gt;<\/pre>\n<p>In short, my method is achieved by making use of the <a href=\"http:\/\/www.w3.org\/TR\/REC-CSS2\/generate.html\">:before (or :after) pseudo element<\/a> and content property, combined with the <a href=\"http:\/\/www.w3.org\/TR\/REC-CSS2\/selector.html#dynamic-pseudo-classes\">:hover pseudo class<\/a>.<\/p>\n<p>First off I grab the value of the divs title attribute by using the content property&#8217;s attribute function. Notice how I set the generated content to display:none -I&#8217;ll make it visible on :hover.<\/p>\n<pre>div:before{\r\n\tcontent:attr(title);\r\n\tdisplay:none;\r\n}<\/pre>\n<p>I now combine both the :hover class and :before pseudo element together, to specify values for the generated content when the div is hovered over.<\/p>\n<pre>div:hover::before{\r\n\twidth:200px;\r\n\tdisplay:block;\r\n\tbackground:yellow;\r\n\tborder:1px solid black;\r\n\tpadding:8px;\r\n\tmargin:25px 0 0 10px;\r\n}<\/pre>\n<p>One of the problems I came across at this stage, was that when having two elements floated left to each other and you hover over one of the elements, although the tooltip displays, it renders underneath the adjacent element. It is a simple case of layering the generated content over the div; my immediate thought was to specify a layering value in the above statement. However, after referring to the relevant W3C spec, it was apparent that in line with their recommendations, user agents ignore the position property in an instance such as the above statement, therefore rendering a z-index value useless.<\/p>\n<p>The solution was to specify the z-index and position properties in a separate statement dealing exclusively with the :hover pseudo class.<\/p>\n<pre>div:hover{\r\n\tz-index:10;\r\n\tposition:relative;\r\n}<\/pre>\n<p>This meant that it now works! It of course works in every browser above IE7 including Safari 3, Firefox 2.0.0.12 &amp; Opera 9.5b<\/p>\n<p>Obviously there&#8217;s nothing you can do to stop the default behaviour of standards compliant browsers regarding rendering of title attributes, so when leaving your cursor on my method for too long, the default browser behaviour will overlay the generated content CSS effect<\/p>\n<p><a href=\"https:\/\/www.css3.info\/wp-content\/uploads\/2008\/02\/tooltip.html\">Live example of the final product<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tweet As this is my first post, I&#8217;d like to introduce myself. My names James and I&#8217;m currently working as a senior User Interface Developer for Teachers TV. Like many of you guys, I&#8217;m fanatical about web standards and about creating the leanest, most semantic markup possible. I too am excited about the new possibilities [&hellip;]<\/p>\n","protected":false},"author":64,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,20,4],"tags":[],"class_list":["post-246","post","type-post","status-publish","format-standard","hentry","category-css3","category-selectors","category-tutorials"],"_links":{"self":[{"href":"https:\/\/www.css3.info\/wp-json\/wp\/v2\/posts\/246","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.css3.info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.css3.info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.css3.info\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.css3.info\/wp-json\/wp\/v2\/comments?post=246"}],"version-history":[{"count":0,"href":"https:\/\/www.css3.info\/wp-json\/wp\/v2\/posts\/246\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.css3.info\/wp-json\/wp\/v2\/media?parent=246"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.css3.info\/wp-json\/wp\/v2\/categories?post=246"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.css3.info\/wp-json\/wp\/v2\/tags?post=246"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}