{"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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">&lt;script type=&quot;text\/javascript&quot;&gt;<br\/>  var link = document.createElement(&quot;link&quot;);<br\/>  link.setAttribute(&quot;rel&quot;,&quot;stylesheet&quot;);<br\/>  link.setAttribute(&quot;href&quot;,&quot;http:\/\/wikitechy.com\/yourstylesheet.css&quot;);<br\/>  var head = document.getElementsByTagName(&quot;head&quot;)[0];<br\/>  head.appendChild(link);<br\/>&lt;\/script&gt;<\/code><\/pre> <\/div>\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&#8217;s extremely inefficient if we need to change more than one element:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Html Code<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">&lt;a<br\/>   href=&quot;abc.html&quot;<br\/>   onMouseOver=&quot;this.style.color=&#039;#0F0&#039;&quot;<br\/>   onMouseOut=&quot;this.style.color=&#039;#00F&#039;&quot;<br\/>&gt;Text&lt;\/a&gt;<\/code><\/pre> <\/div>\n<p>Also, we may have to switch it with document.getElementById(&#8216;idForLink&#8217;).<\/p>\n<p><label class=\"label label-info\">SOLUTION 3:<\/label><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Css Code<\/span> <\/div> <pre class=\"language-css code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-css code-embed-code\">.hover-item {<br\/>\tbackground-color: #FFF;<br\/>}<br\/><br\/>.hover-item:hover {<br\/>\tbackground-color: inherit;<br\/>}<br\/>&lt;a style=&quot;background-color: yellow&quot;&gt;<br\/>\t&lt;div class=&quot;hover-item&quot;&gt;<br\/>\t\tContent<br\/>\t&lt;\/div&gt;<br\/>&lt;\/a<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p>In this case, the inline code: &#8220;background-color: yellow;&#8221; 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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">document.head.insertAdjacentHTML(&#039;beforeend&#039;, &#039;&lt;style&gt;#mydiv:hover{color:red;}&lt;\/style&gt;&#039;);<\/code><\/pre> <\/div>\n<p>or a bit harder method:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">document.getElementById(&quot;mydiv&quot;).onmouseover= function(e){this.className += &#039; my-special-class&#039;; };<br\/>document.getElementById(&quot;mydiv&quot;).onmouseleave= function(e){this.className = this.className.replace(&#039;my-special-class&#039;,&#039;&#039;); };<\/code><\/pre> <\/div>\n<p>Multi-word styles in Javascript:<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">element.style.fontSize=&quot;12px&quot;<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Html Code<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">&lt;html&gt;<br\/>    &lt;style&gt;<br\/>    #uniqueid:hover {do:something;}<br\/>    &lt;\/style&gt;<br\/>    &lt;a id=&quot;uniqueid&quot;&gt;hello wikitechy&lt;\/a&gt;<br\/> &lt;\/html&gt;<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Html Code<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">&lt;style type=&quot;text\/css&quot;&gt;<br\/>    myLinkClass:hover {text-decoration:underline;}<br\/>&lt;\/style&gt;<br\/>&lt;a href=&quot;\/wiki&quot; class=&quot;myLinkClass&quot;&gt;wiki!&lt;\/a&gt;<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><label class=\"label label-info\">SOLUTION 7:<\/label><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Html Code<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">&lt;style&gt;a:hover { }&lt;\/style&gt;<br\/>&lt;a href=&quot;\/&quot;&gt;Go Home&lt;\/a&gt;<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">jQuery Code<\/span> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">style=&quot;cursor: pointer;&quot;<\/code><\/pre> <\/div>\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<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">Html Code<\/span> <\/div> <pre class=\"language-markup code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-markup code-embed-code\">&lt;style&gt;.hover_pointer{cursor:pointer;}&lt;\/style&gt;<br\/>&lt;div class=&quot;hover_pointer&quot; style=&quot;font:bold 12pt Calibri;&quot;&gt;Hello Wikitechy World&lt;\/div&gt;<\/code><\/pre> <\/div>\n<p>we can re-use the class everywhere.<\/p>\n[ad type=&#8221;banner&#8221;]\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 , 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 the onMouseOver and onMouseOut parameters, although it&#8217;s [&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}]}}