2013-02-05 2 views
14

체크 상자 집합을 레이아웃하고 있는데 텍스트가 너무 길면 체크 상자 아래에 오래된 텍스트 줄 바꿈 문제가 있습니다.체크 박스 아래에 텍스트 줄 바꿈 방지

내 HTML :

<div class="right"> 
    <form id="updateProfile"> 
     <fieldset class="checkboxes"> 
      <p>4. What is your favorite type of vacation?</p> 
      <label><input type="checkbox" name="vacation" value="Ski Trips"> Ski Trips</label> 
      <label><input type="checkbox" name="vacation" value="Beach Visits"> Beach Visits</label> 
      <label><input type="checkbox" name="vacation" value="Cruises"> Cruises</label> 
      <label><input type="checkbox" name="vacation" value="Historical and educational trips"> Historical and educational trips</label> 
      <label><input type="checkbox" name="vacation" value="Yachting"> Yachting</label> 
      <label><input type="checkbox" name="vacation" value="Road Trip"> Road Trip</label> 
      <label><input type="checkbox" name="vacation" value="Spa Weekend"> Spa Weekend</label> 
      <label><input type="checkbox" name="vacation" value="Bed and Breakfast"> Bed and Breakfast</label> 
      <label><input type="checkbox" name="vacation" value="Stay home and relax"> Stay home and relax</label> 
      <label><input type="checkbox" name="vacation" value="Gambling Trips"> Gambling Trips</label> 
      <label><input type="checkbox" name="vacation" value="Volunteer"> Volunteer</label> 
     </fieldset> 
    </form> 
</div> 

내 CSS :

div.right{width:580px;} 

form#updateProfile fieldset label{ 
    display: block; 
    margin-bottom: 5px; 
    font-size: 16px; 
    float: left; 
    width: 33%; 
} 

여기 내 바이올린 참조 : http://jsfiddle.net/fmpeyton/7WmGr/

많은 다른 사이트에서 검색 한 후, 나는 합리적인를 찾을 수 없습니다 해결책. 내 마크 업/스타일을 변경하는 것에 대한 제안이 열려 있지만 최대한 깨끗하고 의미있는 코드로 유지하려고합니다.

감사합니다.

답변

22

이 작동하는 것 같다 :

http://jsfiddle.net/7WmGr/5/

내가 label 18px 및 체크 박스 -18px의 margin-leftmargin-left했다.

HTML :

<div class="right"> 
    <form id="updateProfile"> 
     <fieldset class="checkboxes"> 
      <p>4. What is your favorite type of vacation?</p> 
      <label><input type="checkbox" name="vacation" value="Ski Trips"> Ski Trips</label> 
      <label><input type="checkbox" name="vacation" value="Beach Visits"> Beach Visits</label> 
      <label><input type="checkbox" name="vacation" value="Cruises"> Cruises</label> 
      <label><input type="checkbox" name="vacation" value="Historical and educational trips"> Historical and educational trips</label> 
      <label><input type="checkbox" name="vacation" value="Yachting"> Yachting</label> 
      <label><input type="checkbox" name="vacation" value="Road Trip"> Road Trip</label> 
      <label><input type="checkbox" name="vacation" value="Spa Weekend"> Spa Weekend</label> 
      <label><input type="checkbox" name="vacation" value="Bed and Breakfast"> Bed and Breakfast</label> 
      <label><input type="checkbox" name="vacation" value="Stay home and relax"> Stay home and relax</label> 
      <label><input type="checkbox" name="vacation" value="Gambling Trips"> Gambling Trips</label> 
      <label><input type="checkbox" name="vacation" value="Volunteer"> Volunteer</label> 
     </fieldset> 
    </form> 
</div> 

CSS :

div.right{width:598px;} 

form#updateProfile fieldset label{ 
    display: block; 
    margin-bottom: 5px; 
    font-size: 16px; 
    float: left; 
    width: 30%; 
    margin-left:18px; 
} 
form#updateProfile fieldset label input[type='checkbox']{margin-left:-18px;} 

이 Crome 스 & IE9에서 작동하는 것 같다.

+1

18px 대신 왼쪽으로 마주 칠 필요가있었습니다. 이렇게하면 텍스트가 겹치지 않게됩니다. – ccStars

