2

I'm trying to put a spin on this article to provide a nicer-looking user interface for a file upload input. In short, it's primarily a CSS-only method for hiding the browser's native input button and replacing it with a style label. The project I want to use it in also happens to be an Ionic3 project, and so I'd like the label that to be styled to look like a natural ion-button.

The code from the article works great if the label contains a plain text, but if I try and embed a button-element within the label, I get no love, regardless of whether I use the ion-button attribute incidentally.

  .inputfile {
    width: 0.1px;
    height: 0.1px;
    opacity: 0;
    overflow: hidden;
    position: absolute;
    z-index: -1;
  }

  .inputfile + label {
    display: inline-block;
  }

  .inputfile + label {
    cursor: pointer; /* "hand" cursor */
  }
<input type="file" name="file" id="file" class="inputfile" />
<label for="file">Choose a file text label</label> &lt;-- this works
<br/><br/>
<input type="file" name="file" id="file" class="inputfile" />
<label for="file">
  <button>Choose a file button label</button>
</label> &lt;-- this doesn't work

2
  • SO suggested my title was not great, if you have a suggestion for a better one feel free to change it. Commented Jan 2, 2019 at 21:48
  • 1
    I use to hide the input completely and create a styled button, then with javascript(jQuery) onclick on the button I trigger a click on the input... Commented Jan 2, 2019 at 22:00

2 Answers 2

1

This isn't a pure CSS solution or specific to Ionic, but in general for a button to stand in as the UI for a hidden file input element, the following appears to work. You can put a click handler on the button, and then in that handler, call the input element's DOM click handler:

<button (click)="handleClick($event)">Choose a file</button>

// Grab the element (Ionic/Angular)
    @ViewChild('fileInput') fileInput;
handleClick($event) {
  this.fileInput._native.nativeElement.click();
}

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

Comments

0

You are probably better off styling the text label to look like a button.

  .inputfile {
    width: 0.1px;
    height: 0.1px;
    opacity: 0;
    overflow: hidden;
    position: absolute;
    z-index: -1;
  }

  .inputfile + label {
    display: inline-block;
  }

  .inputfile + label {
    cursor: pointer; /* "hand" cursor */
  }
  .button {
    -webkit-appearance: button;
    -webkit-writing-mode: horizontal-tb !important;
    text-rendering: auto;
    color: initial;
    letter-spacing: normal;
    word-spacing: normal;
    text-transform: none;
    text-indent: 0px;
    text-shadow: none;
    display: inline-block;
    text-align: start;
    margin: 0em;
    font: 400 11px system-ui;
    align-items: flex-start;
    text-align: center;
    cursor: default;
    color: buttontext;
    background-color: buttonface;
    box-sizing: border-box;
    padding: 2px 6px 3px;
    border-width: 2px;
    border-style: outset;
    border-color: buttonface;
    border-image: initial;
    border-color: rgb(216, 216, 216) rgb(209, 209, 209) rgb(186, 186, 186);
    border-style: solid;
    border-width: 1px;
    padding: 1px 7px 2px;
  }
<input type="file" name="file" id="file" class="inputfile" />
<label for="file" class="button">Choose a file text label</label> &lt;-- this works
<br/><br/>
<input type="file" name="file" id="file" class="inputfile" />
<label for="file">
  <button>Choose a file button label</button>
</label> &lt;-- this doesn't work

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.