2011-01-09 3 views
94
나는 라디오 버튼의 선택 라벨에 스타일을 추가 할

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

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; 
} 

어떤 아이디어?

답변

224

.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

:

<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 표준 라디오 버튼이 화면에서 사라지고 사용자 정의 버튼 이미지를 중첩 할 @ 스티브 그래, 나는 세상이 잔인하다는 것을 알고있다.': P' –

+1

IE8에서는 작동하지 않는다. – Johncl

+2

@Johncl 사실입니다. IE8는': checked' 셀렉터를 구현하지 않습니다. –

5

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

CSS는 자손을 기반으로 요소를 선택할 수 없으며 그 뒤에 오는 요소도 선택할 수 없습니다.

이 문제를 해결하려면 자바 스크립트를 살펴야합니다.

또는 마크 업을 재 배열 : 모든

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

라벨 안에 체크 박스를 넣으려면 여분의 span 태그를 추가하십시오 (예 :

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; 
} 

.

+0

내가 원하는대로 다른 선택기 ('+'아래에 언급 된 것과 [이 질문에] (http://stackoverflow.com/q/14592768/957950) 사용했지만 나는이 접근 방식을 선호했다. 선택한 항목의 스타일을 지정하기 때문에'for' 속성에 대해 걱정하지 않아도됩니다. – brichins

0

html과 css에 스팬을 추가 할 수 있습니다. 여기

HTML (JSX) ... 내 코드에서 예입니다 :

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; 
} 
관련 문제