近年、WebサイトのUIデザインはますますインタラクティブで魅力的なものになっています。
しかし、インタラクティブな動作を実現するにはJavaScriptが必須だと思っていませんか?
実は、CSSだけで十分に実現できることも多いのです!
この記事では、CSSだけで作れるいくつかのインタラクティブなUIデザインを紹介します。
コードをコピペするだけで使えるので、ぜひ試してみてください!
1. ホバーで動くカード
カードのホバー効果は、よく使われるデザインの1つです。CSSだけで、カードがふわっと浮き上がるアニメーションを作ることができます。
Hover Me!
This is an interactive card.
1 2 3 4 | <div class = "hover-card" > <h3>Hover Me!</h3> <p>This is an interactive card.</p> </div> |
1 2 3 4 5 6 7 8 9 10 11 12 13 | .hover-card { width : 300px ; padding : 20px ; border-radius : 10px ; background : white ; box-shadow : 0 4px 10px rgba ( 0 , 0 , 0 , 0.1 ); transition : transform 0.3 s ease, box-shadow 0.3 s ease; } .hover-card:hover { transform : translateY ( -10px ); box-shadow : 0 8px 20px rgba ( 0 , 0 , 0 , 0.2 ); } |
2. タブ切り替え
タブの切り替えは、ナビゲーションやコンテンツ整理によく使われるパターンです。CSSの :checked 擬似クラスを使えば、JavaScriptなしで実現できます。
1 2 3 4 5 6 7 8 | <div class = "tabs-horizontal" > <input type= "radio" id= "tab1" name= "tab" checked> <label for = "tab1" >Tab 1</label> <input type= "radio" id= "tab2" name= "tab" > <label for = "tab2" >Tab 2</label> <div class = "content" id= "content1" >This is the content for Tab 1.</div> <div class = "content" id= "content2" >This is the content for Tab 2.</div> </div> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | /* タブ全体のスタイル */ .tabs-horizontal { margin : 0 auto ; } /* 横並びのタブ */ .tabs-horizontal label { display : inline-block ; width : 50% ; /* 各タブを均等に */ padding : 10px 20px ; text-align : center ; cursor : pointer ; background : #f0f0f0 ; border : 2px solid #ddd ; font-weight : bold ; transition : background 0.3 s; border-radius : 5px 5px 0px 0px ; padding : 10px 20px 10px 20px !important ; border-bottom : initial; } /* ラジオボタンを非表示 */ .tabs-horizontal input[type= "radio" ] { display : none ; } /* 選択されたタブのスタイル */ .tabs-horizontal input[type= "radio" ]:checked + label { background : #cf5f5f ; border : 2px solid #cf5f5f ; border-bottom : initial; } /* コンテンツのデフォルトは非表示 */ .tabs-horizontal .content { display : none ; padding : 20px ; background : #fafafa ; border : 1px solid #ddd ; border-top : none ; } /* 選択されたタブに対応するコンテンツを表示 */ #tab 1: checked ~ #content 1 , #tab 2: checked ~ #content 2 { display : block ; } |
3. アコーディオンメニュー
コンテンツを縦に展開・収納できるアコーディオンメニューは、動きのあるUIの定番です。
1 2 3 4 5 6 7 8 9 | <div class = "accordion" > <input type= "checkbox" id= "section1" > <label for = "section1" >Section 1</label> <div class = "content" >This is the content for Section 1.</div> <input type= "checkbox" id= "section2" > <label for = "section2" >Section 2</label> <div class = "content" >This is the content for Section 2.</div> </div> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | .accordion input[type= "checkbox" ] { display : none ; } .accordion label { display : block ; padding : 10px ; background : #6a11cb ; color : white ; border : 1px solid #ddd ; cursor : pointer ; } .accordion .content { max-height : 0 ; overflow : hidden ; transition : max-height 0.3 s ease; background : #f9f9f9 ; padding : 0 10px ; } .accordion input[type= "checkbox" ]:checked + label + .content { max-height : 100px ; } |
4. ローディングスピナー
Webアプリでよく見かけるローディングスピナーも、CSSだけで簡単に作れます。
1 | <div class = "spinner" ></div> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | .spinner { width : 40px ; height : 40px ; border : 4px solid #f3f3f3 ; border-top : 4px solid #6a11cb ; border-radius : 50% ; animation : spin 1 s linear infinite ; } @keyframes spin { from { transform : rotate ( 0 deg); } to { transform : rotate ( 360 deg); } } |
CSSだけでここまでのインタラクティブなUIを実現できることに驚きませんか?
JavaScript不要で軽量かつ簡単に実装できるのが魅力です。
今回紹介したコードはすべてコピペしてそのまま使えます。
デザインをカスタマイズして、自分のWebサイトにぜひ活用してください!