Today’s task is to create and style file input without any JavaScript code. We're going to demonstrate a tricky way of creating and styling a file input with HTML and CSS. Let’s build an example and see how it works.
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<div class="container">
<div class="button-wrap">
<label class="button" for="upload">Upload File</label>
<input id="upload" type="file">
</div>
</div>
</body>
</html>
.container {
display: flex;
align-items: flex-start;
justify-content: flex-start;
width: 100%;
}
input[type="file"] {
position: absolute;
z-index: -1;
top: 10px;
left: 8px;
font-size: 17px;
color: #b8b8b8;
}
.button-wrap {
position: relative;
}
.button {
display: inline-block;
padding: 12px 18px;
cursor: pointer;
border-radius: 5px;
background-color: #8ebf42;
font-size: 16px;
font-weight: bold;
color: #fff;
}
So, here’s our custom file input with pure CSS.
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.container {
display: flex;
align-items: flex-start;
justify-content: flex-start;
width: 100%;
}
input[type="file"] {
position: absolute;
z-index: -1;
top: 10px;
left: 8px;
font-size: 17px;
color: #b8b8b8;
}
.button-wrap {
position: relative;
}
.button {
display: inline-block;
padding: 12px 18px;
cursor: pointer;
border-radius: 5px;
background-color: #8ebf42;
font-size: 16px;
font-weight: bold;
color: #fff;
}
</style>
</head>
<body>
<div class="container">
<div class="button-wrap">
<label class="button" for="upload">Upload File</label>
<input id="upload" type="file">
</div>
</div>
</body>
</html>
Another beautiful example with the :hover pseudo-class.
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<style>
.container {
display: flex;
align-items: flex-start;
justify-content: flex-start;
width: 100%;
}
input[type="file"] {
position: absolute;
z-index: -1;
top: 15px;
left: 20px;
font-size: 17px;
color: #b8b8b8;
}
.button-wrap {
position: relative;
}
.button {
display: inline-block;
background-color: #1d6355;
border-radius: 10px;
border: 4px double #cccccc;
color: #ffffff;
text-align: center;
font-size: 20px;
padding: 8px;
width: 100px;
transition: all 0.5s;
cursor: pointer;
margin: 5px;
}
.button:hover {
background-color: #00ab97;
}
</style>
</head>
<body>
<div class="container">
<div class="button-wrap">
<label class="button" for="upload">Upload File</label>
<input id="upload" type="file">
</div>
</div>
</body>
</html>