Proper Alignment of Input Forms in Django and HTML

I am attempting to create a simple layout. Currently, this is what I have: https://i.sstatic.net/2Ji5r.png

The code in question is as follows:

{% extends 'base.html' %}
{% load static %}
{% block content %}
<style>
.center {
  margin: auto;
  width: 50%;
  border: 3px solid green;
  padding: 10px;
}
</style>
<div class='center'>
    <form action='.' method='POST'> {% csrf_token %}
        <div align="right">
            <input type="text" name='title' placeholder='Food title'/>
        </div>
        <div align='left'>
            <input type="text" name='q' placeholder='Search Image'/>
            <input type="submit" value='Search!'/> 
        </div>
        {% for image in images %}
            <img src="{% static 'img/search/' %}{{ image }}", alt=""/>
            <input type="radio" name="imageRadio" value={{image}}/>
            </br>
        {% endfor %}
            <input type="submit" value='Go!'/>
    </form>
</div>
{% endblock content %}

My current dilemma lies in aligning the Food title input and search bar on the same line without one being positioned slightly higher than the other. How can I achieve this desired spacing?

Answer №1

There are several ways you could tackle this issue. One option is to utilize the float property, however, it may not be the most optimal choice. Another solution is to explore FlexBox, which is widely supported in current web development practices.

Here's an example:

{% extends 'base.html' %}
{% load static %}
{% block content %}
<style>
    .center {
      margin: auto;
      width: 50%;
      border: 3px solid green;
      padding: 10px;
    }
    .inputs {
        display: flex;
        justify-content: space-between;
    }
</style>

<div class='center'>
    <form action='.' method='POST'> {% csrf_token %}
        <div class="inputs">
            <div>
                <input type="text" name='title' placeholder='Food title'/>
            </div>
            <div>
                <input type="text" name='q' placeholder='Search Image'/>
                <input type="submit" value='Search!'> 
            </div>
        </div>
        {% for image in images %}
            <img src="{% static 'img/search/' %}{{ image }}", alt=""/>
            <input type="radio" id=image name="gender" value="male">
            </br>
        {% endfor %}
        <input type="submit" value='Go!'>
    </form>
</div>
{% endblock content %}

Answer №2

This solution involves a fundamental understanding of HTML and CSS. The inputs are currently placed in two separate divs, which are block-level elements that each occupy their own "row" on the page. Instead of simply changing the display property of these divs, it is recommended to follow best practice by placing the inputs within a shared div and setting its display to flex. This will allow you to adjust spacing using justify-content:

<style>
.center {
  margin: auto;
  width: 50%;
  border: 3px solid green;
  padding: 10px;
}
.my-inputs {
  display: flex;
  justify-content: space-between;
}
</style>


<div class="my-inputs">
    <input type="text" name='title' placeholder='Food title'/>
    <input type="text" name='q' placeholder='Search Image'/>
    <input type="submit" value='Search!'/>
</div>

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

Verify if an item does not exist in a Django model queryset

Imagine I have a table structured like this: col1 col2 1 completed 2 error 3 initiated 4 error 5 completed 6 error 7 completed Now, within Django, I have a query that looks like this: Model.objects.filter(col1__in=[1,2,8]).values(&apo ...

Curved Edges CSSPIE with a Touch of CSS Gradient

We are facing an issue with the code below, which creates a basic box with rounded corners and a gradient. We have applied CSSPie to achieve this effect, but we have observed that in all versions of Internet Explorer, the rounded corners do not work due to ...

Maintain Open State of Toggle Drop Down Menu

Having an issue with the toggle down menu or list on my webpage. The problem is that the menu is constantly open (the #text_box) when the page loads. My intention was for it to be closed initially, and then open only when the user clicks on the 'Ope ...

Instantly display modal upon website loading

Adding more context to the title, I have a modal that was included in a purchased template and it was originally set to only open when a button is clicked. However, I want the modal to automatically pop up when the page loads. The issue is that the modal ...

How does one go about accessing file fields in forms.py within Django?

Within my Django application, I have defined a class named EmailForm in forms.py like this: class EmailForm(EmailFormWithoutAttachment): attachment = forms.FileField() My goal is to validate the uploaded file in forms.py by checking its name using a ...

The Functionality of JQuery Dropdowns

Just dipping my toes into the world of JQuery.. only about 2 hours in. Started working on a drop-down menu for a login box that looks like this: HTML: <button id="loginButton">Login</button> When you hover over the button, this JQuery functio ...

How can I extract the "href" attribute from an anchor tag using XPath?

I am working with HTML code that looks like this: <a href="/images/big_1.jpg" class="class-a"> <img class="class-img" src="/images/small_1.jpg"/> <span class="class-span"> <img src="/images/img_1.png"> </ ...

Retrieve information from a database based on user input keywords utilizing PHP and SQL

I have been exploring ways to allow users to enter keywords via HTML forms and then use this information to retrieve the relevant data from a database using PHP and SQL. So far, I have only been able to display data from the database using a static SQL qu ...

Encountering problems when working with fixtures in tests using Django's DirtyFields Library

Have you encountered any issues with using fixtures in tests while implementing Django DirtyFields? We utilize django_nose's NoseTestSuiteRunner along with fixtures and coverage. I wanted to integrate django-dirtyfields for custom post_save() tasks. ...

Looking to create a dynamic Angular reactive form using API response data? Seeking guidance on how to achieve this? Let's

[ { "name": "jkjk", "firstName": "hgh", "lastName": "ehtrh", "replytype": "svdv", "prodCode": "svv", "execu ...

How can you align a div next to another using the float property in CSS?

/*Custom Styling for Divs*/ #box1 { border: 1px solid; border-radius: 25px; width: 900px; } #box2 { border: 1px solid; border-radius: 25px; width: 430px; } #box3 { float: right; border: 1px solid; border-radius: ...

Is Javascript the new CSS:hover killer?

Hello, I'm currently working on implementing the Product Categories menu on this specific page: Currently, when the page loads, the CSS hover effect works correctly to display a rollover effect. However, I attempted to use JavaScript to set the backg ...

Is it possible to modify the background color of the firefox address bar according to the type of protocol being used?

Is there a way to customize the background color of the address bar in Firefox according to different protocols? For example, using blue for https, orange for mixed content warning, green for ev-certificate, and red for invalid certificates. ...

Results from the hierarchical drop-down navigation system

I am faced with a situation where I have an array containing data: $arr=[1=>['id'=>1,'value'=>"Main",'id_parent'=>0], 2=>['id'=>2,'value'=>"Port",'id_parent'=>0,&apo ...

The content in the footer is not displaying correctly (Vuejs)

I recently developed a component with a title and a background color meant to cover the entire page, but it seems to have a top margin that I can't seem to remove. Additionally, I created a footer component that should always stay at the bottom of the ...

Error encountered while sending a POST request to Django API due to CSRF issue

I'm in the process of developing an application with a React JS front end and Python Django backend. Upon successful login, the backend generates necessary cookies, including the CSRF token. However, when the front end sends a POST request to the API, ...

Button from Material-UI vanishes upon being clicked

I encountered an issue with a button that disappears when clicked on. Additionally, clicking the button once does not trigger any actions associated with it. In order to execute the button actions, I have to click the area where the button was located afte ...

Error: Unable to allocate "auth.User" to "Token.user" because of ValueError

I'm encountering an error when trying to generate a token with the user ID. The error message I'm receiving is ValueError: Cannot assign "'auth.User'": "Token.user" must be a "User" instance. I would really appreciate it if someone coul ...

Achieving targeted text display on specific viewports using the visible class in Bootstrap

I've recently delved into learning Bootstrap, and I'm experimenting with creating a responsive page where certain divs will only be visible on specific viewports. I used classes like visible-xs, visible-sm, visible-md, and visible-lg for the diff ...

Setting up Django and MongoDB migration in a Docker environment

version: "3" services: rango_api: container_name: my-rango-container build: ./ command: python manage.py runserver 0.0.0.0:8000 working_dir: /usr/src/rango_api environment: REDIS_URI: redis://redis:6379 MONGO_URI: mongodb ...