CSS-선택한 라디오 버튼 레이블의 스타일을 지정하는 방법


141

라디오 버튼의 선택된 레이블에 스타일을 추가하고 싶습니다.

HTML :

<div class="radio-toolbar">
 <label><input type="radio" value="all" checked>All</label>
 <label><input type="radio" value="false">Open</label>
 <label><input type="radio" value="true">Archived</label>
</div>

CSS

.radio-toolbar input[type="radio"] {display:none;}
.radio-toolbar label {
    background:Red;
    border:1px solid green;
    padding:2px 10px;
}
.radio-toolbar label + input[type="radio"]:checked { 
    background:pink !important;
}

내가 잘못하고있는 아이디어가 있습니까?

답변:


296

.radio-toolbar input[type="radio"] {
  display: none;
}

.radio-toolbar label {
  display: inline-block;
  background-color: #ddd;
  padding: 4px 11px;
  font-family: Arial;
  font-size: 16px;
  cursor: pointer;
}

.radio-toolbar input[type="radio"]:checked+label {
  background-color: #bbb;
}
<div class="radio-toolbar">
  <input type="radio" id="radio1" name="radios" value="all" checked>
  <label for="radio1">All</label>

  <input type="radio" id="radio2" name="radios" value="false">
  <label for="radio2">Open</label>

  <input type="radio" id="radio3" name="radios" value="true">
  <label for="radio3">Archived</label>
</div>

우선 name, 라디오 버튼에 속성 을 추가하고 싶을 것입니다 . 그렇지 않으면 동일한 그룹에 속하지 않으며 여러 라디오 버튼을 확인할 수 있습니다.

또한 레이블을 라디오 버튼의 형제로 배치했기 때문에 idfor속성을 사용하여 레이블을 서로 연결해야했습니다.


2
@Johncl 사실입니다. IE8은 :checked선택기를 구현하지 않습니다 .
Šime Vidas

input [type = "radio"] 또는 input [type = "checkbox"]를 선택하지 않으려면 하나의 규칙으로 만 수행하고 수행하지 않는 올바른 구문은 다음과 같습니다. input [type = "radio"], input [type = "checkbox"]
fabregas88

@ fabregas88 이러한 요소에 CSS 클래스를 추가 한 다음.foo { ... }
Šime Vidas

4
당신은 키보드 액세스를 죽였다! 모든 사용자가 몸이 되길 바랍니다. ... 지금 라디오 버튼을 그대로두면 display:none;브라우저 를 사용하지 않고도 브라우저에서 키보드를 사용할 수 있습니다.
gcb

1
@gcb 문제는 표준 라디오 버튼을 숨기는 방법이었습니다. 이 프로토 타입 jsbin.com/miwati/edit?html,css,output을 만들었습니다 . 라디오 버튼은 레이블 뒤에 절대 위치합니다. 아마 최선의 접근 방식은 아닙니다.
Šime Vidas

29

라벨에 체크 박스를 실제로 넣으려면 여분의 스팬 태그를 추가하십시오.

HTML

<div class="radio-toolbar">
 <label><input type="radio" value="all" checked><span>All</span></label>
 <label><input type="radio" value="false"><span>Open</span></label>
 <label><input type="radio" value="true"><span>Archived</span></label>
</div>

CSS

.radio-toolbar input[type="radio"]:checked ~ * { 
    background:pink !important;
}

선택한 라디오 버튼의 모든 형제에 대한 배경이 설정됩니다.


2
다른 선택기 (아래 에서 언급 한 '+' 와이 질문 에서 선택한 항목 만 스타일을 지정하려고 for
했기 때문에이 방법을 선호했습니다

6

+요소가 형제가 아닌 경우 인접한 형제 선택기 ( )를 사용하고 있습니다 . 레이블은 형제가 아니라 입력 의 부모 입니다.

CSS는 그 하위 요소 (또는 그 뒤에 나오는 요소)를 기반으로 요소를 선택할 수있는 방법이 없습니다.

이 문제를 해결하려면 JavaScript를 찾아야합니다.

또는 마크 업을 다시 정렬하십시오.

<input id="foo"><label for="foo"></label>

1

html과 css에 범위를 추가 할 수 있습니다.

내 코드의 예는 다음과 같습니다.

HTML (JSX) :

<input type="radio" name="AMPM" id="radiostyle1" value="AM" checked={this.state.AMPM==="AM"} onChange={this.handleChange}/>  
<label for="radiostyle1"><span></span> am  </label>

<input type="radio" name="AMPM" id="radiostyle2" value="PM" checked={this.state.AMPM==="PM"} onChange={this.handleChange}/>
<label for="radiostyle2"><span></span> pm  </label>

표준 라디오 버튼이 화면에서 사라지고 사용자 정의 버튼 이미지가 겹쳐지는 CSS :

input[type="radio"] {  
    opacity:0;                                      
}

input[type="radio"] + label {
    font-size:1em;
    text-transform: uppercase;
    color: white ;  
    cursor: pointer;
    margin:auto 15px auto auto;                    
}

input[type="radio"] + label span {
    display:inline-block;
    width:30px;
    height:10px;
    margin:1px 0px 0 -30px;                       
    cursor:pointer;
    border-radius: 20%;
}


input[type="radio"] + label span {
    background-color: #FFFFFF 
}


input[type="radio"]:checked + label span{
     background-color: #660006;  
}

0

현재 부모 스타일을 지정하는 CSS 솔루션이 없으므로 여기에서 간단한 jQuery 하나를 사용하여 레이블이 입력 된 레이블을 클래스에 추가합니다.

$(document).on("change","input", function(){
 $("label").removeClass("checkedlabel");
 if($(this).is(":checked")) $(this).closest("label").addClass("checkedlabel");
});

클래스 사전 확인 입력의 라벨을 제공하는 것을 잊지 마세요 checkedlabel너무


0

여기에 접근 가능한 솔루션이 있습니다

label {
  position: relative;
}

label input {
  position: absolute;
  opacity: 0;
}

label:focus-within {
  outline: 1px solid orange;
}
<div class="radio-toolbar">
  <label><input type="radio" value="all" checked>All</label>
  <label><input type="radio" value="false">Open</label>
  <label><input type="radio" value="true">Archived</label>
</div>

당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.