Home  >  Article  >  Web Front-end  >  怎样用纯HTML和CSS更改默认的上传文件按钮样式_html/css_WEB-ITnose

怎样用纯HTML和CSS更改默认的上传文件按钮样式_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:47:271504browse

 如果你曾经试过,你就会知道,用纯CSS样式加HTML实现统一的上传文件按钮可能会很麻烦。看看下面的不同浏览器的截图。很明显的,他们长得很不一样。

  我们的目标是创造一个简洁,用纯CSS实现的,在所有浏览器里的样子和布局是一样的上传文件按钮。我们可以这样: 

  步骤1.创建一个简单的HTML标记 

1

2

3

4

     Upload span >

    

div >

  第2步:CSS: 有点棘手了

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

.fileUpload {

     position : relative ;

     overflow : hidden ;

     margin : 10px ;

}

 

.fileUpload input.upload {  position : absolute ;

     top : 0 ;

     right : 0 ;

     margin : 0 ;

     padding : 0 ;

     font-size : 20px ;

     cursor : pointer ;

     opacity: 0 ;

     filter: alpha(opacity= 0 );

}

  为简单起见,我使用应用了BootstrapCSS样式的按钮  (div.file-upload)。 

  演示: 

  上传按钮,显示选中的文件 

  不幸的是纯CSS的做不到这一点。但是,如果你真的想显示所选文件,下面的JavaScript代码片段可以帮助你。 

  JavaScript: 

1

2

3

document.getElementById( "uploadBtn" ).onchange = function () {

     document.getElementById( "uploadFile" ).value = this .value;

};

  DOM:

1

2

3

4

5

     Upload span >

    

div >

  演示:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn