bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/CSS/CSS Foundations
CSS•CSS Foundations

CSS Form Focus and Icons

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind CSS Form Focus and Icons?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___[type=text]:focus {
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Animated Search Input
Style Input with icon/image
Style Input with Focus

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:

Example

Formatted code
input[type=text]:focus {
  background-color: lightblue;
}

Live preview

Example

Formatted code
input[type=text]:focus {
  border: 3px solid #555;
}

Live preview

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:

Example

Formatted code
input[type=text] {
  background-color: white;
  background-image: url('searchicon.png');
  background-position: 10px 10px;
  background-repeat: no-repeat;
  padding-left: 40px;
}

Live preview

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.

Example

Formatted code
input[type=text] {
  transition: width 0.4s ease-in-out;
}
input[type=text]:focus {
  width: 100%;
}

Live preview

Previous

CSS Form Inputs

Next

CSS Form Elements