ฉันจะเปลี่ยนรูปแบบของเส้นขอบของช่องทำเครื่องหมาย (อินพุต) ได้อย่างไร ฉันใส่border:1px solid #1e5180
ไปแล้ว แต่ใน FireFox 3.5 ไม่มีอะไรเกิดขึ้น!
ฉันจะเปลี่ยนรูปแบบของเส้นขอบของช่องทำเครื่องหมาย (อินพุต) ได้อย่างไร ฉันใส่border:1px solid #1e5180
ไปแล้ว แต่ใน FireFox 3.5 ไม่มีอะไรเกิดขึ้น!
คำตอบ:
หากมีอะไรเกิดขึ้นในเบราว์เซอร์ใด ๆฉันจะประหลาดใจ นี่เป็นหนึ่งในองค์ประกอบรูปแบบที่โดดเด่นที่เบราว์เซอร์มักจะไม่ให้คุณจัดรูปแบบมากขนาดนั้นและผู้คนมักจะพยายามแทนที่ด้วยจาวาสคริปต์เพื่อให้พวกเขาสามารถจัดรูปแบบ / โค้ดให้ดูและทำหน้าที่เหมือนช่องทำเครื่องหมาย
ฉันขอแนะนำให้ใช้ "เส้นขอบ" แทน "เส้นขอบ" ตัวอย่างเช่น:outline: 1px solid #1e5180
.
!important
ใน css ของคุณเมื่อใช้โครงร่าง ฉันกำลังทดสอบสิ่งนี้ใน Firefox 12.0 และไม่มี! ที่สำคัญโครงร่างจะหายไปตราบเท่าที่ฉันคลิกช่องทำเครื่องหมาย
คุณควรใช้
-moz-appearance:none;
-webkit-appearance:none;
-o-appearance:none;
จากนั้นคุณจะกำจัดรูปภาพ / สไตล์ช่องทำเครื่องหมายเริ่มต้นและสามารถจัดรูปแบบได้ อย่างไรก็ตามเส้นขอบจะยังคงอยู่ใน Firefox
คุณสามารถใช้เงากล่องเพื่อปลอมเส้นขอบ:
-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);
นี่คือโซลูชันข้ามเบราว์เซอร์ 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/
ฉันได้ทดสอบสิ่งนี้กับเบราว์เซอร์ต่อไปนี้:
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>
ฉันล้าสมัยแล้วฉันรู้ .. แต่วิธีแก้ปัญหาเล็กน้อยคือใส่ช่องทำเครื่องหมายของคุณไว้ในแท็กป้ายกำกับจากนั้นจัดรูปแบบป้ายกำกับด้วยเส้นขอบ:
<label class='hasborder'><input type='checkbox' /></label>
จากนั้นจัดรูปแบบป้ายกำกับ:
.hasborder { border:1px solid #F00; }
label { position: relative; overflow: hidden; width: 16px; height: 16px; border: 1px solid #0f0; }
จากนั้นlabel > input[type=checkbox] { position: absolute; left: -999px; }
สิ่งนี้จะช่วยให้คุณกำหนดรูปแบบช่องทำเครื่องหมายของคุณเองและช่องทำเครื่องหมายเดิมจะยังคงถูกเลือก / ไม่เลือก แต่ผู้ใช้จะไม่เห็น พวกเขาจะเห็นเฉพาะช่องทำเครื่องหมายที่คุณกำหนดเอง
:hover
และ :checked
เพื่อจัดรูปแบบสถานะอื่น ๆ ของช่องทำเครื่องหมายที่คุณกำหนดเองได้
นี่คือเวอร์ชันของฉันที่ใช้ 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">
สำหรับFirefox , ChromeและSafariไม่มีอะไรเกิดขึ้น
สำหรับIEจะใช้เส้นขอบนอกช่องทำเครื่องหมาย (ไม่ใช่ส่วนหนึ่งของช่องทำเครื่องหมาย) และเอฟเฟกต์การแรเงา "แฟนซี" ในช่องทำเครื่องหมายจะหายไป (แสดงเป็นช่องทำเครื่องหมายแบบเก่า)
สำหรับOperaรูปแบบเส้นขอบจะใช้เส้นขอบในองค์ประกอบช่องทำเครื่องหมาย
Operaยังจัดการกับสไตล์อื่น ๆ ในช่องทำเครื่องหมายได้ดีกว่าเบราว์เซอร์อื่น ๆ : ใช้สีเป็นสีของขีดสีพื้นหลังจะใช้เป็นสีพื้นหลังภายในช่องทำเครื่องหมาย (IE ใช้พื้นหลังราวกับว่าช่องทำเครื่องหมายอยู่ใน<div>
พื้นหลัง)) .
วิธีแก้ปัญหาที่ง่ายที่สุดคือการรวมช่องทำเครื่องหมายไว้ข้างใน<div>
เหมือนที่คนอื่นแนะนำ
หากคุณต้องการควบคุมลักษณะที่ปรากฏอย่างสมบูรณ์คุณจะต้องใช้วิธีการรูปภาพ / จาวาสคริปต์ขั้นสูงซึ่งหมายถึงผู้อื่นด้วย
ช่องทำเครื่องหมายการจัดแต่งทรงผม (และองค์ประกอบอินพุตอื่น ๆ สำหรับวัสดุนั้น) เป็นไปไม่ได้จริง ๆ กับ css บริสุทธิ์หากคุณต้องการเปลี่ยนรูปลักษณ์ของภาพอย่างมาก
ทางออกที่ดีที่สุดของคุณคือการใช้บางสิ่งบางอย่างเช่นjqTransformซึ่งจะแทนที่อินพุตของคุณด้วยรูปภาพและใช้พฤติกรรมจาวาสคริปต์เพื่อเลียนแบบช่องทำเครื่องหมาย (หรือองค์ประกอบอื่น ๆ สำหรับเรื่องนั้น)
ไม่คุณยังไม่สามารถจัดรูปแบบช่องทำเครื่องหมายเองได้ แต่ในที่สุดฉันก็คิดวิธีจัดรูปแบบภาพลวงตาในขณะที่ยังคงฟังก์ชันการคลิกช่องทำเครื่องหมายไว้ หมายความว่าคุณสามารถสลับได้แม้ว่าเคอร์เซอร์จะยังไม่สมบูรณ์โดยไม่ต้องเสี่ยงกับการเลือกข้อความหรือเรียกใช้การลากแล้วปล่อย!
ตัวอย่างนี้ใช้ "ปุ่ม" แบบขยายและข้อความบางส่วนในป้ายกำกับ แต่จะให้แนวคิดว่าคุณจะทำให้ช่องทำเครื่องหมายมองไม่เห็นได้อย่างไรและวาดอะไรไว้ข้างหลัง
โซลูชันนี้อาจเหมาะกับปุ่มตัวเลือก
ต่อไปนี้ใช้งานได้ใน 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> </span>Label text
</label>
นี่คือวิธีง่ายๆ (ใช้ก่อนหรือหลังองค์ประกอบ / คลาสหลอก):
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 */
}
<!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>
ใส่ลงใน div และเพิ่มเส้นขอบให้กับ div
<div style="border-style: solid;width:13px">
<input type="checkbox" name="mycheck" style="margin:0;padding:0;">
</input>
</div>