WordPressのショートコードを使って光るCSS ボタンの作り方と応用デザインを丁寧に解説

光るCSS ボタンの作り方

本サイト内のコンテンツはNORILOGが独自に制作しています。メーカー等から商品提供を受けることもありますが、その場合は記事内に明記しております。コンテンツの内容や評価に関するランキングの決定には一切関与していません。メーカー等の指示による表示部分にはPRを表記します。記事内で紹介する商品を購入することで、当サイトに売り上げの一部が還元されることがあります。詳しくはサイト運営ポリシーをご覧ください。

カスタマイズおすすめ度:[star rating=”100″ type=”percent”]

こんにちは、NORI(@norilog4)です。他のブログ記事などで、光るボタンを見たことありませんか?この光るボタンをどうやって作っているのかわからない人も多いと思います。

この記事では、よくブログで見かける光るボタンの作り方を紹介します。ソースコードはコピペOK!簡単に導入できます。

SANGOのテンプレートを意識してデフォルトのボタンに色を合わせた形の、光るボタンにCSSを設定しています。

NORI

簡単に実装できると思いますのでチャレンジしてみてください。
MEMO

この記事で紹介している内容は、WordPress(ワードプレス)のテーマ、Sango(サンゴ)やPORIPU(ポリプ)に対応しています。

この記事を書いた人

アニメーションで光るボタンのサンプル

この記事を読み終わると、以下のような光るアニメーションがついたボタンが作れます。ショートコードを使って実装しているので、AddQuicktagなどに登録しておくと記事を書くスピードもあがります。

[redshiny] ヘッダー画像テンプレートダウンロード[/redshiny] [yellowshiny] 商品をカートへ追加[/yellowshiny] [blueshiny] メッセンジャーでメッセージを送る[/blueshiny] [greenshiny] 植樹を行う(緑の募金サイト)[/greenshiny]

上のボタンはこちらのソースコードで表示されています。

赤色のボタン
[redshiny]<a href="●" target="_blank" rel="noopener noreferrer"><i class="fas fa-download"></i> ヘッダー画像テンプレートダウンロード</a>[/redshiny]
黄色のボタン
[yellowshiny]<a href="●" target="_blank" rel="noopener noreferrer"><i class="fas fa-cart-arrow-down"></i> 商品をカートへ追加</a>[/yellowshiny]
青色のボタン
[blueshiny]<a href="●" target="_blank" rel="noopener noreferrer"><i class="fab fa-facebook-messenger"></i> メッセンジャーでメッセージを送る</a>[/blueshiny]
緑色のボタン
[greenshiny]<a href="●" target="_blank" rel="noopener noreferrer"><i class="fas fa-tree"></i> 植樹を行う(緑の募金サイト)</a>[/greenshiny]

アニメーションで光るボタンを作るまでの流れ

この光るボタンが完成するまでの手順です。

STEP.1
CSSをコピペ
このボタンに必要なCSSをコピペして自分のブログのテーマ(子テーマ)のCSSに貼り付けて保存
STEP.2
function.phpへ追加
ショートコードで実装するためfunction.phpへコードの追加が必要です。WordPressの functions.php を安全に管理・追加できるプライグイン「Code Snippets」を使って実装します
STEP.3
ショートコードを貼り付け
投稿記事や固定ページにショートコードを貼り付けてテストを行う。
STEP.4
アイコン追加の応用
ボタンにアイコンを追加して表示する方法を伝授!
STEP.4
完成
これでいつでも光るボタンが使えます!

アニメーションで光るボタンの作り方

以下の手順を順番を追って行っていくことで、最終的には光るボタンを実装作ることができます。できない場合には貼り付けたコードが間違っているなどの可能性があるので、よく見直してみてください。

光るボタンのCSSをコピー&ペースト

以下のソースコードをまるっと全部コピーして、自分のブログのテーマ(子テーマ)のCSSに貼り付けて保存しましょう。これでCSSの設定は完了です。

