简体   繁体   English

CSS / HTML:如何在不使用表的情况下实现类似于表的结构?

[英]CSS/HTML: How to achieve a structure like table without using table?

I have found few similar questions on stackoverflow but none of them seems to provide a real clear solution for my case. 我在stackoverflow上发现了很少的类似问题,但是似乎没有一个问题为我的案例提供了一个真正清晰的解决方案。

I hope with a screenshot I can show the pain with using a table: 我希望通过屏幕截图可以显示使用表格的痛苦:

在此处输入图片说明

The bottom two rows are defined as tr and td within a table. 在表中,底部的两行定义为trtd The structure is perfect and alignment of the labels and textfields are perfect. 结构是完美的,标签和文本字段的对齐是完美的。 However if I wanted to style a well class (eg <div class='well'> ... </div> ) around only two rows, the table approach would fail. 但是,如果我只想围绕两行设置well类(例如<div class='well'> ... </div> )的样式,则表方法将失败。 Simply because you are not allowed having any div inside a table, which is only excepting tr and td . 原因很简单,因为不允许在表内包含任何div (仅trtd除外)。

So I took the first two rows out of the table and made it as pure divs. 因此,我从表中取出了前两行,并使其作为纯div。 You can see the result as the first two rows above in the grey well. 您可以在灰色井上方的前两行看到结果。

<div class='well'>
  <div>
    <div class='block_inline'> ... </div>
    <div class='block_inline'> ... </div>
  </div> 
  <div>
    <div class='block_inline'> ... </div>
    <div class='block_inline'> ... </div>
  </div>
</div>

In itself the well class is now beautifully rendered around the two rows, however the alignment is now a mess. 现在, well类本身可以well呈现在两行周围,但是对齐方式现在是一团糟。 How can I make them still be centred and have the text-fields to be aligned vertically next to each other? 如何使它们仍然居中并使文本字段彼此垂直对齐?

To get this effect with using divs, you just us the the display property with table, table-row and table-cell: 为了使用div达到这种效果,您只需将display属性与table,table-row和table-cell一起使用:

HTML: HTML:

<div class='well'>
  <div class="row">
    <div class='block_inline'> Title </div>
    <div class='block_inline'> ... </div>
  </div> 
  <div class="row">
    <div class='block_inline'> Due Date Time </div>
    <div class='block_inline'> ... </div>
  </div>
</div>​

CSS: CSS:

div
{
    border: 1px solid #333;
}

.well
{
    display: table;
    width: 70%;
}

.row
{
    display: table-row;
}

.block_inline
{
    display: table-cell;
    width: 50%;
}

This mimics the behaviour of a table, but leaves the markup nice and semantic. 这模仿了表的行为,但使标记保持了良好的语义。 This is also useful for solving "remaining space columns" issues :) 这对于解决“剩余空间列”问题也很有用:)

http://jsfiddle.net/Kyle_Sevenoaks/e7VeU/ http://jsfiddle.net/Kyle_Sevenoaks/e7VeU/

Well first of all the semantics are a mess.. this is how i do it: 首先,所有语义都是一团糟。这就是我的做法:

<form>
    <div class="row">
        <label for="input_1">Title</label>
        <input type="text" name="input_1" id="input_1">
    </div>
    <div class="row">
        <label for="input_2">Due date time*</label>
        <input type="text" name="input_2" id="input_2">
    </div>

</form>

with style: 风格:

div.row {
    clear: both;
}

label {
    display: inline-block;
    width: 300px;
}

input {
    display: inline-block;
}

Make adjustments where neccesary. 必要时进行调整。

The use of div class="row" could be replaced by fieldsets and definition lists. div class="row"可以替换为字段集和定义列表。 Take a look at http://www.gethifi.com/blog/html-forms-the-right-ways for that. 为此,请访问http://www.gethifi.com/blog/html-forms-the-right-ways

I like to use ULs for form layout: http://jsfiddle.net/BKgB9/ 我喜欢将UL用于表单布局: http : //jsfiddle.net/BKgB9/

<form>
 <div>
    <ul>
        <li><label>Type:</label><input type="text" /></li>
        <li><label>Reminder:</label><input type="text" /></li>
    </ul>
 </div>
</form>

div {
 background:#dcdcdc;
 border:1px solid #999;
 padding:20px;
 display:inline-block;
}

div ul li {
 margin-bottom:10px;
}

div ul li label {
 float:left;
 width:85px;
}

You can also try this method 您也可以尝试这种方法

<div class="first">
<div class="line1">
   <label>One</label>
   <select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
    </div>
    <div class="line2">
    <label>two</label>
   <select>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>
</div>
<div>

Demo; 演示; fiddle 小提琴

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

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