Use custom CSS for image input

Search for a command to run...

No comments yet. Be the first to comment.
Using typeof To check what type is a variable you would most likely use the typeof function. You can even skip the parenthesis (typeof someVariable is same as typeof(someVariable)). typeof works great for: strings numbers (not if checking integer vs...

Here are 2 ready-to-use functions to get the first and last millisecond of a day using a few lines of vanilla JavaScript. Get the first moment of a day /** * Get first millisecond of a date * @param {Date} dateObj - JS Date object, default is curre...

Obviously your best guess is to ask the user. That being said, we generally want to avoid interrupting the user. Websites and apps are (or at least should be) about what the user's needs not ours. That being said, given the many legislation requirem...

Skip to solution If you have different language settings in your website or application wouldn't it be great if you could automatically set the language the user prefers? Doing so may be easer than you think. All you have to do is use navigator.langu...

Skip to solution. Problem Knowing your customer location can help you tailor their experience. Other times you may want to check for this information to compile statistics or comply with regulations. What's the best way to get location data? One way...

HTML supports image input and on mobile it will even do all the heavy lifting for the Camera API for you, providing a nice native experience. Here's what it looks like:
But let's be honest, the design sucks. Why?
I'd be OK if you could just add your own CSS, but it will only let you style the "box". The button, the message, etc. are all untouchable by CSS. It would have been so much better if they just let you manipulate those via pseudo-elements .
The solution to the problem is done in 3 steps:
<input> in a <label> element<input> elementThe solution isn't perfect, but you can solve both drawbacks mentioned below with some JavaScript. If you have an interesting implementation or even better, if you came up with fixes that don't require JS, don't be shy and drop me a line.
Adding tabindex="0" to the label element will make it tab-bable, but pressing ENTER/SPACE doesn't activate it. What you could do is add a JS listener for the keypress. This defeats the purpose which is a CSS only solution.
However, if we were to use JS anyway we might as well be more semantic. Keep in mind clicking this control will activate the hidden input, so using a button would be much more semantic than a label element.
Often it is useful to see if/which image is in the input. The HTML solution is to provide you with a filename or text saying that the value is empty.
Again, we could solve this with some JS and listen to the value change. In this case I would also consider adding an image preview and a button to reset the input (set value to none). Using the built-in HTML input you can reset the input value too, but it's done in such an unintuitive way, doing a JS powered redesign is the way to go.