{"id":1818,"date":"2018-04-05T17:39:14","date_gmt":"2018-04-05T22:39:14","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1818"},"modified":"2018-04-05T17:39:14","modified_gmt":"2018-04-05T22:39:14","slug":"html-css-expand-text-hover","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/05\/html-css-expand-text-hover\/","title":{"rendered":"HTML &#038; CSS: How to expand text on hover"},"content":{"rendered":"<p>In this article, I will show how to display short text on a web page that can expand to longer text on hover. The trick is to use two spans inside of a div. The first span contains the shorter text and the second span contains the longer text. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"html\">&lt;div class=\"expanded-text\"&gt;\r\n  &lt;p class=\"text\"&gt;\r\n    &lt;span class=\"short-name\"&gt;On The Origin Of Species&lt;\/span&gt;\r\n    &lt;span class=\"longer-name\"&gt;On The Origin Of Species By Means Of \r\n      Natural Selection, Or The Preservation Of Favoured Races In \r\n      The Struggle For Life&lt;\/span&gt;\r\n  &lt;\/p&gt;\r\n&lt;\/div&gt;<\/pre>\n<p>Then in the CSS, the longer name starts out hidden with <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">display:none;<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">span.longer-name{\r\n display:none;\r\n}<\/pre>\n<p>The magic happens when hovering over the div. On hover, the short name is hidden and the longer name is displayed in the CSS. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">\/* On hover, hide the short name *\/\r\n.expanded-text:hover span.short-name{\r\n display:none;\r\n}\r\n\/* On hover, display the longer name.  *\/\r\n.expanded-text:hover span.longer-name{\r\n display:block;\r\n}<\/pre>\n<p>Here is the complete CSS:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"css\">body {\r\n    background-color: #555;\r\n}\r\n\/* Container for the text that expands on hover *\/\r\n.expanded-text {\r\n    width: 100%;\r\n}\r\n\/* Longer name hidden by default  *\/\r\nspan.longer-name{\r\n    display:none;\r\n}\r\n\/* On hover, hide the short name *\/\r\n.expanded-text:hover span.short-name{\r\n    display:none;\r\n}\r\n\/* On hover, display the longer name.  *\/\r\n.expanded-text:hover span.longer-name{\r\n    display:block;\r\n}\r\n\/* Decorate the text *\/\r\n.text {\r\n   color: white;\r\n   font-size: 24px;\r\n}\r\n<\/pre>\n<p>Here is a demo on Codepen:<\/p>\n<p class='codepen'  data-height='265' data-theme-id='0' data-slug-hash='qoKZWW' data-default-tab='css,result' data-animations='run' data-editable='' data-embed-version='2'>\nSee the Pen Expand text on hover by Chris Nielsen (@Chris_Nielsen) on CodePen.<\/p>\n\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I will show how to display short text on a web page that can expand to longer text on hover. The trick is to use two spans inside of a div. The first span contains the shorter text and the second span contains the longer text. For example: &lt;div class=&#8221;expanded-text&#8221;&gt; &lt;p class=&#8221;text&#8221;&gt; &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/04\/05\/html-css-expand-text-hover\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">HTML &#038; CSS: How to expand text on hover<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[10,105],"class_list":["post-1818","post","type-post","status-publish","format-standard","hentry","category-html-css","tag-css","tag-html"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1818","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1818"}],"version-history":[{"count":3,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1818\/revisions"}],"predecessor-version":[{"id":1823,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1818\/revisions\/1823"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1818"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1818"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1818"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}