Change file_field_tag appearance to button appearance
Asked Answered
M

3

5

I am working on a Rails project and I want to use file_field_tag but I'd like it looks like a button.

I have this:

enter image description here

with this code:

= file_field_tag 'attachment'

I want something like this:

enter image description here

and I attempted this:

= file_field_tag 'attachment', class: 'btn btn-large btn-warning'

but I got this:

enter image description here

How can I change the file_field_tag's appearance in order to achieve it looks like a button?

Mulkey answered 10/8, 2015 at 17:39 Comment(0)
I
8

HTML:

<span class="btn btn-large btn-warning btn-file">
    Choose File
    <%= file_field_tag :attachment %>
</span>

CSS:

.btn-file {
    position: relative;
    overflow: hidden;
}

.btn-file input[type=file] {
    position: absolute;
    top: 0;
    right: 0;
    min-width: 100%;
    min-height: 100%;
    font-size: 100px;
    text-align: right;
    filter: alpha(opacity=0);
    opacity: 0;
    outline: none;
    background: white;
    cursor: inherit;
    display: block;
}
Inn answered 10/8, 2015 at 17:54 Comment(1)
@IvánCortésRomero Glad I could help!Inn
S
0

the best way to do this is to do a transparent field using css on top of another style button. Add a div with a id and a div class wrapper on the css add the style for the button and hide the file_field_tag.

<div id="id_attachment">
   <div class="hide_attachment">
     <%= file_field_tag 'attachment'%>
   </div>
</div>
Silurian answered 10/8, 2015 at 18:13 Comment(0)
S
0

for tailwind users, this worked for me.

<label class="relative flex justify-center w-full px-4 py-2 overflow-hidden text-sm font-medium text-white bg-blue-600 border border-transparent rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 hover:bg-blue-700">
 Upload logo
 <%= f.file_field :main_image, class:"absolute top-0 right-0 opacity-0 cursor-pointer min-h-full min-w-full" %>
</label>
Spiky answered 26/5, 2021 at 11:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.