Avoid placing the label within a form-inline in Bootstrap to maintain proper formatting and alignment

I am working on a form where I have two fields inline within a form-inline div. I am looking to move the labels of these fields above them rather than next to them. How can I achieve this without affecting the layout of the fields?

Here is a CodePen link

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

                    <label class="" for="lastNameContact">Name</label>
                    <input type="text" class="form-control mb-2 mr-sm-2" id="lastName_contact" placeholder="Prénom" required>
                </div>
                <div class="form-group">
                    <label class="" for="email_contact">E-Mail</label>
                    <input type="email" name="email_contact" id="email_contact" class="form-control" placeholder="E-Mail" required>
                </div>
                <div class="form-group">
                    <label class="" for="message_contact">Message</label>
                    <textarea class="form-control" id="message_contact" placeholder="Message" 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;
}

Answer №1

If my understanding is correct, you don't need to utilize the form-inline class. Instead, you can use the following code snippet for a visually appealing form:

<main id="contact_part">
    <div class="container">
        <article class="mb-5 row">
            <div class="col-12">
                <h1>Contact Us</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">First Name</label>
                                <input type="text" class="form-control" id="name_contact" placeholder="First Name" required>
                            </div>
                        </div>
                        <div class="col-12 col-md-6">
                            <div class="form-group">
                                <label class="custom-control pl-0" for="lastNameContact">Last Name</label>
                                <input type="text" class="form-control" id="lastName_contact" placeholder="Last Name" required>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="form-group">
                                <label class="custom-control pl-0" for="email_contact">E-Mail</label>
                                <input type="email" name="email_contact" id="email_contact" class="form-control" placeholder="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1d78657c706d71785d78657c706d7178337e7270">[email protected]</a>" required>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="form-group">
                                <label class="custom-control pl-0" for="message_contact">Message</label>
                                <textarea class="form-control" id="message_contact" placeholder="Message" rows="5" required></textarea>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </article>
    </div>
</main>

Access Code Pen.

Opt for using col-12 over col-md-12 class for enhanced performance. Avoid utilizing row without either container or container-fluid, as it may result in unintended horizontal scroll bars. A streamlined version of your code would resemble this:

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

Alternatively

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

For Fluid View or 100% Browser Width

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

I trust this information proves valuable!

Answer №2

Check out this link and see if it solves your problem: https://codepen.io/luke-richter/pen/XWJVqWO

<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
#contact_part{
    background-color: #FFF989;
    font-family: main, "Palatino Linotype", "Book Antiqua", Palatino, serif;
    color: #565656;
    padding: 50px;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">

<!------ Include the above in your HEAD tag ---------->


<main id="contact_part">
<div class="container ">
<div class="row">
<h2>Inline form with heading above inputs</h2>
</div>
<form action="#">
<div class="row">
<div class="col-md-3">
<div class="form-group form-group-sm">
<label for="firstname" class="control-label">Firstname1</label>
<input type="text" class="form-control" id="firstname" placeholder="Firstname">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="lastname" class="control-label">Last Name2</label>
<input type="text" class="form-control" id="lastname" placeholder="Last Name">
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="lastname" class="control-label">Last Name3</label>
<input type="text" class="form-control" id="lastname" placeholder="Last Name">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-3">
<textarea class="form-control"></textarea>
</div>
</div>
<div class="row">
<div class="col-xs-3"><br>
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
</div>
</div>

Answer №3

Here's a suggestion for you:

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

Alternatively, consider relocating the label outside the <div class="form-inline">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choosing a menu option with jQuery

Can someone help me with making a menu option selected in HTML? Here is my code: <ul class="ca-menu"> <li> <a href="#"> <span class="ca-icon"><img src="images/home.png" ...

Is there a way to align the input and button side by side using Bootstrap v5?

Is there a way to make my input textbox and button appear side by side without using the d-flex class in the div? I want to center align the content but removing the class messes up the alignment. Any suggestions on how to accomplish this? <!DOCTYPE htm ...

Creating HTML input elements dynamically using Javascript

