Open In App

Create an Image Background Remover App Using HTML CSS & JavaScript

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Building an Image Background Remover using HTML, CSS, and JavaScript demonstrates the power of combining these technologies to create interactive and user-friendly applications. The clean design and intuitive features make this tool not only functional but also enjoyable for users to interact with. The goal of our project is to provide users with a simple and intuitive interface for removing backgrounds from images.

bgRemover

Approach:

  • Start with a basic HTML structure that includes elements for image input, previews, and action buttons.
  • In the CSS file, we’ve defined styles for the container, input file, image previews, and action buttons. The design focuses on clean lines, easy-to-read fonts, and interactive buttons.
  • In the JavaScript file, we’ve implemented logic for handling image input, sending requests to the remove.bg API for background removal, and displaying the results. The user can also download the processed image with a single click.

Example: The below code example illustrates the creation of image background remover app using HTML, CSS, and Javascript.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
"width=device-width, initial-scale=1.0">
    <title>
        Image Background Remover
    </title>
    <link rel="stylesheet" href="style.css">
</head>
  
<body>
    <div class="container">
        <h1>Image Background Remover</h1>
        <div class="input-file">
            <label for="userImg" class="info_text">
                Select a File
            </label>
            <input type="file" id="userImg" 
                class="form-control-file" required>
        </div>
        <div class="d-flex mb-4">
            <div id="imagePreview" class="image-preview"></div>
            <div id="bgRemove" class="image-preview"></div>
        </div>
        <a id="downloadButton" class="btn btn-download" 
            style="display: none;">
            Download
        </a>
        <div>
            <button id="removeBackground" class="btn btn-primary">
                Remove Background
            </button>
        </div>
    </div>
    <script src="script.js"></script>
</body>
  
</html>


CSS




/* Center content vertically and horizontally */
.container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 80vh;
    padding: 20px;
    border: 1px solid #0e0c0c;
    border-radius: 10px;
    max-width: 600px;
    margin: 0 auto;
    background-color: #e3f7f7;
}
  
/* Title styles */
h1 {
    text-align: center;
    margin-bottom: 20px;
}
  
/* Style input file button */
.input-file {
    margin-bottom: 20px;
}
  
/* Style image previews */
.image-preview {
    border: 2px solid #ccc;
    border-radius: 5px;
    margin-right: 10px;
    overflow: hidden;
    width: 200px;
    height: 200px;
    display: inline-block;
}
  
.image-preview img {
    width: 100%;
    height: 100%;
}
  
/* Style remove background button */
.btn-remove-bg {
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 5px;
    padding: 10px 20px;
    font-size: 16px;
    cursor: pointer;
}
  
.btn-remove-bg:hover {
    background-color: #0056b3;
}


Javascript




document.addEventListener('DOMContentLoaded', function () {
    const inputField = 
        document.getElementById('userImg');
    const removeBackgroundButton = 
        document.getElementById('removeBackground');
    const imagePreview = 
        document.getElementById('imagePreview');
    const bgRemove = 
        document.getElementById('bgRemove');
    const downloadButton = 
        document.getElementById('downloadButton');
  
    inputField.addEventListener('change', function () {
        const file = this.files[0];
        const reader = new FileReader();
  
        reader.onload = function (e) {
            const img = document.createElement('img');
            img.src = e.target.result;
            imagePreview.innerHTML = '';
            imagePreview.appendChild(img);
        }
  
        reader.readAsDataURL(file);
    });
  
    removeBackgroundButton.addEventListener('click'
        async function () {
        const file = inputField.files[0];
        const formData = new FormData();
        formData.append('image_file', file);
  
        try {
            const response = 
            await fetch('https://api.remove.bg/v1.0/removebg', {
                method: 'POST',
                headers: {
                    'X-Api-Key': '39nH2XMTcoAxnogxmqVBuAXH',
                },
                body: formData,
            });
  
            const result = await response.blob();
            const imgUrl = URL.createObjectURL(result);
            bgRemove.innerHTML = 
                `<img src="${imgUrl}" alt="Removed Background">`;
            downloadButton.href = imgUrl;
            downloadButton.download = 
                'background_removed_image.png';
            downloadButton.style.display = 
                'inline-block';
        } catch (error) {
            console.error('Error removing background:', error);
        }
    });
});


Output:

bgRemoverGIF



Previous Article
Next Article

Similar Reads

Create an Image Background Remover App Using React
In this article, we will create an Image Background Remover App Using ReactJS. Image editing tools play a crucial role in various industries, from graphic design to e-commerce. One common task is removing backgrounds from images, a process that traditionally requires specialized software. we'll walk through the process of building an image backgrou
4 min read
How to Create a Wave Image for a Background using HTML and CSS ?
This type of background creates uniqueness on your webpage by avoiding regular rectangular sized background or header. The following header design will show your creativity. This design can be achieved in two ways: Using ::before and ::after selector on a div element in CSS.Using SVG in HTML. Example: This example uses ::before and ::after selector
3 min read
Create a CSS Fireflies background using HTML/CSS
In this article, we will see how to create the CSS Fireflies background using HTML &amp; CSS, along with understanding its implementation through the illustration. The CSS fireflies effect can be created by utilizing the CSS animation and we will implement 3 animations to achieve the fireflies effect. To create randomness, we used different animati
7 min read
How to Create a Change Background on Scroll using HTML CSS and JavaScript ?
In this article, we will try to change the background color of the container once the scroll occurs. If a user scrolls down the page the background color will be changed. We will use the below approach to accomplish this task. Using the scroll event on the window objectIn this approach, we will use the scroll event on the window object and pass a c
6 min read
How to Add Image in Text Background using HTML and CSS ?
In this article, we will use HTML and CSS to set the image in the text background. To set the image in the text background, some CSS property is used. To add an image in the text background using HTML and CSS, create a container element (e.g., a `&lt;div&gt;`), set its background to the desired image using CSS ('background-image property), and adju
2 min read
How to create multiple background image parallax in CSS ?
In this article, we will learn How to create multiple background image parallax in CSS. Approach: First, we will add background images using the background-image property in CSS. We can add multiple images using background-image. Syntax: background-image: url(image_url1),url(image_url2),...; Then we apply animation property in CSS that animate the
2 min read
How to Create a Half Page Background Image with CSS?
A half-page background image is an image that covers half of the viewport height. This can be useful for creating visually distinct sections on a webpage, especially for headers or introductory sections. These are the different approaches to creating a half-page background image using CSS: Table of Content Using viewport height (vh) unitsUsing flex
2 min read
Create an Anagram Checker App using HTML CSS and JavaScript
In this article, we will see how to create a web application that can verify if two input words are Anagrams or not, along with understanding the basic implementation through the illustration. An Anagram refers to a word or phrase that's created by rearranging the letters of another word or phrase using each letter once, i.e. An anagram of a string
3 min read
Create a Dictionary App using HTML CSS and JavaScript
In this article, we will be building a very simple Dictionary app with the help of an API, HTML, CSS, and JavaScript. Users can search for word definitions by entering a word in the input field and clicking the search button. The application makes an API request to fetch the word's definition and displays it along with the part of speech and an exa
4 min read
Create a Quiz App with Timer using HTML CSS and JavaScript
In this tutorial, we'll guide you through the process of building an engaging Quiz App with a built-in timer, utilizing HTML, CSS, and JavaScript. This application is designed to present users with a curated set of questions, each accompanied by a countdown timer, providing an interactive and time-bound quiz experience. Additionally, the app keeps
7 min read