不要将标签内嵌在 form-inline 中 – Bootstrap

huangapple go评论78阅读模式
英文:

Do not put the label inline in a form-inline - Bootstrap

问题

Sure, here's the translated content:

我有一个表单,在这个表单中有两个字段排成一行,因为它们在一个 form-inline 的 div 中,我想把这两个字段的标签放在它们上方而不是旁边。如何去掉它们的 form-inline 标签而不影响字段?

以下是代码部分的链接:
CodePen 链接

<main id="contact_part">
    <article class="mb-5 row">
        <h1>联系我们</h1>
        <div class="col-md-12">
            <form action="" method="post" id="contact_form">
                <div class="form-inline">
                    <label class="" for="name_contact">姓名</label>
                    <input type="text" class="form-control mb-2 mr-sm-2" id="name_contact" placeholder="名字" required>

                    <label class="" for="lastNameContact">姓氏</label>
                    <input type="text" class="form-control mb-2 mr-sm-2" id="lastName_contact" placeholder="姓氏" required>
                </div>
                <div class="form-group">
                    <label class="" for="email_contact">电子邮件</label>
                    <input type="email" name="email_contact" id="email_contact" class="form-control" placeholder="电子邮件" required>
                </div>
                <div class="form-group">
                    <label class="" for="message_contact">消息</label>
                    <textarea class="form-control" id="message_contact" placeholder="消息" required></textarea>
                </div>
            </form>
        </div>
    </article>
</main>
#contact_part article {
    background-color: #FFF989;
    font-family: main, "Palatino Linotype", "Book Antiqua", Palatino, serif;
    color: #565656;
    padding: 50px;
}

#contact_part .row {
    display: block;
}

#contact_part h1 {
    font-size: 1.7rem;
    text-transform: uppercase;
    margin-bottom: 3rem;
    text-decoration: underline;
    text-align: center;
}

#contact_form {
    width: 50%;
    margin: 0 auto;
}

#contact_form .form-inline label {
    
}

#contact_form input, #contact_form textarea {
    border: none;
    border-bottom: 1px solid #C6C6C6;
    background-color: #FFF989;
}
英文:

I have a form, I have in this two fields inline because they are in a form-inline div, I want to put the label of these above the fields and not next to them. How to remove form-inline from them without affecting the fields?

Here is a CodePen

&lt;main id=&quot;contact_part&quot;&gt;
    &lt;article class=&quot;mb-5 row&quot;&gt;
        &lt;h1&gt;Nous Contacter&lt;/h1&gt;
        &lt;div class=&quot;col-md-12&quot;&gt;
            &lt;form action=&quot;&quot; method=&quot;post&quot; id=&quot;contact_form&quot;&gt;
                &lt;div class=&quot;form-inline&quot;&gt;
                    &lt;label class=&quot;&quot; for=&quot;name_contact&quot;&gt;Name&lt;/label&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control mb-2 mr-sm-2&quot; id=&quot;name_contact&quot; placeholder=&quot;Nom&quot; required&gt;

                    &lt;label class=&quot;&quot; for=&quot;lastNameContact&quot;&gt;Name&lt;/label&gt;
                    &lt;input type=&quot;text&quot; class=&quot;form-control mb-2 mr-sm-2&quot; id=&quot;lastName_contact&quot; placeholder=&quot;Pr&#233;nom&quot; required&gt;
                &lt;/div&gt;
                &lt;div class=&quot;form-group&quot;&gt;
                    &lt;label class=&quot;&quot; for=&quot;email_contact&quot;&gt;E-Mail&lt;/label&gt;
                    &lt;input type=&quot;email&quot; name=&quot;email_contact&quot; id=&quot;email_contact&quot; class=&quot;form-control&quot; placeholder=&quot;E-Mail&quot; required&gt;
                &lt;/div&gt;
                &lt;div class=&quot;form-group&quot;&gt;
                    &lt;label class=&quot;&quot; for=&quot;message_contact&quot;&gt;Message&lt;/label&gt;
                    &lt;textarea class=&quot;form-control&quot; id=&quot;message_contact&quot; placeholder=&quot;Message&quot; required&gt;&lt;/textarea&gt;
                &lt;/div&gt;
            &lt;/form&gt;
        &lt;/div&gt;
    &lt;/article&gt;
&lt;/main&gt;
#contact_part article {
    background-color: #FFF989;
    font-family: main, &quot;Palatino Linotype&quot;, &quot;Book Antiqua&quot;, Palatino, serif;
    color: #565656;
    padding: 50px;
}

#contact_part .row {
    display: block;
}

#contact_part h1 {
    font-size: 1.7rem;
    text-transform: uppercase;
    margin-bottom: 3rem;
    text-decoration: underline;
    text-align: center;
}