<body> <form action="insertquestion.php" method="POST"> <script> function generateInputs(){ var prefix = "answer"; var number = 1; for(var i = 0; i < 5; i++){ ...

Effortlessly find data in an HTML table and showcase the results instantly without

I am looking for a way to implement search functionality in my table without refreshing the page. The fields above the table are used for searching and I want the results to be displayed within the same table. Here is an example of the code: <?php $for ...

Tips on sending the total value of a form to a PHP script

Looking for help with a PHP script to calculate the sum of checked checkboxes with corresponding numeric values. For example, if checkbox 1 = 80; checkbox 2 = 21; and checkbox 3 = 15, and the user checks checkboxes 2 and 3, the PHP should output 36. I hav ...

The font face feature does not seem to be functioning properly in the IE browser

I'm facing an issue with font face in a jQuery popup. It seems to be working fine on Chrome and Firefox, but it's not rendering properly on IE browsers. Font Face Code in css ---------------------- @font-face { font-family: "pt-sans"; sr ...

What is the best way to choose a random number using jQuery?

<div class="yyy"></div> <p>...</p> <pre><code>let content = $(".xxx").text(); $(".yyy").html(content); http://jsfiddle.net/tQeCv/ I just require this specific input : Number is {1|2|3|4}, and {one|two|three|four} ...

Exploring the functionality of CodePen's code editor in relation to developing a 2D shooting game

Recently, I created a straightforward 2D shooter game with all the code neatly organized in a single HTML file: (file_gist). When I tested the game in my chrome browser, everything worked flawlessly according to my intentions. However, upon transferring th ...

Unable to apply border radius to the div containing the video

Currently, I am attempting to create a video with rounded corners by wrapping the <video> tag in a div element with rounded corners. This method works perfectly in Chrome and Firefox, but unfortunately does not work in Safari. It seems like this ma ...

What is the best way to utilize a JavaScript variable as a background within an inline style sheet?

I have a fun project for this evening - I am trying to make my website load a different background image every time the page is refreshed. Earlier on in this project, I managed to make the background interact with window size and screen resolution similar ...

jQuery modal window extension

Currently, I am utilizing a jQuery popup plugin that opens a popup window using the anchor's href. For example: <a href="/some/site?hello=hi" class="popup">link</a> There could be an unlimited number of these on my page, each pointing to ...

Issues with the CSS animation not properly scaling

I'm currently working on an animation using CSS keyframes. Here is what I have: @keyframes experience { 0% { -webkit-transform: scale(0,0); transform: scale(0,0); -webkit-transform: translateY(40px); transform: translateY(40px); opac ...

Empty Firebase currentUser Redirected After Successful Sign In

I have a webpage named index.html which serves as the sign-in page. After a user successfully signs in, I want to redirect them to a new page called home.html, where their email will be displayed. However, I am encountering an issue with getting a null va ...

Retrieve the hidden value buried within multiple layers of classes

I'm having trouble retrieving the value from the pickup-address class. Here is my current code. Can anyone help me identify where I might be going wrong? JS var pickupAddress = $('.timeline-item.active-item > .timeline-status > .pickup-add ...

Generating a business card example using node-html-pdf on an Ubuntu operating system

I am currently attempting to utilize the node-html-pdf module on Ubuntu 16.04 by following the businesscard example provided at https://github.com/marcbachmann/node-html-pdf. Regrettably, I encountered difficulties in generating the PDF file. To begin wi ...

React state update not triggering a component re-render

I've been attempting to toggle the visibility of a div by clicking on another div, but I'm encountering an issue. The div becomes invisible on the first click only if it was visible initially. After that, it remains invisible and does not update. ...

Icon overlapping text from Font Awesome

Although my contact form is working well, I have noticed that the text in the message area tends to overlap with the icon as more content is added. Initially, everything aligns perfectly, but as the text extends, it ends up overlapping with the icon. Can ...

Select the dropdown menu to access the last line of the table

My custom dropdown is causing a scroll to appear at the bottom of the table when it's open, making it hard to view all the elements. I want to find a way to either expand the body of the dropdown or enlarge it so that this doesn't happen. Can any ...

Encountered a Three.js error labeled as Uncaught TypeError while trying to follow

I've been following a tutorial on how to integrate three.js into my project from this video: https://www.youtube.com/watch?v=1TeMXIWRrqE I copied the source code provided at the bottom of this webpage: After downloading and extracting the model into ...

Offsetting absolute elements in a horizontal landscape view

When I view my page on a mobile browser (tested on Android JB and iOS 6), it renders fine initially in both portrait and landscape modes. However, the issue arises when I switch from landscape to portrait - the absolute positioned divs are offset from thei ...