CSS:表单中的多列部分

时间:2012-12-10 06:38:00

标签: css multiple-columns

我是css的新手。我将尝试解释我想要实现的目标。我有5个“块”。每个块都有一个标题,然后有几个标签 - 输入对用于数据输入。标签和输入应该很好地对齐。每个块的标题应该位于该块的中心

现在的技巧是这5个块应该在包含用于数据输入的其他正常标签输入对的表单中彼此显示。 (这里标签输入已经对齐,意味着在“表单级别”上有一些css:

.cssform {
    padding-top: 2em;
    padding-bottom: 2em;
}

.cssform div{
    max-width: 680px;
    clear: left;
    margin: 0;
    padding: 5px 0 8px 0;
    padding-left: 155px; /*width of left column containing the label elements*/
}

.cssform label{
    color: #990033;
    font-weight: bold;
    float: left;
    margin-left: -155px; /*width of left column*/
    width: 150px; /*width of labels. Should be smaller than left column (155px) to create some right margin*/
}


.cssform input[type="text"]{ /*width of text boxes. IE6 does not understand this attribute*/
    width: 180px;
    padding-bottom: 5px
}

这是在某种程度上搞乱我正在尝试创建的块布局。我目前的主要问题是填充或边距留下。我的“块”在150px右边太多,我不知道如何覆盖。这是我目前的css:

.cssform div.scores{
    max-width: 680px;
    clear: none;
    float: left;
    margin: 0;
    padding: 5px 20px 8px 0;
    padding-left: 0px
}

.cssform div.scores label{
    float: left;
    margin-left: 0px; /*width of left column*/
    width: 100px;
}

.cssform div.scores input[type="text"]{
    width: 20px;
    padding-bottom: 5px
}

.scores h3 {

}

有了这个,我的第一个区块是向右150px而不是最左边的区域。

第二个问题是每个块的对齐标题。

有什么想法吗?

编辑:

这里使用的一般元素:

<!-- Block 1 -->
<div class="scores">
    <h3 class="scoreClass1">Score Class 1</h3>
    <div>
        <label class="scoreClass1">Label 1</label>
        <input name="value1" type="text" class="scoreClass1"/>
    </div>
    <div>
        <label class="scoreClass1">Label 2</label>
        <input name="value2" type="text" class="scoreClass1"/>
    </div>
    <!-- More Values -->
</div>
<!-- Block 2 -->
<div class="scores">
    <h3 class="scoreClass2">Score Class 2</h3>
    <!-- Block 2 Values... -->

1 个答案:

答案 0 :(得分:2)

问题在于:

.cssform label{
    color: #990033;
    font-weight: bold;
    float: left;
    margin-left: -155px; /*width of left column*/
    width: 150px; /*width of labels. Should be smaller than left column (155px) to create some right margin*/
}

.cssform div{
    max-width: 680px;
    clear: left;
    margin: 0;
    padding: 5px 0 8px 0;
    padding-left: 155px; /*width of left column containing the label elements*/
}

我必须承认,当我开始学习CSS时,我曾经从inet中复制粘贴过这个,但现在我看到解决我的问题可以做得更简单了:

.cssform div{
    max-width: 680px;
    clear: left;
    margin: 0;
    padding: 5px 20px 8px 0;        
}
.cssform label{
    color: #990033;
    font-weight: bold;
    float: left;        
    width: 150px; /*width of labels. Should be smaller than left column (155px) to create some right margin*/
}