3

하나의 옵션이 이와 유사합니다. 여기

form#updateProfile fieldset label{ 
    display: block; 
    margin-bottom: 5px; 
    font-size: 16px; 
    float: left; 
    width: 30%; 
    padding-left: 3%; 
    position: relative; 
} 

input { 
    position: absolute; 
    left: -5px; 
} 

데모 : http://jsbin.com/opoqon/1/edit

내가하는 경향이 방법은보다는 쉽게 스타일링 할 수 있습니다

<input id="ski-trips" type="checkbox" name="vacation" value="Ski Trips"><label for="ski-trips">Ski Trips</label> 

같은 일을하고, labelsinputs 포장되지 않은, 다른 . 여기

그 방법의 예입니다 http://jsbin.com/otezut/1/edit

그러나 어느 쪽이든은 작동합니다.

+0

당신은 나의 영웅입니다! 나는 이것을하는 무수한 방법을 시도하고 이것은 나를 위해 가장 안정적으로 일했다! 고맙습니다! – mason81

+0

내가 누락 된 것이 있습니까? 두 번째 방법은 절대적으로 끔찍한 것처럼 보입니다 - 한 줄에있는 체크 박스, 다른 줄에있는 레이블, 나머지 하나의 흐름을 완전히 끊는 하나의 긴 레이블. 기본적으로, 체크 박스 목록으로 잘못 될 수있는 모든 것, 두 번째 jsbin은 시연합니다. – Martha

+0

@ Martha 이것은 2 년이 넘었으므로 확실히 조사해야합니다. 지금 당장은 다른 접근 방식을 택할 것입니다. – gotohales

0

다른 옵션입니다. 그것은 매우 간단하면서도 효과적입니다. 당신은 감싸는 부분을 맡습니다. <label><input type="checkbox" name="vacation" value="Historical and educational trips"> Historical and educational <span class="antiWrap">trips</span></label>이고 클래스가있는 범위를 추가합니다. 나는 반 래핑이라고 불렀다. 그런 다음 CSS를 사용하여 왼쪽 여백을 추가하십시오. .antiWrap { margin-left: 18px; } 여기 myfiddle은 코드를 기반으로합니다. http://jsfiddle.net/alexhram/7WmGr/3/ 그게 네가 간다고 생각해? 알려줘.

+0

@mookamafoob은'label'에서'input'을 중첩하지 않습니다. 왜냐하면'label'이 화면에 아무 것도 렌더링하지 않기 때문에 레이아웃을 바꾸지 않습니다. 그러나 w3schools.com의 예제에 따라 옵션 텍스트를이 경우 '역사 및 교육 여행'라벨에만 넣어야합니다. 그 효과를 추가하는 것으로 충분합니다. – Chakotay

+0

제안 해 주셔서 감사합니다. 추가 마크 업을 추가하지 않고 모든 경우에 적용되는 스타일을 적용 할 수있는 방법이 있는지 확인하려고합니다. –

2

나는 이것을 좋아한다.

HTML :

<input type="checkbox" name="vacation" value="Ski Trips"><label>very long label ...</label> 

CSS :

input[type="checkbox"] { 
    position: absolute; 
} 
input[type="checkbox"] ~ label { 
    padding-left:1.4em; 
    display:inline-block; 
} 
0

여기에 입력 요소의 크기에 덜 의존의 하나입니다.

div {width: 12em;} 
 

 
label { 
 
    display: block; 
 
    white-space: nowrap; 
 
    margin: 10px; 
 
} 
 

 
label input { 
 
    vertical-align: middle; 
 
} 
 

 
label span { 
 
    display: inline-block; 
 
    white-space: normal; 
 
    vertical-align: top; 
 
    position: relative; 
 
    top: 2px; 
 
}
<div> 
 

 
<label><input type="radio"><span>I won't wrap</span></label> 
 
<label><input type="checkbox"><span>I won't wrap</span></label> 
 

 
<label><input type="radio"><span>I am a long label which will wrap</span></label> 
 
<label><input type="checkbox"><span>I am a long label, I will wrap beside the input</span></label> 
 

 
</div>