简体   繁体   English

遇到clearfix类型问题

[英]Running into a clearfix type issue

So Two Divs Walk Into A Form 所以两个Divs走进一个表格

Looks like this: 看起来像这样:

<form>
    <div id="top">
        <label>Gimme a Number:<input type="number" /></label><br />
        <label>Gimme a Word:<input type="textarea" /></label><br />
        <label>Now tell me how you feel about HTML forms: <textarea rows="4" cols="50"></textarea></label><br />
    </div>
    <div id="bottom">
        <label>Gimme a Number:<input type="number" /></label><br />
        <label>Gimme a Word:<input type="textarea" /></label><br />
        <label>Now tell me how you feel about HTML forms: <textarea rows="4" cols="50"></textarea></label><br />
    </div>
</form>

There's some css positioning all the form fields absolute and left , a fiddle of the principal is here (Step 1) 有一些css将所有表单字段定位为absoluteleft此处是主体的小提琴(步骤1)

So I fixed the first problem by adding additional line breaks (fiddle of step 2) . 因此,我通过添加其他换行符解决了第一个问题(步骤2)

This is not an ideal solution for two reasons. 由于两个原因,这不是理想的解决方案。 Extra elements in the DOM for one, and um... well just look at it. DOM中的多余元素仅此一个,嗯...看一下吧。 The textarea still overflows into the bottom div . textarea仍然溢出到底部div

I found a temporary hack when the miscellaneous clearfix answers I tried didn't work (Mostly from other SO questions) by inserting: 当我尝试的其他clearfix答案不起作用(主要来自其他SO问题)时,我通过插入以下内容找到了一个临时hack:

<hr class="clearfix" /><br />

in between the two, and giving the hr visibility:hidden in the CSS. 在两者之间,并赋予hr visibility:hidden在CSS中。

That made it work on my page, but as you can see here (Step 3) it doesn't actually work. 这使它可以在我的页面上运行,但是如您在此处看到的(第3步),它实际上并不起作用。 In the form on my page the div 's have transparent backgrounds, and it puts them far enough apart that the users are happy, and since this is an intranet order form (used from a phone no less) and not a SV startup webapp, I'm kind of content to leave it production for now, but in the end, what was the right way to do this? 在我页面上的表格中, div的背景透明,并且它们之间的距离足够使用户感到满意,并且由于这是一个Intranet订单表格(不少于电话使用),而不是SV启动网络应用程序,我有点满足于现在暂时停止生产,但是最后,正确的方法是什么?

How should I have gotten the div.top to encompass the full content, including the textarea ? 我应该如何使div.top包含完整的内容,包括textarea I've tried a lot of the "go to" solutions for elements with float , but as expected, they don't work since the textarea 's aren't float ed. 我已经尝试了很多使用float元素的“转到”解决方案,但是正如预期的那样,由于textarea不是float ed,所以它们不起作用。

Ideally I want to ditch the extra <br> s, and have the <div> s actually contain their content without either manually positioning every single element or adding a ton of complexity to the page. 理想情况下,我想放弃多余的<br> ,并让<div>实际上包含其内容,而无需手动放置每个元素或在页面上增加大量复杂性。 Any ideas are appreciated. 任何想法表示赞赏。

A nice trick for what you're looking for is using a Html Description List . 使用Html Description List可以满足您的需求

For example, you can write this: 例如,您可以这样编写:

<dl id="top">
    <dt>Gimme a Number:</dt>
    <dd><input type="number" /></dd>
    <dt>Gimme a Word:</dt>
    <dd><input type="textarea" /></dd>
    <dt>Now tell me how you feel about HTML forms:</dt>
    <dd><textarea rows="4" cols="50"></textarea></dd>
</dl>
<dl id="bottom">
    <dt>Gimme a Number:</dt>
    <dd><input type="number" /></dd>
    <dt>Gimme a Word:</dt>
    <dd><input type="textarea" /></dd>
    <dt>Now tell me how you feel about HTML forms:</dt>
    <dd><textarea rows="4" cols="50"></textarea></dd>
</dl>

jsfiddle jsfiddle

And here is a bit more css to make it look like your fiddle: 这里还有更多的CSS使它看起来像您的小提琴:

#top{
    color:white;
    font-weight:700;
    background-color:blue;
}
#bottom{
    font-weight:700;
    background-color:red;
}

dl {
    padding: 0.5em;
    height: 100%;
    display: table;
}

dt {
    float: left;
    clear: left;
}

dd {
    float: right;
    clear: right;
}

fiddle 小提琴

Edit 编辑

About label issue being non-superfluous - there is an easy solution using the "for" attribute. 关于标签问题不是多余的-使用“ for”属性有一个简单的解决方案。 Here is an example: 这是一个例子:

<dt><label for="number1">Gimme a Number:</label></dt>
<dd><input id="number1" type="number" /></dd>

updated fiddle 更新的小提琴

Your original use of label: 您最初使用的标签:

<label>Gimme a Number:<input type="number" /></label><br />

will couple the two tags together and shouldn't be used. 将两个标签耦合在一起,因此不应该使用。 Plus talking about semantics, should the input really be part of the label? 加上谈论语义,输入内容是否真的应该是标签的一部分?

Taking in acount your comment about non-semantic use of dd / dt, you can easily swap between the dt and dd, like this fiddle . 考虑一下您对dd / dt的非语义使用的评论,您可以轻松地在dt和dd之间切换, 就像这个小提琴一样 Here is the updated example: 这是更新的示例:

<dd><label for="number1">Gimme a Number:</label></dd>
<dt><input id="number1" type="number" /></dt>

Now we have a semantic HTML where the CSS takes care of the design choices. 现在,我们有了语义HTML,其中CSS负责设计选择。

Edit 2 编辑2

new fiddle - no need in height: 100%; 新提琴 -不需要height: 100%; on dl 在dl上

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

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