#contact_form {
    width: 50%;
    margin: 0 auto;
}

#contact_form .form-inline label {
    
}

#contact_form input, #contact_form textarea {
    border: none;
    border-bottom: 1px solid #C6C6C6;
    background-color: #FFF989;
}

答案1

得分: 2

如果我理解正确,您无需使用form-inline类,而是可以像下面的代码一样使用。您的表单将看起来很好。

<main id="contact_part">
    <div class="container">
        <article class="mb-5 row">
            <div class="col-12">
                <h1>Nous Contacter</h1>
            </div>
            <div class="col-12">
                <form action="" method="post" id="contact_form">
                    <div class="row">
                        <div class="col-12 col-md-6">
                            <div class="form-group">
                                <label class="custom-control pl-0" for="name_contact">姓</label>
                                <input type="text" class="form-control" id="name_contact" placeholder="姓" required>
                            </div>
                        </div>
                        <div class="col-12 col-md-6">
                            <div class="form-group">
                                <label class="custom-control pl-0" for="lastNameContact">名</label>
                                <input type="text" class="form-control" id="lastName_contact" placeholder="名" required>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="form-group">
                                <label class="custom-control pl-0" for="email_contact">电子邮件</label>
                                <input type="email" name="email_contact" id="email_contact" class="form-control" placeholder="example@example.com" required>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="form-group">
                                <label class="custom-control pl-0" for="message_contact">留言</label>
                                <textarea class="form-control" id="message_contact" placeholder="留言" rows="5" required></textarea>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </article>
    </div>
</main>

为了更好的性能,请使用col-12而不是col-md-12类。不要在没有containercontainer-fluid的情况下使用row。如果只使用row,会出现水平滚动条。您的代码将如下所示:

<div class="container">
    <div class="row">
        <div class="col-12"></div>
    </div>
</div>

或者

<div class="container-fluid">
    <div class="row">
        <div class="container">
            <div class="row">
                <div class="col-12"></div>
            </div>
        </div>
    </div>
</div>

流式视图或100%浏览器宽度

<div class="container-fluid">
    <div class="row">
        <div class="col-12">内容</div>
        <div class="col-12">内容</div>
        <div class="col-12">内容</div>
        <div class="col-12">内容</div>
    </div>
</div>

希望这对您有所帮助!

英文:

If I understand correctly you no need to use form-inline class instead use like below code. Your form will look good.

&lt;main id=&quot;contact_part&quot;&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;article class=&quot;mb-5 row&quot;&gt;
&lt;div class=&quot;col-12&quot;&gt;
&lt;h1&gt;Nous Contacter&lt;/h1&gt;
&lt;/div&gt;
&lt;div class=&quot;col-12&quot;&gt;
&lt;form action=&quot;&quot; method=&quot;post&quot; id=&quot;contact_form&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-12 col-md-6&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;custom-control pl-0&quot; for=&quot;name_contact&quot;&gt;First Name&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;name_contact&quot; placeholder=&quot;First Name&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-12 col-md-6&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;custom-control pl-0&quot; for=&quot;lastNameContact&quot;&gt;Last Name&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;lastName_contact&quot; placeholder=&quot;Last Name&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-12&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;custom-control pl-0&quot; for=&quot;email_contact&quot;&gt;E-Mail&lt;/label&gt;
&lt;input type=&quot;email&quot; name=&quot;email_contact&quot; id=&quot;email_contact&quot; class=&quot;form-control&quot; placeholder=&quot;example@example.com&quot; required&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-12&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;custom-control pl-0&quot; for=&quot;message_contact&quot;&gt;Message&lt;/label&gt;
&lt;textarea class=&quot;form-control&quot; id=&quot;message_contact&quot; placeholder=&quot;Message&quot; rows=&quot;5&quot; required&gt;&lt;/textarea&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/article&gt;
&lt;/div&gt;
&lt;/main&gt;

Code Pen.

Use col-12 instead of col-md-12 class for better performance. Don't use row without container or container-fluid. If you use only row it give you horizontal scroll bar. Your code will look like this.

&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-12&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

Or

&lt;div class=&quot;container-fluid&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-12&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

Fluid view or 100% Browser width

&lt;div class=&quot;container-fluid&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-12&quot;&gt;Content&lt;/div&gt;
&lt;div class=&quot;col-12&quot;&gt;Content&lt;/div&gt;
&lt;div class=&quot;col-12&quot;&gt;Content&lt;/div&gt;
&lt;div class=&quot;col-12&quot;&gt;Content&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

> Hope this help!
1: https://codepen.io/rahulmoral/pen/dyPJeNG

答案2

得分: 1

尝试这个,

.form-inline label
{
margin-bottom:3em;
}

