Animate a Search Form Using HTML, CSS And JavaScript

Animated Search Form Using HTML, CSS & JavaScript for Beginners -Creating an engaging and user-friendly website often involves incorporating interactive elements that enhance the user experience. One such element is an animated search form.

In this article, we will delve into how to create an animated search form using HTML, CSS, and JavaScript, tailored specifically for beginners.

By mastering the implementation of an animated search form, you can significantly improve the functionality and visual appeal of your web pages. The process will involve step-by-step instructions and coding examples, ensuring that even those new to web development can follow along and create their own animated search form effortlessly.

Step-by-Step Guide

1. Setting Up the HTML

First, we’ll create a simple HTML structure for our search form.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Animated Search Form</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="search-container">
        <input type="text" class="search-input" placeholder="Search...">
        <button class="search-button">
            <i class="fa fa-search"></i>
        </button>
    </div>
    <script src="script.js"></script>
</body>
</html>

2. Styling with CSS

Next, we will style the search form and add animations using CSS.

/* styles.css */
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f0f0f0;
    margin: 0;
    font-family: Arial, sans-serif;
}

.search-container {
    position: relative;
    width: 250px;
    height: 50px;
}

.search-input {
    width: 100%;
    height: 100%;
    padding: 10px 50px 10px 10px;
    border: 2px solid #333;
    border-radius: 25px;
    outline: none;
    transition: width 0.4s ease;
}

.search-button {
    position: absolute;
    top: 0;
    right: 0;
    width: 50px;
    height: 50px;
    border: none;
    background: none;
    cursor: pointer;
}

.search-button i {
    font-size: 20px;
    color: #333;
    transition: color 0.4s ease;
}

.search-input:focus {
    width: 300px;
}

.search-input:focus + .search-button i {
    color: #007bff;
}

3. Adding JavaScript for Enhanced Interaction

To add further interactivity, we can use JavaScript. This script will allow the search input to focus automatically when the search button is clicked.

// script.js
document.querySelector('.search-button').addEventListener('click', function() {
    document.querySelector('.search-input').focus();
});

Full HTML, CSS, and JavaScript Code

Here is the complete code combining all the parts above:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Animated Search Form</title>
    <link rel="stylesheet" href="styles.css">
    <!-- Font Awesome for search icon -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css">
</head>
<body>
    <div class="search-container">
        <input type="text" class="search-input" placeholder="Search...">
        <button class="search-button">
            <i class="fa fa-search"></i>
        </button>
    </div>
    <script src="script.js"></script>
</body>
</html>

Output:

To make our animated search form even more engaging and feature-rich, we can add more animations and features such as:

  1. Expanding and Collapsing Search Input: Add animations to smoothly expand and collapse the search input field.
  2. Placeholder Text Animation: Animate the placeholder text to provide better user guidance.
  3. Clear Button: Add a clear button to allow users to quickly clear their search input.
  4. Shadow and Color Transitions: Add subtle shadow and color transitions to enhance the visual appeal.

CSS

/* styles.css */
body {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f0f0f0;
    margin: 0;
    font-family: Arial, sans-serif;
}

.search-container {
    position: relative;
    width: 250px;
    height: 50px;
}

.search-input {
    width: 100%;
    height: 100%;
    padding: 10px 50px 10px 10px;
    border: 2px solid #333;
    border-radius: 25px;
    outline: none;
    transition: width 0.4s ease, box-shadow 0.4s ease, border-color 0.4s ease;
}

.search-button,
.clear-button {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 40px;
    height: 40px;
    border: none;
    background: none;
    cursor: pointer;
    outline: none;
    transition: color 0.4s ease;
}

.search-button {
    right: 10px;
}

.clear-button {
    right: 50px;
}

.search-button i,
.clear-button i {
    font-size: 20px;
    color: #333;
}

.search-input:focus {
    width: 350px;
    box-shadow: 0 0 10px rgba(0, 123, 255, 0.5);
    border-color: #007bff;
}

.search-input:focus + .search-button i {
    color: #007bff;
}

.search-input:not(:placeholder-shown) ~ .clear-button {
    display: block;
}

.search-input::placeholder {
    transition: opacity 0.4s ease;
}

.search-input:focus::placeholder {
    opacity: 0;
}

Javascript

// script.js
document.addEventListener('DOMContentLoaded', function() {
    const searchInput = document.querySelector('.search-input');
    const searchButton = document.querySelector('.search-button');
    const clearButton = document.querySelector('.clear-button');

    searchButton.addEventListener('click', function() {
        searchInput.focus();
    });

    clearButton.addEventListener('click', function() {
        searchInput.value = '';
        clearButton.style.display = 'none';
        searchInput.focus();
    });

    searchInput.addEventListener('input', function() {
        if (searchInput.value.length > 0) {
            clearButton.style.display = 'block';
        } else {
            clearButton.style.display = 'none';
        }
    });
});

Output:

we have enhanced the animated search form by adding more features and animations using HTML, CSS, and JavaScript. We started with a basic structure and progressively added styles and interactivity to make the form more appealing and user-friendly.

By following this guide, you can create a modern, animated search form for your website, enhancing user experience with interactive elements. Feel free to customize the styles and functionality to match your site’s design language and specific needs.

Conclusion

In this article, we demonstrated how to create and animate a search form using HTML, CSS, and JavaScript. We started with the HTML structure, applied CSS for styling and animation, and then used JavaScript for enhanced interactivity. The resulting search form not only looks appealing but also provides a smooth and engaging user experience.

Animations in UI components like search forms can significantly improve user engagement and satisfaction. By following the steps outlined in this guide, you can create a beautiful and functional animated search form for your own projects. Feel free to customize the styles and animations to fit the design language of your website.

By adding subtle animations, you can create a more interactive and modern user experience, making your website stand out and delighting your users with a responsive and visually appealing search form.

Author

Sona Avatar

Written by

Leave a Reply

Trending

CodeMagnet

Your Magnetic Resource, For Coding Brilliance

Programming Languages

Web Development

Data Science and Visualization

Career Section

<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-4205364944170772"
     crossorigin="anonymous"></script>