จะเปลี่ยนรูปแบบเส้นขอบของช่องทำเครื่องหมายใน CSS ได้อย่างไร


122

ฉันจะเปลี่ยนรูปแบบของเส้นขอบของช่องทำเครื่องหมาย (อินพุต) ได้อย่างไร ฉันใส่border:1px solid #1e5180ไปแล้ว แต่ใน FireFox 3.5 ไม่มีอะไรเกิดขึ้น!

คำตอบ:


63

หากมีอะไรเกิดขึ้นในเบราว์เซอร์ใด ๆฉันจะประหลาดใจ นี่เป็นหนึ่งในองค์ประกอบรูปแบบที่โดดเด่นที่เบราว์เซอร์มักจะไม่ให้คุณจัดรูปแบบมากขนาดนั้นและผู้คนมักจะพยายามแทนที่ด้วยจาวาสคริปต์เพื่อให้พวกเขาสามารถจัดรูปแบบ / โค้ดให้ดูและทำหน้าที่เหมือนช่องทำเครื่องหมาย


4
Operaจัดการการจัดแต่งทรงผมได้เป็นอย่างดีในช่องทำเครื่องหมาย IEมีรูปแบบบางอย่างที่ล้อมรอบช่องทำเครื่องหมาย (ไม่ใช่ในช่องทำเครื่องหมายเอง)
กลัว

4
ปัจจุบันพวกเขาน่าเกลียดจริงๆ: D
Gergely Fehérvári

1
ยังคงน่าเกลียดในปี 2020
พอล

210

ฉันขอแนะนำให้ใช้ "เส้นขอบ" แทน "เส้นขอบ" ตัวอย่างเช่น:outline: 1px solid #1e5180 .


3
Outline เป็นทางเลือกที่ดี
Nippysaurus

4
โครงร่างเป็นสิ่งที่ดีมากและใช้ได้กับกล่องอินพุตและกล่องเลือกทั้งหมดทำให้สิ่งนี้มีประโยชน์สำหรับคลาส ".error"
SztupY

2
คุณอาจต้องเพิ่ม!importantใน css ของคุณเมื่อใช้โครงร่าง ฉันกำลังทดสอบสิ่งนี้ใน Firefox 12.0 และไม่มี! ที่สำคัญโครงร่างจะหายไปตราบเท่าที่ฉันคลิกช่องทำเครื่องหมาย
Weezle

3
นี่เป็นทางเลือกที่ดี แต่ปัญหาของโครงร่างคือใน IE7 ไม่ทำงาน !!
Yises

13
@ คุณต้องกังวลเกี่ยวกับ IE7 หรือไม่? อ๊ะฉันขอโทษ
theblang

71

คุณควรใช้

-moz-appearance:none;
-webkit-appearance:none;
-o-appearance:none;

จากนั้นคุณจะกำจัดรูปภาพ / สไตล์ช่องทำเครื่องหมายเริ่มต้นและสามารถจัดรูปแบบได้ อย่างไรก็ตามเส้นขอบจะยังคงอยู่ใน Firefox


2
Opera ช่วยให้คุณสามารถจัดรูปแบบช่องทำเครื่องหมายโดยไม่ต้องมีการแฮ็กเพิ่มเติม - ฉันจะทิ้งสไตล์ -o-Appearance -moz- ลักษณะและ -webkit- ลักษณะที่ดีแม้ว่า
Neall

7
อ๊ะ - พูดเร็วเกินไป: "-webkit-Appearance: none;" ดูเหมือนว่าจะปิดใช้งานการทำงานของช่องทำเครื่องหมาย
Neall

6
สำหรับฉัน (โครเมี่ยม 5) ไม่ได้คุณต้องจัดรูปแบบ: เช็คสถานะพิเศษ
Valerij

1
รูปลักษณ์ไม่เคยเป็นส่วนหนึ่งของมาตรฐาน CSS อย่าใช้มัน
Evgeny

