input[type="file"] CSS

1.原理

基于安全性考虑,IE不支持用程序设置input type=file的值。

所以不能直接用<input type="text"/>和<input type="button"/>代替,

CSS美化file组件的方法是使用position属性将file组件置于<input type="text"/>和<input type="button"/>之上,

然后使其全透明化,操作时使用户感觉在操作<input type="text"/>和<input type="button"/>,实际上还是在操作file组件。 

 

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>定义input type="file" 的样式</title>
<style type="text/css">
.type-file-box {
position:absolute;
width:260px
}

input {
vertical-align:middle;
margin:0;
padding:0
}

.type-file-text {
height:22px;
border:1px solid #cdcdcd;
width:180px;
}

.type-file-button {
background-color:#FFF;
border:1px solid #CDCDCD;
height:24px;
width:70px;
}

.type-file-file {
position:absolute;
top:0;
right:0;
height:24px;
filter:alpha(opacity:0);
opacity: 0;
width:260px
}
</style>
</head>
<body>
<div class="type-file-box">
<form action="" method="post" name="form1" id="form1">
<input type='text' name='textfield' id='textfield' class='type-file-text' />
<input type='button' name='button' id='button' value='浏览...' class='type-file-button' />
<input name="fileField" type="file" class="type-file-file" id="fileField" size="28" οnchange="javascript:document.getElementById('textfield').value=this.value;"/>
</form>
</div>
</body>
</html>

转载于:https://www.cnblogs.com/mguo/archive/2013/04/23/3039280.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要修改input type=file按钮的样式,可以使用CSS来实现。但是由于input type=file的设计,只有很少的CSS属性可以应用于它。所以,我们需要使用一些技巧才能改变它的外观。以下是一些实现方法: 方法一:使用label元素代替input元素,并使用CSS样式来美化label元素。 HTML代码: ``` <label for="file-upload" class="custom-file-upload"> <i class="fa fa-cloud-upload"></i>选择文件 </label> <input id="file-upload" type="file"/> ``` CSS代码: ``` .custom-file-upload { display: inline-block; padding: 6px 12px; cursor: pointer; border: 1px solid #ccc; border-radius: 4px; } .custom-file-upload:hover { background-color: #f5f5f5; } .custom-file-upload i { margin-right: 5px; } ``` 方法二:使用伪元素和背景图片来美化input元素。 HTML代码: ``` <input type="file" class="custom-file-input"/> <label class="custom-file-label" for="custom-file-input">选择文件</label> ``` CSS代码: ``` .custom-file-input { position: absolute; left: -9999px; } .custom-file-label { display: inline-block; padding: 6px 12px; cursor: pointer; border: 1px solid #ccc; border-radius: 4px; font-size: 14px; font-weight: normal; background-image: url('file-upload.png'); background-repeat: no-repeat; background-position: center; background-size: 16px; padding-left: 24px; } .custom-file-label:hover { background-color: #f5f5f5; } ``` 其中,file-upload.png是一个上传图标的图片。 以上两种方法都可以实现美化input type=file按钮的效果。您可以根据自己的需求选择其中一种方法进行使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值