Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a "<input type="file" />" looks like a link with CSS?

Tags:

html

css

Just want to the style of:

<input type="file" value="Choose the file" />

looks like:

<a href="#">Choose the file</a>

just using css.

Is it possible?

like image 205
Freewind Avatar asked Dec 11 '14 09:12

Freewind


People also ask

How do I add a link to input field?

You can set the value of the input tag to some kind of a link, but it would not work as a hyperlink. The only way for the user to access that URL is to copy it and then paste it to the URL bar. What you should do is to put an <a> tag after this input tag, which is what the most sites are doing.

Can we use style in input tag?

A style attribute on an <input> tag assigns a unique style to the input control. Its value is CSS that defines the appearance of the input element.


2 Answers

How about using label instead of anchor and you connect the label with input[type="file"] via for:

label{
  color: blue;
  cursor: pointer;
}

label:hover{
  text-decoration: underline;
}

#file_input_id{
  display:none;
}
<label for="file_input_id">I'm a wannabe link</label>
<input type="file" id="file_input_id">

Note: safari has issues with using display:none to input[type="file"]

like image 175
Vucko Avatar answered Oct 24 '22 13:10

Vucko


It will likely require some fine tuning for the size, hover state etc, but why not do:

span {
  cursor: pointer;
}
a {
  position: relative;
  overflow: hidden;
}
span:hover a {
  color: red;
}
a + input {
  position: absolute;
  top: 0;
  left: 0;
  opacity: 0;
}
<span><a href='#'>Link!</a><input type='file' /></span>

The basic premise is that the input type=file should be absolutely positioned over the link with its opacity set to zero so it still traps the normal user interaction behaviour.

like image 39
SW4 Avatar answered Oct 24 '22 15:10

SW4