ว้าวมันยอดเยี่ยมมากฉันจะกำจัดปลั๊กอิน ฉันทำอะไรก็ได้ => | อินพุต [type = checkbox] | อินพุต [type = checkbox]: hover | input [type = checkbox]: checked | input [type = checkbox]: checked: after ...
h0mayun

21

คุณสามารถใช้เงากล่องเพื่อปลอมเส้นขอบ:

-webkit-box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
-moz-box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);
box-shadow: 0px 0px 0px 1px rgba(255,0,0,1);

@retrovertigo รุ่นไหน
Dennis Heiden

เวอร์ชัน 12.0.2 (14606.3.4) / ล่าสุดบน macOS 10.14
retrovertigo

@retrovertigo ฉันได้ทดสอบวิธีแก้ปัญหาของฉันแล้วและคุณพูดถูก คำตอบของฉันใช้ไม่ได้กับ Safari หากไม่มีการแก้ไขเพิ่มเติม (เช่น -webkit-Appearance: none;) 3 ปีต่อมาฉันจะใช้วิธีแก้ปัญหาที่ pendingfox หรือ Adan มอบให้ แต่ฉันจะพยายามขยายคำตอบของฉันภายในไม่กี่สัปดาห์ ขอบคุณสำหรับความคิดเห็นของคุณ.
Dennis Heiden

11

นี่คือโซลูชันข้ามเบราว์เซอร์ CSS ที่บริสุทธิ์ (ไม่มีภาพ) โดยใช้ Custom Checkboxes และ Radio Buttons ของ Martin พร้อมลิงก์ CSS3: http://martinivanov.net/2012/12/21/imageless-custom-checkboxes-and-radio-buttons เมื่อใช้-CSS3-เยือน /

นี่คือ jsFiddle: http://jsfiddle.net/DJRavine/od26wL6n/

ฉันได้ทดสอบสิ่งนี้กับเบราว์เซอร์ต่อไปนี้:

  • ไฟร์ฟอกซ์ (41.0.2) (42)
  • Google Chrome (46.0.2490.80 ม.)
  • โอเปร่า (33.0.1990.43)
  • Internet Explorer (11.0.10240.16431 [เวอร์ชันอัปเดต: 11.0.22])
  • Microsoft Edge (20.10240.16384.0)
  • Safari มือถือ iPhone iOS9 (601.1.46)

label,
input[type="radio"] + span,
input[type="radio"] + span::before,
label,
input[type="checkbox"] + span,
input[type="checkbox"] + span::before
{
    display: inline-block;
    vertical-align: middle;
}
 
label *,
label *
{
    cursor: pointer;
}
 
input[type="radio"],
input[type="checkbox"]
{
    opacity: 0;
    position: absolute;
}
 
input[type="radio"] + span,
input[type="checkbox"] + span
{
    font: normal 11px/14px Arial, Sans-serif;
    color: #333;
}
 
label:hover span::before,
label:hover span::before
{
    -moz-box-shadow: 0 0 2px #ccc;
    -webkit-box-shadow: 0 0 2px #ccc;
    box-shadow: 0 0 2px #ccc;
}
 
label:hover span,
label:hover span
{
    color: #000;
}
 
