What is the best way to align form elements in the same row using React Bootstrap?

Do you notice how the titles, text boxes, and buttons are all misaligned and messy-looking? How can I make them line up nicely together? I especially want the buttons to anchor themselves at the bottom of this component.

I've already tried a few solutions. I came across a discussion about Cards and Card Decks for aligning button elements within a Card, but none of the styling techniques they suggested seemed to work in my case.

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

Here is the code snippet for the component that contains all these elements:

import { Button, Row, Col, Container, Form } from "react-bootstrap"
import styles from "../styles/ParameterList.module.scss"
import {useState} from 'react'

const ParameterList = ({onRandomList, onNewList, onClear, onDupesCheck, dupesChecked}) => {

    const [newListState, setNewListState] = useState('')
    const [minNumberState, setMinNumberState] = useState('')
    const [maxNumberState, setMaxNumberState] = useState('')

    return (
        <div className={styles.container} id="paramsDiv">
            <Container fluid >
                <Row>
                    <Col align="center">
                        <Form>
                            <Form.Label>Generate an entirely new list of instruments</Form.Label>
                            <Form.Control className={styles.formControlOverride} type="number" placeholder="Minimum" min="0" value={minNumberState} onChange={(e) => setMinNumberState(e.target.value)}/>
                            <Form.Control className={styles.formControlOverride} type="number" placeholder="Maximum" min="0" value={maxNumberState} onChange={(e) => setMaxNumberState(e.target.value)}/>
                            <Button className='align-self-end' type="button" onClick={() => {onRandomList(minNumberState, maxNumberState); setMaxNumberState(''); setMinNumberState('')}}>Generate Random List</Button>
                        </Form>
                    </Col>
                    <Col align="center">
                        <Form>
                            <Form.Label>Add a random group of new instruments</Form.Label>
                            <Form.Control className={styles.formControlOverride} type="number" placeholder="Amount of Instruments" min="0" value={newListState} onChange={(e) => setNewListState(e.target.value)}/>
                            <Button className='align-self-end' type="button" onClick={() => {onNewList(newListState); setNewListState('')}}>Generate Random Instrument(s)</Button>
                        </Form>
                    </Col>

                    <Col align="center">
                        <Form>
                            <Form.Label>Choose an instrument to add to the list</Form.Label>
                            <Button>Select Instrument</Button>
                        </Form>
                    </Col>
                </Row>
            </Container>
        </div>
    )
}


export default ParameterList

And here is the corresponding SCSS code:

.container {
    background-color: #fff;
    padding: 1.5rem;
    margin: 1rem 10rem;
    align-items: center;
    border-radius: .5rem;
}

.formControlOverride {
    margin: .5rem
}

.formCheckOverride {
    margin-bottom: .5rem;
}

Thank you in advance for any assistance!

Answer №1

When dealing with this issue, I have experimented with several different strategies:

  • Placing the labels in a separate row so that the inputs line up on the next row. However, this may cause issues when viewing on mobile devices.
  • Applying a style class to set a min-height for the labels based on the specific situation. Fixed heights are not always ideal and can result in wasted space.
  • Setting overflow to ellipsis for labels to prevent wrapping, and adding titles or hover tooltips with the full text. The effectiveness of this approach depends on the end user's scenario.
  • Adjusting the width to give more space to each label/input pair. This is often my go-to solution for this problem.
  • Reflecting on the wording of your labels. Is using "entirely" redundant when "new" suffices? According to research, it is. Refer to this resource for tips on effective writing.

Answer №2

To create a flexible form layout with equal-height columns, you can use the flexbox properties. If the provided code snippet is not effective, consider examining the columns and applying the 'stretch' option to the row element.

form {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-between;
    height: 100%;
}

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

React Material-UI Multiple Select with Checkboxes not displaying initial selections as checked

I am fairly new to working with React and MUI, so please bear with me. Currently, I am in the process of learning how to create Multiple Select Options with checkboxes by populating the Dropdown Options from an Array. Although I have set up the initial/de ...

Is there a way to make my HTML file search beyond its current directory?

My HTML file seems to be having trouble searching in a different folder for the CSS file that I've linked. How can I make it search in the correct folder? I have my HTML file in a directory named INDEX and the CSS file in another directory named STYL ...

What is the best approach to designing a dynamic class toggler for a React sidebar?

I am trying to add a specific class to a clicked link within a sidebar. While this works for one link, when I attach the handler to another link and click it, both links get assigned with the new class. Is there a way to make this functionality more like ...

