Creating an Avatar File Upload Selector and Preview using HTML, CSS, and JS Tutorial
In this tutorial, you can learn to create an Avatar File Upload Selector and Preview component for web applications using HTML, CSS, and JavaScript. The tutorial aims to provide students and beginners with a reference for learning to build useful website components. Here, I will be providing a simple web page script that demonstrates the creation of an Avatar File Upload Selector and Preview Component.
What is an Avatar File Upload Selector and Preview?
The Avatar File Upload Selector and Preview are of common components in websites or web applications nowadays. It is often used in social networking sites, CMS (Content Management Systems), and mobile apps. This component is an avatar viewer that comes with an option or feature to select a new image to upload and display the selected image on the preview container.
How to create an Avatar File Upload Selector and Preview?
The Avatar File Upload Selector and Preview can be easily achieved or built using HTML elements, CSS codes, and JavaScript built-in events and methods. To create an Avatar File Upload, we will be needing multiple HTML elements that will be used as Preview wrapper/container, Image input file field, and other related containers and wrappers. We can design the Avatar Preview and Selector using CSS codes to our desired looks for the component. Then, using some JS event listeners such as click and change we can detect and trigger the changes of the avatar selected. Check out the web page scripts that I created below to understand more about how it being done.
Sample Web Page Scripts
The scripts below result in a simple web page that contains the Avatar File Upload Selector and Preview Container. The user can simply browse the new avatar to upload by clicking the camera icon located at the button-right side of the Avatar Preview Wrapper/Container.
Page Interface
The script below is the HTML file script named index.html. It contains the page layout and other elements that are needed for this demonstration. Make sure to replace the default image and camera icon paths with the paths that are available on your end.
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <link rel="preconnect" href="https://fonts.googleapis.com">
- <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
- <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
- <link rel="stylesheet" href="style.css">
- <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
- </head>
- <body>
- <div class="content-md-lg py-3">
- <div class="col-lg-6 col-md-10 col-sm-12 col-12 mx-auto">
- </div>
- <hr style="margin:auto; width:25px" class="border-light opacity-100">
- <div class="container-lg">
- <div class="row py-3">
- <div class="col-lg-4 col-md-5 col-sm-10 col-12 mx-auto">
- <div class="card bg-dark rounded-0 border-dark-subtle">
- <div class="card-body rounded-0">
- <!-- Avatar File Input Wrapper -->
- <div id="AvatarFileUpload">
- <!-- Image Preview Wrapper -->
- <div class="selected-image-holder">
- <img src="default-avatar.png" alt="AvatarInput">
- </div>
- <!-- Image Preview Wrapper -->
- <!-- Browse Image to Upload Wrapper -->
- <div class="avatar-selector">
- <a href="#" class="avatar-selector-btn">
- <img src="camera.svg" alt="cam">
- </a>
- <input type="file" accept="images/jpg, images/png" name="avatar">
- </div>
- <!-- Browse Image to Upload Wrapper -->
- </div>
- <!-- Avatar File Input Wrapper -->
- </div>
- </div>
- </div>
- </div>
- </div>
- </div>
- </body>
- </html>
Stylesheet
Next, the below script is the CSS file script named style.css. It contains the custom style codes for some of the design of the page elements.
- @import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@200&family=Space+Mono&display=swap" rel="stylesheet');
- :root{
- --space-mono-font: 'Space Mono', monospace;
- --border-dark-subtle: #373838;
- }
- *{
- box-sizing: border-box;
- }
- body *{
- font-family: var(--space-mono-font);
- }
- /**
- Page Design
- */
- body,
- html{
- height: 100%;
- width: 100%;
- margin: 0;
- padding: 0;
- }
- body{
- background-color: #282A3A;
- }
- .page-title{
- font-size: 2.5rem;
- font-weight: 500;
- color: #fff;
- letter-spacing: 3px;
- font-family: var(--secular-font);
- text-align: center;
- text-shadow: 0px 0px 3px #2020208c;
- }
- .border-dark-subtle{
- border-color: var(--border-dark-subtle) !important;
- }
- /* Avatar File Input Wrapper */
- div#AvatarFileUpload {
- position: relative;
- width: 150px;
- height: 150px;
- background: #f9f9f9;
- border: 3px solid #bbb;
- border-radius: 50% 50%;
- margin: auto;
- }
- /* Image Preview Wrapper and Container */
- div#AvatarFileUpload > .selected-image-holder{
- width: 100%;
- height: 100%;
- border-radius: 50% 50%;
- }
- div#AvatarFileUpload > .selected-image-holder{
- width: 100%;
- overflow: hidden;
- }
- div#AvatarFileUpload > .selected-image-holder>img{
- width: 100%;
- height: 100%;
- object-fit: cover;
- object-fit: center center;
- }
- /* Image Selector to Browse Image to Upload */
- div#AvatarFileUpload > .avatar-selector{
- position: absolute;
- bottom: 8px;
- right: 19%;
- width: 20px;
- height: 20px;
- }
- div#AvatarFileUpload > .avatar-selector input[type="file"]{
- display: none;
- }
- div#AvatarFileUpload > .avatar-selector > .avatar-selector-btn{
- width: 100%;
- height: 100%;
- background: #f5f5f59e;
- padding: 5px 7px;
- border-radius: 50% 50%;
- }
- div#AvatarFileUpload > .avatar-selector > .avatar-selector-btn>img{
- width: 100%;
- height: 100%;
- object-fit: scale-down;
- object-position: center center;
- z-index: 2;
- }
JavaScript
Lastly, here is the JavaScript file script named script.js. It contains the codes for making the Avatar File Upload Component functional.
- // Main Wrapper Selector
- const avatarFileUpload = document.getElementById('AvatarFileUpload')
- // Preview Wrapper Selector
- const imageViewer = avatarFileUpload.querySelector('.selected-image-holder>img')
- // Image Selector button Selector
- const imageSelector = avatarFileUpload.querySelector('.avatar-selector-btn')
- // Image Input File Selector
- const imageInput = avatarFileUpload.querySelector('input[name="avatar"]')
- /** Trigger Browsing Image to Upload */
- imageSelector.addEventListener('click', e => {
- e.preventDefault()
- // Trigger Image input click
- imageInput.click()
- })
- /** IF Selected Image has change */
- imageInput.addEventListener('change', e => {
- // Open File eader
- var reader = new FileReader();
- reader.onload = function(){
- // Preview Image
- imageViewer.src = reader.result;
- };
- // Read Selected Image as DataURL
- reader.readAsDataURL(e.target.files[0]);
- })
Snapshots
Here are the snapshots of the overall result of the scripts that I provided above.
Avatar File Upload Component
Selecting/Browsing New Image
There you go! I have provided also the complete source code zip file of the scripts that I provided above on this website and it is free to download. The download button is located below this tutorial's content. Feel Free to download and modify the script to enhance your own capabilities.
That's it! I hope this Creating an Avatar File Upload Selector and Preview using HTML, CSS, and JS Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.
Explore more on this website for more Tutorials and Free Source Codes.
Happy Coding =)
Add new comment
- 1833 views