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

How to place field label on top of field instead of left side in Grails

我正在使用 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>

以上代码的输出如下:

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

每行一个字段::

表单元素::

    <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>
...
<style>
#fieldlabel {display:block;}
#fieldinput {display:block;}
</style>
...
<body>
<label id="fieldlabel"><input type="text" id="fieldinput"></label>
</body>
...

给你。只需定义以下 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>

换行,你的 4 个元素集,即 div 中的 .fieldcontain 和 class .row,就像我在示例中显示的那样。

尽管 Grails 提供了基本的 CSS 样式,因此可以快速引导新项目,但我建议您使用 Twitter Bootstrap.