Input type=fileスタイルをカスタマイズする方法

2784 ワード

なぜfileコントロールを美化するのですか?考えてみてください.他の子供はみなきれいに着ています.その中の二人の子供はどうしてあなたを鳥にしないのですか.あまりにも調和がとれていません.
元のfileコントロールは次のとおりです.

これはtextとbuttonを組み合わせたものだとは思わないでください.htmlコードは次のとおりです.
<input type="file" name="file" />
では、textとbuttonを使用してこのfileのスタイルを表示します.htmlコードは次のようになります.
<div class="file-box">
<form action="" method="post" enctype="multipart/form-data">
<input type='text' name='textfield' id='textfield' class='txt' />
<input type='button' class='btn' value=' ...' />
<input type="file" name="fileField" class="file" id="fileField" size="28" onchange="document.getElementById('textfield').value=this.value" />
<input type="submit" name="submit" class="btn" value=" " />
</form>
</div>
の外側のdivは、中のinputに位置参照を提供するためです.スタイルを書くときは、シミュレーションの上に本物のfileコントロールを上書きし、fileコントロールを非表示にする必要があるので、cssコードは次のようになります.
.file-box{ position:relative;width:340px}
.txt{ height:22px; border:1px solid #cdcdcd; width:180px;}
.btn{ background-color:#FFF; border:1px solid #CDCDCD;height:24px; width:70px;}
.file{ position:absolute; top:0; right:80px; height:24px; filter:alpha(opacity:0);opacity: 0;width:260px }

    Demo
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> input type="file"  </title>
<style type="text/css">
body{ font-size:14px;}
input{ vertical-align:middle; margin:0; padding:0}
.file-box{ position:relative;width:340px}
.txt{ height:22px; border:1px solid #cdcdcd; width:180px;}
.btn{ background-color:#FFF; border:1px solid #CDCDCD;height:24px; width:70px;}
.file{ position:absolute; top:0; right:80px; height:24px; filter:alpha(opacity:0);opacity: 0;width:260px }
</style>
</head>
<body>
<div class="file-box">
  <form action="" method="post" enctype="multipart/form-data">
 <input type='text' name='textfield' id='textfield' class='txt' />  
 <input type='button' class='btn' value=' ...' />
    <input type="file" name="fileField" class="file" id="fileField" size="28" onchange="document.getElementById('textfield').value=this.value" />
 <input type="submit" name="submit" class="btn" value=" " />
  </form>
</div>
</body>
</html>