← Back to CSS Course | Chapter 8: Animations & Effects | Lesson 16 of 22

CSS Buttons

एक native browser button जिस पल आपके design में कोई personality आती है outdated दिखने लगता है। एक plain button या anchor tag को अपनी site से match करने वाली किसी चीज़ में बदलने में सिर्फ मुट्ठी भर properties लगती हैं।
Syntax
css
.button {
  background-color: color;
  color: color;
  padding: length;
  border: none;
  border-radius: length;
}
.button:hover {
  background-color: color;
}

Basic Button Styling

किसी button को style करना browser default appearance (border, background) हटाकर और अपनी खुद की padding, background-color, और border-radius लागू करके शुरू होता है।

Explicitly cursor: pointer set करना अच्छी practice है भले ही buttons default रूप से पहले से एक pointer cursor दिखाते हों, क्योंकि यह तब ज़रूरी हो जाता है जब आप किसी link को button की तरह style करें।

Note:
  • Accepted values:
  • border: none — default border हटाता है
  • padding: <length> — inner spacing
  • background-color: <color> — fill
  • border-radius: <length> — corner rounding
  • cursor: pointer — hover पर pointer

उदाहरण: Basic Button Styling

css
<style>
button {
  border: none;
  background: royalblue;
  color: white;
  padding: 10px 20px;
  border-radius: 6px;
  cursor: pointer;
}
</style>
<button>Click Me</button>

Hover और Active States

किसी button को उन states के लिए visible feedback चाहिए जिनसे कोई user असल में interact करता है: mouse-over के लिए :hover (आमतौर पर थोड़ा darker या lighter background) और click किए जाने के moment के लिए :active (अक्सर एक थोड़ा और darker shade, या एक physical press simulate करने के लिए एक छोटा scale-down transform)।

Note:
  • Accepted values:
  • :hover — mouse over
  • :active — pressed के दौरान
  • :focus-visible — keyboard focus
  • transition: background-color 0.2s — smooth state change
  • transform: translateY(1px) — pressed feel

उदाहरण: Hover and Active States

css
<style>
button {
  background: royalblue;
  color: white;
  border: none;
  padding: 10px 20px;
}
button:hover {
  background: #3b5fd6;
}
button:active {
  background: #2a49b0;
  transform: scale(0.98);
}
</style>
<button>Press Me</button>

Keyboard Users के लिए Focus State

किसी page में tab करते keyboard users के लिए एक visible :focus (या :focus-visible) outline ज़रूरी है — बिना किसी replacement के outline: none से इसे पूरी तरह हटाना एक common accessibility failure है।

box-shadow या एक styled outline इस्तेमाल करके एक custom focus ring buttons को अच्छा दिखने वाला और accessible दोनों बनाए रखता है।

Note:
  • Accepted values:
  • :focus-visible — सिर्फ keyboard focus
  • outline: <width> <style> <color> — focus ring
  • outline-offset: <length> — element और ring के बीच space
  • outline: none — सिर्फ एक visible replacement के साथ

उदाहरण: Focus State for Keyboard Users

css
<style>
button:focus-visible {
  outline: 3px solid orange;
  outline-offset: 2px;
}
</style>
<button>Tab to me</button>

Disabled Button Styling

disabled attribute ज़्यादातर browsers में default रूप से पहले से ही clicks रोक देता है और cursor को not-allowed में बदल देता है, लेकिन एक disabled button को visually भी अलग दिखना चाहिए, आमतौर पर reduced opacity और एक muted color के साथ, ताकि users एक नज़र में समझ जाएँ कि इसके साथ interact नहीं किया जा सकता।

Note:
  • Accepted values:
  • :disabled — disabled buttons को match करता है
  • opacity: <number> — dimmed look
  • cursor: not-allowed — unavailable होने का signal देता है
  • pointer-events: none — clicks नज़रअंदाज़ करता है

उदाहरण: Disabled Button Styling

css
<style>
button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>
<button disabled>Unavailable</button>

Button Size और Shape Variants

असली interfaces को आमतौर पर कई button variants चाहिए होते हैं: एक primary filled button, एक secondary outlined button (transparent background, colored border), और size variants (small/large) जो padding और font-size को साथ में adjust करके control किए जाते हैं ताकि button proportionally scale हो।

Note:
  • Accepted values:
  • padding: <length> — size control करता है
  • font-size: <length> — text size
  • border-radius: 999px — pill shape
  • border-radius: 0 — square corners
  • width: 100% — full-width button

उदाहरण: Button Size and Shape Variants

css
<style>
.primary { background: royalblue; color: white; padding: 10px 20px; border: none; }
.secondary { background: transparent; border: 2px solid royalblue; color: royalblue; padding: 10px 20px; }
.small { font-size: 12px; padding: 4px 10px; }
</style>
<button class="primary">Primary</button>
<button class="secondary">Secondary</button>
<button class="primary small">Small</button>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. focus पर default outline को बिना replacement जोड़े हटाना, इसलिए keyboard users focus नहीं देख सकते।
  2. <button> के बजाय <div> को buttons की तरह style करना, जो keyboard और accessibility behavior खो देता है।
  3. cursor: pointer और एक :hover state भूल जाना।
ब्राउज़र सपोर्ट

हर modern browser में supported: Chrome, Firefox, Safari, Edge और Opera।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.