Monday, April 23, 2012

input type=file show only button


Is there a way to style (or script) <input type=file /> element to have visible only "Browse" button without text field?



Thanks



Edit : Just to clarify why to I need this. I'm using multi file upload code from http://www.morningz.com/?p=5 and it doesn't need input text field because it never has value. Script just adds newly selected file to collection on page. It would look much better without text field, if it's possible.


Source: Tips4all

9 comments:

  1. That's going to be very hard. The problem with the file input type is that it usually consists of two visual elements, while being treated as a single DOM-element. Add to that that several browsers have their own distinct look and feel for the file input, and you're set for nightmare. See this article on quirksmode.org about the quirks the file input has. I guarantee you it won't make you happy (I speak from experience).

    [EDIT]

    Actually, I think you might get away with putting your input in a container element (like a div), and adding a negative margin to the element. Effectively hiding the textbox part off screen.
    Another option would be to use the technique in the article I linked, to try to style it like a button.

    ReplyDelete
  2. Fix to work in all browsers
    RESOLVED:

    <input type = "button" value = "Choose image"
    onclick ="javascript:document.getElementById('imagefile').click();">
    <input id = "imagefile" type="file" style='visibility: hidden;' name="img"/>


    I have tested in FF, Chrome & IE - working fine, applied styles too :D

    ReplyDelete
  3. I wasted my day today getting this to work. I found none of the solutions here working each of my scenarios.

    Then I remembered I saw an example for the JQuery File Upload without text box. So what I did is that I took their example and stripped it down to the relevant part.

    This solution at least works for IE and FF and can be fully styled. In the above example the file input is hidden under the fancy "Add Files" button.

    <html>

    <head>
    <title>jQuery File Upload Example</title>
    <style type="text/css">
    .myfileupload-buttonbar input
    {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    border: solid transparent;
    border-width: 0 0 100px 200px;
    opacity: 0.0;
    filter: alpha(opacity=0);
    -o-transform: translate(250px, -50px) scale(1);
    -moz-transform: translate(-300px, 0) scale(4);
    direction: ltr;
    cursor: pointer;
    }
    .myui-button
    {
    position: relative;
    cursor: pointer;
    text-align: center;
    overflow: visible;
    background-color: red;
    overflow: hidden;
    }
    </style>
    </head>
    <body>
    <div id="fileupload" >
    <div class="myfileupload-buttonbar ">
    <label class="myui-button">
    <span >Add Files</span>
    <input id="file" type="file" name="files[]" />
    </label>
    </div>
    </div>
    </body>
    </html>

    ReplyDelete
  4. Hide the input-file element and create a visible button that will trigger the click event of that input-file.

    Try this:

    CSS

    #file { width:0; height:0; }


    HTML:

    <input type='file' id='file' name='file' />
    <button id='btn-upload'>Upload</button>


    JAVASCRIPT(jQuery):

    $(function(){
    $('#btn-upload').click(function(e)
    e.preventDefault();
    $('#file').click();
    );
    });

    ReplyDelete
  5. Ive a really hacky solution with this...

    <style type="text/css">
    input[type="file"]
    {
    width: 80px;
    }
    </style>

    <input id="File1" type="file" />


    The problem is the width attribute that is hiding the text field will obvously vary between browsers, vary between Windows XP themes and so on. Maybe its something you can work with though?...

    ReplyDelete
  6. The quirksmode trick will hide the text field but you are stuck having a text selection character over a button which is the problem I have, setting the cursor css style wont work either :(

    ReplyDelete
  7. RESOLVED
    <div style="overflow: hidden;width:83px;">
    <input style='float:right;' name="userfile" id="userfile" type="file"/>
    </div>

    ReplyDelete
  8. my solution is just to set it within a div like "druveen" said, however i ad my own button style to the div (make it look like a button with a:hover) and i just set the style "opacity:0;" to the input. Works a charm for me, hope it does the same for you.

    ReplyDelete
  9. I was having a heck of a time trying to accomplish this. I didn't want to use a Flash solution, and none of the jQuery libraries I looked at were reliable across all browsers.

    I came up with my own solution, which is implemented completely in CSS (except for the onclick style change to make the button appear 'clicked').

    You can try a working example here: http://jsfiddle.net/VQJ9V/35/ (Tested in FF 7, IE 9, Safari 5, Opera 11 and Chrome 14)

    It works by creating a big file input (with font-size:50px), then wrapping it in a div that has a fixed size and overflow:hidden. The input is then only visible through this "window" div. The div can be given a background image or color, text can be added, and the input can be made transparent to reveal the div background:

    HTML:

    <div class="inputWrapper">
    <input class="fileInput" type="file" name="file1"/>
    </div>


    CSS:

    .inputWrapper {
    height: 32px;
    width: 64px;
    overflow: hidden;
    position: relative;
    cursor: pointer;
    /*Using a background color, but you can use a background image to represent a button*/
    background-color: #DDF;
    }
    .fileInput {
    cursor: pointer;
    height: 100%;
    position:absolute;
    top: 0;
    right: 0;
    z-index: 99;
    /*This makes the button huge. If you want a bigger button, increase the font size*/
    font-size:50px;
    /*Opacity settings for all browsers*/
    opacity: 0;
    -moz-opacity: 0;
    filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0)
    }


    Let me know if there are any problems with it and I'll try to fix them.

    ReplyDelete