Can I replace the button displayed by Blazor's InputFile component with another element?

Viewed 4464

I am using the element to upload image files to my hosted Blazor WASM site. The component renders a button with the words "Choose Files" on it.

I would like to replace this button with an image (or my own text, or anything else). I have tried using CSS to set a background image to the URL of an image I would want to use, and set the background-color of the button to "transparent", but this does not seem to change anything.

1 Answers

The source code for this component can be found here: https://github.com/SteveSandersonMS/BlazorInputFile

I studied the code and found that this component is built using the standard Blazor input type.

<input type=file>

Steve shows a way to override the default functionality and style of the button using CSS.

Here is an example I created based on what I found:

<style>
    .file-input-zone {
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: blue;
        color: white;
        cursor: pointer;
        position: relative;
        width: 120px;
        height: 30px;
    }

        .file-input-zone:hover {
            background-color: lightblue;
        }

        .file-input-zone input[type=file] {
            position: absolute;
            width: 100%;
            height: 100%;
            opacity: 0;
            cursor: pointer;
        }
</style>

<div class="file-input-zone">
    <InputFile />
    Get me a file!
</div>

It will give you a button that looks like this:

enter image description here

You might pick up more tips by studying his source code further.

Related