starbucks
starbucks

Reputation: 3016

How to adjust width of input box when using :focus?

I have the following code:

#inputfield
{
width: 300px;
height: 28px;
font-style: italic;
outline: medium none;
padding: 3px 0px 3px 5px;
margin: -2px 1px 3px 0px;
box-shadow:inset 0 0 4px 0 #dfdfdf;
-moz-box-shadow:inset 0 0 4px 0 #dfdfdf;
-wevkit-box-shadow:inset 0 0 4px 0 #dfdfdf;
text-align: center;
font-size: 16px;
color: #9A9A9A;
font-family: Georgia, Arial, sans-serif;
}
#inputfield:focus {
color: #797f86;
text-align: left;
padding-left: 10px;

}

When the inputfield is clicked into for typing the width of the input box increases. The reason for that is that I have padding-left: 10px in the :focus. I require the padding-left so not sure how to still use this solution and not have the width issue?

I tried putting a width in the :focus pseudo-selector as well but didn't work. I tried using this:

    -moz-box-sizing:border-box;
    -webkit-box-sizing:border-box;
    box-sizing:border-box;

but it didn't make a difference.

Upvotes: 1

Views: 12408

Answers (1)

Captain Kirk
Captain Kirk

Reputation: 1196

Simple: Remove the padding from #inputfield and increase the height to 31px instead of 28px and in #inputfield:focus decrease the width to 290px.

HTML:

 <input id="inputfield" placeholder="Enter Text">

CSS:

 #inputfield {
 width: 300px;
 height: 31px;
 font-style: italic;
 outline: medium none;
 margin: -2px 1px 3px 0px;
 box-shadow:inset 0 0 4px 0 #dfdfdf;
 -moz-box-shadow:inset 0 0 4px 0 #dfdfdf;
 -wevkit-box-shadow:inset 0 0 4px 0 #dfdfdf;
 text-align: center;
 font-size: 16px;
 color: #9A9A9A;
 font-family: Georgia, Arial, sans-serif;
 border: 1px solid #206CAF;
 }
 #inputfield:focus {
 color: #797f86;
 text-align: left;
 padding-left: 10px;
 border: 1px solid #9A9A9A;
 width: 290px;
 }

See fiddle: http://jsfiddle.net/HjaTZ/

Upvotes: 2

Related Questions