Two fields placed horizontally next to each other, with their corresponding error messages displayed directly below in perfect

I have come up with a solution to my issue:

http://codepen.io/helloworld/pen/QpRxNr?editors=1100

I am trying to display 2 inputs, each with an error message, in a table column.

The current design is too elongated for my liking to fit in just ONE table column.

My goal is to have the 2 inputs placed next to each other, with their respective error messages aligned directly below and to the left of them.

Similar to the mockup I created where both fields are empty:

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

Is there a way to achieve this layout?

The solution needs to ensure that both inputs occupy the full width of the table column. I am not keen on using a bootstrap container->row->2-columns approach as it would introduce margins/paddings on the sides of the table column.

I would prefer a flexbox solution that can leverage the bootstrap flex utilities :-)

<table> 
<td [formGroup]="customerForm" style="display:flex">

        <input  class="form-control" placeholder="First name" type="text" formControlName="firstName" />
        <div *ngIf="customerForm.controls.firstName.touched">
            <div class="form-error" *ngIf="customerForm.controls.firstName.hasError('required')">
                First name is required.
            </div>
        </div>

        <input class="form-control" placeholder="Last name" type="text" formControlName="lastName" />
        <div *ngIf="customerForm.controls.lastName.touched">
            <div class="form-error" *ngIf="customerForm.controls.lastName.hasError('required')">
                Last name is required.
            </div>
        </div>
    </td>
</table>

Answer №1

Using only the Bootstrap 4 flexbox utilities, you can achieve this layout without the need for custom CSS.

Method 1: Place the two inputs/errors within their own divs and apply d-flex to the td...

        <td class="d-flex">
            <div class="mr-3">
                <input class="form-control" placeholder="First name" type="text" formcontrolname="firstName">
                <div *ngif="customerForm.controls.firstName.touched">
                    <div class="form-error" *ngif="customerForm.controls.firstName.hasError('required')">
                        first name is required.
                    </div>
                </div>
            </div>
            <div>
                <input class="form-control" placeholder="Last name" type="text" formcontrolname="lastName">
                <div *ngif="customerForm.controls.lastName.touched">
                    <div class="form-error" *ngif="customerForm.controls.lastName.hasError('required')">
                        last name is required.
                    </div>
                </div>
            </div>
        </td>

Method 2: Keep the HTML structure as it is and implement width and flexbox order using utilities...

        <td class="d-flex flex-wrap">
            <input class="form-control w-50 flex-first" placeholder="First name" type="text" formcontrolname="firstName">
            <div *ngif="customerForm.controls.firstName.touched" class="w-50">
                <div class="form-error" *ngif="customerForm.controls.firstName.hasError('required')">
                    first name is required.
                </div>
            </div>
            <input class="form-control w-50 flex-first" placeholder="Last name" type="text" formcontrolname="lastName">
            <div *ngif="customerForm.controls.lastName.touched"  class="w-50 flex-last">
                <div class="form-error" *ngif="customerForm.controls.lastName.hasError('required')">
                    last name is required.
                </div>
            </div>
        </td>

Check out the demo here

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

Excessive padding issues arising from Bootstrap 4 columns and rows

Utilizing bootstrap to structure columns and rows for my images is causing excessive padding around them, deviating from the intended design: https://i.sstatic.net/8cQVg.png Here's the HTML structure I employed: <!-- header --> <header> ...

Enhancing interactivity by incorporating GIFs upon clicking a JavaScript button

A JavaScript button has been created to generate Facts (gifs), with a desire to include a social media share option for platforms like Facebook below the GIF. The code snippet provided includes an array of GIF images and the functionality to display them u ...

Adjust the appearance of a button with Jquery Ajax to reflect a color fetched from an external PHP script

This piece of code represents my HTML: <form class="addtowatchlistform" action="logo/insertwatchlist.php" method="POST"> <input type="hidden" name="tmdb_id" value="'.$result[$x]["tmdb_id"].'"/> <button id="addtowatchlistb ...

Efficiently input text box values into a canvas in real-time using HTML canvas and keypress

There are three text boxes labeled textbox1, textbox2, and textbox3. I am looking to transfer the values entered into these text boxes directly onto a canvas (cnv) whenever a user types in them, and remove the value from the canvas when it is deleted fro ...

