Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Input [type=''text''] Busted

DZone's Guide to

Input [type=''text''] Busted

Inputs are extremely important in user experience design and it's crucial that their style is perfectly transferred from the design to the page.

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

Inputs are sooo important in the user experience design and it's crucial that their style is perfectly transferred from the design to the page. It's no secret that for a presentation on a web page, you need CSS, but the tricky part is how to make it as good as possible.

Inputs have more variations, states, and other issues than any other element in HTML, so let's cover everything one should know about the post input type text. Let's bust the input!

Input [type="text"]

Input text presents an empty square where the text is expected. It is a pretty common element and, in most cases, presents a white element surrounded by a color border. It is a good practice to add a placeholder text inside it and have a label outside it.

Default input text.

Default input text in focus state.

Default placeholder text.

Default autofill state.

To add the desired style to the input, there are a few standard options and a few special ones. If there is a label for the input, it is added in the label element next to the input, and its for attribute has the same value as the id attribute of the linked input element. The label element can be styled like the standard HTML text element.

For input, it is standard to change dimensions, border, background-color, border-radius and font options. It is not standard to style a placeholder text, a focus, an autofill and a validation state. These non-standard options will be explained in the next article and after that, some examples of different options used will be presented.

A Placeholder Text

To style a placeholder text set with a placeholder attribute, the ::placeholder pseudo-element is used. All text styling is supported (font, color, background, opacity, word-spacing, text-transform...)

 ::-webkit-input-placeholder { /* Chrome/Opera/Safari */
     ...
 }
 ::-moz-placeholder { /* Firefox 19+ */
     opacity:1;
     ...
 }
 :-ms-input-placeholder { /* IE 10+ */
     ...
 }
 :-moz-placeholder { /* Firefox 18- */
     ...
 }

Example:

 ::placeholder{
    color: coral;
    font-size: 14px;
    font-family: sans-serif;
    letter-spacing: .2px;
    padding-left: 5px;
 }

The Result:

Focus State

The focus state is important for user experience and it exists on every input element. It is used for indication of the currently targeted element. This state is presented with a blue border around an element and in CSS it is selected with the pseudo-class :focus.

The blue border around the focused element is added with the default line outline: auto 5px -webkit-focus-ring-color and that default color value is defined in a browser theme. In some cases, this presentation annoy designers of the element and some people choose to disable it by next code:

 :focus{
     outline:none;
 }
This is a very bad practice and it should be avoided. Instead of neutralizing style for the focus state, you can style it in your own way and it doesn't need to include an outline.

Example:

 :focus{
     outline:none;
     border:1px solid coral;
     box-shadow:0 0 5px coral;
 }

The Result:

It is a nice touch to add a transition in this case (and any other!).

Autocomplete State

Webkit browsers provide a setting that allows users to automatically fill in details for common form fields. You have seen this when completing a form that asks for things like name, address, and email. Autocomplete fields have default yellow background and that is something that can interfere with design, and also that is something that can easily be changed (in this mode you can also change text color).

 :-webkit-autofill {
    -webkit-box-shadow: 0 0 0 1000px mintcream inset !important;
 }

 :-webkit-autofill {
    -webkit-text-fill-color: coral !important;
 }

The Result:

Validation States

There are two relatively new pseudo-classes :valid and :invalid. These pseudo-classes are useful for highlighting success and fail for the user. It is common to see green when content is valid and red if it's not, so now you can easily define that with CSS. Old versions of browsers do not support it, keep that in mind.

 :valid {
    box-shadow: -5px 0 lime;
    border-left: none;     
 }

 :invalid {
    box-shadow: -5px 0 orangered;
    border-left: none;     
 }
box-shadow option is used instead of the border-left because there is a visual problem in the case when borders of one element have different style or color

The Result:

Extra Tip

One more problem with input fields is one on the iOS which appears because default styling is added. There are some extra rounded corners and dropshadows. The best way I found to fix this is one from daretothink blog:

-webkit-appearance: none;
border-radius: 0;

Good Examples







Closing Word

Work on input field design is something that you can always do and something that your user will surely appreciate. Once again, I urge you to use transitions in styling.

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:
forms ,ux ,web app development ,web dev ,tutorial ,inputs

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}