开发者

The cursor:pointer property doesn't apply to file upload buttons in Webkit browsers

i have CSS code that does not really work on webkit browsers such as safari and chrome

if you want live example here it is http://jsfiddle.net/mnjKX/1/

i have this CSS code

.file-wrapper {
    cursor: pointe开发者_JAVA百科r;
    display: inline-block;
    overflow: hidden;
    position: relative;
}
.file-wrapper input {
    cursor: pointer;
    font-size: 100px;
    height: 100%;
    filter: alpha(opacity=1);
    -moz-opacity: 0.01;
    opacity: 0.01;
    position: absolute;
    right: 0;
    top: 0;
}
.file-wrapper .button {
    background: #79130e;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
    color: #fff;
    cursor: pointer;
    display: inline-block;
    font-size: 11px;
    font-weight: bold;
    margin-right: 5px;
    padding: 4px 18px;
    text-transform: uppercase;
}

and this HTML code :

<span class="file-wrapper">
   <input type="file" name="photo" id="photo" />
   <span class="button">Choose a Photo</span>
</span>

this code shows hidden input file tag , the problem here is that the cursor:pointer is does not work on webkit browsers ,

how can i solve it or bypass / overtake this ?


For starters, it works in Chrome if you remove the height declaration from the input rule.

Live demo: http://jsfiddle.net/mnjKX/16/

But this transparent input field is a hell of a hack... I wouldn't rely on it.


Update:

And here is the proper solution:

::-webkit-file-upload-button { cursor:pointer; }

I thought the file upload button is unreachable, but Chrome's user agent style sheet proved my wrong :)


An interesting (cross-browser) solution I came up with:

Give the input a CSS property of cursor:pointer, place the input in a div (with overflow:hidden) and give the input a left padding of 100%. The padded area will have the pointer property.

I personally don't trust -webkit and -moz fixes because I feel like they are arbitrary and temporary, and will be replaced soon.


input[type='file']{
    opacity: 0;
    cursor: pointer;
    width: 24px;
    height: 24px;
    font-size: 0;
    position: absolute;
}

<input type="file">
<img width="24" height="24" title="" alt="" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAADs0lEQVR42rWVa2iOYRjH9+zd+dw2oWaGwkjzRY5flDC1nBaxsTnVYkaWc8oHoZalETWHsVGkZo0yIyEmGzkWpZhD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C67Udqdn63HVYpoufmuebtuR/kXlS9cu3w7H3zBTWB/laOxlqDNlABbu37VUWw9bn+lIdrBnxljbMPpno/6w7Hj/B383E4GEjzq9k+/p78fan0xNyGwEGgAAAAASUVORK5CYII=" />

cursor:pointer does not work on input file just because of the default button. No special reason here. You need to remove its appearance via this code, pay attention with font-size:0.

It works perfectly on Chrome, Firefox and IE for me. I hope, this will also help you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