原生的 <input type="file"> 外觀由各瀏覽器決定,很難與設計稿一致。常見的解法是把真正的 file input 透明化,疊在一張圖片上方——使用者看到的是美觀的圖片,點下去觸發的仍是原生檔案選擇器。
<style>
.fileInputContainer {
height: 256px;
width: 256px;
background: url(upfile.png);
position: relative;
}
.fileInput {
height: 256px;
overflow: hidden;
font-size: 300px;
position: absolute;
right: 0;
top: 0;
opacity: 0;
filter: alpha(opacity=0);
cursor: pointer;
}
</style>
<div class="fileInputContainer">
<input class="fileInput" type="file" name="" id="" />
</div>

還有一個更簡潔的現代做法,不需要任何透明度 hack:
<style>
.fileInput { display: none; }
.uploadLabel {
display: block;
width: 256px;
height: 256px;
background: url(upfile.png);
cursor: pointer;
}
</style>
<label class="uploadLabel" for="myFile"></label>
<input class="fileInput" type="file" id="myFile" />
用 label 的 for 屬性關聯到 file input,點擊 label 等同點擊 input。這個做法的好處是:HTML 語意清楚、不需要透明度與字級 hack、鍵盤操作(Tab 聚焦後按 Enter)也能正常運作,而上面那個透明疊加法的鍵盤 usability 其實是有問題的——使用者看不見焦點在哪。
若要進一步自訂「已選檔案名稱」的顯示,監聽 change 事件即可:
document.getElementById('myFile').addEventListener('change', function (e) {
const name = e.target.files[0] ? e.target.files[0].name : '尚未選擇檔案';
document.querySelector('.filename').textContent = name;
});