ฉันกำลังพยายามทำเครื่องหมายในช่องทำเครื่องหมายโดยใช้สิ่งต่อไปนี้:
<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />
แต่สไตล์ไม่ได้ใช้ ช่องทำเครื่องหมายยังคงแสดงสไตล์เริ่มต้น ฉันจะกำหนดสไตล์ที่ระบุได้อย่างไร
ฉันกำลังพยายามทำเครื่องหมายในช่องทำเครื่องหมายโดยใช้สิ่งต่อไปนี้:
<input type="checkbox" style="border:2px dotted #00f;display:block;background:#ff0000;" />
แต่สไตล์ไม่ได้ใช้ ช่องทำเครื่องหมายยังคงแสดงสไตล์เริ่มต้น ฉันจะกำหนดสไตล์ที่ระบุได้อย่างไร
คำตอบ:
UPDATE:
คำตอบด้านล่างนี้อ้างอิงถึงสถานะของสิ่งต่าง ๆ ก่อนที่ CSS 3 จะมีวางจำหน่ายอย่างกว้างขวางในเบราว์เซอร์สมัยใหม่ (รวมถึง Internet Explorer 9 และใหม่กว่า) มันเป็นเรื่องตรงไปตรงมามากขึ้นในการสร้างช่องทำเครื่องหมาย
นี่คือลิงค์ที่มีประโยชน์:
เป็นที่น่าสังเกตว่าปัญหาพื้นฐานไม่ได้เปลี่ยนไป คุณยังไม่สามารถใช้สไตล์ (เส้นขอบ ฯลฯ ) โดยตรงกับองค์ประกอบช่องทำเครื่องหมายและให้สไตล์เหล่านั้นส่งผลต่อการแสดงช่องทำเครื่องหมาย HTML อย่างไรก็ตามสิ่งที่เปลี่ยนไปคือตอนนี้คุณสามารถซ่อนกล่องกาเครื่องหมายจริงและแทนที่ด้วยองค์ประกอบที่มีสไตล์ของคุณเองโดยไม่ใช้อะไรนอกจาก CSS โดยเฉพาะอย่างยิ่งเนื่องจากตอนนี้ CSS มี:checkedตัวเลือกที่รองรับอย่างกว้างขวางคุณจึงสามารถทำการแทนที่ได้อย่างถูกต้องตามสถานะของกล่อง
คำตอบที่เก่ากว่า
ต่อไปนี้เป็นบทความที่มีประโยชน์เกี่ยวกับช่องทำเครื่องหมายจัดแต่งทรงผม โดยพื้นฐานแล้วผู้เขียนคนนั้นพบว่ามันแตกต่างกันอย่างมากมายจากเบราว์เซอร์ไปจนถึงเบราว์เซอร์และเบราว์เซอร์จำนวนมากมักแสดงช่องทำเครื่องหมายเริ่มต้นเสมอไม่ว่าคุณจะจัดสไตล์อย่างไร ดังนั้นมันจึงไม่ใช่วิธีที่ง่าย
ไม่ใช่เรื่องยากที่จะจินตนาการวิธีแก้ปัญหาที่คุณจะใช้ JavaScript เพื่อวางซ้อนภาพในช่องทำเครื่องหมายและมีการคลิกที่ภาพนั้นทำให้ช่องทำเครื่องหมายจริงถูกตรวจสอบ ผู้ใช้ที่ไม่มี JavaScript จะเห็นช่องทำเครื่องหมายเริ่มต้น
แก้ไขเพิ่ม: นี่เป็นสคริปต์ที่ดีที่ไม่นี้สำหรับคุณ ; มันซ่อนองค์ประกอบช่องทำเครื่องหมายจริงแทนที่ด้วยช่วงสไตล์และเปลี่ยนเส้นทางเหตุการณ์คลิก
มีวิธีการทำเช่นนี้โดยใช้เพียง CSS เราสามารถ (ab) ใช้labelองค์ประกอบและสไตล์ขององค์ประกอบนั้นแทน ข้อแม้คือว่าสิ่งนี้จะไม่ทำงานสำหรับ Internet Explorer 8 และรุ่นที่ต่ำกว่า
.myCheckbox input {
position: relative;
z-index: -9999;
}
.myCheckbox span {
width: 20px;
height: 20px;
display: block;
background: url("link_to_image");
}
.myCheckbox input:checked + span {
background: url("link_to_another_image");
}
<label for="test">Label for my styled "checkbox"</label>
<label class="myCheckbox">
<input type="checkbox" name="test" />
<span></span>
</label>
inputแป้นพิมพ์ที่ซ่อนอยู่ไม่เคยโฟกัสแป้นพิมพ์ดังนั้นแป้นพิมพ์เหล่านี้จึงไม่สามารถเข้าถึงได้
คุณสามารถสร้างเอฟเฟกต์ช่องทำเครื่องหมายแบบกำหนดเองได้โดยใช้ความสามารถใหม่ที่มาพร้อมกับ:afterและ:beforeคลาสเทียม ข้อได้เปรียบของสิ่งนี้คือ: คุณไม่จำเป็นต้องเพิ่มอะไรมากไปยัง DOM เพียงทำเครื่องหมายในช่องมาตรฐาน
หมายเหตุสิ่งนี้จะใช้ได้กับเบราว์เซอร์ที่ใช้งานร่วมกันได้เท่านั้น ฉันเชื่อว่าสิ่งนี้เกี่ยวข้องกับความจริงที่ว่าเบราว์เซอร์บางตัวไม่อนุญาตให้คุณตั้งค่า:afterและ:beforeในองค์ประกอบอินพุต ซึ่งน่าเสียดายที่เบราว์เซอร์ WebKit รองรับเท่านั้น Firefox + Internet Explorer จะยังคงอนุญาตให้ช่องทำเครื่องหมายทำงานเพียงแค่ไม่มีการจัดรูปแบบและหวังว่าจะมีการเปลี่ยนแปลงในอนาคต (รหัสไม่ได้ใช้คำนำหน้าผู้ขาย)
นี่เป็นโซลูชันเบราว์เซอร์ WebKit เท่านั้น (Chrome, Safari, เบราว์เซอร์มือถือ)
$(function() {
$('input').change(function() {
$('div').html(Math.random());
});
});
/* Main Classes */
.myinput[type="checkbox"]:before {
position: relative;
display: block;
width: 11px;
height: 11px;
border: 1px solid #808080;
content: "";
background: #FFF;
}
.myinput[type="checkbox"]:after {
position: relative;
display: block;
left: 2px;
top: -11px;
width: 7px;
height: 7px;
border-width: 1px;
border-style: solid;
border-color: #B3B3B3 #dcddde #dcddde #B3B3B3;
content: "";
background-image: linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
background-repeat: no-repeat;
background-position: center;
}
.myinput[type="checkbox"]:checked:after {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAQAAABuW59YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAB2SURBVHjaAGkAlv8A3QDyAP0A/QD+Dam3W+kCAAD8APYAAgTVZaZCGwwA5wr0AvcA+Dh+7UX/x24AqK3Wg/8nt6w4/5q71wAAVP9g/7rTXf9n/+9N+AAAtpJa/zf/S//DhP8H/wAA4gzWj2P4lsf0JP0A/wADAHB0Ngka6UmKAAAAAElFTkSuQmCC'), linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
}
.myinput[type="checkbox"]:disabled:after {
-webkit-filter: opacity(0.4);
}
.myinput[type="checkbox"]:not(:disabled):checked:hover:after {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAcAAAAHCAQAAABuW59YAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAIGNIUk0AAHolAACAgwAA+f8AAIDpAAB1MAAA6mAAADqYAAAXb5JfxUYAAAB2SURBVHjaAGkAlv8A3QDyAP0A/QD+Dam3W+kCAAD8APYAAgTVZaZCGwwA5wr0AvcA+Dh+7UX/x24AqK3Wg/8nt6w4/5q71wAAVP9g/7rTXf9n/+9N+AAAtpJa/zf/S//DhP8H/wAA4gzWj2P4lsf0JP0A/wADAHB0Ngka6UmKAAAAAElFTkSuQmCC'), linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);
}
.myinput[type="checkbox"]:not(:disabled):hover:after {
background-image: linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);
border-color: #85A9BB #92C2DA #92C2DA #85A9BB;
}
.myinput[type="checkbox"]:not(:disabled):hover:before {
border-color: #3D7591;
}
/* Large checkboxes */
.myinput.large {
height: 22px;
width: 22px;
}
.myinput.large[type="checkbox"]:before {
width: 20px;
height: 20px;
}
.myinput.large[type="checkbox"]:after {
top: -20px;
width: 16px;
height: 16px;
}
/* Custom checkbox */
.myinput.large.custom[type="checkbox"]:checked:after {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23fZidLAAAAk0lEQVQ4y2P4//8/AyUYwcAD+OzN/oMwshjRBoA0Gr8+DcbIhhBlAEyz+qZZ/7WPryHNAGTNMOxpJvo/w0/uP0kGgGwGaZbrKgfTGnLc/0nyAgiDbEY2BCRGdCDCnA2yGeYVog0Aae5MV4c7Gzk6CRqAbDM2w/EaQEgzXgPQnU2SAcTYjNMAYm3GaQCxNuM0gFwMAPUKd8XyBVDcAAAAAElFTkSuQmCC'), linear-gradient(135deg, #B1B6BE 0%, #FFF 100%);
}
.myinput.large.custom[type="checkbox"]:not(:disabled):checked:hover:after {
background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGHRFWHRBdXRob3IAbWluZWNyYWZ0aW5mby5jb23fZidLAAAAk0lEQVQ4y2P4//8/AyUYwcAD+OzN/oMwshjRBoA0Gr8+DcbIhhBlAEyz+qZZ/7WPryHNAGTNMOxpJvo/w0/uP0kGgGwGaZbrKgfTGnLc/0nyAgiDbEY2BCRGdCDCnA2yGeYVog0Aae5MV4c7Gzk6CRqAbDM2w/EaQEgzXgPQnU2SAcTYjNMAYm3GaQCxNuM0gFwMAPUKd8XyBVDcAAAAAElFTkSuQmCC'), linear-gradient(135deg, #8BB0C2 0%, #FFF 100%);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table style="width:100%">
<tr>
<td>Normal:</td>
<td><input type="checkbox" /></td>
<td><input type="checkbox" checked="checked" /></td>
<td><input type="checkbox" disabled="disabled" /></td>
<td><input type="checkbox" disabled="disabled" checked="checked" /></td>
</tr>
<tr>
<td>Small:</td>
<td><input type="checkbox" class="myinput" /></td>
<td><input type="checkbox" checked="checked" class="myinput" /></td>
<td><input type="checkbox" disabled="disabled" class="myinput" /></td>
<td><input type="checkbox" disabled="disabled" checked="checked" class="myinput" /></td>
</tr>
<tr>
<td>Large:</td>
<td><input type="checkbox" class="myinput large" /></td>
<td><input type="checkbox" checked="checked" class="myinput large" /></td>
<td><input type="checkbox" disabled="disabled" class="myinput large" /></td>
<td><input type="checkbox" disabled="disabled" checked="checked" class="myinput large" /></td>
</tr>
<tr>
<td>Custom icon:</td>
<td><input type="checkbox" class="myinput large custom" /></td>
<td><input type="checkbox" checked="checked" class="myinput large custom" /></td>
<td><input type="checkbox" disabled="disabled" class="myinput large custom" /></td>
<td><input type="checkbox" disabled="disabled" checked="checked" class="myinput large custom" /></td>
</tr>
</table>
$(function() {
var f = function() {
$(this).next().text($(this).is(':checked') ? ':checked' : ':not(:checked)');
};
$('input').change(f).trigger('change');
});
body {
font-family: arial;
}
.flipswitch {
position: relative;
background: white;
width: 120px;
height: 40px;
-webkit-appearance: initial;
border-radius: 3px;
-webkit-tap-highlight-color: rgba(0, 0, 0, 0);
outline: none;
font-size: 14px;
font-family: Trebuchet, Arial, sans-serif;
font-weight: bold;
cursor: pointer;
border: 1px solid #ddd;
}
.flipswitch:after {
position: absolute;
top: 5%;
display: block;
line-height: 32px;
width: 45%;
height: 90%;
background: #fff;
box-sizing: border-box;
text-align: center;
transition: all 0.3s ease-in 0s;
color: black;
border: #888 1px solid;
border-radius: 3px;
}
.flipswitch:after {
left: 2%;
content: "OFF";
}
.flipswitch:checked:after {
left: 53%;
content: "ON";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<h2>Webkit friendly mobile-style checkbox/flipswitch</h2>
<input type="checkbox" class="flipswitch" />
<span></span>
<br>
<input type="checkbox" checked="checked" class="flipswitch" />
<span></span>
คำถามและคำตอบเดิมตอนนี้ ~ 5 ปี เช่นนี้เป็นการอัปเดตเล็กน้อย
ประการแรกมีหลายวิธีเมื่อมันมาถึงกล่องใส่สไตล์ ทฤษฎีพื้นฐานคือ:
คุณจะต้องซ่อนการควบคุมช่องทำเครื่องหมายเริ่มต้นซึ่งเป็นสไตล์ของเบราว์เซอร์ของคุณและไม่สามารถแทนที่ด้วยวิธีที่มีความหมายใด ๆ โดยใช้ CSS
ด้วยการซ่อนการควบคุมคุณจะยังคงสามารถตรวจจับและสลับสถานะการตรวจสอบได้
สถานะการตรวจสอบของช่องทำเครื่องหมายจะต้องสะท้อนให้เห็นโดยจัดแต่งองค์ประกอบใหม่
ข้างต้นสามารถทำได้โดยวิธีการต่าง ๆ - และคุณมักจะได้ยินการใช้องค์ประกอบหลอกหลอก CSS3 เป็นวิธีที่ถูกต้อง จริง ๆ แล้วไม่มีวิธีที่ถูกหรือผิดจริงขึ้นอยู่กับวิธีที่เหมาะสมที่สุดสำหรับบริบทที่คุณจะใช้สิ่งต่อไปนี้ที่กล่าวว่าฉันมีสิ่งที่ต้องการ
ล้อมช่องทำเครื่องหมายของคุณไว้ในlabelองค์ประกอบ ซึ่งหมายความว่าแม้ว่าจะซ่อนอยู่คุณยังสามารถสลับสถานะที่เลือกเมื่อคลิกที่ใดก็ได้ภายในป้ายกำกับ
ซ่อนช่องทำเครื่องหมายของคุณ
เพิ่มองค์ประกอบใหม่หลังจากช่องทำเครื่องหมายที่คุณจะจัดรูปแบบตามลำดับ มันจะต้องปรากฏหลังช่องทำเครื่องหมายเพื่อให้สามารถเลือกได้โดยใช้ CSS และสไตล์ขึ้นอยู่กับ:checkedสถานะ CSS ไม่สามารถเลือก 'ย้อนกลับ'
แต่เดี๋ยวก่อน! ฉันได้ยินคุณตะโกน จะเป็นอย่างไรถ้าฉันต้องการที่จะแสดงเห็บน้อย ๆ หรือข้ามในกล่อง และฉันไม่ต้องการใช้ภาพพื้นหลัง!
นี่คือสิ่งที่องค์ประกอบหลอกของ CSS3 สามารถเข้ามาเล่นได้ สิ่งเหล่านี้สนับสนุนcontentคุณสมบัติที่ช่วยให้คุณสามารถฉีดไอคอน Unicode ที่เป็นตัวแทนของทั้งสองรัฐ หรือคุณสามารถใช้แหล่งไอคอนแบบอักษรของบุคคลที่สามเช่นแบบอักษรที่ยอดเยี่ยม (แต่ให้แน่ใจว่าคุณได้ตั้งค่าที่เกี่ยวข้องfont-familyเช่นFontAwesome)
label input {
display: none; /* Hide the default checkbox */
}
/* Style the artificial checkbox */
label span {
height: 10px;
width: 10px;
border: 1px solid grey;
display: inline-block;
position: relative;
}
/* Style its checked state...with a ticked icon */
[type=checkbox]:checked + span:before {
content: '\2714';
position: absolute;
top: -5px;
left: 0;
}
<label>
<input type='checkbox'>
<span></span>
Checkbox label text
</label>
display: noneกับ
visibility: hidden;ด้วยopacity: 0 !important;หากคุณยังมีปัญหากับการแท็บ
ฉันจะทำตามคำแนะนำของคำตอบของ SW4 - เพื่อซ่อนกล่องกาเครื่องหมายและเพื่อให้ครอบคลุมด้วยการกำหนดเองแนะนำ HTML นี้:
<label>
<input type="checkbox">
<span>send newsletter</span>
</label>
การตัดในเลเบลอนุญาตให้คลิกข้อความโดยไม่จำเป็นต้องเชื่อมโยงแอตทริบิวต์ "for-id" อย่างไรก็ตาม
visibility: hiddenหรือdisplay: noneมันทำงานได้โดยคลิกหรือแตะ แต่นั่นเป็นวิธีที่อ่อนแอในการใช้ช่องทำเครื่องหมาย บางคนยังคงใช้งานได้อย่างมีประสิทธิภาพมากขึ้นTabในการย้ายโฟกัสSpaceเพื่อเปิดใช้งานและการซ่อนด้วยวิธีดังกล่าวจะปิดใช้งาน หากแบบฟอร์มมีความยาวหนึ่งจะบันทึกข้อมือของใครบางคนที่จะใช้tabindexหรือaccesskeyคุณลักษณะ และถ้าคุณสังเกตพฤติกรรมของช่องทำเครื่องหมายระบบจะมีเงาที่เหมาะสมเมื่อวางเมาส์เหนือ ช่องทำเครื่องหมายสไตล์ดีควรเป็นไปตามพฤติกรรมนี้
คำตอบของ cobberboyแนะนำFont Awesomeซึ่งโดยปกติจะดีกว่าบิตแมปเนื่องจากแบบอักษรเป็นเวกเตอร์ที่ปรับขนาดได้ ทำงานกับ HTML ด้านบนฉันจะแนะนำกฎ CSS เหล่านี้:
ซ่อนช่องทำเครื่องหมาย
input[type="checkbox"] {
position: absolute;
opacity: 0;
z-index: -1;
}
ฉันใช้ลบเพียงz-indexเพราะตัวอย่างของฉันใช้กล่องกาเครื่องหมายขนาดใหญ่พอที่จะครอบคลุมได้อย่างเต็มที่ ฉันไม่แนะนำleft: -999pxเนื่องจากไม่สามารถใช้ซ้ำได้ในทุกโครงร่าง คำตอบของ Bushan waghให้วิธีการกันกระสุนเพื่อซ่อนมันและโน้มน้าวให้เบราว์เซอร์ใช้ tabindex ดังนั้นจึงเป็นทางเลือกที่ดี อย่างไรก็ตามทั้งคู่เป็นเพียงแฮ็ค วิธีที่เหมาะสมในวันนี้คือappearance: noneดูคำตอบของ Joost :
input[type="checkbox"] {
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}ป้ายกำกับช่องทำเครื่องหมายสไตล์
input[type="checkbox"] + span {
font: 16pt sans-serif;
color: #000;
}เพิ่มช่องทำเครื่องหมายผิว
input[type="checkbox"] + span:before {
font: 16pt FontAwesome;
content: '\00f096';
display: inline-block;
width: 16pt;
padding: 2px 0 0 3px;
margin-right: 0.5em;
}
\00f096เป็นแบบอักษรที่ยอดเยี่ยมของการsquare-oขยายมีการปรับเพื่อให้เค้าร่างจุดประแม้ในโฟกัส (ดูด้านล่าง)
เพิ่มช่องทำเครื่องหมายตรวจสอบผิว
input[type="checkbox"]:checked + span:before {
content: '\00f046';
}
\00f046คือ Font Awesome check-square-oซึ่งไม่กว้างเท่าsquare-oกันซึ่งเป็นสาเหตุของสไตล์ความกว้างด้านบน
เพิ่มเค้าร่างโฟกัส
input[type="checkbox"]:focus + span:before {
outline: 1px dotted #aaa;
}
Safari ไม่ได้มีคุณสมบัตินี้ (ดูความคิดเห็นของ @Jason Sankey) คุณควรใช้window.navigatorเพื่อตรวจจับเบราว์เซอร์และข้ามมันหากเป็น Safari
ตั้งค่าสีเทาสำหรับช่องทำเครื่องหมายที่ปิดใช้งาน
input[type="checkbox"]:disabled + span {
color: #999;
}ตั้งค่าเงาโฮเวอร์บนช่องทำเครื่องหมายที่ไม่ได้ปิดใช้งาน
input[type="checkbox"]:not(:disabled) + span:hover:before {
text-shadow: 0 1px 2px #77F;
}ลองเลื่อนเม้าส์ไปที่ช่องทำเครื่องหมายและใช้TabและShift+ Tabเพื่อย้ายและSpaceสลับ
คุณสามารถจัดสไตล์ช่องทำเครื่องหมายด้วยเล่ห์เหลี่ยมเล็ก ๆ โดยใช้labelองค์ประกอบดังตัวอย่างด้านล่าง:
.checkbox > input[type=checkbox] {
visibility: hidden;
}
.checkbox {
position: relative;
display: block;
width: 80px;
height: 26px;
margin: 0 auto;
background: #FFF;
border: 1px solid #2E2E2E;
border-radius: 2px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
}
.checkbox:after {
position: absolute;
display: inline;
right: 10px;
content: 'no';
color: #E53935;
font: 12px/26px Arial, sans-serif;
font-weight: bold;
text-transform: capitalize;
z-index: 0;
}
.checkbox:before {
position: absolute;
display: inline;
left: 10px;
content: 'yes';
color: #43A047;
font: 12px/26px Arial, sans-serif;
font-weight: bold;
text-transform: capitalize;
z-index: 0;
}
.checkbox label {
position: absolute;
display: block;
top: 3px;
left: 3px;
width: 34px;
height: 20px;
background: #2E2E2E;
cursor: pointer;
transition: all 0.5s linear;
-webkit-transition: all 0.5s linear;
-moz-transition: all 0.5s linear;
border-radius: 2px;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
z-index: 1;
}
.checkbox input[type=checkbox]:checked + label {
left: 43px;
}
<div class="checkbox">
<input id="checkbox1" type="checkbox" value="1" />
<label for="checkbox1"></label>
</div>
และFIDDLEสำหรับโค้ดด้านบน โปรดทราบว่า CSS บางตัวไม่ทำงานในเบราว์เซอร์เวอร์ชันเก่า แต่ฉันแน่ใจว่ามีตัวอย่าง JavaScript แฟนซีอยู่ที่นั่น!
หลังจากการค้นหาและทดสอบมากมายฉันได้รับโซลูชันนี้ซึ่งใช้งานง่ายและปรับแต่งได้ง่ายขึ้น ในการแก้ปัญหานี้ :
ใส่ CSS แบบต่อเนื่องที่ด้านบนของหน้าของคุณและสไตล์ช่องทำเครื่องหมายทั้งหมดจะเปลี่ยนไปเช่นนี้
input[type=checkbox] {
transform: scale(1.5);
}
input[type=checkbox] {
width: 30px;
height: 30px;
margin-right: 8px;
cursor: pointer;
font-size: 17px;
visibility: hidden;
}
input[type=checkbox]:after {
content: " ";
background-color: #fff;
display: inline-block;
margin-left: 10px;
padding-bottom: 5px;
color: #00BFF0;
width: 22px;
height: 25px;
visibility: visible;
border: 1px solid #00BFF0;
padding-left: 3px;
border-radius: 5px;
}
input[type=checkbox]:checked:after {
content: "\2714";
padding: -5px;
font-weight: bold;
}
คุณสามารถหลีกเลี่ยงการเพิ่มมาร์กอัปพิเศษ นี้ทำงานได้ทุกที่ยกเว้น IE สำหรับเดสก์ท็ ( แต่ผลงานใน IE สำหรับ Windows Phone และ Microsoft ขอบ) ผ่านการตั้งค่า CSS appearance:
input[type="checkbox"] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
/* Styling checkbox */
width: 16px;
height: 16px;
background-color: red;
}
input[type="checkbox"]:checked {
background-color: green;
}
<input type="checkbox" />
ฉันชอบที่จะใช้แบบอักษรไอคอน (เช่น FontAwesome) เนื่องจากง่ายต่อการปรับเปลี่ยนสีของพวกเขาด้วย CSS และพวกเขาปรับขนาดได้ดีบนอุปกรณ์ที่มีความหนาแน่นพิกเซลสูง ดังนั้นนี่คือตัวแปร CSS บริสุทธิ์อีกตัวโดยใช้เทคนิคที่คล้ายคลึงกับที่กล่าวมาข้างต้น
(ด้านล่างเป็นรูปแบบภาพนิ่งเพื่อให้คุณสามารถเห็นภาพผลลัพธ์ดูJSFiddleสำหรับรุ่นแบบโต้ตอบ)

เช่นเดียวกับโซลูชันอื่น ๆ มันใช้labelองค์ประกอบ ที่อยู่ติดกันspanถือตัวละครช่องทำเครื่องหมายของเรา
span.bigcheck-target {
font-family: FontAwesome; /* Use an icon font for the checkbox */
}
input[type='checkbox'].bigcheck {
position: relative;
left: -999em; /* Hide the real checkbox */
}
input[type='checkbox'].bigcheck + span.bigcheck-target:after {
content: "\f096"; /* In fontawesome, is an open square (fa-square-o) */
}
input[type='checkbox'].bigcheck:checked + span.bigcheck-target:after {
content: "\f046"; /* fontawesome checked box (fa-check-square-o) */
}
/* ==== Optional - colors and padding to make it look nice === */
body {
background-color: #2C3E50;
color: #D35400;
font-family: sans-serif;
font-weight: 500;
font-size: 4em; /* Set this to whatever size you want */
}
span.bigcheck {
display: block;
padding: 0.5em;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
<span class="bigcheck">
<label class="bigcheck">
Cheese
<input type="checkbox" class="bigcheck" name="cheese" value="yes" />
<span class="bigcheck-target"></span>
</label>
</span>
นี่คือJSFiddleสำหรับมัน
font-family: FontAwesome;ด้วยfont-family: 'Font Awesome\ 5 Free';และอัปเดตเนื้อหา Unicode หากคุณต้องการให้มันทำงานในเวอร์ชั่นใหม่
เมื่อเร็ว ๆ นี้ฉันพบวิธีแก้ไขปัญหาที่น่าสนใจทีเดียว
คุณสามารถใช้appearance: none;เพื่อปิดช่องทำเครื่องหมายรูปแบบเริ่มต้นและจากนั้นเขียนของคุณเองมากกว่านั้นชอบอธิบายนี่ (ตัวอย่างที่ 4)
น่าเสียดายที่การสนับสนุนเบราว์เซอร์ค่อนข้างแย่สำหรับappearanceตัวเลือก จากการทดสอบส่วนตัวของฉันฉันทำให้ Opera และ Chrome ทำงานได้อย่างถูกต้อง แต่นี่จะเป็นวิธีที่จะทำให้มันง่ายเมื่อมีการสนับสนุนที่ดีกว่าหรือคุณต้องการใช้ Chrome / Opera เท่านั้น
appearanceคือสิ่งที่เราต้องการสำหรับสิ่งนี้ เพียงจำไว้ว่ามัน"เป็นที่ไม่ได้มาตรฐานและไม่ได้อยู่ในมาตรฐานการติดตาม"
ด้วย CSS บริสุทธิ์ไม่มีอะไรน่าแปลกใจด้วย:beforeและ:afterไม่มีการแปลงคุณสามารถปิดลักษณะที่ปรากฏเป็นค่าเริ่มต้นแล้วจัดสไตล์ด้วยภาพพื้นหลังแบบอินไลน์เช่นตัวอย่างต่อไปนี้ สามารถใช้งานได้ใน Chrome, Firefox, Safari และตอนนี้ Edge (Chromium Edge)
INPUT[type=checkbox]:focus
{
outline: 1px solid rgba(0, 0, 0, 0.2);
}
INPUT[type=checkbox]
{
background-color: #DDD;
border-radius: 2px;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
width: 17px;
height: 17px;
cursor: pointer;
position: relative;
top: 5px;
}
INPUT[type=checkbox]:checked
{
background-color: #409fd6;
background: #409fd6 url("data:image/gif;base64,R0lGODlhCwAKAIABAP////3cnSH5BAEKAAEALAAAAAALAAoAAAIUjH+AC73WHIsw0UCjglraO20PNhYAOw==") 3px 3px no-repeat;
}
<form>
<label><input type="checkbox">I Agree To Terms & Conditions</label>
</form>
input[type="checkbox"] {
cursor: pointer;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
outline: 0;
background: lightgray;
height: 16px;
width: 16px;
border: 1px solid white;
}
input[type="checkbox"]:checked {
background: #2aa1c0;
}
input[type="checkbox"]:hover {
filter: brightness(90%);
}
input[type="checkbox"]:disabled {
background: #e6e6e6;
opacity: 0.6;
pointer-events: none;
}
input[type="checkbox"]:after {
content: '';
position: relative;
left: 40%;
top: 20%;
width: 15%;
height: 40%;
border: solid #fff;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
display: none;
}
input[type="checkbox"]:checked:after {
display: block;
}
input[type="checkbox"]:disabled:after {
border-color: #7b7b7b;
}
<input type="checkbox"><br>
<input type="checkbox" checked><br>
<input type="checkbox" disabled><br>
<input type="checkbox" disabled checked><br>
คุณสามารถใช้appearance: noneกับเบราว์เซอร์ที่ทันสมัยเพื่อไม่ให้มีสไตล์เริ่มต้นและสไตล์ทั้งหมดของคุณจะถูกนำไปใช้อย่างถูกต้อง:
input[type=checkbox] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
display: inline-block;
width: 2em;
height: 2em;
border: 1px solid gray;
outline: none;
vertical-align: middle;
}
input[type=checkbox]:checked {
background-color: blue;
}
ฉันได้รับการเลื่อนและเลื่อนและตันของคำตอบเหล่านี้ก็โยนการเข้าถึงออกประตูและละเมิด WCAGในมากกว่าหนึ่งวิธี ฉันขว้างปุ่มตัวเลือกนับตั้งแต่เวลาส่วนใหญ่เมื่อคุณใช้ช่องทำเครื่องหมายที่กำหนดเองคุณต้องการปุ่มตัวเลือกที่กำหนดเองเช่นกัน
ซอ :
ไปงานปาร์ตี้สาย แต่อย่างใดนี่ก็ยังคงเป็นเรื่องยากในปี 2019 2020 ดังนั้นฉันจึงได้เพิ่มสามโซลูชั่นของฉันซึ่งสามารถเข้าถึงได้และง่ายต่อการดรอป
ทั้งหมดนี้เป็นJavaScript ฟรี , ไลบรารีภายนอกฟรี * , และเข้าถึง ...
หากคุณต้องการ plug-n-play กับสิ่งเหล่านี้เพียงแค่คัดลอกสไตล์ชีทจาก fiddles แก้ไขรหัสสีใน CSS เพื่อให้เหมาะกับความต้องการของคุณและไปในทางของคุณ คุณสามารถเพิ่มไอคอนเครื่องหมายถูก svg ที่กำหนดเองหากคุณต้องการสำหรับช่องทำเครื่องหมาย ฉันได้เพิ่มความคิดเห็นมากมายสำหรับคนที่ไม่ใช่ CSS'y
หากคุณมีข้อความยาวหรือภาชนะขนาดเล็กและกำลังเผชิญกับการตัดข้อความใต้ช่องทำเครื่องหมายหรืออินพุตปุ่มตัวเลือกจากนั้นเพียงแค่แปลงเป็น div เช่นนี้เช่นนี้
คำอธิบายอีกต่อไป:
ฉันต้องการโซลูชันที่ไม่ละเมิด WCAG, ไม่ต้องพึ่งพา JavaScript หรือไลบรารี่ภายนอก, และไม่ทำลายการนำทางคีย์บอร์ดเช่นการแท็บหรือสเปซบาร์เพื่อเลือก, ที่ช่วยให้โฟกัสเหตุการณ์ มีทั้งการตรวจสอบและไม่ได้ตรวจสอบและในที่สุดก็แก้ปัญหาที่ฉันสามารถปรับแต่งรูปลักษณ์ของช่อง แต่ฉันต้องการที่แตกต่างกันกับbackground-color's, border-radius, svgพื้นหลัง, ฯลฯ
ฉันใช้การรวมกันของคำตอบนี้จาก @Jan Turoňเพื่อหาทางออกของตัวเองซึ่งดูเหมือนว่าจะทำงานได้ค่อนข้างดี ฉันได้ทำซอปุ่มตัวเลือกที่ใช้รหัสเดียวกันจำนวนมากจากช่องทำเครื่องหมายเพื่อที่จะทำให้การทำงานกับปุ่มตัวเลือกนี้เกินไป
ฉันยังคงเรียนรู้การเข้าถึงดังนั้นหากฉันพลาดบางสิ่งบางอย่างโปรดส่งความคิดเห็นและฉันจะพยายามแก้ไขให้ถูกต้องที่นี่คือตัวอย่างโค้ดของช่องทำเครื่องหมายของฉัน:
input[type="checkbox"] {
position: absolute;
opacity: 0;
z-index: -1;
}
/* Text color for the label */
input[type="checkbox"]+span {
cursor: pointer;
font: 16px sans-serif;
color: black;
}
/* Checkbox un-checked style */
input[type="checkbox"]+span:before {
content: '';
border: 1px solid grey;
border-radius: 3px;
display: inline-block;
width: 16px;
height: 16px;
margin-right: 0.5em;
margin-top: 0.5em;
vertical-align: -2px;
}
/* Checked checkbox style (in this case the background is red) */
input[type="checkbox"]:checked+span:before {
/* NOTE: Replace the url with a path to an SVG of a checkmark to get a checkmark icon */
background-image: url('https://cdnjs.cloudflare.com/ajax/libs/ionicons/4.5.6/collection/build/ionicons/svg/ios-checkmark.svg');
background-repeat: no-repeat;
background-position: center;
/* The size of the checkmark icon, you may/may not need this */
background-size: 25px;
border-radius: 2px;
background-color: #e7ffba;
color: white;
}
/* Adding a dotted border around the active tabbed-into checkbox */
input[type="checkbox"]:focus+span:before,
input[type="checkbox"]:not(:disabled)+span:hover:before {
/* Visible in the full-color space */
box-shadow: 0px 0px 0px 2px rgba(0, 150, 255, 1);
/* Visible in Windows high-contrast themes
box-shadow will be hidden in these modes and
transparency will not be hidden in high-contrast
thus box-shadow will not show but the outline will
providing accessibility */
outline-color: transparent; /*switch to transparent*/
outline-width: 2px;
outline-style: dotted;
}
/* Disabled checkbox styles */
input[type="checkbox"]:disabled+span {
cursor: default;
color: black;
opacity: 0.5;
}
/* Styles specific to this fiddle that you do not need */
body {
padding: 1em;
}
h1 {
font-size: 18px;
}
<h1>
NOTE: Replace the url for the background-image in CSS with a path to an SVG in your solution or CDN. This one was found from a quick google search for a checkmark icon cdn
</h1>
<label>
<input type="checkbox">
<span>Try using tab and space</span>
</label>
<br>
<label>
<input type="checkbox" checked disabled>
<span>Disabled Checked Checkbox</span>
</label>
<br>
<label>
<input type="checkbox" disabled>
<span>Disabled Checkbox</span>
</label>
<br>
<label>
<input type="checkbox">
<span>Normal Checkbox</span>
</label>
<br>
<label>
<input type="checkbox">
<span>Another Normal Checkbox</span>
</label>
นี่คือโซลูชัน CSS อย่างง่ายที่ไม่มี jQuery หรือรหัส JavaScript
ฉันใช้ไอคอน FontAwseome แต่คุณสามารถใช้ภาพใดก็ได้
input[type=checkbox] {
display: inline-block;
font-family: FontAwesome;
font-style: normal;
font-weight: normal;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
visibility: hidden;
font-size: 14px;
}
input[type=checkbox]:before {
content: @fa-var-square-o;
visibility: visible;
/*font-size: 12px;*/
}
input[type=checkbox]:checked:before {
content: @fa-var-check-square-o;
}
labelหรือspanองค์ประกอบ มันใช้งานได้!
จาก googling ของฉันนี่เป็นวิธีที่ง่ายที่สุดในการใส่สไตล์ของเช็คบ็อกซ์ เพียงเพิ่ม:afterและ:checked:afterCSS ตามการออกแบบของคุณ
body{
background: #DDD;
}
span{
margin-left: 30px;
}
input[type=checkbox] {
cursor: pointer;
font-size: 17px;
visibility: hidden;
position: absolute;
top: 0;
left: 0;
transform: scale(1.5);
}
input[type=checkbox]:after {
content: " ";
background-color: #fff;
display: inline-block;
color: #00BFF0;
width: 14px;
height: 19px;
visibility: visible;
border: 1px solid #FFF;
padding: 0 3px;
margin: 2px 0;
border-radius: 8px;
box-shadow: 0 0 15px 0 rgba(0,0,0,0.08), 0 0 2px 0 rgba(0,0,0,0.16);
}
input[type=checkbox]:checked:after {
content: "\2714";
display: unset;
font-weight: bold;
}
<input type="checkbox"> <span>Select Text</span>
แก้ไขสไตล์ช่องทำเครื่องหมายด้วย CSS3 ธรรมดาไม่จำเป็นต้องใช้การจัดการ JS & HTML ใด ๆ
.form input[type="checkbox"]:before {
display: inline-block;
font: normal normal normal 14px/1 FontAwesome;
font-size: inherit;
text-rendering: auto;
-webkit-font-smoothing: antialiased;
content: "\f096";
opacity: 1 !important;
margin-top: -25px;
appearance: none;
background: #fff;
}
.form input[type="checkbox"]:checked:before {
content: "\f046";
}
.form input[type="checkbox"] {
font-size: 22px;
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<form class="form">
<input type="checkbox" />
</form>
เทมเพลตที่เรียบง่ายและมีน้ำหนักเบาเช่นกัน:
input[type=checkbox] {
cursor: pointer;
}
input[type=checkbox]:checked:before {
content: "\2713";
background: #fffed5;
text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
font-size: 20px;
text-align: center;
line-height: 8px;
display: inline-block;
width: 13px;
height: 15px;
color: #00904f;
border: 1px solid #cdcdcd;
border-radius: 4px;
margin: -3px -3px;
text-indent: 1px;
}
input[type=checkbox]:before {
content: "\202A";
background: #ffffff;
text-shadow: 1px 1px 1px rgba(0, 0, 0, .2);
font-size: 20px;
text-align: center;
line-height: 8px;
display: inline-block;
width: 13px;
height: 15px;
color: #00904f;
border: 1px solid #cdcdcd;
border-radius: 4px;
margin: -3px -3px;
text-indent: 1px;
}
<input type="checkbox" checked="checked">checked1<br>
<input type="checkbox">unchecked2<br>
<input type="checkbox" checked="checked" id="id1">
<label for="id1">checked2+label</label><br>
<label for="id2">unchecked2+label+rtl</label>
<input type="checkbox" id="id2">
<br>
inputs รองรับเฉพาะใน Chrome โค้ดของคุณไม่ทำงานเหมือนกันในทุกเบราว์เซอร์
ฉันคิดว่าวิธีที่ง่ายที่สุดที่จะทำคือการใส่สไตล์labelและสร้างcheckboxมองไม่เห็น
HTML
<input type="checkbox" id="first" />
<label for="first"> </label>
CSS
checkbox {
display: none;
}
checkbox + label {
/* Style for checkbox normal */
width: 16px;
height: 16px;
}
checkbox::checked + label,
label.checked {
/* Style for checkbox checked */
}
checkboxแม้ว่ามันจะถูกซ่อนอยู่จะยังคงสามารถเข้าถึงได้และความคุ้มค่าของมันจะถูกส่งเมื่อส่งแบบฟอร์ม เบราว์เซอร์เก่าที่คุณอาจจะมีการเปลี่ยนแปลงระดับของlabelการตรวจสอบการใช้ JavaScript เพราะผมไม่คิดว่ารุ่นเก่าของ Internet Explorer เข้าใจใน::checkedcheckbox
::checked :checked(b) checkboxผิด - ควรเป็น[type=checkbox]
อ๊ะ! วิธีแก้ไขปัญหาทั้งหมดเหล่านี้ทำให้ฉันสรุปได้ว่าช่องทำเครื่องหมาย HTML นั้นแย่มากหากคุณต้องการจัดสไตล์
เป็นการเตือนล่วงหน้านี่ไม่ใช่การใช้ CSS ฉันแค่คิดว่าฉันจะแบ่งปันวิธีแก้ปัญหาที่ฉันคิดไว้ในกรณีที่คนอื่นอาจเห็นว่ามีประโยชน์
ฉันใช้canvasองค์ประกอบHTML5
ข้อเสียคือคุณไม่จำเป็นต้องใช้รูปภาพภายนอกและอาจช่วยประหยัดแบนด์วิดท์ได้
ข้อเสียคือถ้าเบราว์เซอร์ด้วยเหตุผลบางอย่างไม่สามารถแสดงได้อย่างถูกต้องแล้วไม่มีทางเลือก แม้ว่าจะเป็นปัญหาในปี 2560 หรือไม่
ฉันพบรหัสเก่าค่อนข้างน่าเกลียดดังนั้นฉันจึงตัดสินใจที่จะเขียนใหม่
Object.prototype.create = function(args){
var retobj = Object.create(this);
retobj.constructor(args || null);
return retobj;
}
var Checkbox = Object.seal({
width: 0,
height: 0,
state: 0,
document: null,
parent: null,
canvas: null,
ctx: null,
/*
* args:
* name default desc.
*
* width 15 width
* height 15 height
* document window.document explicit document reference
* target this.document.body target element to insert checkbox into
*/
constructor: function(args){
if(args === null)
args = {};
this.width = args.width || 15;
this.height = args.height || 15;
this.document = args.document || window.document;
this.parent = args.target || this.document.body;
this.canvas = this.document.createElement("canvas");
this.ctx = this.canvas.getContext('2d');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.canvas.addEventListener("click", this.ev_click(this), false);
this.parent.appendChild(this.canvas);
this.draw();
},
ev_click: function(self){
return function(unused){
self.state = !self.state;
self.draw();
}
},
draw_rect: function(color, offset){
this.ctx.fillStyle = color;
this.ctx.fillRect(offset, offset,
this.width - offset * 2, this.height - offset * 2);
},
draw: function(){
this.draw_rect("#CCCCCC", 0);
this.draw_rect("#FFFFFF", 1);
if(this.is_checked())
this.draw_rect("#000000", 2);
},
is_checked: function(){
return !!this.state;
}
});
รุ่นใหม่ใช้ต้นแบบและการสืบทอดที่แตกต่างกันเพื่อสร้างระบบที่มีประสิทธิภาพสำหรับการสร้างช่องทำเครื่องหมาย ในการสร้างช่องทำเครื่องหมาย:
var my_checkbox = Checkbox.create();
การดำเนินการนี้จะเพิ่มช่องทำเครื่องหมายใน DOM และเชื่อมโยงเหตุการณ์ หากต้องการสอบถามว่ามีการทำเครื่องหมายในช่องทำเครื่องหมาย:
my_checkbox.is_checked(); // True if checked, else false
สิ่งสำคัญที่ควรทราบคือฉันกำจัดวงวน
สิ่งที่ฉันละเลยที่จะพูดถึงในการอัปเดตครั้งล่าสุดคือการใช้ Canvas มีข้อดีมากกว่าแค่สร้างช่องทำเครื่องหมายที่มีลักษณะตามที่คุณต้องการ คุณสามารถสร้างช่องทำเครื่องหมายหลายสถานะได้ถ้าต้องการ
Object.prototype.create = function(args){
var retobj = Object.create(this);
retobj.constructor(args || null);
return retobj;
}
Object.prototype.extend = function(newobj){
var oldobj = Object.create(this);
for(prop in newobj)
oldobj[prop] = newobj[prop];
return Object.seal(oldobj);
}
var Checkbox = Object.seal({
width: 0,
height: 0,
state: 0,
document: null,
parent: null,
canvas: null,
ctx: null,
/*
* args:
* name default desc.
*
* width 15 width
* height 15 height
* document window.document explicit document reference
* target this.document.body target element to insert checkbox into
*/
constructor: function(args){
if(args === null)
args = {};
this.width = args.width || 15;
this.height = args.height || 15;
this.document = args.document || window.document;
this.parent = args.target || this.document.body;
this.canvas = this.document.createElement("canvas");
this.ctx = this.canvas.getContext('2d');
this.canvas.width = this.width;
this.canvas.height = this.height;
this.canvas.addEventListener("click", this.ev_click(this), false);
this.parent.appendChild(this.canvas);
this.draw();
},
ev_click: function(self){
return function(unused){
self.state = !self.state;
self.draw();
}
},
draw_rect: function(color, offsetx, offsety){
this.ctx.fillStyle = color;
this.ctx.fillRect(offsetx, offsety,
this.width - offsetx * 2, this.height - offsety * 2);
},
draw: function(){
this.draw_rect("#CCCCCC", 0, 0);
this.draw_rect("#FFFFFF", 1, 1);
this.draw_state();
},
draw_state: function(){
if(this.is_checked())
this.draw_rect("#000000", 2, 2);
},
is_checked: function(){
return this.state == 1;
}
});
var Checkbox3 = Checkbox.extend({
ev_click: function(self){
return function(unused){
self.state = (self.state + 1) % 3;
self.draw();
}
},
draw_state: function(){
if(this.is_checked())
this.draw_rect("#000000", 2, 2);
if(this.is_partial())
this.draw_rect("#000000", 2, (this.height - 2) / 2);
},
is_partial: function(){
return this.state == 2;
}
});
ฉันปรับเปลี่ยนเล็กน้อยที่Checkboxใช้ในตัวอย่างสุดท้ายเพื่อให้เป็นเรื่องทั่วไปมากขึ้นทำให้สามารถ "ขยาย" ด้วยช่องทำเครื่องหมายที่มี 3 สถานะ นี่คือการสาธิต อย่างที่คุณเห็นมันมีฟังก์ชั่นมากกว่าช่องทำเครื่องหมายในตัวอยู่แล้ว
สิ่งที่ต้องพิจารณาเมื่อคุณเลือกระหว่าง JavaScript และ CSS
ก่อนอื่นให้ตั้งค่าผืนผ้าใบ
var canvas = document.createElement('canvas'),
ctx = canvas.getContext('2d'),
checked = 0; // The state of the checkbox
canvas.width = canvas.height = 15; // Set the width and height of the canvas
document.body.appendChild(canvas);
document.body.appendChild(document.createTextNode(' Togglable Option'));
ถัดไปกำหนดวิธีที่จะปรับปรุงผ้าใบเอง
(function loop(){
// Draws a border
ctx.fillStyle = '#ccc';
ctx.fillRect(0,0,15,15);
ctx.fillStyle = '#fff';
ctx.fillRect(1, 1, 13, 13);
// Fills in canvas if checked
if(checked){
ctx.fillStyle = '#000';
ctx.fillRect(2, 2, 11, 11);
}
setTimeout(loop, 1000/10); // Refresh 10 times per second
})();
ส่วนสุดท้ายคือการทำให้มันโต้ตอบ โชคดีที่มันค่อนข้างง่าย:
canvas.onclick = function(){
checked = !checked;
}
นี่คือที่ที่คุณอาจมีปัญหาใน IE เนื่องจากรูปแบบการจัดการเหตุการณ์แปลก ๆ ใน JavaScript
ฉันหวังว่านี่จะช่วยให้ใครบางคน; มันเหมาะกับความต้องการของฉันอย่างแน่นอน
unchecked (e.g. unchecked -> checked -> "kinda" checked -> unchecked; รัฐสามารถแสดง "ชัดแจ้งเท็จ", "ชัดเจนจริง", "ใช้ค่าเริ่มต้น" เป็นต้น) ฉันกำลังพิจารณาเพิ่มตัวอย่างในคำตอบ
นี่เป็นวิธีที่ง่ายที่สุดและคุณสามารถเลือกช่องทำเครื่องหมายเพื่อให้สไตล์นี้
CSS:
.check-box input {
display: none;
}
.check-box span:before {
content: ' ';
width: 20px;
height: 20px;
display: inline-block;
background: url("unchecked.png");
}
.check-box input:checked + span:before {
background: url("checked.png");
}
HTML:
<label class="check-box">
<input type="checkbox">
<span>Check box Text</span>
</label>
ไม่มี JavaScript หรือ jQuery ต้อง
เปลี่ยนสไตล์ช่องทำเครื่องหมายของคุณด้วยวิธีง่ายๆ
input[type="checkbox"] {
display: none;
border: none !important;
box-shadow: none !important;
}
input[type="checkbox"] + label span {
background: url(http://imgh.us/uncheck.png);
width: 49px;
height: 49px;
display: inline-block;
vertical-align: middle;
}
input[type="checkbox"]:checked + label span {
background: url(http://imgh.us/check_2.png);
width: 49px;
height: 49px;
vertical-align: middle;
}
<input type="checkbox" id="option" />
<label for="option"> <span></span> Click me </label>
นี่คือ CSS / HTML-only version ไม่จำเป็นต้องใช้ jQuery หรือ JavaScript เลย HTML ที่ง่ายและสะอาดและ CSS ที่ง่ายและสั้นจริงๆ
นี่คือ JSFiddle
นี่คือ HTML:
<div id="myContainer">
<input type="checkbox" name="myCheckbox" id="myCheckbox_01_item" value="red" />
<label for="myCheckbox_01_item" class="box"></label>
<label for="myCheckbox_01_item" class="text">I accept the Terms of Use.</label>
</div>
นี่คือ CSS
#myContainer {
outline: black dashed 1px;
width: 200px;
}
#myContainer input[type="checkbox"][name="myCheckbox"] {
display: none;
}
#myContainer input[type="checkbox"][name="myCheckbox"]:not(:checked) + label.box {
display: inline-block;
width: 25px;
height: 25px;
border: black solid 1px;
background: #FFF ;
margin: 5px 5px;
}
#myContainer input[type="checkbox"][name="myCheckbox"]:checked + label.box {
display: inline-block;
width: 25px;
height: 25px;
border: black solid 1px;
background: #F00;
margin: 5px 5px;
}
#myContainer input[type="checkbox"][name="myCheckbox"] + label + label.text {
font: normal 12px arial;
display: inline-block;
line-height: 27px;
vertical-align: top;
margin: 5px 0px;
}
สิ่งนี้สามารถปรับให้มีวิทยุหรือช่องทำเครื่องหมายแต่ละตัวร่องของช่องทำเครื่องหมายและกลุ่มของปุ่มตัวเลือกได้เช่นกัน
html / css นี้จะช่วยให้คุณสามารถจับคลิกบนฉลากดังนั้นช่องทำเครื่องหมายจะถูกตรวจสอบและไม่เลือกแม้ว่าคุณจะคลิกที่ป้ายกำกับก็ตาม
ช่องทำเครื่องหมาย / ปุ่มตัวเลือกประเภทนี้ทำงานได้อย่างสมบูรณ์แบบกับทุกรูปแบบไม่มีปัญหาเลย ได้รับการทดสอบโดยใช้ PHP, ASP.NET (.aspx), JavaServer FacesและColdFusionด้วย
**Custom checkbox with css** (WebKit browser solution only Chrome, Safari, Mobile browsers)
<input type="checkbox" id="cardAccptance" name="cardAccptance" value="Yes">
<label for="cardAccptance" class="bold"> Save Card for Future Use</label>
/* The checkbox-cu */
.checkbox-cu {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 0;
cursor: pointer;
font-size: 16px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox-cu */
.checkbox-cu input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox-cu */
.checkmark {
position: absolute;
top: 4px;
left: 0;
height: 20px;
width: 20px;
background-color: #eee;
border: 1px solid #999;
border-radius: 0;
box-shadow: none;
}
/* On mouse-over, add a grey background color */
.checkbox-cu:hover input~.checkmark {
background-color: #ccc;
}
/* When the checkbox-cu is checked, add a blue background */
.checkbox-cu input:checked~.checkmark {
background-color: transparent;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.checkbox-cu input:checked~.checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.checkbox-cu .checkmark::after {
left: 7px;
top: 3px;
width: 6px;
height: 9px;
border: solid #28a745;
border-width: 0 2px 2px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
z-index: 100;
}
input[type=checkbox].css-checkbox {
position: absolute;
overflow: hidden;
clip: rect(0 0 0 0);
height: 1px;
width: 1px;
margin: -1px;
padding: 0;
border: 0;
}
input[type=checkbox].css-checkbox + label.css-label {
padding-left: 20px;
height: 15px;
display: inline-block;
line-height: 15px;
background-repeat: no-repeat;
background-position: 0 0;
font-size: 15px;
vertical-align: middle;
cursor: pointer;
}
input[type=checkbox].css-checkbox:checked + label.css-label {
background-position: 0 -15px;
}
.css-label{
background-image:url(http://csscheckbox.com/checkboxes/dark-check-green.png);
}
ไม่คุณยังคงไม่สามารถจัดสไตล์ช่องทำเครื่องหมายเองได้ แต่ฉัน (สุดท้าย) หาวิธีจัดสไตล์ภาพลวงตาขณะที่ยังคงใช้งานฟังก์ชันการคลิกช่องทำเครื่องหมาย หมายความว่าคุณสามารถสลับได้แม้ว่าเคอร์เซอร์จะยังไม่สมบูรณ์โดยไม่ต้องเลือกข้อความหรือเรียกใช้การลากแล้วปล่อย!
วิธีนี้อาจเหมาะกับปุ่มตัวเลือก
การทำงานต่อไปนี้ใน Internet Explorer 9, Firefox 30.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> </span>Label text
</label>
เนื่องจากเบราว์เซอร์เช่น Edge และ Firefox ไม่รองรับ: ก่อน: หลังบนแท็กอินพุตช่องทำเครื่องหมายที่นี่เป็นทางเลือกเดียวกับ HTML และ CSS แน่นอนคุณควรแก้ไข CSS ตามความต้องการของคุณ
ทำให้ช่องทำเครื่องหมาย HTML เป็นแบบนี้:
<div class='custom-checkbox'>
<input type='checkbox' />
<label>
<span></span>
Checkbox label
</label>
</div>
ใช้สไตล์นี้สำหรับช่องทำเครื่องหมายเพื่อเปลี่ยนฉลากสี
<style>
.custom-checkbox {
position: relative;
}
.custom-checkbox input{
position: absolute;
left: 0;
top: 0;
height:15px;
width: 50px; /* Expand the checkbox so that it covers */
z-index : 1; /* the label and span, increase z-index to bring it over */
opacity: 0; /* the label and set opacity to 0 to hide it. */
}
.custom-checkbox input+label {
position: relative;
left: 0;
top: 0;
padding-left: 25px;
color: black;
}
.custom-checkbox input+label span {
position: absolute; /* a small box to display as checkbox */
left: 0;
top: 0;
height: 15px;
width: 15px;
border-radius: 2px;
border: 1px solid black;
background-color: white;
}
.custom-checkbox input:checked+label { /* change label color when checked */
color: orange;
}
.custom-checkbox input:checked+label span{ /* change span box color when checked */
background-color: orange;
border: 1px solid orange;
}
</style>
ดูเหมือนว่าคุณสามารถเปลี่ยนสีของช่องทำเครื่องหมายในระดับสีเทาโดยใช้ CSS เท่านั้น
ต่อไปนี้แปลงช่องทำเครื่องหมายจากสีดำเป็นสีเทา (ซึ่งเป็นเรื่องเกี่ยวกับสิ่งที่ฉันต้องการ):
input[type="checkbox"] {
opacity: .5;
}
สำหรับผู้ที่ใช้ SCSS (หรือแปลงเป็น SASS ได้ง่าย) สิ่งต่อไปนี้จะเป็นประโยชน์ อย่างมีประสิทธิภาพทำองค์ประกอบถัดจากช่องทำเครื่องหมายซึ่งเป็นองค์ประกอบที่คุณจะสไตล์ เมื่อคลิกที่ช่องทำเครื่องหมาย CSS จะปรับองค์ประกอบน้องสาวใหม่ (ตามสไตล์ใหม่ที่คุณเลือก) รหัสด้านล่าง:
label.checkbox {
input[type="checkbox"] {
visibility: hidden;
display: block;
height: 0;
width: 0;
position: absolute;
overflow: hidden;
&:checked + span {
background: $accent;
}
}
span {
cursor: pointer;
height: 15px;
width: 15px;
border: 1px solid $accent;
border-radius: 2px;
display: inline-block;
transition: all 0.2s $interpol;
}
}
<label class="checkbox">
<input type="checkbox" />
<span></span>
Label text
</label>
คำเตือน : สิ่งต่อไปนี้เป็นจริงในขณะที่เขียน แต่ในขณะเดียวกันก็มีความคืบหน้า
เบราว์เซอร์สมัยใหม่ของ AFAIK แสดงช่องทำเครื่องหมายโดยใช้การควบคุม OS ดั้งเดิมดังนั้นจึงไม่มีวิธีจัดรูปแบบ