CSS에서 확인란의 테두리 스타일을 변경하는 방법은 무엇입니까?


122

체크 박스 (입력) 테두리 스타일은 어떻게 변경하나요? 나는 border:1px solid #1e5180그것을 입었지만 FireFox 3.5에서는 아무 일도 일어나지 않습니다!

답변:


63

어떤 브라우저 에서 어떤 일이 발생하면 놀랄 것입니다. 이것은 브라우저가 그다지 스타일링을 허용하지 않는 경향이있는 뛰어난 양식 요소 중 하나이며, 사람들은 일반적으로 체크 박스처럼 보이고 작동하도록 무언가를 스타일링 / 코딩 할 수 있도록 자바 스크립트로 대체하려고합니다.


4
Opera 는 확인란에서 스타일링을 매우 잘 처리합니다. IE 에는 체크 박스 자체가 아닌 체크 박스를 감싸는 스타일이 있습니다.
경외

4
그들은 요즘 실제로 못생긴 : D
게르 겔리 Fehérvári

1
여전히 추악한 2020 년
Paul

210

"테두리"대신 "개요"를 사용하는 것이 좋습니다. 예 : outline: 1px solid #1e5180.


3
개요는 훌륭한 대안입니다.
Nippysaurus 2011

4
outline은 매우 좋으며 모든 입력 및 선택 상자에서 작동하므로 ".error"클래스에 유용합니다.
SztupY

2
!important개요를 사용할 때 CSS 에 추가해야 할 수도 있습니다 . 나는 이것을 Firefox 12.0에서 테스트하고 있었는데! 중요하지 않으면 체크 박스를 클릭하는 한 개요가 사라질 것입니다.
Weezle

3
이것은 훌륭한 대안이지만 개요의 문제는 IE7에서 작동하지 않는다는 것입니다!
Yises

13
@Yises IE7에 대해 걱정해야하나요? 미안 해요.
theblang

71

당신은 사용해야합니다

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

그런 다음 기본 확인란 이미지 / 스타일을 제거하고 스타일을 지정할 수 있습니다. 어쨌든 테두리는 여전히 Firefox에 있습니다.


2
Opera를 사용하면 추가 해킹없이 체크 박스의 스타일을 지정할 수 있습니다. -o-appearance 스타일은 생략하겠습니다. -moz-appearance 및 -webkit-appearance는 훌륭합니다.
Neall 2010 년

7
죄송합니다. 너무 빨리 말했습니다. "-webkit-appearance : none;" 확인란 동작을 비활성화하는 것 같습니다.
Neall 2010 년

6
나를 위해 (크롬 5) 그렇지 않습니다, 당신은 스타일을 추가해야합니다 : checked state extra
Valerij

1
외관은 CSS 표준의 일부가 아니 었습니다. 그것을 사용하지 마십시오.
Evgeny

와우 이거 굉장 해요. 플러그인을 없앨 게요. 나는 무엇이든 할 수있다 => | 입력 [유형 = 체크 박스] | input [type = checkbox] : hover | input [type = checkbox] : 선택 | 입력 [TYPE = 선택란] 체크 : 후 ...
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

다음은 Martin의 사용자 정의 체크 박스 및 CSS3 LINK가있는 라디오 버튼을 기반으로하는 순수한 CSS (이미지 없음) 크로스 브라우저 솔루션입니다. http://martinivanov.net/2012/12/21/imageless-custom-checkboxes-and-radio-buttons -with-css3- 재검토 됨 /

다음은 jsFiddle입니다. http://jsfiddle.net/DJRavine/od26wL6n/

다음 브라우저에서 이것을 테스트했습니다.

  • FireFox (41.0.2) (42)
  • Google Chrome (46.0.2490.80m)
  • Opera (33.0.1990.43)
  • Internet Explorer (11.0.10240.16431 [업데이트 버전 : 11.0.22])
  • Microsoft Edge (20.10240.16384.0)
  • Safari Mobile 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

들어 파이어 폭스 , 크롬사파리 아무 일도 발생하지 않습니다.

들어 IE 경계 체크 박스 (안 확인란의 일부로서) 외부에 적용되고 체크 박스 효과를 음영은 "공상은"사라 (AN oldfashioned 체크 박스로 표시)됩니다.

들어 오페라 테두리 스타일은 실제로 체크 박스 요소에 테두리를 적용합니다.
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에서 작동하지 않습니다. 더 중요한 것은 void 요소 가 콘텐츠를 가질 수 없기 때문에 Chrome 의 잘못된 구현입니다 .
Leo

Leo, "내용"이라는 용어를 잘못 해석 한 것입니다. 닫는 태그가 없기 때문에 그 공백 요소는 그 사이에 내용을 가질 수 없다는 것뿐입니다. 예를 들어 <a> content </a>에는 닫는 태그가 있으므로 해당 태그 사이에 콘텐츠가있을 수 있습니다. 그러나 입력에는 닫는 태그가 없으므로 원칙적으로 내용을 가질 수 없습니다. 반면에 의사 요소에는 모든 곳에서 사용할 수있는 "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>


첫째 : ": after"create √; 두 번째 : "-webkit-appearance : none"오리 지온 스타일이 작동하지 않게 한 다음 스타일을 정의 할 수 있습니다.
KeepLearn

2
귀하의 답변에 대한 간단한 설명을 작성하십시오.
Angel F Syrus

-5

그것을 div에 넣고 div에 테두리를 추가하십시오.


5
그러면 이중 테두리가 생깁니다.
Egor Pavlikhin

<DIV 스타일 = "국경 스타일 : 고체, 폭 : 120 픽셀;"> <입력 유형 = "체크 박스"이름 = "mycheck"> 내 체크 박스 </ 입력> </ 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.