Hide the browse button on a input type=file

前端 未结 10 768
再見小時候
再見小時候 2020-12-11 14:33

Is there a way to hide the browse button and only leave the text box that works in all browsers?

I have tried setting the margins but they show up different in each

相关标签:
10条回答
  • 2020-12-11 15:13

    Just add negative text intent as so:

    input[type=file] {
      text-indent: -120px;
    }
    

    before:

    after:

    0 讨论(0)
  • 2020-12-11 15:14

    So I found this solution that is very easy to implement and gives a very clean GUI

    put this in your HTML

    <label class="att-each"><input type="file"></label>
    

    and this in your CSS

    label.att-each {
    width: 68px;
    height: 68px;
    background: url("add-file.png") no-repeat;
    text-indent: -9999px;
    }
    

    add-file.png can be any graphic you wish to show on the webpage. Clicking the graphic will launch the default file explorer.

    Working Example: http://www.projectnaija.com/file-picker17.html

    0 讨论(0)
  • 2020-12-11 15:18

    the best way for it

    <input type="file" id="file">
    <label for="file" class="file-trigger">Click Me</label> 
    

    And you can style your "label" element

    #file { 
       display: none;
    }
    .file-trigger {
    /* your style */
    }
    
    0 讨论(0)
  • 2020-12-11 15:20

    Just an additional hint for avoiding too much JavaScript here: if you add a label and style it like the "browse button" you want to have, you could place it over the real browse button provided by the browser or hide the button somehow differently. By clicking the label the browser behavior is to open the dialog to browse for the file (don't forget to add the "for" attribute on the label with value of the id of the file input field to make this happen). That way you can customize the button in almost any way you want.

    In some cases, it might be necessary to add a second input field or text element to display the value of the file input and hide the input completely as described in other answers. Still the label would avoid to simulate the click on the text input button by JavaScript.

    BTW a similar hack can be used for customizing checkboxes or radiobuttons. by adding a label for them, clicking the label causes to select the checkbox/radiobutton. The native checkbox/radiobutton then can be hidden somewere and be replaced by a custom element.

    0 讨论(0)
  • 2020-12-11 15:21

    No, what you can do is a (ugly) workaround, but largely used

    1. Create a normal input and a image
    2. Create file input with opacity 0
    3. When the user click on the image, you simulate a click on the file input
    4. When file input change, you pass it's value to the normal input (so user can see the path)

    Here you can see a full explanation, along with code:

    http://www.quirksmode.org/dom/inputfile.html

    0 讨论(0)
  • 2020-12-11 15:23

            .dropZoneOverlay, .FileUpload {
                width: 283px;
                height: 71px;
            }
    
            .dropZoneOverlay {
                border: dotted 1px;
                font-family: cursive;
                color: #7066fb;
                position: absolute;
                top: 0px;
                text-align: center;
            }
    
            .FileUpload {
                opacity: 0;
                position: relative;
                z-index: 1;
            }
            <div class="dropZoneContainer">
                <input type="file" id="drop_zone" class="FileUpload" accept=".jpg,.png,.gif" onchange="handleFileSelect(this) " />
                <div class="dropZoneOverlay">Drag and drop your image <br />or<br />Click to add</div>
            </div>

    I find a good way of achieving this at Remove browse button from input=file.

    The rationale behind this solution is that it creates a transparent input=file control and creates an layer visible to the user below the file control. The z-index of the input=file will be higher than the layer.

    With this, it appears that the layer is the file control itself. But actually when you clicks on it, the input=file is the one clicked and the dialog for choosing file will appear.

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