Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS outline is different for input and input:focus

Tags:

html

css

I'm having a problem with an box and its associated css outline style. When the box is focused, it should have a blue outline (working). On form validation, if there is a problem, the .error class is added changing the outline and background color red (not working)

On focus I have a style:

input, select {
    font-size: 10pt;
    border: solid 1px #9598a0;
    padding: 2px;
}

input:focus{
    background: #EFF5FF;
    color: black;
    outline: solid 2px #73A6FF;
}

For the error:

input.error:focus, .error {
    outline: 2px solid red;
    background: rgb(255,240,240);
}

The problem is that the outline without focus is on the outside of the input box while the outline on focus is on the inside of the box so the element jumps as you click on it (CHROME).

Please see this image:

enter image description here

First is on focus, second is no focus with error, third is error with focus. Notice how the no focus causes the border to expand outside the object.

Is there a good way to fix this?

like image 286
Aaron Avatar asked Feb 12 '13 00:02

Aaron


People also ask

What is input focus in CSS?

The :focus CSS pseudo-class represents an element (such as a form input) that has received focus. It is generally triggered when the user clicks or taps on an element or selects it with the keyboard's Tab key.

What is difference between outline and border?

Note: Outline differs from borders! Unlike border, the outline is drawn outside the element's border, and may overlap other content. Also, the outline is NOT a part of the element's dimensions; the element's total width and height is not affected by the width of the outline.

What is outline used for in CSS?

The outline-style CSS property sets the style of an element's outline. An outline is a line that is drawn around an element, outside the border .


1 Answers

Try setting outline-offset explicitly. Any valid (see Syntax section) value should do, but for moving outline inside the element a negative one can be applied, for example:

JSFiddle

input {
    background: #EFF5FF;
    outline: solid 2px #73A6FF;
    outline-offset: -2px;
}

input.error {
    outline: 2px solid red;
    background: rgb(255,240,240);
}

Although you are asking about Chrome, be aware that outline-offset property is not supported in IE.

like image 117
ko la Avatar answered Oct 04 '22 21:10

ko la