HTML+CSS优化“文件选择”控件(<input type="file">)

本文介绍了一种使用text和button元素替代默认HTML文件输入框的方法,并通过CSS样式美化界面。同时探讨了如何解决按钮交互问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

碰到个页面的美化问题,效果图如下:

而html默认的空间是难看的,就像下面这个优快云编辑博客的苦逼控件一样:


那么就需要我们来解决,我的方法是使用text与button代替这个控件,使用了覆盖的实现方式,不多说,看代码:

        <div class="secend">
            <p>选择文件</p>
            <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='btn1' 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="btn2" value="上传" />
                    <input type="button" name="submit" class="btn3" value="重置" />
                </form>
            </div>
        </div>
.file-box{ position:relative;width:600px}
.txt{ height:30px; border:1px solid #cdcdcd; width:500px;}
.btn1{color: white; background-color:#FFF; border:1px solid #CDCDCD;height:32px; width:90px;background-color: #2688C1;}
.btn2{color: white; background-color:#FFF; border:1px solid #CDCDCD;height:50px; width:110px;background-color: #56cf86;margin-top: 2%;margin-right: 0.5%}
.btn3{ background-color:#FFF; border:1px solid #CDCDCD;height:50px; width:110px;background-color: #ececec;margin-top: 2%;}
.btn1:hover,.btn2:hover,.btn3:hover{opacity: 0.7;}
.file{ position:absolute; top:0; height:30px; filter:alpha(opacity:0);opacity: 0;width:600px }

目前存在的问题是:“浏览”按钮点击没有交互,因为实际上点击的是file控件,那么如何解决这个问题呢?

希望以后可以找到答案,渴望各路大神提点大笑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值