我用官方引導文檔中的代碼創建了一個簡單的復選框:
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
<label class="custom-control-label" for="customCheck">Check this custom checkbox</label>
</div>
我想知道如何使復選框比現在更大?我已經嘗試編輯customCheck的css并更改高度和寬度屬性,但這并沒有增加框的大小。
我為引導開關添加了像*-sm,*-md,*-lg,*-xl這樣的類。
在這里,我用一個@mixin實現了所有分辨率的切換(@mixin與JS函數非常相似,但它不返回任何內容)。
對于引導程序4 定制開關sm、定制開關md、定制開關lg、定制開關xl
https://codepen.io/nisharg/pen/VwLbYvv SCSS
CSS:https://codepen.io/nisharg/pen/mdJmywW
實時片段(CSS)
/* for sm */
.custom-switch.custom-switch-sm .custom-control-label {
padding-left: 1rem;
padding-bottom: 1rem;
}
.custom-switch.custom-switch-sm .custom-control-label::before {
height: 1rem;
width: calc(1rem + 0.75rem);
border-radius: 2rem;
}
.custom-switch.custom-switch-sm .custom-control-label::after {
width: calc(1rem - 4px);
height: calc(1rem - 4px);
border-radius: calc(1rem - (1rem / 2));
}
.custom-switch.custom-switch-sm .custom-control-input:checked ~ .custom-control-label::after {
transform: translateX(calc(1rem - 0.25rem));
}
/* for md */
.custom-switch.custom-switch-md .custom-control-label {
padding-left: 2rem;
padding-bottom: 1.5rem;
}
.custom-switch.custom-switch-md .custom-control-label::before {
height: 1.5rem;
width: calc(2rem + 0.75rem);
border-radius: 3rem;
}
.custom-switch.custom-switch-md .custom-control-label::after {
width: calc(1.5rem - 4px);
height: calc(1.5rem - 4px);
border-radius: calc(2rem - (1.5rem / 2));
}
.custom-switch.custom-switch-md .custom-control-input:checked ~ .custom-control-label::after {
transform: translateX(calc(1.5rem - 0.25rem));
}
/* for lg */
.custom-switch.custom-switch-lg .custom-control-label {
padding-left: 3rem;
padding-bottom: 2rem;
}
.custom-switch.custom-switch-lg .custom-control-label::before {
height: 2rem;
width: calc(3rem + 0.75rem);
border-radius: 4rem;
}
.custom-switch.custom-switch-lg .custom-control-label::after {
width: calc(2rem - 4px);
height: calc(2rem - 4px);
border-radius: calc(3rem - (2rem / 2));
}
.custom-switch.custom-switch-lg .custom-control-input:checked ~ .custom-control-label::after {
transform: translateX(calc(2rem - 0.25rem));
}
/* for xl */
.custom-switch.custom-switch-xl .custom-control-label {
padding-left: 4rem;
padding-bottom: 2.5rem;
}
.custom-switch.custom-switch-xl .custom-control-label::before {
height: 2.5rem;
width: calc(4rem + 0.75rem);
border-radius: 5rem;
}
.custom-switch.custom-switch-xl .custom-control-label::after {
width: calc(2.5rem - 4px);
height: calc(2.5rem - 4px);
border-radius: calc(4rem - (2.5rem / 2));
}
.custom-switch.custom-switch-xl .custom-control-input:checked ~ .custom-control-label::after {
transform: translateX(calc(2.5rem - 0.25rem));
}
<link rel="stylesheet" >
<div class="custom-control custom-switch custom-switch-sm">
<input type="checkbox" class="custom-control-input" id="customSwitch1">
<label class="custom-control-label" for="customSwitch1">Default Unchcked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-sm">
<input type="checkbox" class="custom-control-input" id="customSwitch2" checked>
<label class="custom-control-label" for="customSwitch2">Default Checked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-md">
<input type="checkbox" class="custom-control-input" id="customSwitch3">
<label class="custom-control-label" for="customSwitch3">Default Unchcked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-md">
<input type="checkbox" class="custom-control-input" id="customSwitch4" checked>
<label class="custom-control-label" for="customSwitch4">Default Checked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-lg">
<input type="checkbox" class="custom-control-input" id="customSwitch5">
<label class="custom-control-label" for="customSwitch5">Default Unchcked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-lg">
<input type="checkbox" class="custom-control-input" id="customSwitch6" checked>
<label class="custom-control-label" for="customSwitch6">Default Checked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-xl">
<input type="checkbox" class="custom-control-input" id="customSwitch7">
<label class="custom-control-label" for="customSwitch7">Default Unchcked Switch</label>
</div>
<div class="custom-control custom-switch custom-switch-xl">
<input type="checkbox" class="custom-control-input" id="customSwitch8" checked>
<label class="custom-control-label" for="customSwitch8">Default Checked Switch</label>
</div>
實際上,解決方案很簡單,要么應用自定義css/scss來使按鈕變大,要么
可以使用transform:scale();屬性來增加按鈕的大小,相信我,這是解決問題的最簡單的方法,而且行之有效。
<div class="form-check form-switch py-5">
<!-- Size of the default switch will increase 1.8 times -->
<input class="form-check-input mx-2"
type="checkbox"
role="switch"
id="flexSwitchCheckDefault"
style="transform: scale(1.8);">
<label class="form-check-label"
for="flexSwitchCheckDefault"
id="AucTu">Autocomplete Turned Off</label>
</div>
PS。你不需要添加自定義的css/scss,只是不要忘記從開關中添加一些空白,或者從div中添加填充(或者它的某些部分將被隱藏) :)
在bootstrap 4中,如果你想改變或增加自定義開關(一個來自iOS的漂亮開關),你應該深入bootstrap的css文件,改變它的自定義開關類的維度。html代碼如下:
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="switchA" name="switch">
<label class="custom-control-label" for="switchA">Custom checkbox</label>
</div>
在boostrap.css(版本4.3.1且未縮小)文件中,您應該可以找到 自定義開關類,并在 下面的。你必須刪除我添加的所有評論。
.custom-switch {
padding-left: 2.25rem;
padding-bottom: 1rem; // added for positioning
}
.custom-control-label { // added for alignment with the switch
padding-top: 0.5rem;
padding-left: 2rem;
padding-bottom: 0.1rem;
}
.custom-switch .custom-control-label::before {
left: -2.25rem;
height: 2rem;
width: 3.5rem; // it was 1.75rem before. Sliding way is longer than before.
pointer-events: all;
border-radius: 1rem;
}
.custom-switch .custom-control-label::after {
top: calc(0.25rem + 2px);
left: calc(-2.25rem + 2px);
width: calc(2rem - 4px); // it was calc(1rem - 4px) before. Oval is bigger than before.
height: calc(2rem - 4px); // it was calc(1rem - 4px) before. Oval is bigger than before.
background-color: #adb5bd;
border-radius: 2rem; // it was 0.5rem before. Oval is bigger than before.
transition: background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-transform 0.15s ease-in-out;
transition: transform 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
transition: transform 0.15s ease-in-out, background-color 0.15s ease-in-out, border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out, -webkit-transform 0.15s ease-in-out;
}
@media (prefers-reduced-motion: reduce) {
.custom-switch .custom-control-label::after {
transition: none;
}
}
.custom-switch .custom-control-input:checked ~ .custom-control-label::after {
background-color: #fff;
-webkit-transform: translateX(1.5rem); //translateX(0.75rem);
transform: translateX(1.5rem); //translateX(0.75rem);
}
將這些代碼復制到引導文件后,您必須刪除所有代碼 我寫的評論。否則不會增加尺寸。我希望你喜歡 你現在變得更漂亮了。
最好有自己的文件,并更改開關尺寸的css變量:
@import "bootstrap/scss/_functions.scss";
@import "bootstrap/scss/_variables.scss";
$custom-control-indicator-size: 1.5rem;
$custom-switch-width: $custom-control-indicator-size * 1.75;
$custom-switch-indicator-border-radius: $custom-control-indicator-size / 2;
$custom-switch-indicator-size: subtract(
$custom-control-indicator-size,
$custom-control-indicator-border-width * 4
);
@import "bootstrap/scss/bootstrap";
改變bootstrap組件的尺寸不是一個好主意,你應該改變bootstrap中的變量,但這意味著要重新編譯庫。 不管怎樣,你想做什么都可以這樣做:
.custom-control-label::before ,.custom-control-label::after{width:20px; height:20px}
這門課。custom-switch-adaptive將使開關適應文本大小,并在其中包含標簽文本。
演示
注:一個類似的功能將被添加到引導版本5中
SCSS
.custom-switch.custom-switch-adaptive {
padding-left: 0;
.custom-control-label {
padding: 0 1.5em;
position: relative;
border-radius: 1em;
line-height: 1.4em;
color: $text-muted;
border: 1px solid $text-muted;
background-color: $light;
transition: background-color 200ms;
&::before {
content: none;
}
&::after {
position: absolute;
height: 1em;
top: 0.2em;
left: 0.2em;
width: 1em;
border-radius: 1em;
transition: left 200ms;
}
}
.custom-control-input:checked ~ .custom-control-label {
color: $white;
background-color: $success;
border-color: $success;
&::after {
background-color: $white;
left: calc(100% - 1.2em);
transform: none;
-webkit-transform: none;
}
}
}
超文本標記語言
<div class="form-group h1">
<div class="custom-control custom-switch custom-switch-adaptive">
<input id="test4" type="checkbox" class="custom-control-input">
<label for="test4" class="custom-control-label">h1 text size</label>
</div>
</div>
我修改了Nisharg Shah的css,使得文本現在很好地與Bootstrap V5示例的開關對齊。 因此,我查看了Bootstrap 5 css源代碼,增加了所有與大小調整有關的屬性:左填充、高、左邊距、寬度和頂部填充。 我認為結果看起來更好,并符合默認的開關外觀風格。
CSS和HTML:
.form-switch.form-switch-md {
padding-left: 4.5em;
height: 2.5em;
}
.form-switch.form-switch-md .form-check-input {
margin-left: -4.5em;
height: 2em;
width: 4em;
}
.form-switch.form-switch-md .form-check-label {
padding-top: 0.5em;
}
.form-switch.form-switch-lg {
padding-left: 8.5em;
height: 4.5em;
}
.form-switch.form-switch-lg .form-check-input {
margin-left: -8.5em;
height: 4em;
width: 8em;
}
.form-switch.form-switch-lg .form-check-label {
padding-top: 1.5em;
}
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Original switch unchecked</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Original switch checked</label>
</div>
<div class="form-check form-switch form-switch-md">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Medium switch unchecked</label>
</div>
<div class="form-check form-switch form-switch-md">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Medium switch checked</label>
</div>
<div class="form-check form-switch form-switch-lg">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckDefault">
<label class="form-check-label" for="flexSwitchCheckDefault">Large switch unchecked</label>
</div>
<div class="form-check form-switch form-switch-lg">
<input class="form-check-input" type="checkbox" id="flexSwitchCheckChecked" checked>
<label class="form-check-label" for="flexSwitchCheckChecked">Large switch checked</label>
</div>
因為您試圖在bootstrap中完成它,所以很容易。 首先使用btn-group-toggle添加一個div容器,以便在組中使用按鈕(如果您愿意的話。).
在標簽中添加p-3或p-5類,根據您的喜好進行擴展
<div class="btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary p-3">
<input type="checkbox" id="customCheck" name="example1"> Check this custom checkbox
</label>
</div>
你可以試試下面的方法:
.custom-control-label::before,
.custom-control-label::after {
width: 2.25rem !important;
height: 2.25rem !important;
margin: 1rem;
}
.custom-control-label{
margin: 1.5rem 2rem;
}
<link rel="stylesheet" integrity="sha384-Zug+QiDoJOrZ5t4lssLdxGhVrurbmBWopoEl+M6BdEfwnCJZtKxi1KgxUyJq13dy" crossorigin="anonymous">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
<label class="custom-control-label" for="customCheck">Check this custom checkbox</label>
</div>
下面是一個使用自定義css的工作示例
.custom-checkbox-lg .custom-control-label::before,
.custom-checkbox-lg .custom-control-label::after {
width: 2rem;
height: 2rem;
}
.custom-checkbox-lg .custom-control-label {
margin: .5rem 1rem;
}
<link rel="stylesheet" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" id="customCheck1" name="example1">
<label class="custom-control-label" for="customCheck1">Check this custom checkbox</label>
</div>
<div class="custom-control custom-checkbox custom-checkbox-lg">
<input type="checkbox" class="custom-control-input" id="customCheck2" name="example1">
<label class="custom-control-label" for="customCheck2">Check this custom checkbox</label>
</div>
在尋找相同問題的解決方案時,我發現了這個問題,但之前的答案都不適合我,尤其是當標簽上有復選框/開關時。
我開始考慮邊距和行高,但是看起來解決方案可能要簡單得多——至少在Bootstrap V5中,所有的尺寸和邊距都是用& quotem & quot,所以更改基本表單檢查元素的字體大小可以很好地調整和對齊所有內容,不管有沒有標簽。
編輯:剛注意到當沒有標簽時,下邊距有點偏離,但通過添加一個空標簽或根據需要手動調整邊距/填充,這很容易修復。
.form-check-lg {
font-size: 150%;
}
<link rel="stylesheet"/>
<h3>default font size:</h3>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" id="checkbox_1">
<label class="form-check-label" for="checkbox_1">default</label>
</div>
<div class="form-check"><input class="form-check-input" type="checkbox" id="checkbox_2">
<label class="form-check-label" for="checkbox_2">default</label>
</div>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox">
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox">
</div>
<h3>larger font size:</h3>
<div class="form-check form-switch form-check-lg">
<input class="form-check-input" type="checkbox" id="checkbox_3">
<label class="form-check-label" for="checkbox_3">large</label>
</div>
<div class="form-check form-check-lg">
<input class="form-check-input" type="checkbox" id="checkbox_4">
<label class="form-check-label" for="checkbox_4">large</label>
</div>
<div class="form-check form-switch form-check-lg">
<input class="form-check-input" type="checkbox">
</div>
<div class="form-check form-check-lg">
<input class="form-check-input" type="checkbox">
</div>
這是我的解決方案...剛剛使用的自舉& ampFontAvesome....
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/js/all.min.js"></script>
<link rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ps-3">
<div class="h3">bootstrap@5.1.3 & font-awesome/6.2.0</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch fa-2x">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch fa-2x">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch fa-3x">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
<div class="col-12">
<label>Label Top</label>
<span class="form-check form-switch fa-4x">
<input class="form-check-input" type="checkbox" role="switch">
<label class="form-check-label">Label Side</label>
</span>
</div>
</div>
Bootstrap 5提供了一個優雅的解決方案,通過使用框架提供的預定義字體大小類(fs-x)來調整復選框的大小。下面是一個代碼示例:
<div class="form-check fs-3">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
<label class="form-check-label" for="flexRadioDefault1">
Default radio
</label>
</div>
<div class="form-check fs-4">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
<label class="form-check-label" for="flexRadioDefault1">
Default radio
</label>
</div>
<div class="form-check fs-5">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
<label class="form-check-label" for="flexRadioDefault1">
Default radio
</label>
</div>
<div class="form-check fs-6 mb-3">
<input class="form-check-input" type="radio" name="flexRadioDefault" id="flexRadioDefault1">
<label class="form-check-label" for="flexRadioDefault1">
Default radio
</label>
</div>
結果是: