LoginSignup
15
21

More than 3 years have passed since last update.

[Rails][file_field]画像のアップロードのボタンデザインを変更する

Posted at

Railsのアプリを作っている際に画像アップロードボタンのデザインを変えたかったが、
クラス名をつけるだけでは上手くいかなかったのでメモ。

初めの状態

普通にコードを書いた場合はこのようなデザインになるかと思います

        = p.file_field :image_file

 2019-12-26 11.40.24.png

もっとかっこいいデザインにしたいので、変更していきます。

変更するとき

file_field以外にlabelを追加します。

= p.label :image_file, "画像をアップロード", class: "image_input_btn"
        = p.file_field :image_file, class: "image_form"

どちらにもクラス名を追記します。

そして、CSSで

   .image_input_btn{
        display: block;
        background-color: #228B22;
        width: 180px;
        height: 30px;
        margin-top: 10px;
        margin-left: 10px;
        border-radius: 5px;
        padding-top: 2px;
        color: white;
        text-decoration: none;
        text-align: center;
      }

      .image_form{
        display: none;
      }

file_fieldのクラス名は非表示にし、labelに装飾していきます。

このように書くと

 2019-12-26 15.13.26.png

このようなボタンが出来上がります。

少しかっこよくなりました!!

15
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
21