萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> Div CSS 教學

CSS 表單輸入框邊框美化:Border-Radius、Box-Shadow、Focus Ring、自訂 Checkbox/Radio 實戰

基礎邊框重置

.form-control {
  border: 1px solid #ced4da;
  border-radius: .375rem;           /* 6px 圓角 */
  transition: border-color .15s, box-shadow .15s;
}
.form-control:focus {
  border-color: #86b7fe;
  box-shadow: 0 0 0 .25rem rgba(13,110,253,.25);
  outline: none;                    /* 移除預設藍框 */
}

自訂 Checkbox / Radio(純 CSS,無圖片)

/* 隱藏原生 */
.custom-check-input { position: absolute; opacity: 0; pointer-events: none; }

/* 自訂框 */
.custom-check-label {
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
  padding-left: 1.5rem;
}
.custom-check-label::before {
  content: "";
  position: absolute;
  left: 0; top: 50%; transform: translateY(-50%);
  width: 1rem; height: 1rem;
  border: 2px solid #adb5bd;
  border-radius: .25rem;
  background: #fff;
  transition: background-color .15s, border-color .15s;
}

/* 勾選態 */
.custom-check-input:checked + .custom-check-label::before {
  background-color: #0d6efd;
  border-color: #0d6efd;
  background-image: url("data:image/svg+xml,%3csvg xmlns='https://www.w3.org/2000/svg' viewBox='0 0 20 20'%3e%3cpath fill='none' stroke='%23fff' stroke-linecap='round' stroke-linejoin='round' stroke-width='3' d='m6 10 3 3 6-6'/%3e%3c/svg%3e");
  background-size: 65% 65%;
  background-position: center;
  background-repeat: no-repeat;
}

/* Focus 可見性 */
.custom-check-input:focus + .custom-check-label::before {
  box-shadow: 0 0 0 .25rem rgba(13,110,253,.25);
}

Input 邊框變體

場景CSS
底線輸入框border:0; border-bottom:2px solid #ccc; border-radius:0;
發光邊框box-shadow:0 0 0 3px rgba(13,110,253,.4);
錯誤態border-color:#dc3545; box-shadow:0 0 0 .25rem rgba(220,53,69,.25);
成功態border-color:#198754; box-shadow:0 0 0 .25rem rgba(25,135,84,.25);

行內樣式快速驗證(臨時測試用)

<input type="radio" name="action" value="sub"
       style="border:dashed 1px; background:#FEF5C8;">

生產環境請移至外部 CSS 類別,便於維護與主題切換。

copyright © 萬盛學電腦網 all rights reserved