Styling an input type=“file” button

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

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

30条回答
  •  名媛妹妹
    2020-11-21 12:07

    Only this approach gives you the whole flexibility! ES6 / VanillaJS!

    html:

    
    
    

    javascript:

    document.querySelector('button').addEventListener('click', () => {
      document.querySelector('input[type="file"]').click();
    });
    

    This hides the input-file button, but under the hood clicks it from another normal button, that you can obviously style like any other button. This is the only solution with no downside apart from a useless DOM-node. Thanks to display:none;, the input-button does not reserve any visible space in the DOM.

    (I don't know anymore to whom to give props for this. But I got that idea from somewhere here on Stackoverflow.)

提交回复
热议问题