/************************************
** 光る赤いボタン
************************************/
.red-shiny a {
display: block;
position: relative;
z-index: 2;
overflow: hidden;
width: 100%;
font-weight: bold ;
border-bottom: solid 5px #bd6060 ;
border-radius: 5px;
background-color: #f88080 ;
color: #ffffff !important;
padding: 12px 0;
text-align: center;
text-decoration: none;
transition: .25s linear;
-webkit-transition: .25s linear;
-moz-transition: .25s linear;
}
.red-shiny {
display: block;
width: 100%;
margin-bottom: 1em;
}
.red-shiny a:hover {
-ms-transform: translateY(4px);
-webkit-transform: translateY(4px);
transform: translateY(4px);/*下に動く*/
border-bottom: none;/*線を消す*/
}
.red-shiny:active {
border-bottom: none;
box-shadow: 0 0 1px rgba(0, 0, 0, 0.2); /*影を小さく*/
-webkit-transform: translateY(4px);
-ms-transform: translateY(4px);
transform: translateY(4px); /*下に動く*/
}
.red-shiny a:before {
display: block;
position: absolute;
z-index: -1;
left: -30%;
top: -50%;
content: "";
width: 100px;
height: 100px;
transform: rotate(20deg);
-webkit-transform: rotate(20deg);
-moz-transform: rotate(20deg);
background-image: linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 50%,rgba(255,255,255,0) 100%);
background-image: -webkit-gradient(linear, left bottom, right bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(67%,rgba(255,255,255,1)),color-stop(100%,rgba(255,255,255,0)));
background-image: -moz-linear-gradient(left, rgba(255,255,255,0) 0%, #ffffff rgba(255,255,255,1),rgba(255,255,255,0) 100%);
animation: shiny 3s infinite linear;
-webkit-animation: shiny 3s infinite linear;
-moz-animation: shiny 3s infinite linear;
}
@keyframes shiny {
0% { transform: scale(0) rotate(45deg); opacity: 0; }
80% { transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { transform: scale(4) rotate(45deg); opacity: 1; }
100% { transform: scale(50) rotate(45deg); opacity: 0; }
}
@-webkit-keyframes shiny {
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; }
}
@-moz-keyframes shiny {
0% { -moz-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -moz-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -moz-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -moz-transform: scale(50) rotate(45deg); opacity: 0; }
}
@-ms-keyframes shiny {
0% { -ms-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -ms-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -ms-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -ms-transform: scale(50) rotate(45deg); opacity: 0; }
}
@-o-keyframes shiny {
0% { -o-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -o-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -o-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -o-transform: scale(50) rotate(45deg); opacity: 0; }
}
/************************************
** 光る黄色いボタン
************************************/
.yellow-shiny a {
display: block;
position: relative;
z-index: 2;
overflow: hidden;
width: 100%;
font-weight: bold ;
border-bottom: solid 5px #bf9d1e ;
border-radius: 5px;
background-color: #fad02d ;
color: #ffffff !important;
padding: 12px 0;
text-align: center;
text-decoration: none;
transition: .25s linear;
-webkit-transition: .25s linear;
-moz-transition: .25s linear;
}
.yellow-shiny {
display: block;
width: 100%;
margin-bottom: 1em;
}
.yellow-shiny a:hover {
-ms-transform: translateY(4px);
-webkit-transform: translateY(4px);
transform: translateY(4px);/*下に動く*/
border-bottom: none;/*線を消す*/
}
.yellow-shiny:active {
border-bottom: none;
box-shadow: 0 0 1px rgba(0, 0, 0, 0.2); /*影を小さく*/
-webkit-transform: translateY(4px);
-ms-transform: translateY(4px);
transform: translateY(4px); /*下に動く*/
}
.yellow-shiny a:before {
display: block;
position: absolute;
z-index: -1;
left: -30%;
top: -50%;
content: "";
width: 100px;
height: 100px;
transform: rotate(20deg);
-webkit-transform: rotate(20deg);
-moz-transform: rotate(20deg);
background-image: linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 50%,rgba(255,255,255,0) 100%);
background-image: -webkit-gradient(linear, left bottom, right bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(67%,rgba(255,255,255,1)),color-stop(100%,rgba(255,255,255,0)));
background-image: -moz-linear-gradient(left, rgba(255,255,255,0) 0%, #ffffff rgba(255,255,255,1),rgba(255,255,255,0) 100%);
animation: shiny 3s infinite linear;
-webkit-animation: shiny 3s infinite linear;
-moz-animation: shiny 3s infinite linear;
}
@keyframes shiny {
0% { transform: scale(0) rotate(45deg); opacity: 0; }
80% { transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { transform: scale(4) rotate(45deg); opacity: 1; }
100% { transform: scale(50) rotate(45deg); opacity: 0; }
}
@-webkit-keyframes shiny {
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; }
}
@-moz-keyframes shiny {
0% { -moz-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -moz-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -moz-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -moz-transform: scale(50) rotate(45deg); opacity: 0; }
}
@-ms-keyframes shiny {
0% { -ms-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -ms-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -ms-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -ms-transform: scale(50) rotate(45deg); opacity: 0; }
}
@-o-keyframes shiny {
0% { -o-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -o-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -o-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -o-transform: scale(50) rotate(45deg); opacity: 0; }
}
/************************************
** 光る青いボタン
************************************/
.blue-shiny a {
display: block;
position: relative;
z-index: 2;
overflow: hidden;
width: 100%;
font-weight: bold ;
border-bottom: solid 5px #3a76ba ;
border-radius: 5px;
background-color: #4f9df4 ;
color: #ffffff !important;
padding: 12px 0;
text-align: center;
text-decoration: none;
transition: .25s linear;
-webkit-transition: .25s linear;
-moz-transition: .25s linear;
}
.blue-shiny {
display: block;
width: 100%;
margin-bottom: 1em;
}
.blue-shiny a:hover {
-ms-transform: translateY(4px);
-webkit-transform: translateY(4px);
transform: translateY(4px);/*下に動く*/
border-bottom: none;/*線を消す*/
}
.blue-shiny:active {
border-bottom: none;
box-shadow: 0 0 1px rgba(0, 0, 0, 0.2); /*影を小さく*/
-webkit-transform: translateY(4px);
-ms-transform: translateY(4px);
transform: translateY(4px); /*下に動く*/
}
.blue-shiny a:before {
display: block;
position: absolute;
z-index: -1;
left: -30%;
top: -50%;
content: "";
width: 100px;
height: 100px;
transform: rotate(20deg);
-webkit-transform: rotate(20deg);
-moz-transform: rotate(20deg);
background-image: linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 50%,rgba(255,255,255,0) 100%);
background-image: -webkit-gradient(linear, left bottom, right bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(67%,rgba(255,255,255,1)),color-stop(100%,rgba(255,255,255,0)));
background-image: -moz-linear-gradient(left, rgba(255,255,255,0) 0%, #ffffff rgba(255,255,255,1),rgba(255,255,255,0) 100%);
animation: shiny 3s infinite linear;
-webkit-animation: shiny 3s infinite linear;
-moz-animation: shiny 3s infinite linear;
}
@keyframes shiny {
0% { transform: scale(0) rotate(45deg); opacity: 0; }
80% { transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { transform: scale(4) rotate(45deg); opacity: 1; }
100% { transform: scale(50) rotate(45deg); opacity: 0; }
}
@-webkit-keyframes shiny {
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; }
}
@-moz-keyframes shiny {
0% { -moz-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -moz-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -moz-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -moz-transform: scale(50) rotate(45deg); opacity: 0; }
}
@-ms-keyframes shiny {
0% { -ms-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -ms-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -ms-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -ms-transform: scale(50) rotate(45deg); opacity: 0; }
}
@-o-keyframes shiny {
0% { -o-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -o-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -o-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -o-transform: scale(50) rotate(45deg); opacity: 0; }
}
/************************************
** 光る緑色のボタン
************************************/
.green-shiny a {
display: block;
position: relative;
z-index: 2;
overflow: hidden;
width: 100%;
font-weight: bold ;
border-bottom: solid 5px #6ca25f ;
border-radius: 5px;
background-color: #90d581 ;
color: #ffffff !important;
padding: 12px 0;
text-align: center;
text-decoration: none;
transition: .25s linear;
-webkit-transition: .25s linear;
-moz-transition: .25s linear;
}
.green-shiny {
display: block;
width: 100%;
margin-bottom: 1em;
}
.green-shiny a:hover {
-ms-transform: translateY(4px);
-webkit-transform: translateY(4px);
transform: translateY(4px);/*下に動く*/
border-bottom: none;/*線を消す*/
}
.green-shiny:active {
border-bottom: none;
box-shadow: 0 0 1px rgba(0, 0, 0, 0.2); /*影を小さく*/
-webkit-transform: translateY(4px);
-ms-transform: translateY(4px);
transform: translateY(4px); /*下に動く*/
}
.green-shiny a:before {
display: block;
position: absolute;
z-index: -1;
left: -30%;
top: -50%;
content: "";
width: 100px;
height: 100px;
transform: rotate(20deg);
-webkit-transform: rotate(20deg);
-moz-transform: rotate(20deg);
background-image: linear-gradient(left, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 50%,rgba(255,255,255,0) 100%);
background-image: -webkit-gradient(linear, left bottom, right bottom, color-stop(0%,rgba(255,255,255,0)), color-stop(67%,rgba(255,255,255,1)),color-stop(100%,rgba(255,255,255,0)));
background-image: -moz-linear-gradient(left, rgba(255,255,255,0) 0%, #ffffff rgba(255,255,255,1),rgba(255,255,255,0) 100%);
animation: shiny 3s infinite linear;
-webkit-animation: shiny 3s infinite linear;
-moz-animation: shiny 3s infinite linear;
}
@keyframes shiny {
0% { transform: scale(0) rotate(45deg); opacity: 0; }
80% { transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { transform: scale(4) rotate(45deg); opacity: 1; }
100% { transform: scale(50) rotate(45deg); opacity: 0; }
}
@-webkit-keyframes shiny {
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; }
}
@-moz-keyframes shiny {
0% { -moz-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -moz-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -moz-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -moz-transform: scale(50) rotate(45deg); opacity: 0; }
}
@-ms-keyframes shiny {
0% { -ms-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -ms-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -ms-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -ms-transform: scale(50) rotate(45deg); opacity: 0; }
}
@-o-keyframes shiny {
0% { -o-transform: scale(0) rotate(45deg); opacity: 0; }
80% { -o-transform: scale(0) rotate(45deg); opacity: 0.5; }
81% { -o-transform: scale(4) rotate(45deg); opacity: 1; }
100% { -o-transform: scale(50) rotate(45deg); opacity: 0; }
}

function.phpへコードを追加

8年間、WordPressをいじってますが function.php を触っては画面を真っ白(エラー)にして来た経験から現在は functions.php を触る際には、安全に管理・追加できるプライグイン「Code Snippets」を使って実装しています。

WordPress おすすめプラグイン code snippets 設定方法 WordPressの functions.php を安全に管理・追加できるプライグイン「Code Snippets」

Code Snippetsへのコード追記方法

Code Snippets(コードスニペット)へは以下の動画の通り、以下で紹介するコードを赤色・黄色・青色・緑色それぞれ追加していきます。

ソースコードを追加後に Save Changes and Activate というボタンをクリック後、「Snippet added and activated.」と表示されると登録した色のボタンが、ショートコードで記事へ埋め込みすることが可能となります。

上の動画を参考にしたのコードをそれぞれ追加していきます。

赤色ボタンのショートコードを追加

/*赤い光るボタンをショートコード化*/
function ShinyBtn( $atts, $content = null ) {
return '<div class="red-shiny">' . $content . '</div>';
}
add_shortcode('redshiny', 'ShinyBtn');
view raw functions.php hosted with ❤ by GitHub

黄色ボタンのショートコードを追加

/*黄色い光るボタンをショートコード化*/
function shinyyellow( $atts, $content = null ) {
return '<div class="yellow-shiny">' . $content . '</div>';
}
add_shortcode('yellowshiny', 'shinyyellow');
view raw functions.php hosted with ❤ by GitHub

青色ボタンのショートコードを追加

/*青い光るボタンをショートコード化*/
function shinyblue( $atts, $content = null ) {
return '<div class="blue-shiny">' . $content . '</div>';
}
add_shortcode('blueshiny', 'shinyblue');
view raw functions.php hosted with ❤ by GitHub

緑色ボタンのショートコードを追加

/*緑の光るボタンをショートコード化*/
function shinygreen( $atts, $content = null ) {
return '<div class="green-shiny">' . $content . '</div>';
}
add_shortcode('greenshiny', 'shinygreen');
view raw functions.php hosted with ❤ by GitHub

Code Snippetsへ4つのコードが全て完了した状態

Code Snippets(コードスニペット)へ各ボタン 赤色・黄色・青色・緑色 をすべて完了すると、以下の画像のようになっていれば問題ありません。これで4色の光るボタンが利用できます。

ショートコードで光るボタンの作り方

ショートコードを貼り付けて動作確認

以下のソースコードを記事または固定ページに追加して動作テストをしてみてください。このブログと同じように光っていれば問題なく設定できています。

赤色のボタン
[redshiny]<a href="●" target="_blank" rel="noopener noreferrer"> ここにテキストを入力</a>[/redshiny]
黄色のボタン
[yellowshiny]<a href="●" target="_blank" rel="noopener noreferrer"> ここにテキストを入力</a>[/yellowshiny]
青色のボタン
[blueshiny]<a href="●" target="_blank" rel="noopener noreferrer"> ここにテキストを入力</a>[/blueshiny]
緑色のボタン
[greenshiny]<a href="●" target="_blank" rel="noopener noreferrer"> ここにテキストを入力</a>[/greenshiny]

赤色のボタン
[redshiny] ここにテキストを入力[/redshiny] 黄色のボタン
[yellowshiny] ここにテキストを入力[/yellowshiny] 青色のボタン
[blueshiny] ここにテキストを入力[/blueshiny] 緑色のボタン
[greenshiny] ここにテキストを入力[/greenshiny]

ボタンにアイコンを追加する方法【応用設定】

ボタンにアイコンを追加するには「Font Awsome(フォント オーサム)」が必要となります。

設定方法などについては以下の記事を参考にしてみてください。

【保存版】Font Awesomeの使い方:Webアイコンフォントを使おう

ボタンに入れるアイコンを選ぶ

今回はダウンロードするボタンを作って実装していきます。Font Awsome(フォント オーサム)の左上の検索ボックスに「download」と入力してエンターキーを押します。

Download(ダウンロード)に関連したアイコンが表示されるので、その中からアイコンを選んでクリックし i classから始まるコードをクリックしてコードをコピーします。

WordPress 光るボタン カスタマイズ アイコン追加

光るCSSアニメーションボタンにアイコンを追加

以下のようにテキストの前にコードを入れてあげます。

コード

[redshiny]<a target="_blank" rel="noopener noreferrer"><i class="fas fa-download"></i> ヘッダー画像テンプレートダウンロード</a>[/redshiny]

実際のボタン

[redshiny] ヘッダー画像テンプレートダウンロード[/redshiny]

これで光るボタンにアイコンの追記ができます。アイコンはそのボタンに合わせたものを設定することで、「何のボタン」なのかがよりわかりやすくなります。(クリック後のアクションが想像できる)

WordPressで光るCSSアニメーションボタンの作り方まとめ

本記事は、WordPressで光るアニメーションボタンの作り方の解説記事です。

通常のHTMLでも表示はできますが、ショートコードで実装するとタグがみやすいので記事を書く際にも非常に楽になります。

NORI

この記事をみて実装してみてね!

質問や回答はコメント欄、またはTwitterなどをフォローしてリプライでお知らせください。

以上、NORI(@norilog4)でした。

初心者からCSSを学ぶおすすめの書籍
1冊ですべて身につくHTML & CSSとWebデザイン入門講座

WordPress超入門 AddQuicktagの使い方 【プラグイン】AddQuicktag 記事装飾をワンクリックで簡単に!使い方と設定方法を解説

コメントの通知/購読設定
受け取る通知
guest


0 件のコメント
古い順
新しい順 評価順
Inline Feedbacks
全てのコメントを見る