butang CSS
Butang CSS
Dalam bab ini kami akan memperkenalkan anda menggunakan CSS untuk membuat butang.
Mari kita lihat dahulu pada butang lalai dan butang yang dibuat dengan css
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } </style> </head> <body> <h2>CSS 按钮</h2> <button>默认按钮</button> <a href="#" class="button">链接按钮</a> <button class="button">按钮</button> <input type="button" class="button" value="输入框按钮"> </body> </html>
Jalankan program dan lihat
Warna butang
Kita boleh menggunakan atribut warna latar belakang untuk menetapkan warna butang:
Instance
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .button2 {background-color: #008CBA;} /* Blue */ .button3 {background-color: #f44336;} /* Red */ .button4 {background-color: #e7e7e7; color: black;} /* Gray */ .button5 {background-color: #555555;} /* Black */ </style> </head> <body> <h2>按钮颜色</h2> <p>我们可以使用 background-color 属性来设置按钮颜色:</p> <button class="button">Green</button> <button class="button button2">Blue</button> <button class="button button3">Red</button> <button class="button button4">Gray</button> <button class="button button5">Black</button> </body> </html>
Jalankan Lihatlah program ini
Saiz butang
Kita boleh menggunakan saiz fon atribut untuk menetapkan saiz butang:
Contoh
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .button1 {font-size: 10px;} .button2 {font-size: 12px;} .button3 {font-size: 16px;} .button4 {font-size: 20px;} .button5 {font-size: 24px;} </style> </head> <body> <h2>按钮大小</h2> <p>我们可以使用 font-size 属性来设置按钮大小:</p> <button class="button button1">10px</button> <button class="button button2">12px</button> <button class="button button3">16px</button> <button class="button button4">20px</button> <button class="button button5">24px</button> </body> </html>
Jalankan program dan lihat
Butang Bulat
Kita boleh menggunakan atribut jejari sempadan untuk menetapkan butang bulat:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .button1 {border-radius: 2px;} .button2 {border-radius: 4px;} .button3 {border-radius: 8px;} .button4 {border-radius: 12px;} .button5 {border-radius: 50%;} </style> </head> <body> <h2>圆角按钮</h2> <p>我们可以使用 border-radius 属性来设置圆角按钮:</p> <button class="button button1">2px</button> <button class="button button2">4px</button> <button class="button button3">8px</button> <button class="button button4">12px</button> <button class="button button5">50%</button> </body> </html>
Jalankan program dan lihat
Warna sempadan butang
Kita boleh menggunakan atribut sempadan untuk menetapkan warna sempadan butang:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .button1 { background-color: white; color: black; border: 2px solid #4CAF50; } .button2 { background-color: white; color: black; border: 2px solid #008CBA; } .button3 { background-color: white; color: black; border: 2px solid #f44336; } .button4 { background-color: white; color: black; border: 2px solid #e7e7e7; } .button5 { background-color: white; color: black; border: 2px solid #555555; } </style> </head> <body> <h2>按钮边框颜色</h2> <p>我们可以使用 border 属性设置按钮边框颜色:</p> <button class="button button1">Green</button> <button class="button button2">Blue</button> <button class="button button3">Red</button> <button class="button button4">Gray</button> <button class="button button5">Black</button> </body> </html>
Jalankan program dan lihat
Butang tetikus
Kita boleh menggunakan pemilih :hover untuk mengubah suai gaya butang tuding tetikus.
Petua: Kita boleh menggunakan atribut tempoh peralihan untuk menetapkan kelajuan kesan "legar":
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 16px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; -webkit-transition-duration: 0.4s; /* Safari */ transition-duration: 0.4s; cursor: pointer; } .button1 { background-color: white; color: black; border: 2px solid #4CAF50; } .button1:hover { background-color: #4CAF50; color: white; } .button2 { background-color: white; color: black; border: 2px solid #008CBA; } .button2:hover { background-color: #008CBA; color: white; } .button3 { background-color: white; color: black; border: 2px solid #f44336; } .button3:hover { background-color: #f44336; color: white; } .button4 { background-color: white; color: black; border: 2px solid #e7e7e7; } .button4:hover {background-color: #e7e7e7;} .button5 { background-color: white; color: black; border: 2px solid #555555; } .button5:hover { background-color: #555555; color: white; } </style> </head> <body> <h2>鼠标悬停按钮</h2> <button class="button button1">Green</button> <button class="button button2">Blue</button> <button class="button button3">Red</button> <button class="button button4">Gray</button> <button class="button button5">Black</button> </body> </html>
Jalankan program dan lihat
Bayang Butang
Tunjukkan butang Bayang bayang demi tetikus
Kita boleh menggunakan atribut box-shadow untuk menambah bayang pada butang:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; -webkit-transition-duration: 0.4s; /* Safari */ transition-duration: 0.4s; } .button1 { box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2), 0 6px 20px 0 rgba(0,0,0,0.19); } .button2:hover { box-shadow: 0 12px 16px 0 rgba(0,0,0,0.24),0 17px 50px 0 rgba(0,0,0,0.19); } </style> </head> <body> <h2>按钮阴影</h2> <button class="button button1">阴影按钮</button> <button class="button button2">鼠标悬停后出现阴影</button> </body> </html>
Jalankan program dan lihat
Lumpuhkan butang
Kita boleh menggunakan atribut kelegapan untuk menambah ketelusan pada butang (kelihatan seperti kesan atribut "kurang upaya").
Petua: Kami boleh menambah atribut kursor dan menetapkannya kepada "tidak dibenarkan" untuk menetapkan imej yang dilumpuhkan:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .disabled { opacity: 0.6; cursor: not-allowed; } </style> </head> <body> <h2>禁用按钮</h2> <button class="button">正常按钮</button> <button class="button disabled">禁用按钮</button> </body> </html>
Jalankan program Lihatlah
Lebar Butang
Saiz lalai bagi butang Ditentukan oleh kandungan teks pada butang (panjang sepadan berdasarkan kandungan teks). Kita boleh menggunakan atribut lebar untuk menetapkan lebar butang:
Petua: Jika anda ingin menetapkan lebar tetap, anda boleh menggunakan piksel (px) sebagai unit, jika anda ingin menetapkan butang responsif, anda boleh menetapkannya sebagai peratusan .
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; margin: 4px 2px; cursor: pointer; } .button1 {width: 250px;} .button2 {width: 50%;} .button3 { padding-left: 0; padding-right: 0; width: 100%; } </style> </head> <body> <h2>按钮宽度</h2> <button class="button button1">250px</button><br> <button class="button button2">50%</button><br> <button class="button button3">100%</button> </body> </html>
Jalankan program dan lihat
Kumpulan butang
Alih keluar jidar dan tambah float:left untuk menetapkan kumpulan butang:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: none; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; cursor: pointer; float: left; } .button:hover { background-color: #3e8e41; } </style> </head> <body> <h2>按钮组</h2> <p>移除外边距并添加 float:left 来设置按钮组:</p> <button class="button">Button</button> <button class="button">Button</button> <button class="button">Button</button> <button class="button">Button</button> <p style="clear:both"><br>记住要清除浮动,否则下一个 p 元素的按钮也会显示在同一行。</p> </body> </html>
Jalankan program dan lihat
Kumpulan butang bersempadan
Kita boleh menggunakan atribut sempadan untuk menetapkan kumpulan butang bersempadan:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { background-color: #4CAF50; /* Green */ border: 1px solid green; color: white; padding: 15px 32px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; cursor: pointer; float: left; } .button:hover { background-color: #3e8e41; } </style> </head> <body> <h2>带边框按钮组</h2> <p>Add borders to create a bordered button group:</p> <button class="button">Button</button> <button class="button">Button</button> <button class="button">Button</button> <button class="button">Button</button> <p style="clear:both"><br>记住要清除浮动,否则下一个 p 元素的按钮也会显示在同一行。</p> </body> </html>
Jalankan program untuk mengambil lihat
Animasi butang
Tambah anak panah selepas menggerakkan tetikus ke atas butang Tandakan:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { display: inline-block; border-radius: 4px; background-color: #f4511e; border: none; color: #FFFFFF; text-align: center; font-size: 28px; padding: 20px; width: 200px; transition: all 0.5s; cursor: pointer; margin: 5px; } .button span { cursor: pointer; display: inline-block; position: relative; transition: 0.5s; } .button span:after { content: '»'; position: absolute; opacity: 0; top: 0; right: -20px; transition: 0.5s; } .button:hover span { padding-right: 25px; } .button:hover span:after { opacity: 1; right: 0; } </style> </head> <body> <h2>按钮动画</h2> <button class="button" style="vertical-align:middle"><span>Hover </span></button> </body> </html>
Jalankan program dan lihat
Klik Tambah kesan "riak":
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { position: relative; background-color: #4CAF50; border: none; font-size: 28px; color: #FFFFFF; padding: 20px; width: 200px; text-align: center; -webkit-transition-duration: 0.4s; /* Safari */ transition-duration: 0.4s; text-decoration: none; overflow: hidden; cursor: pointer; } .button:after { content: ""; background: #90EE90; display: block; position: absolute; padding-top: 300%; padding-left: 350%; margin-left: -20px!important; margin-top: -120%; opacity: 0; transition: all 0.8s } .button:active:after { padding: 0; margin: 0; opacity: 1; transition: 0s } </style> </head> <body> <h2>按钮动画 - 波纹效果</h2> <button class="button">Click Me</button> </body> </html>
Jalankan program dan lihat
Tambah kesan "tekan ke bawah" apabila diklik:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文网(php.cn)</title> <style> .button { display: inline-block; padding: 15px 25px; font-size: 24px; cursor: pointer; text-align: center; text-decoration: none; outline: none; color: #fff; background-color: #4CAF50; border: none; border-radius: 15px; box-shadow: 0 9px #999; } .button:hover {background-color: #3e8e41} .button:active { background-color: #3e8e41; box-shadow: 0 5px #666; transform: translateY(4px); } </style> </head> <body> <h2>按钮动画 - "按压效果"</h2> <button class="button">Click Me</button> </body> </html>
Jalankan program dan lihat