{"id":1803,"date":"2017-03-23T11:39:32","date_gmt":"2017-03-23T06:09:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1803"},"modified":"2017-03-28T18:26:44","modified_gmt":"2017-03-28T12:56:44","slug":"write-ahover-inline-css","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/write-ahover-inline-css\/","title":{"rendered":"[ Solved -9 Answers ] How to write a:hover in inline CSS"},"content":{"rendered":"<p><label class=\"label label-Warning\">PROBLEM :<\/label><\/p>\n<p>How to use a:hover in inline CSS inside the HTML style attribute?<\/p>\n<p><label class=\"label label-info\">SOLUTION 1:<\/label><\/p>\n<p>By adding links to external stylesheets ,<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201d%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%20%20var%20link%20%3D%20document.createElement(%22link%22)%3B%0A%20%20link.setAttribute(%22rel%22%2C%22stylesheet%22)%3B%0A%20%20link.setAttribute(%22href%22%2C%22http%3A%2F%2Fwikitechy.com%2Fyourstylesheet.css%22)%3B%0A%20%20var%20head%20%3D%20document.getElementsByTagName(%22head%22)%5B0%5D%3B%0A%20%20head.appendChild(link)%3B%0A%3C%2Fscript%3E%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Caution: the above code assumes there is a head section.<\/p>\n<p><label class=\"label label-info\">SOLUTION 2:<\/label><\/p>\n<p>We can get the same effect by changing your styles with JavaScript in the onMouseOver and onMouseOut parameters, although it\u2019s extremely inefficient if we need to change more than one element:<\/p>\n[pastacode lang=\u201dmarkup\u201d manual=\u201d%3Ca%0A%20%20%20href%3D%22abc.html%22%0A%20%20%20onMouseOver%3D%22this.style.color%3D\u2019%230F0\u2019%22%0A%20%20%20onMouseOut%3D%22this.style.color%3D\u2019%2300F\u2019%22%0A%3EText%3C%2Fa%3E%0A%0A\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Also, we may have to switch it with document.getElementById(\u2018idForLink\u2019).<\/p>\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n[pastacode lang=\u201dcss\u201d manual=\u201d.hover-item%20%7B%0A%09background-color%3A%20%23FFF%3B%0A%7D%0A%0A.hover-item%3Ahover%20%7B%0A%09background-color%3A%20inherit%3B%0A%7D%0A%3Ca%20style%3D%22background-color%3A%20yellow%22%3E%0A%09%3Cdiv%20class%3D%22hover-item%22%3E%0A%09%09Content%0A%09%3C%2Fdiv%3E%0A%3C%2Fa%0A\u201d message=\u201dCss Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>In this case, the inline code: \u201cbackground-color: yellow;\u201d is the switch color on hover, so we got to put the color we need into there and thereby this solution works.<\/p>\n<p><label class=\"label label-info\">SOLUTION 4:<\/label><\/p>\n<p>Adding inline style using Javascript:<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201ddocument.head.insertAdjacentHTML(\u2018beforeend\u2019%2C%20\u2019%3Cstyle%3E%23mydiv%3Ahover%7Bcolor%3Ared%3B%7D%3C%2Fstyle%3E\u2019)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>or a bit harder method:<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201ddocument.getElementById(%22mydiv%22).onmouseover%3D%20function(e)%7Bthis.className%20%2B%3D%20\u2019%20my-special-class\u2019%3B%20%7D%3B%0Adocument.getElementById(%22mydiv%22).onmouseleave%3D%20function(e)%7Bthis.className%20%3D%20this.className.replace(\u2018my-special-class\u2019%2C\u201d)%3B%20%7D%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Multi-word styles in Javascript:<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201delement.style.fontSize%3D%2212px%22%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 5:<\/label><\/p>\n<p>a:hover is part of the selector, not the CSS rules. A stylesheet has two components:<\/p>\n<p>selector {rules}<br \/>\nInline styles only have rules; the selector is implicit to be the current element.<\/p>\n<p>The selector is an expressive language that describes a set of criteria to match elements in an XML-like document.<\/p>\n<p>However, a style set can go most anywhere in the code:<\/p>\n[pastacode lang=\u201dmarkup\u201d manual=\u201d%3Chtml%3E%0A%20%20%20%20%3Cstyle%3E%0A%20%20%20%20%23uniqueid%3Ahover%20%7Bdo%3Asomething%3B%7D%0A%20%20%20%20%3C%2Fstyle%3E%0A%20%20%20%20%3Ca%20id%3D%22uniqueid%22%3Ehello%20wikitechy%3C%2Fa%3E%0A%20%3C%2Fhtml%3E%0A\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 6:<\/label><\/p>\n<p>Just define a style block directly to the link you want to style:<\/p>\n[pastacode lang=\u201dmarkup\u201d manual=\u201d%3Cstyle%20type%3D%22text%2Fcss%22%3E%0A%20%20%20%20myLinkClass%3Ahover%20%7Btext-decoration%3Aunderline%3B%7D%0A%3C%2Fstyle%3E%0A%3Ca%20href%3D%22%2Fwiki%22%20class%3D%22myLinkClass%22%3Ewiki!%3C%2Fa%3E\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n[pastacode lang=\u201dmarkup\u201d manual=\u201d%3Cstyle%3Ea%3Ahover%20%7B%20%7D%3C%2Fstyle%3E%0A%3Ca%20href%3D%22%2F%22%3EGo%20Home%3C%2Fa%3E%0A\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>Hover is a pseudo class, and thus cannot be applied with a style attribute. It is part of the selector.<\/p>\n<p><label class=\"label label-info\">SOLUTION 8:<\/label><\/p>\n<p>we cannot set arbitrary inline styles for hover, but we can change the style of the hover cursor in CSS using the following in the appropriate tag:<\/p>\n[pastacode lang=\u201djavascript\u201d manual=\u201dstyle%3D%22cursor%3A%20pointer%3B%22%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION 9:<\/label><\/p>\n<p>we can do id by adding a class but never inline.<\/p>\n[pastacode lang=\u201dmarkup\u201d manual=\u201d%3Cstyle%3E.hover_pointer%7Bcursor%3Apointer%3B%7D%3C%2Fstyle%3E%0A%3Cdiv%20class%3D%22hover_pointer%22%20style%3D%22font%3Abold%2012pt%20Calibri%3B%22%3EHello%20Wikitechy%20World%3C%2Fdiv%3E%0A\u201d message=\u201dHtml Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p>we can re-use the class everywhere.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>PROBLEM : How to use a:hover in inline CSS inside the HTML style attribute? SOLUTION 1: By adding links to external stylesheets , [pastacode lang=\u201djavascript\u201d manual=\u201d%3Cscript%20type%3D%22text%2Fjavascript%22%3E%0A%20%20var%20link%20%3D%20document.createElement(%22link%22)%3B%0A%20%20link.setAttribute(%22rel%22%2C%22stylesheet%22)%3B%0A%20%20link.setAttribute(%22href%22%2C%22http%3A%2F%2Fwikitechy.com%2Fyourstylesheet.css%22)%3B%0A%20%20var%20head%20%3D%20document.getElementsByTagName(%22head%22)%5B0%5D%3B%0A%20%20head.appendChild(link)%3B%0A%3C%2Fscript%3E%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] Caution: the above code assumes there is a head section. SOLUTION 2: We can get the same effect by changing your styles with JavaScript in [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,24],"tags":[3838,3834,3829,3844,3836,3841,3831,3835,3830,3828,3842,3832,3839,3837,3840,3833,3843],"class_list":["post-1803","post","type-post","status-publish","format-standard","hentry","category-css","category-html","tag-a-href-style-hover","tag-ahover-etc-in-an-email-newsletter","tag-css-hover-selector","tag-css-hover-image","tag-css-inline-styles-hover","tag-hover-html-text","tag-how-can-change-color-in-hover-with-inline-css","tag-how-can-i-define-ahover-using-inline-css","tag-how-to-put-hover-inline","tag-html-how-to-write-hover-using-inline-style","tag-html-hover-image","tag-inline-ahover-tag","tag-inline-hover-style-email","tag-inline-hover-style-in-html","tag-inline-style-hover-react","tag-inline-styles-for-alink","tag-onmouseover-background-color"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1803","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1803"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1803\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}