Design Textbox, Input elemt from Scratch

Guid to create an interactive and creative text feild design using CSS properties.

 Yogesh    08 Sep, 2020    1413

IMG: Design Textbox, Input elemt from Scratch

In this tutorial, we will learn how to create a text box design from scratch.

The basic syntax of the text box …

<input type="text" placeholder="Textbox" />

And the design will look/render different on different OS and Browsers.

In the example, we've displayed the default Text box in Chrome(left side) and Firefox(right side)

The design of the text box is not uniform. The rendering is dependent on the OS and Browser.

So we will design the text fields using the CSS step by step from scratch. 

The basic HTML structure of the input text field

 The basic HTML structure will include tag(input), attribute and it's value. 

Next, we will design the input elements step by step.

→ Basic CSS Class

.form-control {
  position: relative;
  display: inline-block;
  width: 100%;
  height: 30px;
  border: 1px solid #ccc;
  outline: none;
}

Output: 

Next, we will set the below CSS properties one by one

  • Sizes(width+height, padding)
  • Fonts
  • Background
  • Borders
  • Shadows
  • Pseudo-classes(:hover, :active, :focus)
  • Image/Icon inside the text field
  • Transition & Animations

→ Sizes (define the size of the text box)

Width is an essential property for text boxes. We can set height using height property or using padding.

width: 100%;
/* Or */
width: 50vw;
/* Or */
width: 400px;
/* Or */
width: 10rem;
  • width: 100% will fit 100% of your screen relative to the parent component.
  • width: 50vw fit the 50% of your view-port width.
  • width: 400px is fixed 400px width.
  • width: 10rem(or em) is working relative to the font size of the root(or parent) element.

Height:

height: 40px;
/* or */
height: 2rem;
/* or */
padding: 10px 0;
/* or */
padding: 1rem 0;

→ Fonts

The nice font will change the look and feel of the text box. 

Select font family

font-family: 'Open Sans', sans-seif; 
/* or select the theme font or system font */
font-family: var(--theme-font);
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;

Font size:

font-size: 1rem;
/* or */
font-size: 5vw;
/* or */
font-size: 15px;

→ Backgrounds

Set backgrounds

background: #ccc; 
/* or */
background: var(--bg);
background-color: #e6e6e6 (light gray bg)
background-color: #FDA1A1
background-color: linear-gradient(90deg, rgb(238, 174, 202) 0%, rgb(148, 187, 233) 100%)
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='4' height='4' viewBox='0 0 4 4'%3E%3Cpath fill='%236b588b' fill-opacity='0.4' d='M1 3h1v1H1V3zm2-2h1v1H3V1z'%3E%3C/path%3E%3C/svg%3E")

Background options:

  • plain background color background-color: red; (hex, rgb(a), hsl …)
  • background gradient background-color: linear-gradient(color1, color2) …
    - Gradient options: linear-gradient, radial-gradient, elliptical gradients…
  • Background Image/pattern background-image: url(…)

→ Borders

The border is an important part of the input text boxes.

Border styles

- Simple Gray border

border: 1px solid #ccc

- Solid Blue border

border-style: solid; border-color: var(--color);

- Dashed border

border-style: dashed;

- Dotted border

border-style: dotted;

- Double border

border-style: double;

- Groove border

border-style: groove;

- Outset border

border-style: outset;

- Border Bottom

border: none; border-bottom: 2px solid #3973bd

- Border-left

border-left: 5px solid #1a6e97

- Border Image

Set border background image: Either using gradient CSS or background-image property.

border-width: 5px;
border-image: linear-gradient( 20deg ,#62c8b0, #3566c6) 5;
border-image: url(https://picsum.photos/640/360) 7;

Border Radius:

border-radius: 5px;
border-radius: 50px;
border-radius: 50% / 25%;
border-radius: 59% 41% 70% 30% / 37% 42% 58% 63%;

Reference: Fancy border raduis generator

→ Shadows

box-shadow: 0 0 0 3px #1575d966; border-color: #1575d9;

 

box-shadow: 5px 5px 0px 0px #666; border-color: #666;

 

box-shadow: 0 0 7px #1575d966; border-color: #1575d9;
box-shadow: 0 0 5px 0 rgba(0, 0, 0, .2) inset; border-color: #90c0f2;

Next, add the pseudo classes to add transition or animate the text box.

Pseudo Classes

You can combine pseudo-classes with elements/class.

Syntax: 

.form-input:hover {
--- css properties ---
}

:hover

The hover class is activated when you hover over the input element. 

You can combine any of the effects, for example, give a shadow, opacity, change the border, animate the borders, etc…

.form-input:hover {
border-color: #1575d9;
box-shadow: 0 0 8px #ccc inset; }

:focus

The focus class is activated when you focus/click  the input element. 

.form-input:focus {
border-color:  #1575d9;
box-shadow: 0 0 0 3px #1575d966; }

→ Set the Image/Icon inside the Text Field

Using the background-image property we can set the Icon/Image inside the text fields. 

bagkroudn-image: url('/path-to-you-ico');
padding-left: 40px;

→ Transition and Animations: 

The transition/animation can be done either using CSS(Pseudo class) or using the Javascript. 

In the below example, we will increase the width of the input field on focus.

.form-input {
width: 150px;
transition: all .5s linear; }
.form-input:focus {
width: 100%; }

In the above example, the default width of the text box is 150px. Then we apply the transition/animation on focus. 
To make smooth animation between the state we've added a .5s transition time.

You can combine any of the above properties or custom classes to animate the text field. 
Thanks!

Also, checkout the snippet: Input Text Box Effects


Credits / Resources

  • Pexels free stock photos: https://pexels.com/
  • Picsum photos: https://picsum.photos/
  • Tabler Icons: https://tablericons.com/