CSS Buttons
In this page:
.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 करें।
- 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
<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)।
- 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
<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 दोनों बनाए रखता है।
- 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
<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 नहीं किया जा सकता।
- Accepted values:
- :disabled — disabled buttons को match करता है
- opacity: <number> — dimmed look
- cursor: not-allowed — unavailable होने का signal देता है
- pointer-events: none — clicks नज़रअंदाज़ करता है
उदाहरण: Disabled Button Styling
<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 हो।
- 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
<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>
- focus पर default
outlineको बिना replacement जोड़े हटाना, इसलिए keyboard users focus नहीं देख सकते। <button>के बजाय<div>को buttons की तरह style करना, जो keyboard और accessibility behavior खो देता है।cursor: pointerऔर एक:hoverstate भूल जाना।
हर modern browser में supported: Chrome, Firefox, Safari, Edge और Opera।
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Gradients
- CSS Shadows
- CSS Filters
- CSS Blend Modes
- CSS Transforms
- CSS 2D Transforms
- CSS 3D Transforms
- CSS Transitions
- CSS Animations
- CSS Image Styling
- CSS Image Effects
- CSS Hover Overlays
- CSS Image Modal
- CSS Image Centering
- CSS Image Shapes
- CSS Buttons
- CSS Columns
- CSS Clip Path
- CSS Shapes
- CSS Scroll Snap
- CSS Architecture: BEM
- CSS Custom Properties