How do I add a Font Awesome icon to file input field

前端 未结 3 2021
Happy的楠姐
Happy的楠姐 2021-02-04 21:26

I have a file input button, instead of browse I want to show Font Awesome upload icon there but whatever I tried nothing gave me result.

Here is what I\'ve tried

相关标签:
3条回答
  • 2021-02-04 22:06

    Since you are using Bootstrap - if you are open to using a small plug-in, there is one that will do what you want. The plug-in is Bootstrap Filestyle - http://markusslima.github.io/bootstrap-filestyle/.

    All you have to do is add a few attributes to your input and download/include the plug-in's JS after Bootstrap's JS.

    <input type="file" class="filestyle" name="image_src" id="image_src" data-input="false" data-iconName="fa fa-upload" data-buttonText="Upload File" />
    

    Your font-awesome icon class will go in data-iconName and your verbiage will go in data-buttonText.

    Additionally, you can add or remove the text input portion with data-input. True to show it and false (as in the demo below) to hide it.

    Working Demo Here

    0 讨论(0)
  • 2021-02-04 22:16

    There's another way you can handle this scenario using just CSS and FontAwesome. If you go to FontAwesome's website and look at the examples, I'm going to use "fa fa-asterisk" for this example, you will notice once you click the icon and it takes you to the page, it will give you a UNICODE value for the fontawesome icon. For example the UNICODE value for asterisk is "f069".

    This being the case, you can use the "after or before" pseudo class in your CSS like so:

    [input]#img_src::before {
    font-family: 'FontAwesome';
    content: '\f069'
    }
    

    This will place a fontawesome asterisk BEFORE (in this case) any input button text. If you want the button to only show the icon, simply don't enter any input text and just use the pseudo classes to handle the distribution of the fontawesome icons using only CSS.

    Hope this helps.

    0 讨论(0)
  • 2021-02-04 22:26

    You could hide file input with display: none and create custom button or in this case font-awesome icon that will trigger input. Also you can use span to display input's value that will change on input value change.

    $("i").click(function () {
      $("input[type='file']").trigger('click');
    });
    
    $('input[type="file"]').on('change', function() {
      var val = $(this).val();
      $(this).siblings('span').text(val);
    })
    .element {
      display: inline-flex;
      align-items: center;
    }
    i.fa-camera {
      margin: 10px;
      cursor: pointer;
      font-size: 30px;
    }
    i:hover {
      opacity: 0.6;
    }
    input {
      display: none;
    }
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="element">
      <i class="fa fa-camera"></i><span class="name">No file selected</span>
      <input type="file" name="" id="">
    </div>

    0 讨论(0)
提交回复
热议问题