Reputation: 59
This is probably super simple but I'm completely stuck and I can't find anything helpful. Sorry if this was asked already, if so please link me to it and sorry to have wasted your time.
So, I have this code:
#nav {
position: fixed;
top: 70px;
left: 70px;
}
.button {
position: relative;
background: white;
text-transform: uppercase;
font-size: 11px;
letter-spacing: 2px;
border: 1px solid #eee;
width: 20px;
height: 20px;
line-height: 20px;
}
.button:hover {
width:auto;
}
.text {
overflow: hidden;
}
.icon {
float: left;
margin-right: 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet"/>
<div id="nav">
<div class="button">
<div class="icon"><i class="fa fa-home" aria-hidden="true"></i></div>
<div class="text">home</div>
</div>
</div>
What I'd like to do is:
Sorry if this is too much to ask. I'm really stuck with this, as you can see I kind of got the basics working already, all I need is really these two points I listed above. Thank you so much for your help.
Upvotes: 2
Views: 6894
Reputation: 1943
Use transition and a set width. width: auto
doesn't work with transitions.
#nav {
position: fixed;
top: 70px;
left: 70px;
}
.button {
position: relative;
background: white;
text-transform: uppercase;
font-size: 11px;
letter-spacing: 2px;
border: 1px solid #eee;
width: 20px;
height: 20px;
line-height: 20px;
overflow: hidden;
transition: linear 0.3s;
}
.button:hover {
width: 100%;
}
.icon {
float: left;
margin-right: 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet"/>
<div id="nav">
<div class="button">
<div class="icon"><i class="fa fa-home" aria-hidden="true"></i></div>
<span class="text">home</span>
</div>
</div>
Upvotes: 1
Reputation: 547
As Paulie_D mentioned it is not possible to transition to width: auto
. You can instead add a max-width
of the intended width of the non-hover state and transition to a max-width
equal or greater as your intended hover state width. Example below:
#nav {
position: fixed;
top: 70px;
left: 70px;
}
.button {
position: relative;
background: white;
text-transform: uppercase;
font-size: 11px;
letter-spacing: 2px;
border: 1px solid #eee;
width: auto;
min-width: auto;
max-width: 15px;
height: 20px;
line-height: 20px;
padding-right: 5px;
transition: max-width 1s;
}
.button:hover {
max-width: 200px;
}
.text {
overflow: hidden;
}
.icon {
width: 20px;
float: left;
margin-right: 5px;
text-align: center;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet"/>
<div id="nav">
<div class="button">
<div class="icon"><i class="fa fa-home" aria-hidden="true"></i></div>
<div class="text">home</div>
</div>
</div>
Upvotes: 1
Reputation: 108
What you want is a CSS Transition. Note the transition properties added to the initial css block for the button.
But in order to implement this properly, you'll also want your :hover selector width to be more precise than a variable 'auto' value - try something like '100%'. Take a look at this:
#nav {
position: fixed;
top: 70px;
left: 70px;
}
.button {
position: relative;
background: white;
text-transform: uppercase;
font-size: 11px;
letter-spacing: 2px;
border: 1px solid #eee;
width: 12px;
height: 20px;
line-height: 20px;
-webkit-transition: width 1s;
transition: width 1s;
}
.button:hover {
width: 100%;
}
.text {
overflow: hidden;
}
.icon {
float: left;
margin-right: 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet"/>
<div id="nav">
<div class="button">
<div class="icon"><i class="fa fa-home" aria-hidden="true"></i></div>
<div class="text">home</div>
</div>
</div>
Notice you'll define in your transition property what OTHER property is going to be changing - in this case, width - and how long that change should take place - such as 1 second. You can also use tenths of a second as well if 1s is too slow.
"transition: width .[x]s;"
Upvotes: 3
Reputation: 16936
You can center the icon with some left margin and use CSS transitions for the hover effect (see w3schools for some more examples):
#nav {
position: fixed;
top: 70px;
left: 70px;
}
.button {
position: relative;
background: white;
text-transform: uppercase;
font-size: 11px;
letter-spacing: 2px;
border: 1px solid #eee;
width: 20px;
height: 20px;
line-height: 20px;
-webkit-transition: width 1s;
transition: width 1s;
}
.button:hover {
width: 100%;
}
.text {
overflow: hidden;
}
.icon {
float: left;
margin: 0 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet" />
<div id="nav">
<div class="button">
<div class="icon"><i class="fa fa-home" aria-hidden="true"></i>
</div>
<div class="text">home</div>
</div>
</div>
Upvotes: 3
Reputation: 4259
Use transition: all 1s;
Reference: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions
#nav {
position: fixed;
top: 70px;
left: 70px;
}
.button {
position: relative;
background: white;
text-transform: uppercase;
font-size: 11px;
letter-spacing: 2px;
border: 1px solid #eee;
width: 18px; /* <-- match this width better */
height: 18px;
line-height: 20px;
transition: all 1s; /* <-- CSS3 transition */
}
.button:hover {
width:6em; /* <-- set a defined width */
}
.text {
overflow: hidden;
}
.icon {
float: left;
margin-right: 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.1/css/font-awesome.min.css" rel="stylesheet"/>
<div id="nav">
<div class="button">
<div class="icon"><i class="fa fa-home" aria-hidden="true"></i></div>
<div class="text">home</div>
</div>
</div>
Upvotes: 1