Make sure that the input field and label are aligned side by side in the

Is there a way to arrange the following HTML in the specified layout?

<div>
    <div>
        <label>Counterparty</label>
        <input id="paymentsApp-inpt-cpty" ng-model="selectedPaymentCopy.counterparty" ng-required="true"  />
    </div>
    <div>
        <label>Value Date</label>
        <input id="paymentsApp-inpt-date" ng-model="selectedPaymentCopy.valueDate" />
    </div>
    <div>
        <label>Credit Account</label>
        <input id="paymentsApp-inpt-acc" ng-model="selectedPaymentCopy.creditAccount" />
    </div>
    <div>
        <label>Amount</label>
        <input id="paymentsApp-inpt-amt" ng-model="selectedPaymentCopy.amount" />
    </div>
</div>

I am not using bootstrap for this project. Could someone guide me on the CSS needed to achieve this look?

https://i.stack.imgur.com/JspVG.png

Answer №1

Technique #1: Flexbox:

div > div {
    display: flex;
}

div > div > label {
    flex-basis: 125px;
}

div > div > input {
    flex: 1;
}

DEMO


Method #2: Using display: inline-block:

div > div > label {
    display: inline-block;
    width: 125px;
}

div > div > input {
    display: inline-block; /* optional */
}

DEMO


Keep in mind that while flexbox is widely supported, some older browsers like IE 8 & 9 may not fully support it. Certain browsers like Safari 8 and IE10 may require vendor prefixes for flexbox. To easily add the necessary prefixes, you can use a tool like Autoprefixer.

Answer №2

Although flex is a reliable choice and I often use it effectively, there are subtle issues that may arise if you are not fully acquainted with its behavior.

If you prefer a simpler alternative, consider floating the inputs to the right. In this case, place the input before the label and keep the label as is.

Here are the styles (slightly verbose for clarity, with a fixed width set for the container):

.test {
    width: 400px;
}
.test > div > input {
    float: right;
}

Markup (note the switched position of input and label):

<div class="test">
    <div>
        <input id="paymentsApp-inpt-cpty" ng-model="selectedPaymentCopy.counterparty" ng-required="true"  />
        <label>Counterparty</label>
    </div>
    <div>
        <input id="paymentsApp-inpt-date" ng-model="selectedPaymentCopy.valueDate" />
        <label>Value Date</label>
    </div>
    <div>
        <input id="paymentsApp-inpt-acc" ng-model="selectedPaymentCopy.creditAccount" />
        <label>Credit Account</label>
    </div>
    <div>
        <input id="paymentsApp-inpt-amt" ng-model="selectedPaymentCopy.amount" />
        <label>Amount</label>
    </div>
</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

Choose a specific div element from a collection of dynamically generated divs in Angular

I have been working on a code to dynamically generate div elements using the *ngFor directive. Here is what I have so far: <div *ngFor = "let item of Items"> <p>Item : {{item}} </p> </div> The challenge I encountered is that w ...

The vanishing act of custom fonts in Internet Explorer

This issue with a custom font embed is new to me. I have successfully used custom fonts on many client sites without any problems. One specific client has their own custom font files that are causing trouble. The application in question is embedded in an ...

Tips for applying a class to two elements when a checkbox is clicked and avoiding duplicated URL strings

Issue with Checkbox Selection In my project, I have implemented two tabbed containers for grid and list view. Each container contains multiple elements called info boxes with checkbox inputs. Whenever a checkbox is checked, the background color of the res ...

I'm having trouble retrieving a list of elements from a Python class in order to showcase them in an HTML file

I've written a snippet of code in my views.py file where I defined a class called Pizza. from django.shortcuts import render from .models import Pizza def index(request): pizzas = Pizza.objects.all() return render(request, 'menu/index. ...

Form tag definitions are missing on the login page

Here is a snippet of code I am using: <%= form_tag :action => 'process_login'%> Username: <%= text_field "user", "fullname" %>&#x00A; Password: <%= password_field "user", "password" %>&#x00A; <%= submit_t ...

