简体   繁体   中英

How to apply css class on hover for just one html element

i have a span like below :

<span  class="fa fa-fw fa-close">

and i want to have the fa-close in red color on hover, when I create the css class :

.fa-close:hover{color :red;}

it is applied to all spans with fa-close but i want it to be applied for just one span. How can i do it please?

Add extra class to specific span where you want to apply red colour

For example

HTML

<span  class="fa fa-fw fa-close extra">

and CSS like

.fa-close.extra {color :red;}

So now, wherever you want to apply red colour you can add this class.

Hope this helps :)

Update as per PO requirement

If you want red colour only on hover then your code be like below

HTML

<span  class="fa fa-fw fa-close extra">

and CSS like

.fa-close.extra:hover {color :red;}

仅将内部CSS应用于该范围: <span class="fa fa-fw fa-close" style=color:red>

If you want to apply css to only one element then you should use id instead of class.

#fa-close{color :red;}

Add new class in span and then write css by using that class

HTML

<span  class="fa fa-fw fa-close apply_only_one">

CSS

.apply_only_one.fa-close{color :red;}

This is the best solution of your Problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM