1

How do I properly style a 'file' button with an image, so that it is seen clean and nice in all browsers and no overlapping text from the default control is there?

Thank you!

3 Answers 3

1

The way it's usually done is by making the file input element 100% transparent with the css opacity property and putting a pretty button behind it. That way the file field still receives the click, but the look of the underlying element can be styled and scripted any way you want.

Sign up to request clarification or add additional context in comments.

1 Comment

Is it necessary for me to use JavaScript or HTML and CSS would be sufficient?
0

File buttons are a major headache. I personally would use http://swfupload.org/

2 Comments

There is Flash involved with SWFUpload. Won't it prevent the button to be seen in iPhone/iPad?
How will you browse for and select files to upload from your iPhone/iPad?
0

I like this KISS approach, as usual. This worked for me in chrome and firefox.

HTML:

<div class="form-group">
     <label>Cover Image:</label>
     <div id="uploadLabelDiv" class="fade">
         <p>Change cover image</p>
         <label for="upload">
              <img [src]="project.cover" class="img-responsive" id="coverImage" />
          </label>
      </div>
</div>
<input type="file" id="upload" (change)="fileuploaded($event)" style="visibility:hidden;" />

CSS:

#uploadLabelDiv {
  position: relative;
  max-width: 400px;
  background: transparent;
  color: #fff;
  transition: all 0.3s;
}

.fade {
  opacity: 1;
}

.fade:hover > label {
  opacity: 0.4;
}

.fade > p {
  opacity: 0;
  transition: none;
}

.fade:hover > p {
  opacity: 1;
  transition: all 0.3s;
}

p {
  position: absolute;
  top: calc(50% - 30px);
  left: calc(50% - 60px);
  color: black;
  font: bold;
}

Missing classes are bootstrap.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.