如何在Grails中将字段标签放在字段顶部而不是左侧

时间:2015-11-22 08:38:27

标签: css grails grails-2.4

我正在使用Grails 2.4.2。我想使用Grails默认CSS进行一些修改。现在我需要将字段标签放在字段的顶部而不是左侧。有人可以帮我这个吗?我在CSS方面很弱。提前谢谢。

这是我的表单元素,我想连续显示4个字段:

    <div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'fullName', 'error')} required">
    <label for="fullName">
        <g:message code="users.fullName.label" default="Full Name"/>
        <span class="required-indicator">*</span>
    </label>
    <g:textField name="fullName" required="" value="${usersInstance?.fullName}"/>

    <label for="email">
        <g:message code="users.email.label" default="Email"/>
        <span class="required-indicator">*</span>
    </label>
    <g:field type="email" name="email" required="" value="${usersInstance?.email}"/>

</div>

以上代码的输出如下:

enter image description here

现在我想连续添加4个字段,所有字段的标签都在顶部。你能帮忙吗?!!!

每行中的单个字段::

enter image description here

表单元素::

    <div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'username', 'error')} required">
    <label for="username">
        <g:message code="users.username.label" default="Username"/>
        <span class="required-indicator">*</span>
    </label>
    <g:textField name="username" required="" value="${usersInstance?.username}"/>

</div>

<div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'password', 'error')} required">
    <label for="password">
        <g:message code="users.password.label" default="Password"/>
        <span class="required-indicator">*</span>
    </label>
    <g:textField name="password" required="" value="${usersInstance?.password}"/>

</div>

<div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'fullName', 'error')} required">
    <label for="fullName">
        <g:message code="users.fullName.label" default="Full Name"/>
        <span class="required-indicator">*</span>
    </label>
    <g:textField name="fullName" required="" value="${usersInstance?.fullName}"/>

</div>

<div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'email', 'error')} required">
    <label for="email">
        <g:message code="users.email.label" default="Email"/>
        <span class="required-indicator">*</span>
    </label>
    <g:field type="email" name="email" required="" value="${usersInstance?.email}"/>

</div>

<div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'phone', 'error')} required">
    <label for="phone">
        <g:message code="users.phone.label" default="Phone"/>
        <span class="required-indicator">*</span>
    </label>
    <g:textField name="phone" required="" value="${usersInstance?.phone}"/>

</div>

<div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'fax', 'error')} required">
    <label for="fax">
        <g:message code="users.fax.label" default="Fax"/>
        <span class="required-indicator">*</span>
    </label>
    <g:textField name="fax" required="" value="${usersInstance?.fax}"/>

</div>

<div class="fieldcontain ${hasErrors(bean: usersInstance, field: 'address', 'error')} required">
    <label for="address">
        <g:message code="users.address.label" default="Address"/>
        <span class="required-indicator">*</span>
    </label>
    <g:textField name="address" required="" value="${usersInstance?.address}"/>

</div>

2 个答案:

答案 0 :(得分:2)

你走了。只需定义以下CSS:

* {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}
*:before,
*:after {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}
.fieldcontain label, .fieldcontain .property-label {
    display: block;
    text-align: left;
    width: 100%
}

.fieldcontain input {
    width: 100%;
}

.fieldcontain {
    width: 25%;
    display: inline-block;
    float: left
}

.row:before, .row:after {
     display: table;
    content: " ";
}

您的HTML应如下所示:

<fieldset class="form">
    <div class="row">
        <div class="fieldcontain  required">
            <label for="firstName">
                First Name
                <span class="required-indicator">*</span>
            </label>
            <input type="text" name="firstName" required="" value="" id="firstName">
        </div>

        <div class="fieldcontain  required">
            <label for="lastName">
                Last Name
                <span class="required-indicator">*</span>
            </label>
            <input type="text" name="lastName" required="" value="" id="lastName">
        </div>

        <div class="fieldcontain  required">
            <label for="email">
                Email
                <span class="required-indicator">*</span>
            </label>
            <input type="text" name="email" required="" value="" id="email">
        </div>

        <div class="fieldcontain  required">
            <label for="number">
                Number
                <span class="required-indicator">*</span>
            </label>
            <input type="text" name="number" required="" value="" id="number">
        </div>
    </div>
</fieldset>

Wrap,你的4个元素集合,.fieldcontaindiv.row,类{ "my-class": [ { "class_name": "1H", "class_room": "201aTD", "day": "26", "month": "10", "period": "2", "subject": "IF", "teacher": "J04", "year": "2015" }] } 就像我在示例中所示。

虽然,Grails提供了一个基本的CSS样式,所以新项目可以快速启动,但我建议你使用Twitter Bootstrap

答案 1 :(得分:0)

...
<style>
#fieldlabel {display:block;}
#fieldinput {display:block;}
</style>
...
<body>
<label id="fieldlabel"><input type="text" id="fieldinput"></label>
</body>
...