input[type="radio"] + span::before,
input[type="checkbox"] + span::before
{
    content: "";
    width: 12px;
    height: 12px;
    margin: 0 4px 0 0;
    border: solid 1px #a8a8a8;
    line-height: 14px;
    text-align: center;
     
    -moz-border-radius: 100%;
    -webkit-border-radius: 100%;
    border-radius: 100%;
     
    background: #f6f6f6;
    background: -moz-radial-gradient(#f6f6f6, #dfdfdf);
    background: -webkit-radial-gradient(#f6f6f6, #dfdfdf);
    background: -ms-radial-gradient(#f6f6f6, #dfdfdf);
    background: -o-radial-gradient(#f6f6f6, #dfdfdf);
    background: radial-gradient(#f6f6f6, #dfdfdf);
}
 
input[type="radio"]:checked + span::before,
input[type="checkbox"]:checked + span::before
{
    color: #666;
}
 
input[type="radio"]:disabled + span,
input[type="checkbox"]:disabled + span
{
    cursor: default;
     
    -moz-opacity: .4;
    -webkit-opacity: .4;
    opacity: .4;
}
 
input[type="checkbox"] + span::before
{
    -moz-border-radius: 2px;
    -webkit-border-radius: 2px;
    border-radius: 2px;
}
 
input[type="radio"]:checked + span::before
{
    content: "\2022";
    font-size: 30px;
    margin-top: -1px;
}
 
input[type="checkbox"]:checked + span::before
{
    content: "\2714";
    font-size: 12px;
}



input[class="blue"] + span::before
{
    border: solid 1px blue;
    background: #B2DBFF;
    background: -moz-radial-gradient(#B2DBFF, #dfdfdf);
    background: -webkit-radial-gradient(#B2DBFF, #dfdfdf);
    background: -ms-radial-gradient(#B2DBFF, #dfdfdf);
    background: -o-radial-gradient(#B2DBFF, #dfdfdf);
    background: radial-gradient(#B2DBFF, #dfdfdf);
}
input[class="blue"]:checked + span::before
{
    color: darkblue;
}



input[class="red"] + span::before
{
    border: solid 1px red;
    background: #FF9593;
    background: -moz-radial-gradient(#FF9593, #dfdfdf);
    background: -webkit-radial-gradient(#FF9593, #dfdfdf);
    background: -ms-radial-gradient(#FF9593, #dfdfdf);
    background: -o-radial-gradient(#FF9593, #dfdfdf);
    background: radial-gradient(#FF9593, #dfdfdf);
}
input[class="red"]:checked + span::before
{
    color: darkred;
}
 <label><input type="radio" checked="checked" name="radios-01" /><span>checked radio button</span></label>
 <label><input type="radio" name="radios-01" /><span>unchecked radio button</span></label>
 <label><input type="radio" name="radios-01" disabled="disabled" /><span>disabled radio button</span></label>

<br/>

 <label><input type="radio" checked="checked" name="radios-02"  class="blue" /><span>checked radio button</span></label>
 <label><input type="radio" name="radios-02" class="blue" /><span>unchecked radio button</span></label>
 <label><input type="radio" name="radios-02" disabled="disabled" class="blue" /><span>disabled radio button</span></label>

<br/>

 <label><input type="radio" checked="checked" name="radios-03"  class="red" /><span>checked radio button</span></label>
 <label><input type="radio" name="radios-03" class="red" /><span>unchecked radio button</span></label>
 <label><input type="radio" name="radios-03" disabled="disabled" class="red" /><span>disabled radio button</span></label>

<br/>
 
<label><input type="checkbox" checked="checked" name="checkbox-01" /><span>selected checkbox</span></label>
<label><input type="checkbox" name="checkbox-02" /><span>unselected checkbox</span></label>
<label><input type="checkbox" name="checkbox-03" disabled="disabled" /><span>disabled checkbox</span></label>

<br/>
 
<label><input type="checkbox" checked="checked" name="checkbox-01" class="blue" /><span>selected checkbox</span></label>
<label><input type="checkbox" name="checkbox-02" class="blue" /><span>unselected checkbox</span></label>
<label><input type="checkbox" name="checkbox-03" disabled="disabled" class="blue" /><span>disabled checkbox</span></label>

<br/>
 
<label><input type="checkbox" checked="checked" name="checkbox-01" class="red" /><span>selected checkbox</span></label>
<label><input type="checkbox" name="checkbox-02" class="red" /><span>unselected checkbox</span></label>
<label><input type="checkbox" name="checkbox-03" disabled="disabled" class="red" /><span>disabled checkbox</span></label>


10

ฉันล้าสมัยแล้วฉันรู้ .. แต่วิธีแก้ปัญหาเล็กน้อยคือใส่ช่องทำเครื่องหมายของคุณไว้ในแท็กป้ายกำกับจากนั้นจัดรูปแบบป้ายกำกับด้วยเส้นขอบ:

<label class='hasborder'><input type='checkbox' /></label>

จากนั้นจัดรูปแบบป้ายกำกับ:

.hasborder { border:1px solid #F00; }

1
นอกจากนี้โปรดทราบว่าองค์ประกอบป้ายกำกับจะทำเครื่องหมายในช่องทำเครื่องหมายทุกครั้งที่มีการคลิกแม้ว่าช่องทำเครื่องหมายจะไม่สามารถดูได้จากภายในก็ตาม ดังนั้นบนป้ายกำกับคุณสามารถทำสิ่งที่ต้องการlabel { position: relative; overflow: hidden; width: 16px; height: 16px; border: 1px solid #0f0; }จากนั้นlabel > input[type=checkbox] { position: absolute; left: -999px; }สิ่งนี้จะช่วยให้คุณกำหนดรูปแบบช่องทำเครื่องหมายของคุณเองและช่องทำเครื่องหมายเดิมจะยังคงถูกเลือก / ไม่เลือก แต่ผู้ใช้จะไม่เห็น พวกเขาจะเห็นเฉพาะช่องทำเครื่องหมายที่คุณกำหนดเอง
Gavin

1
ลืมเพิ่มคุณสามารถใช้ตัวเลือกเช่น:hoverและ :checkedเพื่อจัดรูปแบบสถานะอื่น ๆ ของช่องทำเครื่องหมายที่คุณกำหนดเองได้
Gavin

10

นี่คือเวอร์ชันของฉันที่ใช้ FontAwesome สำหรับเครื่องหมายช่องทำเครื่องหมายฉันคิดว่า FontAwesome ถูกใช้โดยเกือบทุกคนดังนั้นจึงปลอดภัยที่จะถือว่าคุณมีเช่นกัน ไม่ได้ทดสอบใน IE / Edge และฉันไม่คิดว่าจะมีใครสนใจ

input[type=checkbox] {
	-moz-appearance:none;
	-webkit-appearance:none;
	-o-appearance:none;
	outline: none;
	content: none;	
}

input[type=checkbox]:before {
	font-family: "FontAwesome";
    content: "\f00c";
    font-size: 15px;
    color: transparent !important;
    background: #fef2e0;
    display: block;
    width: 15px;
    height: 15px;
    border: 1px solid black;
    margin-right: 7px;
}

input[type=checkbox]:checked:before {

	color: black !important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css" rel="stylesheet"/>

<input type="checkbox">


9

สำหรับFirefox , ChromeและSafariไม่มีอะไรเกิดขึ้น

สำหรับIEจะใช้เส้นขอบนอกช่องทำเครื่องหมาย (ไม่ใช่ส่วนหนึ่งของช่องทำเครื่องหมาย) และเอฟเฟกต์การแรเงา "แฟนซี" ในช่องทำเครื่องหมายจะหายไป (แสดงเป็นช่องทำเครื่องหมายแบบเก่า)

สำหรับOperaรูปแบบเส้นขอบจะใช้เส้นขอบในองค์ประกอบช่องทำเครื่องหมาย
Operaยังจัดการกับสไตล์อื่น ๆ ในช่องทำเครื่องหมายได้ดีกว่าเบราว์เซอร์อื่น ๆ : ใช้สีเป็นสีของขีดสีพื้นหลังจะใช้เป็นสีพื้นหลังภายในช่องทำเครื่องหมาย (IE ใช้พื้นหลังราวกับว่าช่องทำเครื่องหมายอยู่ใน<div>พื้นหลัง)) .

ข้อสรุป

วิธีแก้ปัญหาที่ง่ายที่สุดคือการรวมช่องทำเครื่องหมายไว้ข้างใน<div>เหมือนที่คนอื่นแนะนำ
หากคุณต้องการควบคุมลักษณะที่ปรากฏอย่างสมบูรณ์คุณจะต้องใช้วิธีการรูปภาพ / จาวาสคริปต์ขั้นสูงซึ่งหมายถึงผู้อื่นด้วย


2
+1 สำหรับการวิจัย แต่การรวมไว้ใน div ไม่ใช่วิธีแก้ปัญหา ไม่อนุญาตให้คุณจัดรูปแบบช่องทำเครื่องหมาย แต่จะช่วยให้คุณสามารถจัดรูปแบบรอบ ๆช่องทำเครื่องหมายได้เว้นแต่คุณจะเริ่มเล่นโดยพยายามซ่อนและทาสีทับซึ่งจะมีปัญหาในตัวเอง

เบราว์เซอร์เดียวที่ให้คุณจัดรูปแบบช่องทำเครื่องหมายคือ Opera การห่อภายใน div เป็นวิธีแก้ปัญหาที่ดีพอ แต่จริงๆแล้วขึ้นอยู่กับสิ่งที่คุณต้องการ IE จะทำหน้าที่เหมือนที่คุณใส่ไว้ใน div หากคุณพยายามจัดรูปแบบช่องทำเครื่องหมาย
กลัว

7

ช่องทำเครื่องหมายการจัดแต่งทรงผม (และองค์ประกอบอินพุตอื่น ๆ สำหรับวัสดุนั้น) เป็นไปไม่ได้จริง ๆ กับ css บริสุทธิ์หากคุณต้องการเปลี่ยนรูปลักษณ์ของภาพอย่างมาก

ทางออกที่ดีที่สุดของคุณคือการใช้บางสิ่งบางอย่างเช่นjqTransformซึ่งจะแทนที่อินพุตของคุณด้วยรูปภาพและใช้พฤติกรรมจาวาสคริปต์เพื่อเลียนแบบช่องทำเครื่องหมาย (หรือองค์ประกอบอื่น ๆ สำหรับเรื่องนั้น)


4

ไม่คุณยังไม่สามารถจัดรูปแบบช่องทำเครื่องหมายเองได้ แต่ในที่สุดฉันก็คิดวิธีจัดรูปแบบภาพลวงตาในขณะที่ยังคงฟังก์ชันการคลิกช่องทำเครื่องหมายไว้ หมายความว่าคุณสามารถสลับได้แม้ว่าเคอร์เซอร์จะยังไม่สมบูรณ์โดยไม่ต้องเสี่ยงกับการเลือกข้อความหรือเรียกใช้การลากแล้วปล่อย!

ตัวอย่างนี้ใช้ "ปุ่ม" แบบขยายและข้อความบางส่วนในป้ายกำกับ แต่จะให้แนวคิดว่าคุณจะทำให้ช่องทำเครื่องหมายมองไม่เห็นได้อย่างไรและวาดอะไรไว้ข้างหลัง

โซลูชันนี้อาจเหมาะกับปุ่มตัวเลือก

ต่อไปนี้ใช้งานได้ใน IE9, FF30.0 และ Chrome 40.0.2214.91 และเป็นเพียงตัวอย่างพื้นฐาน คุณยังสามารถใช้ร่วมกับภาพพื้นหลังและองค์ประกอบหลอกได้

http://jsfiddle.net/o0xo13yL/1/

label {
    display: inline-block;
    position: relative; /* needed for checkbox absolute positioning */
    background-color: #eee;
    padding: .5rem;
    border: 1px solid #000;
    border-radius: .375rem;
    font-family: "Courier New";
    font-size: 1rem;
    line-height: 1rem;
}

label > input[type="checkbox"] {
    display: block;
    position: absolute; /* remove it from the flow */
    width: 100%;
    height: 100%;
    margin: -.5rem; /* negative the padding of label to cover the "button" */
    cursor: pointer;
    opacity: 0; /* make it transparent */
    z-index: 666; /* place it on top of everything else */
}

label > input[type="checkbox"] + span {
    display: inline-block;
    width: 1rem;
    height: 1rem;
    border: 1px solid #000;
    margin-right: .5rem;
}

label > input[type="checkbox"]:checked + span {
    background-color: #666;
}

<label>
    <input type="checkbox" />
    <span>&nbsp;</span>Label text
</label>

2

นี่คือวิธีง่ายๆ (ใช้ก่อนหรือหลังองค์ประกอบ / คลาสหลอก):

input[type=checkbox] {
    position: relative;
}

input[type=checkbox]:after {
    position: absolute;
    top: 0;
    left: 0;
    /* Above three lines allow the checkbox:after position at checkbox's position */
    content: '';
    width: 32px;
    height: 32px;
    z-index: 1; /* This allows the after overlap the checkbox */
    /* Anything you want */
}

ฉันคิดว่านี่เป็นวิธีที่ชาญฉลาดในการปรับรูปแบบช่องทำเครื่องหมายของคุณและอะไรทำนองนี้
Alex Fang

1
ไม่ทำงานบน Firefox และ IE ที่สำคัญกว่านั้นคือการใช้งาน Chrome ที่ไม่ถูกต้องเนื่องจากองค์ประกอบที่เป็นโมฆะไม่ได้รับอนุญาตให้มีเนื้อหาใด ๆ
ลีโอ

ลีโอคุณตีความคำว่า "เนื้อหา" ผิดทั้งหมดที่กล่าวมาก็คือองค์ประกอบโมฆะเหล่านั้นไม่สามารถมีเนื้อหาคั่นกลางได้เนื่องจากไม่มีแท็กปิด ตัวอย่างเช่น <a> เนื้อหา </a> มีแท็กปิดเพื่อให้มีเนื้อหาระหว่างแท็กเหล่านั้นได้ แต่อินพุตไม่มีแท็กปิดจึงไม่สามารถมีเนื้อหาตามหลักการได้ ในทางกลับกัน Pseudo-elements มีคุณสมบัติที่เรียกว่า "content" ซึ่งอนุญาตให้ใช้ได้ทุกที่
pendingfox

-1

<!DOCTYPE html>
<html>
<head>
<style> 

.abc123
{
	-webkit-appearance:none;
    width: 14px;
    height: 14px;
    display: inline-block;
    background: #FFFFFF;
	border: 1px solid rgba(220,220,225,1);
}
.abc123:after {
  content: "";
  display: inline-block;
  position: relative;
  top: -3px;
  left: 4px;
  width: 3px;
  height: 5px;
  border-bottom: 1px solid #fff;
  border-right: 1px solid #fff;
  -webkit-transform: rotate(45deg);
}

input[type=checkbox]:checked   {
    background: #327DFF;
    outline: none;
    border: 1px solid rgba(50,125,255,1);
}
input:focus,input:active {
	outline: none;
}

input:hover {
   border: 1px solid rgba(50,125,255,1);
}

</style>
</head>
<body>

<input class="abc123" type="checkbox"></input>


</body>
</html>


แรก: ": หลัง" สร้าง√; ประการที่สอง: "-webkit-Appearance: none" ปล่อยให้รูปแบบดั้งเดิมไม่สามารถใช้งานได้จากนั้นคุณสามารถกำหนดสไตล์ของคุณได้
KeepLearn

2
โปรดเขียนคำอธิบายสั้น ๆ เกี่ยวกับคำตอบของคุณ
Angel F Syrus

-5

ใส่ลงใน div และเพิ่มเส้นขอบให้กับ div


5
จากนั้นคุณจะมีเส้นขอบสองชั้น
Egor Pavlikhin

<div style = "border-style: solid; width: 120px;"> <input type = "checkbox" name = "mycheck"> ช่องทำเครื่องหมายของฉัน </input> </div> ทำงานได้ดีกับ htmlsandbox
Midhat

8
กำหนด 'ทำงานได้ดี' ตัวอย่างเช่นฉันไม่เคยเห็นช่องทำเครื่องหมายกว้าง 120px

-8
<div style="border-style: solid;width:13px"> 
   <input type="checkbox" name="mycheck" style="margin:0;padding:0;">
   </input> 
</div>

7
@lexu ไม่มีสิ่งที่เรียกว่า </input>
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.