Styling an input type=“file” button

后端 未结 30 1662
终归单人心
终归单人心 2020-11-21 11:50

How do you style an input type=\"file\" button?

30条回答
  •  南方客
    南方客 (楼主)
    2020-11-21 12:02

    All rendering engines automatically generate a button when an is created. Historically, that button has been completely un-styleable. However, Trident and WebKit have added hooks through pseudo-elements.

    Trident

    As of IE10, the file input button can be styled using the ::-ms-browse pseudo-element. Basically, any CSS rules that you apply to a regular button can be applied to the pseudo-element. For example:

    ::-ms-browse {
      background: black;
      color: red;
      padding: 1em;
    }

    This displays as follows in IE10 on Windows 8:

    This displays as follows in IE10 on Windows 8:

    WebKit

    WebKit provides a hook for its file input button with the ::-webkit-file-upload-button pseudo-element. Again, pretty much any CSS rule can be applied, therefore the Trident example will work here as well:

    ::-webkit-file-upload-button {
      background: black;
      color: red;
      padding: 1em;
    }

    This displays as follows in Chrome 26 on OS X:

    This displays as follows in Chrome 26 on OS X:

提交回复
热议问题