简体   繁体   English

防止文本在复选框下换行

[英]Prevent text from wrapping under a checkbox

I'm laying out a set of checkboxes and I am running across the age-old issue of text wrapping underneath a checkbox if the text is too long.我正在布置一组复选框,如果文本太长,我会遇到一个古老的问题,即文本在复选框下方换行。

My HTML:我的 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>

My CSS:我的CSS:

div.right{width:580px;}

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

See my fiddle here: http://jsfiddle.net/fmpeyton/7WmGr/在这里查看我的小提琴: http : //jsfiddle.net/fmpeyton/7WmGr/

After much searching on different sites, I can't seem to find a reasonable solution.在不同的网站上进行了大量搜索后,我似乎找不到合理的解决方案。 I am open for suggestions on changing my markup/styles, but I would like to keep the code as clean and semantic as possible.我对更改标记/样式的建议持开放态度,但我希望尽可能保持代码的简洁和语义。

Thanks!谢谢!

This seems to work:这似乎有效:

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

I gave the label a margin-left of 18px and the checkboxes a margin-left of -18px.我给label margin-left了 18px 的margin-left ,给复选框margin-left了 -18px 的边距。

Seems to work in Chrome & IE9.似乎适用于 Chrome 和 IE9。

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

I like this ...我喜欢这个 ...

HTML: HTML:

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

CSS: CSS:

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

One option would be something like this.一种选择是这样的。

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

Demo here: http://jsbin.com/opoqon/1/edit演示在这里: http : //jsbin.com/opoqon/1/edit

The way I tend to do it is different, which is not wrapping inputs with labels , rather doing something like我倾向于这样做的方式不同,它不是用labels包装inputs ,而是做类似的事情

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

which then allows for easier styling.这样可以更轻松地进行造型。

Here is an example of that way: http://jsbin.com/otezut/1/edit这是这种方式的一个例子: http : //jsbin.com/otezut/1/edit

But either way would work.但无论哪种方式都会奏效。

Here's one that's less reliant on the size of the input elements.这是一个不太依赖输入元素大小的方法。

 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>

The simplest option最简单的选择

HTML: HTML:

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

CSS: CSS:

label {
    display:flex;
    align-items: baseline;
}

input[type=checkbox] {
    margin-right: 8px;
}

I'd tried all the options and the best solution for was:我尝试了所有选项,最佳解决方案是:

<div>
  <label><input type="checkbox"><span>Text</span></label>
  <label><input type="checkbox"><span>Text</span></label>
</div>

HTML like Pudica and Fillip suggested.像 Pudica 和 Fillip 建议的 HTML。

And the CSS you only set the float:left to the checkboxes and to prevent the text wrap around the checkbox you can simply use overflow:hidden on the span .而 CSS 您只将float:left设置为checkboxes ,为了防止文本环绕复选框,您可以简单地在span上使用overflow:hidden

[type="checkbox] {
   float: left;
}
span {
   overflow: hidden;
}

You can use margin-left to give desired space from text to checkbox.您可以使用margin-left为从文本到复选框提供所需的空间。

This is another option.这是另一种选择。 It's very simple yet effective.它非常简单而有效。 You take the part that's wrapping.你负责包装的部分。 <label><input type="checkbox" name="vacation" value="Historical and educational trips"> Historical and educational <span class="antiWrap">trips</span></label> and you add a span with a class. <label><input type="checkbox" name="vacation" value="Historical and educational trips"> Historical and educational <span class="antiWrap">trips</span></label>并添加一个 span一类。 I called the class antiWrap.我称类为 antiWrap。 Then you use css to add a left margin to it like.然后您使用 css 为其添加左边距。 .antiWrap { margin-left: 18px; } .antiWrap { margin-left: 18px; } Here's myfiddle based using your code. .antiWrap { margin-left: 18px; }下面是使用你的代码基于myfiddle。 http://jsfiddle.net/alexhram/7WmGr/3/ I think that's what your going for? http://jsfiddle.net/alexhram/7WmGr/3/我认为这就是你的目的? Let me know.让我知道。

The easiest solution is to use a table for layout in this case.在这种情况下,最简单的解决方案是使用表格进行布局。 Rather than fiddling with a thousand different possible permutations of css because supposedly tables are evil, this just works 100% of the time.而不是摆弄一千种不同的 css 可能排列,因为据说表格是邪恶的,这只是 100% 的时间。

<table>
  <tr>
    <td><input type=checkbox id=blah><label ='blah'></label></td>
    <td><label for='blah'>Long label here...</td>
   </tr>
</table>

And if you need a label next to your checkbox for styling purposes then just use an empty label tag there and use the real label in the second table cell.如果您需要复选框旁边的标签用于样式目的,那么只需在那里使用一个空的标签标签,并在第二个表格单元格中使用真实标签。 (Yes, you can use multiple labels for the same checkbox) (是的,您可以为同一个复选框使用多个标签)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM