Search code examples
htmlcssbutton

Dynamically change the size of the button based on the text?


ButtonI am new to HTML. I am using the button style is css as given below

    .button_style_enabled {
    background  : url("images/button_down.png") ;
    color       : #FFFFFF;
    height      : 21px;
    line-height : 21px;
    text-align  : center;
    width       : 112px;
    border      : 0px;

}

How do I adjust my button size based on the text?

Thanks.


Solution

  • Use padding instead of width to set your input styles. This way you can still set the "thickness" of the control, without constraining it to a fixed width.

    Regarding a background-image being cut off short, you can add a min-width to fix this:

    input {
       color       : #FFFFFF;
       height      : 21px;
       line-height : 21px;
       text-align  : center;
       width       : auto;
       border      : 0px;
       padding-left:10px;
       padding-right:10px;
       min-width:100px;
    }
    

    http://jsfiddle.net/kBxz2/1/