Tips for repositioning form fields using HTML, CSS, and Flask-WTF

I am currently developing my first web application. I require a basic form for users to input a few values. Although I have successfully implemented it, I would prefer the data entry fields to be arranged in a row instead of a column.

The snippets below showcase the use of Bootstrap. However, I believe Bootstrap is not causing the fields to appear in the same place even without its usage.

In base.html:

{% extends 'bootstrap/base.html' %}

{% block title %}
...
{% endblock %}

{% block navbar %}
...
{% endblock %}

{% block content %}
    <div class="container">
...
        {# application content needs to be provided in the app_content block #}
        {% block app_content %}{% endblock %}
    </div>
{% endblock %}

In forms.py:

from flask_wtf import FlaskForm
from wtforms import StringField, IntegerField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired, IPAddress, NumberRange, length

class SnmpQueryForm(FlaskForm):
    ip = StringField('IP Address', validators=[IPAddress()])
    mask = IntegerField('Mask bits',
                         validators=[DataRequired(),
                         NumberRange(22, 32)]
                       )
    snmpcmstr = StringField('Community String',
                            validators=[DataRequired(),
                            length(max=20)]
                           )
    submit = SubmitField('Submit Query')

In template.html:

{% extends "base.html" %}
{% import 'bootstrap/wtf.html' as wtf %}

{% block app_content %}
    <h1>Enter SNMP Query Information</h1>
    <form action="", method="post">
    <div class="row">
        <div class="col-md-2">
            {{ wtf.quick_form(form) }}
        </div>
    </div>
    </form>
    <br>
    <br>
    <div class="container">
        ...

Check out the outcome here:

https://i.sstatic.net/GRRcx.png

I want all three fields to align on the same line (in one row). Any recommendations?

Appreciate your assistance

To provide the solution I utilized:

All I needed to do was modify Lakindu's POC code to:

<div class="col-md-4">
  <div class="form-group">
    {{wtf.form_field(form.ip, class="form-control", placeholder="Enter IP") }}
  </div>
</div>

for each input field and include:

<div class="col-md-4">
  <div class="input submit">
      <input type="submit" value="Submit">
  </div>  
</div>

for the submit button.

Much obliged for the prompt assistance.

Answer №1

Here is a demonstration of how to achieve your goal. Utilize the col-md-4 within a row and the container class to place three input fields in a single row.

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">


<form action="" , method="post">
  <div class="container">
    <div class="row">
      <div class="col-md-4">
        <div class="form-group">
          <label>IP address</label>
          <input type="text" class="form-control">
        </div>
      </div>

      <div class="col-md-4">
        <div class="form-group">
          <label>Mask bits</label>
          <input type="text" class="form-control">
        </div>
      </div>

      <div class="col-md-4">
        <div class="form-group">
          <label>Community string</label>
          <input type="text" class="form-control">
        </div>
      </div>

    </div>
  </div>
</form>

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

Show input fields in a row

In an attempt to align my select form fields on the same line, I have tried adding display:inline; to both the select and label elements in my code. However, it did not produce the desired result. Here is the HTML code snippet: <form id ="myform1"> ...

Sending the value from a for loop through AJAX and populating it into a form field

Currently, I have implemented a piece of JavaScript code that captures user input, sends a request to an endpoint using AJAX, and appends a specific field from the returned results as an option within a datalist. This functionality is working perfectly fin ...

Avoid clicking in areas outside the perimeter of the blue circle in the SVG

Is there a way to restrict clicking outside the blue circle? The goal is to only allow opening by clicking on the svg itself, This includes everything, even white space inside the svg. How can this be achieved in the code? The current behavior is that ...

My attempts to integrate PHP into jQuery tabs have been unsuccessful

My code seems to be acting strangely, as the PHP code I originally entered in tab 2 has somehow escaped that tab and spread into all the other tabs. This means it is visible in multiple tabs instead of just one. The purpose of this PHP code is to retrieve ...

Center an image and superimpose text in the middle of the webpage

I am attempting to center an image with text overlay in the middle of a page. Although the image is centered correctly, the overlay text remains off-center. Below is the code snippet. The text should be positioned in the top left corner of the image. &l ...

Uploading Multiple Files with Base64 Encoding in PHP

I am looking for a way to store images in a database and retrieve them using base64 encoding/decoding. Can anyone guide me on how to achieve this? Here is the HTML code: <form method="POST" action="add_script.php" enctype="multipart/form-data"> ...

Creating a function that counts the number of times a button is clicked

I want to have the "game" button appear after the user clicks the "next-btn" 2 times. Once the multiple choice test has been completed twice, I'd like the game button to show up and redirect the user to a separate HTML page called "agario.html." I&ap ...

Using a snippet of HTML code from one Angular component in another component

Is it possible to use a specific div element from one Angular component in another component? main component.html <html> <div1> some elements </div1> <div2> some elements </div2> In the child ...

CSS for decorating an image with a border and transparent background

Struggling to add a border while converting a PSD to HTML? Is it possible to apply a border to an image with a transparent background? For instance, let's consider the following image: Imagine wanting to place a border around the caret in the image l ...

How can I include a ?ref query parameter in a URL when linking from external websites?

I've always wondered about how websites are able to include the ?ref parameter in their URLs when they are referred from another website. Do both sites need to incorporate this into their code? How does this function exactly? ...

Controlling the page scroll when adding data using jQuery

I am encountering an issue with a webpage that displays around 20 pictures in separate divs stacked vertically. Below these 20 pictures, there is a "show more" button that, when clicked, loads another set of 20 pictures and appends them to the existing dat ...

Exploring ways to loop through a JSON array and embed it into an HTML element

After the ajax request, the data returned is structured as follows: Data = [ ["18/02/2019", "A"], ["19/03/2019", "B"], ["21/05/2019", "C"], ] The ajax request was successful and the data is stored in a variable named Data within a function. ...

What steps do I need to take in Bootstrap 5 to add a search icon to the navbar that reveals a search box beneath it when clicked?

I've made progress on the navbar design Now, I'm looking to add a search icon next to the login button. Clicking on the search icon should reveal a search box below the navbar, similar to the image shown below. My transparent navbar has a relati ...

Incorporate text into the URL of the image

Got a URL of an image like this: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg', and looking to add 240x240 within the URL. Current Url: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg Desire ...

Manipulating Object origin data

I'm unsure if this can be achieved. I am aiming to create a header and footer that remain the same while only switching the content pages. The resources I have at my disposal cover HTML, JS, CSS, and JQuery. Below is a simplified version of my curre ...

What is the most effective method for embedding a Kotlin program into a website?

I have created a combat simulation tool in Kotlin for an online gaming community. Users can input the combat levels of two players, choose the number of duels to simulate, and then initiate the simulation which will provide win percentages and other stats. ...

Modify only unprocessed text within the HTML content

Consider the following string: html_string = '<span><span class=\"ip\"></span> Do not stare <span class=\"img\"></span> at the monitor continuously </span>\r\n' I am looking to ...

How can you center a left floated div?

I am seeking to showcase a series of images in a horizontal layout on my page. Beneath each image, there are several links that need to be grouped together within a container. So far, I have tried placing them in floating divs, which align them to the lef ...

Tips for ensuring a flexbox stays within the bounds of its container and doesn't exceed the available space

I am in the process of developing a modal that needs to be centered on my page and expand organically, but only up to a specific width and height limit. Using flexbox, I have successfully centered this modal (.modal) on the page. Within the modal, there ...

The unitPngFix plugin ensures that the display of hidden div elements cannot be altered

I have been trying to resolve an issue with PNG files and transparency in IE browsers on my website. I have made progress, but there seems to be a problem specifically with IE6. To display transparent PNG images correctly on my site in IE browsers, I am u ...