Change FontAwesome icon with text on hover

跟風遠走 提交于 2019-12-04 14:01:14

Have to change your <li> structure a little bit, like this:

<li class="home">
    <a href="index.html"><i class="icon fa fa-home fa-2x"></i><b>Home</b></a>
</li>

Live demo: http://jsfiddle.net/evykes9q/9/

.nav ul {
    list-style: none;
    margin: 0;
    padding: 0;
    position: absolute;
    top: 0;
    right: 0;
}
.nav li {
    font-size:12pt;
    display:block;
    float: left;
    height:90px;
    width: 145px; /*new*/
    text-align: center; /*new*/
    transition: all 0.2s ease-in-out;
}
.nav .home {
    background: #a3d39c;
}
.nav .about {
    background: #7accc8;
}
.nav .projects {
    background: #4aaaa5;
}
.nav .contact {
    background: #35404f;
}
.nav li a {
    font-family: FontAwesome;
    color:#eee;
    font-size:22pt;
    text-decoration: none;
    display: block;
    padding:15px;
}
.nav li i {
    color:#fff;
    padding:0 10px;
}
.nav li b {
    padding: 15px 0;
    display: none;
}
.nav a:hover {
    color: #fff;
}
.nav a:hover i {
    display: none;
}
.nav a:hover b {
    display: block;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<div class="nav">
    <ul>
        <li class="home">
        	<a href="index.html"><i class="icon fa fa-home fa-2x"></i><b>Home</b></a>
        </li>
        <li class="about">
        	<a href="about"><i class="icon fa fa-coffee fa-2x"></i><b>About</b></a>
        </li>
        <li class="projects">
        	<a href="#projects"><i class="icon fa fa-code fa-2x"></i><b>Projects</b></a>
        </li>
        <li class="contact">
        	<a href="#contact"><i class="icon fa fa-comment fa-2x"></i><b>Contact</b></a>
        </li>
    </ul>
</div>

I think a have the answer for you. I changed your HTML around just a little bit nth-child wasn't appropriate for this http://jsfiddle.net/evykes9q/4/:

<div class="nav">
    <ul>
        <li class="home"><i class="icon fa fa-home fa-2x"></i><a href="index.html">Home</a>
        </li>
        <li class="about"><i class="icon fa fa-coffee fa-2x"></i><a href="about">About</a>
        </li>
        <li class="code"><i class="icon fa fa-code fa-2x"></i><a href="#projects">Projects</a>
        </li>
        <li class="contact"><i class="icon fa fa-comment fa-2x"></i><a href="#contact">Contact</a>
        </li>
    </ul>
</div>

CSS:

.nav ul .home {
    background: #a3d39c;
}
.nav ul .about {
    background: #7accc8;
}
.nav ul .code {
    background: #4aaaa5;
}
.nav ul .contact {
    background: #35404f;
}
.nav ul li:hover i:before {
    content:"";
}

Just add the following CSS and it will do the trick with no structure change:

.nav ul li i{
    display: block;
}
.nav ul li:hover i{
    display: none;
}
.nav ul li a{
    display: none;
}
.nav ul li:hover a{
    display: block;
}

Fiddle

I changed the dowomenfart fiddle and add some visible rules:

New fiddle

You just set the visibility to none to the text and show it on hover, and reverse for the icons:

.nav ul li i {
    display: block;
}
.nav ul li a {
    display: none;
}
.nav ul li:hover i {
    display: none;
}
.nav ul li:hover a {
    display: block;
}
Kiran Mistry

This is your solution

@import url("https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css");

.nav ul {
    list-style: none;
    margin: 0;
    padding: 0;
    position: absolute;
    top: 0;
    right: 0;
}
.nav li {
    font-size:12pt;
    display:block;
    float: left;
    height:90px;
    width: 145px; /*new*/
    text-align: center; /*new*/
    transition: all 0.2s ease-in-out;
}
.nav .home {
    background: #a3d39c;
}
.nav .about {
    background: #7accc8;
}
.nav .projects {
    background: #4aaaa5;
}
.nav .contact {
    background: #35404f;
}
.nav li a {
    font-family: FontAwesome;
    color:#eee;
    font-size:22pt;
    text-decoration: none;
    display: block;
    padding:15px;
}
.nav li i {
    color:#fff;
    padding:0 10px;
}
.nav li b {
    padding: 15px 0;
    display: none;
}
.nav a:hover {
    color: #fff;
}
.nav a:hover i {
    display: none;
}
.nav a:hover b {
    display: block;
}
<div class="nav">
    <ul>
        <li class="home">
        	<a href="index.html"><i class="icon fa fa-home fa-2x"></i><b>Home</b></a>
        </li>
        <li class="about">
        	<a href="about"><i class="icon fa fa-coffee fa-2x"></i><b>About</b></a>
        </li>
        <li class="projects">
        	<a href="#projects"><i class="icon fa fa-code fa-2x"></i><b>Projects</b></a>
        </li>
        <li class="contact">
        	<a href="#contact"><i class="icon fa fa-comment fa-2x"></i><b>Contact</b></a>
        </li>
    </ul>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!