今流行りのCSSで簡単にボタンを光らせる

CSSでボタンを光らせることができるのはご存じでしょうか?

ここにテキスト

このボタンを使用するとクリック率があがるみたいです。

色んなサイトでやり方は載ってますが自分がハマったポイントだけ説明します。

ちなみに今回はCSSで作ったボタンですが画像ファイルを使ったimgタグでも可能です。

やり方はcssとhtmlをそのままコピってテキストとか画像を変更すれば使えます笑

自分がハマったのは以下のkeyframesの名称とanimationの名称を合わせる必要があるというところにハマりました。(ここではreflectionの部分)ちゃんと隅から隅まで読まないとだめですね。

@keyframes reflection {
 transform: rotate(45deg);
    animation: reflection 2s ease-in-out infinite;
    -webkit-transform: rotate(45deg);
    -webkit-animation: reflection 2s ease-in-out infinite;

animationとかtransformとかしっかり理解して使いこなせるようになったら凄そう。。

時間があるときにまた調べてみようと思います。

1つ目のボタンのソースコード

HTML部分
<a href="リンク先のURL" class="shiny-btn1">ここにテキスト</a>
CSS部分
.shiny-btn1 {
    display: block;
    position: relative;
    width: 80%;/*ボタンの幅*/
    padding: 10px 0;
    margin: 30px auto;
    background-color: #ed4545;/*ボタンの色*/
    box-shadow: 0 3px 0 0 rgba(198, 39, 39, 1);/*影の色(rgbaの値を変更)*/
    border-radius: 5px;
    font-weight: bold;
    font-size: 18px;
    color: #fff !important;
    text-align: center;
    text-decoration: none;
    overflow: hidden;
}
.shiny-btn1:hover {
    text-decoration: none;
    color: #fff;
}
.shiny-btn1::before {
    position: absolute;
    content: '';
    display: inline-block;
    top: -180px;
    left: 0;
    width: 30px;
    height: 100%;
    background-color: #fff;
    animation: shiny-btn1 3s ease-in-out infinite;
}
@-webkit-keyframes shiny-btn1 {
    0% { -webkit-transform: scale(0) rotate(45deg); opacity: 0; }
    80% { -webkit-transform: scale(0) rotate(45deg); opacity: 0.5; }
    81% { -webkit-transform: scale(4) rotate(45deg); opacity: 1; }
    100% { -webkit-transform: scale(50) rotate(45deg); opacity: 0; }
}

2つ目の画像のソースコード

<div style="display: block; text-align: center; margin-top:-25px;">
  <a href="https://producewaves.com">
    <div class="btn shiny"><img src="https://producewaves.com/wp-content/uploads/2022/02/logo_sp-1.png" width="300"></div>
  </a>
</div>
.shiny {
    display: inline-block;
    position: relative;
    overflow: hidden;
}
.shiny::after {
    content: "";
    display: inline-block;
    height: 100%;
    width: 30px;
    position: absolute;
    top: -180px;
    left: 0;
    background-color: #fff;
    opacity: 0;
    transform: rotate(45deg);
    animation: reflection 2s ease-in-out infinite;
    -webkit-transform: rotate(45deg);
    -webkit-animation: reflection 2s ease-in-out infinite;
}

@keyframes reflection {
  0% { -webkit-transform: scale(0) rotate(45deg); opacity: 0; }
  80% { -webkit-transform: scale(0) rotate(45deg); opacity: 0.5; }
  81% { -webkit-transform: scale(4) rotate(45deg); opacity: 1; }
  100% { -webkit-transform: scale(50) rotate(45deg); opacity: 0; }
  }

関連記事

  1. python で slackbot

  2. HTML/CSS学習の初心者に警告!模写サイトに気を付けろ

  3. Java経験5年以上のエンジニアがRailsを触ってみた感想

  4. 【キャリアアップ】技術資産を作るという考え方

  5. djangoのbulkupdateを使うときにupdated_atが更…

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。