top
Loading...
Bootstrap4 自定義表單

Bootstrap4 自定義表單

Bootstrap4 可以自定義一些表單的樣式來替換瀏覽器默認的樣式。

自定義復選框

如果要自定義一個復選框,可以設置 <div> 為父元素,類為 .custom-control.custom-checkbox,復選框作為子元素放在該 <div> 里頭,然後復選框設置為 type="checkbox",類為 .custom-control-input

復選框的文本使用 label 標籤,標籤使用 .custom-control-label 類,labelfor 屬性值需要匹配復選框的 id。

Bootstrap4 實例

<form> <div class="custom-control custom-checkbox"> <input type="checkbox" class="custom-control-input" id="customCheck" name="example1"> <label class="custom-control-label" for="customCheck">自定義復選框</label> </div> </form>

嘗試一下 »

自定義單選框

如果要自定義一個單選框,可以設置 <div> 為父元素,類為 .custom-control.custom-radio,復選框作為子元素放在該 <div> 里頭,然後單選框設置為 type="radio",類為 .custom-control-input

單選框的文本使用 label 標籤,標籤使用 .custom-control-label 類,labelfor 屬性值需要匹配單選框的 id

Bootstrap4 實例

<form> <div class="custom-control custom-radio"> <input type="radio" class="custom-control-input" id="customRadio" name="example1" value="customEx"> <label class="custom-control-label" for="customRadio">自定義單選框</label> </div> </form>

嘗試一下 »

自定義控件顯示在同一行

我們可以在外部元素上使用 .custom-control-inline 類來包裹自定義表單控件,這樣自定義表單控件就能顯示在同一行:

Bootstrap4 實例

<form> <div class="custom-control custom-radio custom-control-inline"> <input type="radio" class="custom-control-input" id="customRadio" name="example" value="customEx"> <label class="custom-control-label" for="customRadio">自定義單選框 1</label> </div> <div class="custom-control custom-radio custom-control-inline"> <input type="radio" class="custom-control-input" id="customRadio2" name="example" value="customEx"> <label class="custom-control-label" for="customRadio2">自定義單選框 2</label> </div> </form>

嘗試一下 »

自定義選擇菜單

創建自定義選擇菜單可以在 <select> 元素上添加 .custom-select 類:

Bootstrap4 實例

<form> <select name="cars" class="custom-select-sm"> <option selected>自定義選擇菜單</option> <option value="Google">Google</option> <option value="Runoob">Runoob</option> <option value="Taobao">Taobao</option> </select> </form>

嘗試一下 »

如果我們要設置自定義選擇菜單大小,可以使用 .custom-select-sm.custom-select-lg 來設置它們的大小:

Bootstrap4 實例

<form> <!----> <select name="cars" class="custom-select-sm"> <option selected>比較小的自定義選擇菜單</option> <option value="Google">Google</option> <option value="Runoob">Runoob</option> <option value="Taobao">Taobao</option> </select> <!----> <select name="cars" class="custom-select-lg"> <option selected>比較大的自定義選擇菜單</option> <option value="Google">Google</option> <option value="Runoob">Runoob</option> <option value="Taobao">Taobao</option> </select> </form>

嘗試一下 »

自定義滑塊控件

我們可以在 inputtype="range" 的輸入框中添加 .custom-range 類來設置自定義滑塊控件:

Bootstrap4 實例

<form> <label for="customRange">自定義滑塊控件</label> <input type="range" class="custom-range" id="customRange" name="points1"> </form>

嘗試一下 »

自定義文件上傳控件

我們可以在父元素添加 .custom-file 類,然後在 input 設置為 type="file" 併添加 .custom-file-label:

上傳控件的文本使用 label 標籤,標籤使用 .custom-file-label 類,labelfor 屬性值需要匹配復選框的 id

Bootstrap4 實例

<form> <div class="custom-file"> <input type="file" class="custom-file-input" id="customFile"> <label class="custom-file-label" for="customFile">選擇文件</label> </div> </form>

嘗試一下 »
北斗有巢氏 有巢氏北斗