Tips on accessing session data with next-auth once loading is complete

When I use useSession to fetch data and store it in my state, the issue is that it returns a null object because the data is still loading. Is there a way to prevent this and only get the data once it's not in a loading state while blocking the page u ...

When using font size in rem units, it will remain consistent across different screen sizes and not be subject to scaling

When setting padding, margin, and font size using rem units, I noticed that on larger screens the output looks good. However, on smaller screen devices, the sizes do not reduce proportionally - instead, the measurements from the larger screen persist. Wh ...

Instructions for utilizing a variable or prop in a ternary operator

This element displays beautifully: const Footer = () => { let dataDiv = <div>Data is: Some data</div>; return ( <div> {data ? dataDiv : ''} </div> ) } However, when I replace the text "some data" w ...

What is the proper way to conceal overflow content effectively?

$('.child_1').click(function(){ $(this).toggleClass('left').siblings().toggleClass('left'); }); * {box-sizing: border-box;} .parent { width: 500px; position: relative; border: 1px solid #ccc; overflow: hidden ...

Component attachment for SharePoint Framework solution (Web part)

I'm seeking guidance on developing an attachment component for my SPFx solution (webpart) and I'm currently stuck. So far, I've successfully created a TextField component as shown below: import * as React from 'react'; import ...

Is there a way to retrieve the PDF file created using the html-pdf-node library in conjunction with Node.js, React JS, and

Currently, I am receiving a response from the backend. My goal is to enable users to download the generated PDF from the frontend by simply clicking on the Export button. Here's my Export button click handler code: const handleExportProjectData = () = ...

A guide to resizing a canvas correctly in React with the help of p5.js

Currently, I am working on a project that involves resizing the model in the canvas when the canvas or window is resized. I have consulted the documentation for resizeCanvas() and implemented it. To achieve this, I first determine the ratio by dividing th ...

Content for Bootstrap carousel items can be divided into two separate divs arranged horizontally

I'm currently working with a bootstrap carousel that displays images and text on each carousel item. However, I want the content of each carousel item to be displayed horizontally, rather than vertically as shown in the image below. Here is the curre ...

What is the best way to design a form with two dropdown menus, where the options in the second dropdown menu are determined by the selection made in the first dropdown menu

Is it possible to create two select menus using one dictionary in Django, where the values in the second menu are dependent on the key selected in the first menu? In my Django views.py file, I've constructed a dictionary with Projects as keys and cor ...

Guide to starting, running, and debugging a react/redux application using Intellij

As I work on my react/redux app, I can easily run it using npm start from the command line and debug it in the browser. However, I now want to transition to developing it in Intellij. The advice snippets in the link provided don't offer enough context ...

Having trouble uploading images and css files to a JSP page in a Spring MVC project

The directory structure appears as follows - projectroot -- src --> main-->java-->com-->controllers etc -- src --> resources--> myresources --> css-->.css -- src --> resources--> myresources --> ima ...

The Bootstrap responsive form is experiencing issues with properly displaying columns

I have implemented a simple bootstrap form with rows and columns in groups of 4. My goal was to create 3 total columns using this layout. Here is the code snippet: <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" c ...

What are your strategies for designing a website specifically for mobile users?

Today, most modern smartphones come with 720p or 1080p resolution screens. This means that even on smaller screens, like a 4-inch display on a Galaxy s3, we can still view all the text and GUI elements (such as email textboxes) on a website when we first o ...

CSS: float issues occurring at unpredictable intervals

In the creation of my website, I have incorporated small login and logout buttons that are positioned in the top right corner. Within a "header_content" division, there is another div with a style of "float: right" to keep it aligned in the top right corne ...

Encountering a 404 error in .Net Core 2.1 React and Signalr 1.0.4 while trying to negotiate connectivity

Currently, I am delving into React and SignalR while working with .Net Core 2.1 and the Microsoft.AspNetCore.App package to access the latest SignalR features. Despite also installing @aspnet/signalr, I keep encountering a persistent 404 error as it cont ...

Is there a CSS workaround for the "not" statement? Perhaps an alternative method like superfish?

I've searched online for a solution but haven't found one yet. I'm currently using the superfish dropdown menu and I want to round the top li items, excluding those with ul's inside. You can see a demo on this test page: Check out the ...

What is the best way to separate two <a> tags using only Bootstrap?

Currently, I am utilizing a PHP for loop to display 10 tags on an HTML page. My challenge lies in wanting the <a> tags to be displayed on separate lines with some spacing. It's common practice to handle this through custom CSS, however, is ther ...