开发者

How do i change the color of a specific part of a link on hover?

开发者 https://www.devze.com 2023-04-10 14:31 出处:网络
Its hard to explain so i have an example I have a left part that should remain black and a right part that is green which changes to red on hover see http://jsfiddle.net/z9jSS/

Its hard to explain so i have an example

I have a left part that should remain black and a right part that is green which changes to red on hover see http://jsfiddle.net/z9jSS/

I want this to LOOK THE开发者_JS百科 SAME but i want make the left part a link too and have the right part change color when i hover over it like the first link. I know how to disable the underline but what i dont know how to do is not have the left part change red on hover while having the right part change colors http://jsfiddle.net/z9jSS/2/

Is there some trick i can do so a:hover will make a color change red but force the left part to stay black?


Simply override the styles for your spans:

.c a { text-decoration: none; color: inherit; }
.c a:hover .r { color:red; text-decoration: underline; }

Code: http://jsfiddle.net/z9jSS/21/


Seems like everything above was missing one thing or another. This includes everything you asked for, with part of the link being plain black text, and the rest being a green underlined link that turns red when hovering.

http://jsfiddle.net/z9jSS/30/

.c a          { color: black; text-decoration: none; }
.c a .r       { color: green; text-decoration: underline; }
.c a:hover .r { color: red;   text-decoration: underline; }

How do i change the color of a specific part of a link on hover?


wrap the part of anchor inside a span or an other tag

try this jsfiddle


Override the styles with a higher specificy style.

.c a:hover span.l { color: blue; }

Working Example.


Try this:

.c a {
    color: #000;
    text-decoration: none
}
.c a span:first-child + span {
    text-decoration: underline;
    color: green;
}
.c a:hover span:first-child + span {
    color: red
}

See: http://jsfiddle.net/thirtydot/z9jSS/31/

This has the benefit of not needing either of the .l or .r classes.

It will work in all browsers except IE6.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号