Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
581 views
in Technique[技术] by (71.8m points)

html - How do I prevent line breaks between a radio button and its label, while still allowing line breaks within the label itself?

I'd like to ensure that there's never a line break between a radio button and the start of its adjacent label. However, I want text within the label to be allowed to wrap. Is this possible? You can see my failed attempts by rendering the following HTML:

<html>
 <head>
   <style type="text/css">
.box {
  border: solid gray 2px;
  width: 200px;
  margin: 5px;
}
.chopped {
  overflow: hidden;
}
   </style>
 </head>
<body>

The boxes need to be fixed-width, so long content needs to be wrapped, as seen in the first box below. And if someone tries to post a ridiculously long string without any spaces, we need it to be truncated, rather than extend beyond the edge of the box -- the problem is visible in the second box:

 <div class="box">
  <input type="radio"/>
  <label>This is a really long string with no spaces</label>
 </div>

 <div class="box">
  <input type="radio"/>
  <label>This_is_a_really_long_string_with_no_spaces</label>
 </div>

 <hr/>

So I add "overflow: hidden", and things are somewhat better, but I still don't like how the second box has a line break between the radio button and its label:

 <div class="chopped box">
  <input type="radio"/>
  <label>This is a really long string with no spaces</label>
 </div>

 <div class="chopped box">
  <input type="radio"/>
  <label>This_is_a_really_long_string_with_no_spaces</label>
 </div>

 <hr/>

If I add <nobr>, the radio buttons are next to their labels, and so the unspaced string now looks perfect. However, this breaks the first string (the one with spaces), since it no longer wraps:

 <div class="chopped box">
  <nobr>
    <input type="radio"/>
    <label>This is a really long string with no spaces</label>
  </nobr>
 </div>
 <div class="chopped box">
  <nobr>
    <input type="radio"/>
    <label>This_is_a_really_long_string_with_no_spaces</label>
  </nobr>
 </div>

</body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, move the radio buttons inside your labels. This adds the nice feature that you can select the radio buttons by clicking the text. Then add a span around the text.

<div class="chopped box">
 <label>
  <input type="radio"/>
  <span class="wrappable">This is a really long string with no spaces</span>
 </label>
</div>

<div class="chopped box">
 <label>
  <input type="radio"/>
  <span class="wrappable">This_is_a_really_long_string_with_no_spaces</span>
 </label>
</div>

Second, add the following style to your css:

label {
    white-space:nowrap;
}

.wrappable {
    white-space:normal;
}

The white-space style on the label prevents the linebreak between the radio button and the text, and the span around the text allows it to wrap just within the text.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...