<div class="file-input">
<input type="file" id="file" class="file">
<label for="file">
Select file
<p class="file-name"></p>
</label>
</div>
.file {
opacity: 0;
width: 0.1px;
height: 0.1px;
position: absolute;
}
.file-input label {
display: block;
position: relative;
width: 200px;
height: 50px;
border-radius: 25px;
background: linear-gradient(40deg,#ff6ec4,#7873f5);
box-shadow: 0 4px 7px rgba(0, 0, 0, 0.4);
display: flex;
align-items: center;
justify-content: center;
color: #fff;
font-weight: bold;
cursor: pointer;
transition: transform .2s ease-out;
}
.file-name {
position: absolute;
bottom: -35px;
left: 10px;
font-size: 0.85rem;
color: #555;
}
input:hover + label,
input:focus + label {
transform: scale(1.02);
}
/* Adding an outline to the label on focus */
input:focus + label {
outline: 1px solid #000;
outline: -webkit-focus-ring-color auto 2px;
}
const file = document.querySelector('#file');
file.addEventListener('change', (e) => {
// Get the selected file
const [file] = e.target.files;
// Get the file name and size
const { name: fileName, size } = file;
// Convert size in bytes to kilo bytes
const fileSize = (size / 1000).toFixed(2);
// Set the text content
const fileNameAndSize = `${fileName} - ${fileSize}KB`;
document.querySelector('.file-name').textContent = fileNameAndSize;
});
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.