實際上它可以在純CSS來完成,它是很容易...
HTML代碼
<label class="filebutton">
Browse For File!
<span><input type="file" id="myfile" name="myfile"></span>
</label>
CSS樣式
label.filebutton {
width:120px;
height:40px;
overflow:hidden;
position:relative;
background-color:#ccc;
}
label span input {
z-index: 999;
line-height: 0;
font-size: 50px;
position: absolute;
top: -2px;
left: -700px;
opacity: 0;
filter: alpha(opacity = 0);
-ms-filter: "alpha(opacity=0)";
cursor: pointer;
_cursor: hand;
margin: 0;
padding:0;
}
的想法是絕對定位您的標籤內部的輸入。將輸入的字體大小設置爲很大,這將增加「瀏覽」按鈕的大小。然後使用負左/上屬性進行試驗和錯誤,將輸入瀏覽按鈕放置在標籤後面。
在定位按鈕,設置透明度爲1.當您完成設置回0(所以你可以看到你在做什麼!)
確保你跨瀏覽器測試,因爲他們」都會使輸入按鈕的大小略有不同。
你可以在這裏看到一個例子(添加軌道按鈕):http://rakmastering.com/upload/
對於這裏所有人來說,http://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/也許值得一看。 (不以任何方式附屬於該網站,形狀或形式) – CBroe 2016-04-20 14:40:18