Search code examples
c#blazorblazorinputfile

Custom InputFile in Blazor


I want to replace the InputFile's rectangle with 'attach Icon'. I tried to set the URL of the icon to 'background-image' of InputFile but it had no effect. This only demonstrates how to change the color of InputFile, not exactly what I need.


Solution

  • I think maybe this is what you are looking for.

    HTML/Razor code

    <div class="file-input-zone">
        <InputFile />
    </div>
    

    CSS

    <style>
    .file-input-zone {
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: transparent;
        color: black;
        cursor: pointer;
        position: relative;
        width: 120px;
        height: 120px;
        background-image: url('paper-clip.png');
    }
    
        .file-input-zone:hover {
            background-color: lightblue;
        }
    
        .file-input-zone input[type=file] {
            position: absolute;
            width: 100%;
            height: 100%;
            opacity: 0;
            cursor: pointer;
        }
    </style>
    

    In the CSS code above, the file paper-clip.png is installed in wwwroot directory.

    The button will look like a transparent paper-clip. In the image below, color changes on hover as well.

    enter image description here