原始的file控件是这么写的:
<input type="file" name="file" />
比较丑。它在外观上是由一个text和一个button组合成的,实际是一个控件
既然这样我们就用一个text和一个button来显示这个file的样式,html代码是这样:
<form action="" method="post" enctype="multipart/form-data">
<div class="file-box">
<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="上传" />
</div>
</form>
外面的一层div是为了给里面的input提供位置参考,因为写样式的时候需要相对定位,使真正的file控件覆盖在模拟的上面,然后隐藏掉file控件(即使file控件不可见),所以css代码是这样的:
<style>
.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>
效果图:
美化HTML文件输入框
本文介绍了一种通过使用HTML和CSS来美化默认文件输入框的方法。通过将原本的文件选择控件替换为自定义的文本框和按钮,可以显著提升用户体验。文章详细展示了如何实现这一效果,包括必要的代码片段。

6576

被折叠的 条评论
为什么被折叠?



