Open Playground
CSS TutorialLesson #101

Input Focus / Icons

By default, some browsers will add a blue outline around the input when it gets

Style Input with Focus

By default, some browsers will add a blue outline around the input when it gets focus (clicked on). You can remove this behavior by adding outline: none; to the input.

Use the :focus selector to do something with the input field when it gets focus:

<input class="focus1" type="text" name="firstname">

Example

Example Code

css
Try it Yourself
input[type=text]:focus
{

  background-color: lightblue;

}

<input class="focus2" type="text" name="firstname">

Example

Example Code

css
Try it Yourself
input[type=text]:focus
{

  border: 3px solid #555;

}

Style Input with icon/image

If you want an icon inside the input, use the background-image property and position it with the background-position property. Also notice that we add a large left padding to reserve the space of the icon:

<input type="text" name="search" placeholder="Search.." class="icon">

Example

Example Code

css
Try it Yourself
input[type=text]
{
  background-color: white;
  background-image: url('searchicon.png');

background-position: 10px 10px;
  background-repeat:
no-repeat;

padding-left: 40px;

}

Animated Search Input

In this example we use the CSS transition property to animate the width of the search input when it gets focus. You will learn more about the transition property later, in our CSS Transitions chapter.

<input id="myInp" type="text" name="search" placeholder="Search..">

Example

Example Code

css
Try it Yourself
input[type=text] {
  transition: width 0.4s ease-in-out;
}

input[type=text]:focus {

width: 100%;
}


Interactive Code Examples

CSS Example 1

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 2

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 3

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 4

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview: