How to create a bottom-right fixed button in CSS with expanding hover animation<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to create a bottom-right fixed button in CSS with expanding hover animation

Posted on: August 18, 2015

If you need to have a button with minimal information and when the user hovers this one give more input before executing the action, here is something interesting. The code illustrates a use case that the button is fixed to the browser at the bottom-right of the screen but this kind of button can be used everywhere. The main purpose is to have a rich user experience with pro-active feedback in a minimal way. The initial state of the button is just a single icon.


Expected rendering

For that example, I haven't use a glyph icon, neither an image, just a single plus character. This could be replaced with what ever you want. The idea is that when the user hover the icon that we have a transition to a bigger button.

Here is the button's Html. The container is not required and is just there for the purpose of having this button sticky at the bottom-right of the window. The button element is the real button. This one will grows when hovering. It contains two texts: one when collapsed and one when extended.

1<div class="right-corder-container">
2 <button class="right-corder-container-button">
3 <span class="short-text">+</span>
4 <span class="long-text">Add Me</span>
5 </button>

The css does 90% of the job. The remaining 10% is that we need to have a hover JavaScript to add and remove a class to add the long text visibility. The first CSS style is about fixing the container at the bottom-right of the browser's window.

1.right-corder-container {
2 position:fixed;
3 right:20px;
4 bottom:20px;

The next CSS style is what round up the button into a perfect circle. This is also where we setup the transition effect timing.

1.right-corder-container .right-corder-container-button {
2 height: 62px;
3 width: 62px;
4 border:none;
5 background-color:#6FB583;
6 border-radius: 62px; /*Transform the square into rectangle, sync that value with the width/height*/
7 transition: all 300ms; /*Animation to close the button (circle)*/ box-shadow:2px 2px 5px rgb(25, 73, 15);
8 cursor:pointer;

The span specify the plus sign to be placed in the middle. We use absolute position to have fined precision about where to place the text. Also, this is easier to ensure that even during the effect that this one remains at the same place.

1.right-corder-container .right-corder-container-button span {
2 font-size: 72px;
3 color:white;
4 position: absolute;
5 left: 10px;
6 top: 16px;
7 line-height: 28px;

The hover is where we specify the size of the final stage of the button. We also change the right side to be less round. This way, the button appears to be less than an oval, more to a label.

1.right-corder-container .right-corder-container-button:hover {
2 transition: all 400ms cubic-bezier(.62,.1,.5,1);
3 width:200px;
4 border-top-right-radius: 5px;
5 border-bottom-right-radius: 5px;

The next style block is about the long text effect. We want this text to appear slowly. We need to tweak some properties to be sure that the vertical scroll-bar does not get affected.

2Long text appears slowly with an animation. That code prepare the animation by hidding the text.
3The use of display is not there because it does not work well with CSS3 animation.
5.right-corder-container .right-corder-container-button .long-text { transition: opacity 1000ms; /*Only the text fadein/fadeout is animated*/
6opacity:0; /*By default we do not display the text, we want the text to fade in*/
8white-space: nowrap;
9font-size: 0; /*Set to 0 to not have overflow on the right of the browser*/
10width: 0; /*Set to 0 to not have overflow on the right of the browser*/
11margin:0; /*Set to 0 to not have overflow on the right of the browser*/ }

This is how we show the long text. We need to add a delay in the transition opacity because we do not want to start showing the text too early. Mostly because the container size is animated and not big enough to receive the text yet.

2 Animation to have a text that appear progressively. We need to play with a delay
3 on the width and the font-size to not have the browser have the text appears on the right
4 side of the browser view port. This has the side-effect of having an horizontal bar.
5 */
6.right-corder-container .right-corder-container-button{
7 transition: opacity 700ms,
8 width 1ms linear 270ms, /*two thirds of the animation on the container width*/
9 font-size 1ms linear 270ms; /*two thirds of the animation on the container width*/
10 opacity:1;
11 margin-top: 2px; /*Center the position vertically*/
12 margin-left: 65px; /*Center between the + and the right end side*/
13 font-size: 20px; /*Text size, cannot be defined initially without moving the scrollbar*/
14 width: auto; /*Required to be set with a delay (see animation) to not have scrollbar. Delay is to wait container to size up*/

The JavaScript is minimal and you do not have to use JQuery for a simple task as this. However, I had this one already available to here is the code with JQuery.

2 function()
3 {
4 $(".long-text").addClass("show-long-text");
5 },
6 function () {
7 $(".long-text").removeClass("show-long-text");
8 }

This code is available in JSFiddle.