Search code examples
htmlcsstransition

css animated button with an auto width


I'm working on an animated button with css transition and I have a problem. the button should resize itself so the whole text is always visible in the element. The problem is that my text is not always the same. I set the width and the padding right as auto but it doesn't work, I don't know how to solve that. Here is my code. can any one help me please. Thanks

.a-btn{
    color: #ffffc2;
  background-color: #ffffc2;
  border: 1px solid #dfdfdf;
    padding-left:20px;
    padding-right:20px;
    position:relative;
    float:left;
    overflow:hidden;
    -webkit-transition:all 0.3s linear;
    -moz-transition:all 0.3s linear;
    -o-transition:all 0.3s linear;
    transition:all 0.3s linear;
}
.a-btn-text{
    display:block;
    white-space:nowrap;
    color:#446388;
    -webkit-transition:all 0.2s linear;
    -moz-transition:all 0.2s linear;
    -o-transition:all 0.2s linear;
    transition:all 0.2s linear;
}
.a-btn-slide-text{
    position:absolute;
    height:100%;
    top:0px;
    right:20px;
    width:0px;
    background:#ffffc2;
    color:#ff0000;
    white-space:nowrap;
    text-align:left;
    text-indent:10px;
    overflow:hidden;
    line-height:28px;
    -webkit-transition:width 0.3s linear;
    -moz-transition:width 0.3s linear;
    -o-transition:width 0.3s linear;
    transition:width 0.3s linear;
}
.a-btn:hover{
    background-color: #fdfd85;
    padding-right:130px;
}
.a-btn:hover .a-btn-slide-text{
    background-color: #fdfd85;
    width:auto;
}
<html>
<head>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
</head>
  <body>

<a href="#" onclick="removeSelectedExercise();" class="btn a-btn btn-sm margin-right">
						<span class="a-btn-text"><i class="fa fa-trash red"></i></span> 
						<span class="a-btn-slide-text">Retirer articles s&eacute;lectionn&eacute;s</span>
					</a>

    </body>
  </html>
      


Solution

  • The solution is to change width to max-width

    .a-btn{
        color: #ffffc2;
      background-color: #ffffc2;
      border: 1px solid #dfdfdf;
        padding-left:15px;
        padding-right:20px;
        position:relative;
        float:left;
        overflow:hidden;
        max-width: 30px;
        -webkit-transition:max-width 0.3s ease-in-out;
        -moz-transition:max-width 0.3s ease-in-out;
        -o-transition:max-width 0.3s ease-in-out;
        transition:max-width 0.3s linear;
    }
    .a-btn-text{
        display:block;
        white-space:nowrap;
        color:#ff0000;
    }
    
    .a-btn:hover{
        background-color: #fdfd85;
        max-width: 300px;
    }
    <html>
    <head>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
    <link href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet"/>
    </head>
      <body>
    
    <a href="#" onclick="removeUnselectedExercise();" class="btn a-btn btn-sm ">
    						<span class="a-btn-text"><i class="fa fa-trash red " style="padding-right:20px;"></i> Remove Selected</span> 
    					</a>
    
    
        </body>
      </html>