或者你可以将标签移到<div class="form-inline">之外。

英文:

Try this,

.form-inline label
{
margin-bottom:3em;
}

Or You can move the label outside the&lt;div class=&quot;form-inline&quot;&gt;

答案3

得分: 1

这里是你需要的翻译:

这是您要的翻译:

&lt;!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false --&gt;

&lt;!-- 语言:lang-js --&gt;

    &lt;script src=&quot;//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
    &lt;script src=&quot;//code.jquery.com/jquery-1.11.1.min.js&quot;&gt;&lt;/script&gt;

&lt;!-- 语言:lang-css --&gt;

    #contact_part{
        background-color: #FFF989;
        font-family: main, &quot;Palatino Linotype&quot;, &quot;Book Antiqua&quot;, Palatino, serif;
        color: #565656;
        padding: 50px;
    }

&lt;!-- 语言:lang-html --&gt;

    &lt;link href=&quot;//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; id=&quot;bootstrap-css&quot;&gt;

    &lt;!------ 在 HEAD 标签中包含以上内容 ----------&gt;


    &lt;main id=&quot;contact_part&quot;&gt;
    &lt;div class=&quot;container &quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
    &lt;h2&gt;带有输入字段上方标题的内联表单&lt;/h2&gt;
    &lt;/div&gt;
    &lt;form action=&quot;#&quot;&gt;
    &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-md-3&quot;&gt;
    &lt;div class=&quot;form-group form-group-sm&quot;&gt;
    &lt;label for=&quot;firstname&quot; class=&quot;control-label&quot;&gt;名字1&lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;firstname&quot; placeholder=&quot;名字&quot;&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-md-3&quot;&gt;
    &lt;div class=&quot;form-group&quot;&gt;
    &lt;label for=&quot;lastname&quot; class=&quot;control-label&quot;&gt;姓氏2&lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;lastname&quot; placeholder=&quot;姓氏&quot;&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;col-md-3&quot;&gt;
    &lt;div class=&quot;form-group&quot;&gt;
    &lt;label for=&quot;lastname&quot; class=&quot;control-label&quot;&gt;姓氏3&lt;/label&gt;
    &lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;lastname&quot; placeholder=&quot;姓氏&quot;&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-xs-3&quot;&gt;
    &lt;textarea class=&quot;form-control&quot;&gt;&lt;/textarea&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class=&quot;row&quot;&gt;
    &lt;div class=&quot;col-xs-3&quot;&gt;&lt;br&gt;
    &lt;button type=&quot;submit&quot; class=&quot;btn btn-default&quot;&gt;提交&lt;/button&gt;
    &lt;/div&gt;
    &lt;/div&gt;
    &lt;/form&gt;
    &lt;/div&gt;
    &lt;/div&gt;


&lt;!-- 结束代码片段 --&gt;

这是你需要的内容的翻译,不包括代码部分。

英文:

Here you go, let me know if this works: https://codepen.io/luke-richter/pen/XWJVqWO

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

&lt;script src=&quot;//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;//code.jquery.com/jquery-1.11.1.min.js&quot;&gt;&lt;/script&gt;

<!-- language: lang-css -->

#contact_part{
background-color: #FFF989;
font-family: main, &quot;Palatino Linotype&quot;, &quot;Book Antiqua&quot;, Palatino, serif;
color: #565656;
padding: 50px;
}

<!-- language: lang-html -->

&lt;link href=&quot;//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; id=&quot;bootstrap-css&quot;&gt;
&lt;!------ Include the above in your HEAD tag ----------&gt;
&lt;main id=&quot;contact_part&quot;&gt;
&lt;div class=&quot;container &quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;h2&gt;Inline form with heading above inputs&lt;/h2&gt;
&lt;/div&gt;
&lt;form action=&quot;#&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-md-3&quot;&gt;
&lt;div class=&quot;form-group form-group-sm&quot;&gt;
&lt;label for=&quot;firstname&quot; class=&quot;control-label&quot;&gt;Firstname1&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;firstname&quot; placeholder=&quot;Firstname&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-3&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;lastname&quot; class=&quot;control-label&quot;&gt;Last Name2&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;lastname&quot; placeholder=&quot;Last Name&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;col-md-3&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label for=&quot;lastname&quot; class=&quot;control-label&quot;&gt;Last Name3&lt;/label&gt;
&lt;input type=&quot;text&quot; class=&quot;form-control&quot; id=&quot;lastname&quot; placeholder=&quot;Last Name&quot;&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-xs-3&quot;&gt;
&lt;textarea class=&quot;form-control&quot;&gt;&lt;/textarea&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-xs-3&quot;&gt;&lt;br&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn btn-default&quot;&gt;Submit&lt;/button&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月7日 00:08:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定