Powered by NeGD | MeitY Government of India® UX4G
Buttons
Use UX4G’s custom button styles for actions in forms,dialogs,and more with support for multiple sizes,states,and more.
Examples#
UX4G comes with a variety of predefinedbutton
styles,each having a distinct semantic function and a few extras for further control.
<button type="button"class="btn btn-primary">Primary</button><button type="button"class="btn btn-secondary">Secondary</button><button type="button"class="btn btn-success">Success</button><button type="button"class="btn btn-danger">Danger</button>
RESULT
UX4G comes with a variety of predefined button styles ripple effects.
<button type="button"class="btn btn-primary ripple-button">Primary</button><button type="button"class="btn btn-secondary ripple-button">Secondary</button><button type="button"class="btn btn-success ripple-button">Success</button><button type="button"class="btn btn-danger ripple-button">Danger</button>
Add this custom js for ripple effect.
function createRipple(event) {
const button = event.currentTarget;
// Remove existing ripple if it exists for a clean start
const existingRipple = button.querySelector(".ripple-effect");
if (existingRipple) {
existingRipple.remove();
}
const circle = document.createElement("span");
const diameter = Math.max(button.clientWidth, button.clientHeight);
const radius = diameter / 2;
circle.style.width = circle.style.height = `${diameter}px`;
circle.style.left = `${event.clientX - button.getBoundingClientRect().left - radius}px`;
circle.style.top = `${event.clientY - button.getBoundingClientRect().top - radius}px`;
circle.classList.add("ripple-effect");
button.appendChild(circle);
// Remove the ripple effect after the animation ends
setTimeout(() => {
circle.remove();
}, 600);
}
document.querySelectorAll(".ripple-button").forEach(button => {
button.addEventListener("click", createRipple);
});
RESULT
Conveying meaning to assistive technologies
Color-coding to add meaning
just gives a visual cue,which users of assistive technologies like screen readers won't be able to understand. Make sure the information the color designates is either evident from the content itself (such as the visible text) or is included in another way, such as by hiding additional text using the .visually-hidden class.
Disable text wrapping #
It is possible to give the button .text-nowrap
class if it doesn't want the button text to wrap.In Sass,anyone may prevent text wrapping for each button by
setting$btn-white-space
:nowrap.
Button tags#
The<button>
element is intended to be used in conjunction with the.btn
classes.The<a>
and<input>
elements can also
utilize these classes,albeit some browsers may apply a slightly different rendering.
Instead of linking to new pages or sections within the current page when using button classes on<a>
elements that are used to activate in-page functionality(like
collapsing content),these links should be given arole="button"
to clearly communicate their function to assistive technologies like screen readers.
<a class="btn btn-primary"href="#"role="buttolt;/a><button class="btn btn-primary ripple"type="submit">Button</button><input class="btn btn-primary"type="button"value="Input"/><input class="btn btn-primary"type="submit"value="Submit"/><input class="btn btn-primary"type="reset"value="Reset"/>
RESULT
Outline buttons#
In need of a button? But, don't want the strong backdrop colors they bring. To exclude all background colors and pictures from any button, substitute the
.btn-outline-*
modifier classes for the default ones.
<button type="button" class="btn btn-outline-primary">Primary</button>
<button type="button" class="btn btn-outline-secondary">Secondary</button>
<button type="button" class="btn btn-outline-success">Success</button>
<button type="button" class="btn btn-outline-danger">Danger</button>
RESULT
Sizes #
Fancy larger or smaller buttons? Add .btn-lg
or .btn-sm
for additional sizes.
<button type="button" class="btn btn-primary btn-lg">Large button</button><button type="button" class="btn btn-secondary btn-lg">Large button</button>
RESULT
<button type="button" class="btn btn-primary btn-sm ">Small button</button><button type="button" class="btn btn-secondary btn-sm ">Small button</button>
RESULT
You can even roll your own custom sizing with CSS variables:
<button type="button" class="btn btn-primary">Custom button</button>
RESULT
Disabled state #
Add the disabled boolean attribute to any button element to make buttons appear inactive. Pointer-events
: none are applied to disabled buttons, preventing the activation
of hover and active states.
<button type="button" class="btn btn-primary" disabled>Primary button</button><button type="button" class="btn btn-secondary" disabled>Button</button><button type="button" class="btn btn-outline-primary" disabled>Primary button</button><button type="button" class="btn btn-outline-secondary" disabled>Button</button>
RESULT
The <a>
element's disabled buttons have a somewhat different behavior:
- The disabled attribute is not supported by
<a>
s,thus must add the.disabled class to make it visually appear disabled. - There are several styles that can be used in the future to disable all pointer-events on anchor buttons.
- A disabled button should have the
aria-disabled="true"
attribute to let assistive technologies know that it is disabled. - The href property should not be used on disabled buttons utilizing the<a>tag.
Link functionality caveat#
To cover cases where you have to keep thehref
attribute on a disabled link,the.disabled
class usespointer-events:none
to try to disable the link
functionality of<a>
s.Note that this CSS property is not yet standardized for HTML,but all modern browsers support it.In addition,even in browsers that do
supportpointer-events:none
,keyboard navigation remains unaffected,meaning that sighted keyboard users and users of assistive technologies will still be able to activate
these links.So to be safe,in addition toaria-disabled="true"
,also include atabindex="-1"
attribute on these links to prevent them from receiving keyboard
focus,and use custom JavaScript to disable their functionality altogether.
<a href="#"class="btn btn-primary ripple disabled"tabindex="-1"role="button"aria-disabled="!0">Primary link</a><a href="#"class="btn btn-secondary disabled"tabindex="-1"role="button"aria-disabled="!0">Link</a>
RESULT
Block buttons#
Create responsive stacks of full-width,“block buttons” like those in UX4G with a mix of our display and gap utilities.By using utilities instead of button-specific classes,we have much greater control over spacing,alignment,and responsive behaviors.
<div class="d-grid gap-2"><button class="btn btn-primary ripple"type="button">Button</button><button class="btn btn-primary ripple"type="button">Button</button></div>
RESULT
Here we create a responsive variation,starting with vertically stacked buttons until themd
breakpoint,where.d-md-block
replaces the.d-grid
class,thus
nullifying thegap-2
utility.Resize your browser to see them change.
<div class="d-grid gap-2 d-md-block"><button class="btn btn-primary ripple"type="button">Button</button><button class="btn btn-primary ripple"type="button">Button</button></div>
RESULT
You can adjust the width of your block buttons with grid column width classes.For example,for a half-width “block button”,use.col-6
.Center it horizontally
with.mx-auto
,too.
<div class="d-grid gap-2 col-6 mx-auto"><button class="btn btn-primary ripple"type="button">Button</button><button class="btn btn-primary ripple"type="button">Button</button></div>
RESULT
Additional utilities can be used to adjust the alignment of buttons when horizontal.Here we’ve taken our previous responsive example and added some flex utilities and a margin utility on the button to right-align the buttons when they’re no longer stacked.
<div class="d-grid gap-2 d-md-flex justify-content-md-end"><button class="btn btn-primary ripple me-md-2"type="button">Button</button><button class="btn btn-primary ripple"type="button">Button</button></div>
RESULT
Button plugin#
The button plugin allows you to create simple on/off toggle buttons.
Visually,these toggle buttons are identical to thecheckbox toggle buttons.However,they are conveyed differently by assistive technologies:the checkbox toggles will be announced by screen readers as “checked”/“not checked”(since,despite their appearance,they are fundamentally still checkboxes),whereas these toggle buttons will be announced as “button”/“button pressed”.The choice between these two approaches will depend on the type of toggle you are creating,and whether or not the toggle will make sense to users when announced as a checkbox or as an actual button.
Toggle states#
Adddata-bs-toggle="button"
to toggle a button’sactive
state.If you’re pre-toggling a button,you must manually add the.active
class
andaria-pressed="true"
to ensure that it is conveyed appropriately to assistive technologies.
<p class="d-inline-flex gap-1"><button type="button"class="btn"data-bs-toggle="button">Toggle button</button><button type="button"class="btn active"data-bs-toggle="button"aria-pressed="!0">Active toggle button</button><button type="button"class="btn"disabled data-bs-toggle="button">Disabled toggle button</button></p><p class="d-inline-flex gap-1"><button type="button"class="btn btn-primary ripple"data-bs-toggle="button">Toggle button</button><button type="button"class="btn btn-primary ripple active"data-bs-toggle="button"aria-pressed="!0">Active toggle button</button><button type="button"class="btn btn-primary ripple"disabled data-bs-toggle="button">Disabled toggle button</button></p>
RESULT
<p class="d-inline-flex gap-1"><a href="#"class="btn"role="button"data-bs-toggle="button">Toggle link</a><a href="#"class="btn active"role="button"data-bs-toggle="button"aria-pressed="!0">Active toggle link</a><a class="btn disabled"aria-disabled="!0"role="button"data-bs-toggle="button">Disabled toggle link</a></p><p class="d-inline-flex gap-1"><a href="#"class="btn btn-primary ripple"role="button"data-bs-toggle="button">Toggle link</a><a href="#"class="btn btn-primary ripple active"role="button"data-bs-toggle="button"aria-pressed="!0">Active toggle link</a><a class="btn btn-primary ripple disabled"aria-disabled="!0"role="button"data-bs-toggle="button">Disabled toggle link</a></p>
RESULT
Tonal button#
UX4G comes with a variety of predefined tonal button styles.
Methods#
You can create a button instance with the button constructor,for example:
const bsButton=new ux4g.Button('#myButton')
Method | Description |
---|---|
dispose |
Destroys an element’s button.(Removes stored data on the DOM element) |
getInstance |
Static method which allows you to get the button instance associated with a DOM element,you can use it like this:ux4g.Button.getInstance(element) . |
getOrCreateInstance |
Static method which returns a button instance associated with a DOM element or creates a new one in case it wasn’t initialized.You can use it like
this:ux4g.Button.getOrCreateInstance(element) .
|
toggle |
Toggles push state.Gives the button the appearance that it has been activated. |
For example,to toggle all buttons
const bsButton=new ux4g.Button('#myButton')