Set the webpage to a fixed width

Is there a way to make my webpage fixed-width instead of liquid? I've tried changing the container size to pixels but the text still collapses when I resize the browser. Any suggestions on how to keep it at a fixed width? <!DOCTYPE html PUBLIC "-/ ...

How can I make Div elements disappear based on conditions using If/else Statements in a JSP Page?

I've implemented a JSP function that manages user login by verifying their name and password input. However, even if the user enters incorrect information, they can still view the page. I want to hide all content on the page so that the user cannot se ...

How to customize the image size in a Bootstrap slider

Hey there! I'm new to learning Bootstrap and I recently created a website using Bootstrap 4. I added a slideshow, but the photos don't fill the entire page even though I specified height:100; width:100; in the CSS. I also tried height:auto; and w ...

Concealing the NavBar When Showing a Popup Using React

I am facing a challenge with hiding my bootstrap navbar that has a CSS property of position: sticky-top when I display a pop-up slideshow. I am working with React. I have attempted to target the navbar element in my click handler and adjust the display pro ...

Error message: Selenium is having trouble finding the search bar, regardless of the selector method (such as xpath, css_selector, fullXpath, etc.)

My attempt to automate the search process on Facebook has hit a roadblock. I have tried the following code: search = driver.find_element_by_xpath('//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input') ...

Is there a distinction between the values 'normal' and 'stretch' in the CSS property known as 'align-content'?

When it comes to the CSS declaration 'align-content', the default value is 'normal'. But, there is also another option: 'stretch'. I have been having a hard time figuring out if there is any actual difference between the two. ...

The anchor tag causes the tooltip to display outside of the td element

One of my requirements is to display the click percentage for each link in an HTML template that is dynamically fetched from a database. I attempted to show the click percentage for each anchor link by adding them to the `data-title` attribute using jQuery ...

Discovering the best method for accessing CSS classes locally within an Angular component

In order to style a 3rd-party component with custom styles, I need to access the dynamically generated css classname from within an angular component. Angular applies transformations to local css classnames for scoping purposes. However, when trying to st ...

Unable to locate CSS in Wordpress Theme on localhost

I have been utilizing the Wordpress theme offered by , and it works flawlessly when I upload it to my online servers. However, when I try to use it locally with a Wordpress Bitnami Stack, the theme is unable to locate the CSS. In the header.php file, I hav ...

Elements with inline-block display not aligning horizontally next to each other

I am puzzled as to why the two sections, section.structure and section.specificity, are not aligning properly. It seems that when I reduce the width, they line up correctly at a certain point which I haven't identified yet. My goal is to have the wid ...

Challenges encountered while using colspan in CSS tables

I'm looking to create a table layout where the first column appears twice as wide as the other two columns. However, when I use colspan=2 in the table, it doesn't have the desired effect. Check out the code in action: http://jsfiddle.net/US96B/ ...

The submission of the HTML Form is experiencing difficulties in Internet Explorer 9

Having an issue with my basic HTML form not submitting in IE9, although it works fine in Chrome, FF, and IE10. I've attempted to submit the form in IE9 on Windows 7 and IE10 on Windows 8. In IE10, everything is functional. However, if I switch the b ...

Transferring vast amounts of data between two HTML pages exclusively on the client side

Imagine we have two separate pages: X.html and Y.html. Even though they are not from the same origin (different domain, port, etc.), I have the ability to edit both of them. My goal is to incorporate Y.html into X.html using an iframe. The content of Y.ht ...

Transform every individual word in the paragraphs of the website into a clickable button with corresponding text on it

I've been developing a Google Chrome extension that extracts all the words from paragraphs within a web page and organizes them into buttons. This feature is just one part of a larger project I'm working on. You can check out a demo of this funct ...

Conditional rendering of Materialize navbar links

I am currently working with React Materialize and React Router Dom. My goal is to display navlinks only to authenticated users using conditional rendering. However, when I implement it as shown below, the navlinks are displayed vertically instead of hori ...

Showing the json encoded array received from an ajax response

I have encountered this data result {"policy":[{"id":"1","policy_name":"Policy 1","description":"Testing","status":"Active","valid_until":"2022-05-18& ...