There are many, many, many ways to hack a file input so it looks and behaves consistently across all browsers. There’s even a pretty slick plugin to help get the job done. Unfortunately, most of these solutions are cumbersome, prone to cross-browser issues, and require JavaScript. Today, we would like to propose a more fundamental approach for file inputs in Bootstrap. Back to the basicsLet’s start by making a fake button with a nested file input:
And then apply some CSS:
So far so good. Not only will the “button” trigger the file input, but it will also acquire the You can use these buttons like you normally would…by themselves, in a button group, or even in an input group. Providing feedback Now with the hard part out of the way, it’s a good practice to provide users with a bit of feedback about their selection. A touch of jQuery magic will keep an eye on your file inputs and fire an event called
Here’s an example to demonstrate the event:
You can use the This method was tested to work in IE8–IE11 and recent versions of Chrome, Safari, Firefox, and Opera. See it in action |
|