Nā pihi
E hoʻohana i nā ʻano pihi maʻamau o Bootstrap no nā hana ma nā ʻano, nā kamaʻilio, a me nā mea hou aʻe me ke kākoʻo no nā ʻano nui, nā mokuʻāina, a me nā mea hou aku.
Nā laʻana
Loaʻa i ka Bootstrap kekahi mau ʻano pihi i koho mua ʻia, e lawelawe ana kēlā me kēia me kāna kumu semantic ponoʻī, me kekahi mau mea hou i hoʻolei ʻia no ka mana hou aku.
<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>
<button type="button" class="btn btn-warning">Warning</button>
<button type="button" class="btn btn-info">Info</button>
<button type="button" class="btn btn-light">Light</button>
<button type="button" class="btn btn-dark">Dark</button>
<button type="button" class="btn btn-link">Link</button>
Ka lawe ʻana i ka manaʻo i nā ʻenehana kōkua
ʻO ka hoʻohana ʻana i ke kala e hoʻohui i ka manaʻo e hāʻawi wale i kahi hōʻailona ʻike, ʻaʻole e hāʻawi ʻia i nā mea hoʻohana i nā ʻenehana kōkua - e like me nā mea heluhelu pale. E hōʻoia i ka ʻike i hōʻike ʻia e ka waihoʻoluʻu i ʻike ʻia mai ka maʻiʻo ponoʻī (e laʻa i ke kikokikona ʻike ʻia), a i ʻole e hoʻokomo ʻia ma o nā ala ʻē aʻe, e like me nā kikokikona hou i hūnā ʻia me ka .visually-hidden
papa.
Hoʻopau i ka ʻōwili kikokikona
Inā 'a'ole 'oe makemake i ka kikokikona pihi e 'ōwili, hiki iā 'oe ke ho'ohui i ka .text-nowrap
papa i ke pihi. Ma Sass, hiki iā ʻoe ke hoʻonohonoho $btn-white-space: nowrap
i ka hoʻopau ʻana i ka hoʻopili kikokikona no kēlā me kēia pihi.
Nā lepili pihi
Hoʻolālā .btn
ʻia nā papa e hoʻohana me ka <button>
mea. Eia nō naʻe, hiki iā ʻoe ke hoʻohana i kēia mau papa ma luna a i <a>
ʻole <input>
nā mea (ʻoiai e hoʻohana paha kekahi mau polokalamu kele i kahi unuhi ʻokoʻa).
I ka hoʻohana ʻana i nā papa pihi ma <a>
nā mea i hoʻohana ʻia e hoʻāla i ka hana i loko o ka ʻaoʻao (e like me ka hāʻule ʻana o ka maʻiʻo), ma mua o ka hoʻopili ʻana i nā ʻaoʻao hou a i ʻole nā ʻāpana i loko o ka ʻaoʻao o kēia manawa, pono e hāʻawi ʻia kēia mau loulou e hoʻopuka role="button"
kūpono i kā lākou kumu i nā ʻenehana kōkua e like me mea heluhelu pale.
<a class="btn btn-primary" href="#" role="button">Link</a>
<button class="btn btn-primary" 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">
Nā pihi outline
Pono ʻoe i kahi pihi, ʻaʻole naʻe nā kala hefty kā lākou e lawe mai ai? E hoʻololi i nā papa hoʻololi paʻamau me nā .btn-outline-*
mea e wehe i nā kiʻi hope a me nā kala ma kekahi pihi.
<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>
<button type="button" class="btn btn-outline-warning">Warning</button>
<button type="button" class="btn btn-outline-info">Info</button>
<button type="button" class="btn btn-outline-light">Light</button>
<button type="button" class="btn btn-outline-dark">Dark</button>
Nui
Makemake i nā pihi nui a liʻiliʻi paha? Hoʻohui a i .btn-lg
ʻole .btn-sm
no nā nui ʻē aʻe.
<button type="button" class="btn btn-primary btn-lg">Large button</button>
<button type="button" class="btn btn-secondary btn-lg">Large button</button>
<button type="button" class="btn btn-primary btn-sm">Small button</button>
<button type="button" class="btn btn-secondary btn-sm">Small button</button>
Hiki iā ʻoe ke ʻōwili i kāu nui maʻamau me nā ʻano CSS:
<button type="button" class="btn btn-primary"
style="--bs-btn-padding-y: .25rem; --bs-btn-padding-x: .5rem; --bs-btn-font-size: .75rem;">
Custom button
</button>
Mokuʻāina kīnā
E hana i nā pihi me ka ʻole o ka hoʻohui ʻana i ke disabled
ʻano boolean i kekahi <button>
mea. Ua hoʻohana ʻia nā pihi hoʻopaʻa pointer-events: none
ʻole, e pale ana i ka hover a me nā kūlana hana mai ka hoʻomaka ʻana.
<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>
ʻOkoʻa ka ʻano o nā pihi hoʻopaʻa ʻia e hoʻohana ana i ka <a>
element:
<a>
ʻAʻole kākoʻo ʻo s i kadisabled
ʻano, no laila pono ʻoe e hoʻohui i ka.disabled
papa i mea e ʻike ʻia ai ke kīnā.- Hoʻokomo ʻia kekahi mau ʻano hoʻokipa e hiki mai ana e hoʻopau i nā mea āpau
pointer-events
ma nā pihi heleuma. <a>
Pono e hoʻohana ʻia nā pihi hoʻohana i kaaria-disabled="true"
ʻano e hōʻike i ke kūlana o ka mea i nā ʻenehana kōkua.<a>
ʻAʻole pono e hoʻokomo ʻia ke ʻano o nā pihi hoʻohana ʻiahref
.
<a class="btn btn-primary disabled" role="button" aria-disabled="true">Primary link</a>
<a class="btn btn-secondary disabled" role="button" aria-disabled="true">Link</a>
Hoʻopaʻa hana loulou
No ka uhi ʻana i nā hihia kahi āu e mālama ai i ke href
ʻano ma kahi loulou disable, .disabled
hoʻohana ka papa pointer-events: none
e hoʻāʻo e hoʻopau i ka hana loulou o <a>
s. E hoʻomaopopo ʻaʻole i hoʻopaʻa ʻia kēia waiwai CSS no HTML, akā kākoʻo nā mea hoʻokele hou a pau iā ia. Eia kekahi, ʻoiai i loko o nā polokalamu kele pūnaewele e kākoʻo ana, ʻaʻole i hoʻopilikia ʻia pointer-events: none
ka hoʻokele keyboard, ʻo ia hoʻi, hiki i nā mea hoʻohana keyboard ʻike a me nā mea hoʻohana i nā ʻenehana kōkua ke hoʻāla i kēia mau loulou. No laila, i mea e palekana ai, ma waho aʻe o aria-disabled="true"
, e hoʻokomo pū i kahi tabindex="-1"
hiʻohiʻona ma kēia mau loulou e pale aku iā lākou mai ka loaʻa ʻana o ka manaʻo keyboard, a hoʻohana i ka JavaScript maʻamau e hoʻopau i kā lākou hana.
<a href="#" class="btn btn-primary disabled" tabindex="-1" role="button" aria-disabled="true">Primary link</a>
<a href="#" class="btn btn-secondary disabled" tabindex="-1" role="button" aria-disabled="true">Link</a>
Paʻa pihi
E hana i nā pūʻulu pane o ka laulā piha, "nā pihi poloka" e like me nā mea ma Bootstrap 4 me kahi hui o kā mākou hōʻike a me nā pono āpau. Ma ka hoʻohana ʻana i nā pono hana ma kahi o nā papa kikoʻī, ua ʻoi aku ka nui o ka mana ma luna o ka spacing, alignment, a me nā ʻano pane.
<div class="d-grid gap-2">
<button class="btn btn-primary" type="button">Button</button>
<button class="btn btn-primary" type="button">Button</button>
</div>
Maʻaneʻi mākou e hana i kahi hoʻololi pane, e hoʻomaka me nā pihi i hoʻopaʻa ʻia a hiki i ka md
breakpoint, kahi .d-md-block
e pani ai i ka .d-grid
papa, no laila e hoʻopau i ka gap-2
pono. E hoʻololi i kāu polokalamu kele pūnaewele e ʻike iā lākou e loli.
<div class="d-grid gap-2 d-md-block">
<button class="btn btn-primary" type="button">Button</button>
<button class="btn btn-primary" type="button">Button</button>
</div>
Hiki iā ʻoe ke hoʻololi i ka laula o kāu mau pihi poloka me nā papa ākea kolamu. No ka laʻana, no ka hapalua-ākea "pā pihi", hoʻohana .col-6
. E hoʻokaʻawale iā ia me ka .mx-auto
, pū kekahi.
<div class="d-grid gap-2 col-6 mx-auto">
<button class="btn btn-primary" type="button">Button</button>
<button class="btn btn-primary" type="button">Button</button>
</div>
Hiki ke hoʻohana ʻia nā pono hana hou no ka hoʻoponopono ʻana i ka hoʻopololei ʻana o nā pihi i ka wā e pae ana. Ma ʻaneʻi ua lawe mākou i kā mākou hiʻohiʻona pane mua a hoʻohui i kekahi mau pono pono a me kahi pono palena ma ke pihi e hoʻolike pono i nā pihi ke ʻole lākou i hoʻopaʻa hou ʻia.
<div class="d-grid gap-2 d-md-flex justify-content-md-end">
<button class="btn btn-primary me-md-2" type="button">Button</button>
<button class="btn btn-primary" type="button">Button</button>
</div>
Pihi pihi
Hāʻawi ka plugin pihi iā ʻoe e hana i nā pihi hoʻololi maʻalahi.
E hoʻololi i nā mokuʻāina
Hoʻohui data-bs-toggle="button"
e hoʻololi i ke kūlana o kahi pihi active
. Inā ʻoe e hoʻololi mua i kahi pihi, pono ʻoe e hoʻohui lima i ka .active
papa a aria-pressed="true"
e hōʻoia i ka lawe ʻia ʻana o ia mea i nā ʻenehana kōkua.
<button type="button" class="btn btn-primary" data-bs-toggle="button">Toggle button</button>
<button type="button" class="btn btn-primary active" data-bs-toggle="button" aria-pressed="true">Active toggle button</button>
<button type="button" class="btn btn-primary" disabled data-bs-toggle="button">Disabled toggle button</button>
<a href="#" class="btn btn-primary" role="button" data-bs-toggle="button">Toggle link</a>
<a href="#" class="btn btn-primary active" role="button" data-bs-toggle="button" aria-pressed="true">Active toggle link</a>
<a class="btn btn-primary disabled" aria-disabled="true" role="button" data-bs-toggle="button">Disabled toggle link</a>
Nā ʻano hana
Hiki iā ʻoe ke hana i kahi laʻana pihi me ka mea kūkulu pihi, no ka laʻana:
const bsButton = new bootstrap.Button('#myButton')
Kaʻina hana | wehewehe |
---|---|
dispose |
Hoʻopau i ke pihi o kahi mea. (Wehe i ka ʻikepili i mālama ʻia ma ka mea DOM) |
getInstance |
ʻO ke ala static e hiki ai iā ʻoe ke kiʻi i ke pihi pihi pili i kahi mea DOM, hiki iā ʻoe ke hoʻohana e like me kēia: bootstrap.Button.getInstance(element) . |
getOrCreateInstance |
ʻO ke ʻano static e hoʻihoʻi i kahi laʻana pihi pili i kahi mea DOM a i ʻole hana i kahi mea hou inā ʻaʻole i hoʻomaka ʻia. Hiki iā ʻoe ke hoʻohana e like me kēia: bootstrap.Button.getOrCreateInstance(element) . |
toggle |
Hoʻololi i ke kūlana pana. Hāʻawi i ke pihi i ke ʻano ua hoʻāla ʻia. |
No ka laʻana, e hoʻololi i nā pihi āpau
document.querySelectorAll('.btn').forEach(buttonElement => {
const button = bootstrap.Button.getOrCreateInstance(buttonElement)
button.toggle()
})
CSS
Nā mea hoʻololi
Hoʻohui ʻia ma v5.2.0Ma ke ʻano o ka hoʻololi ʻana o ka CSS variables a Bootstrap, hoʻohana nā pihi i nā ʻano CSS kūloko .btn
no ka hoʻomaikaʻi ʻana i ka manawa maoli. Hoʻonohonoho ʻia nā waiwai no nā ʻano CSS ma o Sass, no laila ke kākoʻo mau ʻia nei ka hana maʻamau Sass.
--#{$prefix}btn-padding-x: #{$btn-padding-x};
--#{$prefix}btn-padding-y: #{$btn-padding-y};
--#{$prefix}btn-font-family: #{$btn-font-family};
@include rfs($btn-font-size, --#{$prefix}btn-font-size);
--#{$prefix}btn-font-weight: #{$btn-font-weight};
--#{$prefix}btn-line-height: #{$btn-line-height};
--#{$prefix}btn-color: #{$body-color};
--#{$prefix}btn-bg: transparent;
--#{$prefix}btn-border-width: #{$btn-border-width};
--#{$prefix}btn-border-color: transparent;
--#{$prefix}btn-border-radius: #{$btn-border-radius};
--#{$prefix}btn-hover-border-color: transparent;
--#{$prefix}btn-box-shadow: #{$btn-box-shadow};
--#{$prefix}btn-disabled-opacity: #{$btn-disabled-opacity};
--#{$prefix}btn-focus-box-shadow: 0 0 0 #{$btn-focus-width} rgba(var(--#{$prefix}btn-focus-shadow-rgb), .5);
Hoʻololi kēlā me kēia .btn-*
papa hoʻololi i nā ʻano CSS kūpono e hōʻemi i nā lula CSS hou me kā mākou button-variant()
, button-outline-variant()
, a me button-size()
nā hui.
Eia kahi laʻana o ke kūkulu ʻana i kahi papa .btn-*
hoʻololi maʻamau e like me kā mākou e hana ai no nā pihi kūikawā i kā mākou docs ma ke kau hou ʻana i nā ʻano hoʻololi CSS o Bootstrap me ka hui ʻana o kā mākou CSS ponoʻī a me Sass.
.btn-bd-primary {
--bs-btn-font-weight: 600;
--bs-btn-color: var(--bs-white);
--bs-btn-bg: var(--bd-violet);
--bs-btn-border-color: var(--bd-violet);
--bs-btn-border-radius: .5rem;
--bs-btn-hover-color: var(--bs-white);
--bs-btn-hover-bg: #{shade-color($bd-violet, 10%)};
--bs-btn-hover-border-color: #{shade-color($bd-violet, 10%)};
--bs-btn-focus-shadow-rgb: var(--bd-violet-rgb);
--bs-btn-active-color: var(--bs-btn-hover-color);
--bs-btn-active-bg: #{shade-color($bd-violet, 20%)};
--bs-btn-active-border-color: #{shade-color($bd-violet, 20%)};
}
Nā hoʻololi Sass
$btn-padding-y: $input-btn-padding-y;
$btn-padding-x: $input-btn-padding-x;
$btn-font-family: $input-btn-font-family;
$btn-font-size: $input-btn-font-size;
$btn-line-height: $input-btn-line-height;
$btn-white-space: null; // Set to `nowrap` to prevent text wrapping
$btn-padding-y-sm: $input-btn-padding-y-sm;
$btn-padding-x-sm: $input-btn-padding-x-sm;
$btn-font-size-sm: $input-btn-font-size-sm;
$btn-padding-y-lg: $input-btn-padding-y-lg;
$btn-padding-x-lg: $input-btn-padding-x-lg;
$btn-font-size-lg: $input-btn-font-size-lg;
$btn-border-width: $input-btn-border-width;
$btn-font-weight: $font-weight-normal;
$btn-box-shadow: inset 0 1px 0 rgba($white, .15), 0 1px 1px rgba($black, .075);
$btn-focus-width: $input-btn-focus-width;
$btn-focus-box-shadow: $input-btn-focus-box-shadow;
$btn-disabled-opacity: .65;
$btn-active-box-shadow: inset 0 3px 5px rgba($black, .125);
$btn-link-color: var(--#{$prefix}link-color);
$btn-link-hover-color: var(--#{$prefix}link-hover-color);
$btn-link-disabled-color: $gray-600;
// Allows for customizing button radius independently from global border radius
$btn-border-radius: $border-radius;
$btn-border-radius-sm: $border-radius-sm;
$btn-border-radius-lg: $border-radius-lg;
$btn-transition: color .15s ease-in-out, background-color .15s ease-in-out, border-color .15s ease-in-out, box-shadow .15s ease-in-out;
$btn-hover-bg-shade-amount: 15%;
$btn-hover-bg-tint-amount: 15%;
$btn-hover-border-shade-amount: 20%;
$btn-hover-border-tint-amount: 10%;
$btn-active-bg-shade-amount: 20%;
$btn-active-bg-tint-amount: 20%;
$btn-active-border-shade-amount: 25%;
$btn-active-border-tint-amount: 10%;
Sass mixins
ʻEkolu hui ʻana no nā pihi: pihi a me ke pihi outline mixins ʻano like ʻole (ma muli o nā mea ʻelua ma luna o $theme-colors
), a me kahi hui nui pihi.
@mixin button-variant(
$background,
$border,
$color: color-contrast($background),
$hover-background: if($color == $color-contrast-light, shade-color($background, $btn-hover-bg-shade-amount), tint-color($background, $btn-hover-bg-tint-amount)),
$hover-border: if($color == $color-contrast-light, shade-color($border, $btn-hover-border-shade-amount), tint-color($border, $btn-hover-border-tint-amount)),
$hover-color: color-contrast($hover-background),
$active-background: if($color == $color-contrast-light, shade-color($background, $btn-active-bg-shade-amount), tint-color($background, $btn-active-bg-tint-amount)),
$active-border: if($color == $color-contrast-light, shade-color($border, $btn-active-border-shade-amount), tint-color($border, $btn-active-border-tint-amount)),
$active-color: color-contrast($active-background),
$disabled-background: $background,
$disabled-border: $border,
$disabled-color: color-contrast($disabled-background)
) {
--#{$prefix}btn-color: #{$color};
--#{$prefix}btn-bg: #{$background};
--#{$prefix}btn-border-color: #{$border};
--#{$prefix}btn-hover-color: #{$hover-color};
--#{$prefix}btn-hover-bg: #{$hover-background};
--#{$prefix}btn-hover-border-color: #{$hover-border};
--#{$prefix}btn-focus-shadow-rgb: #{to-rgb(mix($color, $border, 15%))};
--#{$prefix}btn-active-color: #{$active-color};
--#{$prefix}btn-active-bg: #{$active-background};
--#{$prefix}btn-active-border-color: #{$active-border};
--#{$prefix}btn-active-shadow: #{$btn-active-box-shadow};
--#{$prefix}btn-disabled-color: #{$disabled-color};
--#{$prefix}btn-disabled-bg: #{$disabled-background};
--#{$prefix}btn-disabled-border-color: #{$disabled-border};
}
@mixin button-outline-variant(
$color,
$color-hover: color-contrast($color),
$active-background: $color,
$active-border: $color,
$active-color: color-contrast($active-background)
) {
--#{$prefix}btn-color: #{$color};
--#{$prefix}btn-border-color: #{$color};
--#{$prefix}btn-hover-color: #{$color-hover};
--#{$prefix}btn-hover-bg: #{$active-background};
--#{$prefix}btn-hover-border-color: #{$active-border};
--#{$prefix}btn-focus-shadow-rgb: #{to-rgb($color)};
--#{$prefix}btn-active-color: #{$active-color};
--#{$prefix}btn-active-bg: #{$active-background};
--#{$prefix}btn-active-border-color: #{$active-border};
--#{$prefix}btn-active-shadow: #{$btn-active-box-shadow};
--#{$prefix}btn-disabled-color: #{$color};
--#{$prefix}btn-disabled-bg: transparent;
--#{$prefix}btn-disabled-border-color: #{$color};
--#{$prefix}gradient: none;
}
@mixin button-size($padding-y, $padding-x, $font-size, $border-radius) {
--#{$prefix}btn-padding-y: #{$padding-y};
--#{$prefix}btn-padding-x: #{$padding-x};
@include rfs($font-size, --#{$prefix}btn-font-size);
--#{$prefix}btn-border-radius: #{$border-radius};
}
Sass loops
Hoʻohana nā ʻano ʻokoʻa (no nā pihi maʻamau a outline) i kā lākou hui pū ʻana me kā mākou $theme-colors
palapala ʻāina e hana i nā papa hoʻololi i scss/_buttons.scss
.
@each $color, $value in $theme-colors {
.btn-#{$color} {
@if $color == "light" {
@include button-variant(
$value,
$value,
$hover-background: shade-color($value, $btn-hover-bg-shade-amount),
$hover-border: shade-color($value, $btn-hover-border-shade-amount),
$active-background: shade-color($value, $btn-active-bg-shade-amount),
$active-border: shade-color($value, $btn-active-border-shade-amount)
);
} @else if $color == "dark" {
@include button-variant(
$value,
$value,
$hover-background: tint-color($value, $btn-hover-bg-tint-amount),
$hover-border: tint-color($value, $btn-hover-border-tint-amount),
$active-background: tint-color($value, $btn-active-bg-tint-amount),
$active-border: tint-color($value, $btn-active-border-tint-amount)
);
} @else {
@include button-variant($value, $value);
}
}
}
@each $color, $value in $theme-colors {
.btn-outline-#{$color} {
@include button-outline-variant($value);
}
}