دکمه (button)
تگ «دکمه (Button)» یک دکمه کلیکی می سازد. داخلش می توانی متن و حتی تگ ها بگذاری. برعکس «ورودی (Input)»، محتوا می پذیرد. همیشه نوع دکمه را با ویژگی type مشخص کن.
تعریف و کاربرد دکمه
<button> یک دکمه قابل کلیک است. می توانی تگ هایی مثل <i>، <b>، <strong>، <br> یا <img> داخلش بگذاری. برای دکمه متنی ساده هم از تگ input کمک بگیر، اما یادت باشد محتوای HTML نمی گیرد.
نمونه سریع: یک دکمه کلیکی
<button type="button">Click Me!</button>
ویژگی های مهم button
type: یکی ازbutton،submit،reset.disabled: دکمه را غیرفعال می کند.autofocus: هنگام لود، فوکوس می گیرد.form: دکمه را به یک فرم مشخص وصل می کند.formaction،formmethod،formenctype,formtarget: فقط برایtype="submit".formnovalidate: اعتبارسنجی فرم را رد می کند.nameوvalue: نام و مقدار دکمه.popovertargetوpopovertargetaction: کنترل یک پاپ اور.
استایل دهی دکمه با CSS
<!DOCTYPE html>
<html>
<head>
<style>
.button {
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: #04AA6D;
}
.button2 {
background-color: #008CBA;
}
</style>
</head>
<body>
<button class="button button1">Green</button>
<button class="button button2">Blue</button>
</body>
</html>
افکت هاور برای دکمه
<!DOCTYPE html>
<html>
<head>
<style>
.button {
border: none;
color: white;
padding: 16px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
transition-duration: 0.4s;
cursor: pointer;
}
.button1 {
background-color: white;
color: black;
border: 2px solid #04AA6D;
}
.button1:hover {
background-color: #04AA6D;
color: white;
}
.button2 {
background-color: white;
color: black;
border: 2px solid #008CBA;
}
.button2:hover {
background-color: #008CBA;
color: white;
}
</style>
</head>
<body>
<button class="button button1">Green</button>
<button class="button button2">Blue</button>
</body>
</html>
گام های عملی ساخت دکمه
- یک تگ
<button>بساز و متن بگذار. - ویژگی
typeرا تعیین کن. - با کلاس های CSS، ظاهر دکمه را تنظیم کن.
پشتیبانی و نکته مهم
تمام مرورگرهای اصلی از <button> پشتیبانی می کنند. نکته: همیشه type را صریح بگذار تا رفتار فرم اشتباه نشود. درباره فرم ها صفحه فرم ها را ببین.
جمع بندی سریع
<button>محتوا و تگ داخلی می پذیرد.typeرا همیشه مشخص کن.- برای
submit، ویژگی های فرم را می توانی تنظیم کنی. - استایل را کامل با CSS کنترل کن.