Accessing JS code from HTML is not possible in React

Attempting to create a list using React, I have utilized the module found here: https://github.com/pqx/react-ui-tree I am currently testing out the sample application provided in this link: https://github.com/pqx/react-ui-tree/blob/gh-pages/example/app.js ...

What is the best way to load the contents of an HTML file into a <div> element without it behaving as an object?

Currently, I am importing an HTML file into a <div> in this manner: function load_content() { document.getElementById('content').innerHTML = '<object type="text/html" width="100%" height="100%" data="./content.html"></obj ...

Tips for deleting part of the URL at the end of all hyperlinks on a webpage

I have a large number of links on the same page that I would like to clean up by removing everything from specific characters until the end of the URL. Currently, there are too many links like this scattered throughout my page. The string "$amp;rs" or "&a ...

Issues with the aligning of buttons using the justify-content property

The issue I'm facing involves a parent container with 3 buttons nested inside. The parent container is set to display:inline-flex, however, the justify-content: space-between property is not behaving as expected. Each button has a defined max-width in ...

Disable the display of meridian in the UI Bootstrap datetime picker

Currently, I am utilizing the ui-bootstrap-datetime-picker with angularJS. I am wondering if it is feasible to directly set the show-meridian options of the time-picker inside the timepickerOptions. ... <input type="text" class="form-control" ...

Using JQuery to animate a division towards the left

Currently, I am in the process of developing a website that is meant to incorporate JQuery Animations. My goal is to create an animation effect for the menu items so that each item moves to the right when the mouse hovers over it. Despite my efforts to fi ...

Display or conceal form elements depending on the JSON response

Upon making an api call, a json Response is received with the following structure: {"Field":"Amount","FieldName":"Amount","FieldType":"Numeric","MaximumLength":128,"MinimumLength":0,"Options":"Mandatory"} This api call yields between 5-10 of these object ...

How can I make CSS text-overflow ellipsis trigger a pop-up to display lengthy text when clicked?

I'm encountering an issue with a lengthy sentence in a table cell which I've truncated using text-overflow to display ellipsis. Is there a way to enable users to click on the dots (...) and view the full text in a popup window? table { wi ...

Error: Java Applet cannot be found

As I've come to understand, Java applets are no longer widely used and supported by most browsers. Could it be that my issue is simply due to lack of support? I'm currently testing my HTML file using Internet Explorer on Windows 10 with the follo ...

Sending text from a Tinymce textarea using Laravel 4.2

I am currently facing an issue with a form that includes a tinymce textarea, allowing users to edit text and save it into a database with HTML tags for display on their profile. The problem arises when Laravel 4.2 escapes the HTML tags, making it difficult ...

The alignment of Div elements is off in IE8

I have been struggling to align two divs side by side without any empty spaces on IE 8. Everything looks perfect on Chrome Version 35.0.1916.153 m and Firefox 30.0, but IE 8 seems to be causing some issues. All I want is a main div with two child divs ali ...

Discovering the method to access a file without the standard .html or .php extension on the local host at 127.0.0

Is there a way to access my file without the .html or .php extension? My folder structure looks like this: C:\Apache24\htdocs\search-html\test search-html test low.html high.html ...

Image not being shown by ng-src

I'm struggling to get my picture to display using ng-src. <img ng-src="http://localhost:3000/images/{{image.image}}" class="img-circle" alt="User" style="width: 130px; height: auto;"> Although the data is present when I console log this: ...

What is the best way to adjust the size of a div element so that it is

I am facing an issue with a table that contains a TreeView within a cell. The code snippet is shown below: <style> #leftPanel { width:60px; height:'100%'; border:1px solid red; ...

JavaScript is having trouble locating the HTML select element

Having trouble selecting the HTML select element with JavaScript? You're not alone. Despite trying different solutions found online, like waiting for the window to fully load: window.onload = function(){ var opt = document.getElementsByName("prod ...