Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inline style to act as :hover in CSS

Tags:

html

css

I know that embedding CSS styles directly into the HTML tags they affect defeats much of the purpose of CSS, but sometimes it's useful for debugging purposes, as in:

<p style="font-size: 24px">asdf</p> 

What's the syntax for embedding a rule like:

a:hover {text-decoration: underline;} 

into the style attribute of an A tag? It's obviously not this...

<a href="foo" style="text-decoration: underline">bar</a> 

...since that would apply all the time, as opposed to just during hover.

like image 452
raldi Avatar asked Sep 25 '08 05:09

raldi


People also ask

Can we use hover in inline CSS?

Short answer: you can't. Long answer: you shouldn't. Give it a class name or an id and use stylesheets to apply the style. :hover is a pseudo-selector and, for CSS, only has meaning within the style sheet.

How do you use hover inline style?

It is called pseudo-selector and used to select all the elements when the user move mouse over the elements. It can be used on all the element. A <!

How do you hover style in CSS?

The :hover selector is used to select elements when you mouse over them. Tip: The :hover selector can be used on all elements, not only on links. Tip: Use the :link selector to style links to unvisited pages, the :visited selector to style links to visited pages, and the :active selector to style the active link.

How add hover inline CSS react?

Set the onMouseEnter and onMouseLeave props on the element. When the user hovers over or out of the element, update a state variable. Conditionally set inline styles on the element.


2 Answers

I'm afraid it can't be done, the pseudo-class selectors can't be set in-line, you'll have to do it on the page or on a stylesheet.

I should mention that technically you should be able to do it according to the CSS spec, but most browsers don't support it

Edit: I just did a quick test with this:

<a href="test.html" style="{color: blue; background: white}              :visited {color: green}             :hover {background: yellow}             :visited:hover {color: purple}">Test</a> 

And it doesn't work in IE7, IE8 beta 2, Firefox or Chrome. Can anyone else test in any other browsers?

like image 159
Glenn Slaven Avatar answered Oct 13 '22 05:10

Glenn Slaven


If you are only debugging, you might use javascript to modify the css:

<a onmouseover="this.style.textDecoration='underline';"      onmouseout="this.style.textDecoration='none';">bar</a> 
like image 22
Aleksi Yrttiaho Avatar answered Oct 13 '22 07:10

Aleksi Yrttiaho