Using CSS to leverage the power of both Grid and Flex simultaneously

Help Needed: CSS Challenge! I'm not a fan of CSS and can't seem to crack this code conundrum. Here's what I want the end result to look like: https://i.sstatic.net/z06qO.png

Current Situation:

#newOrderControl {
    border-style: solid;
    border-color: black;
    border-width: 1px;
    width: min-content;
    font-size: 14px;
}

#newOrderInputFields {
    display: grid;
    grid-template-columns: auto auto auto auto;
    column-gap: 40px;
}

#newOrderInputFields .inputField {
    display: flex;
    align-items: center;
    flex-direction: row;
}

#newOrderInputFields label {
    width: auto;
}

/* Updated styling starts here */

#newOrderButtons button {
    float: right;
    border-style: solid;
    border-color: black;
    border-width: 2px;
    color: white;
    height: 30px;
    width: auto;
    padding: 0px 15px;
    text-align: center;
    text-decoration: none;
    font-size: 16px;
    border-radius: 10px;
    margin: 0px 5px;
}

.btnAll {
    background-color: #6DA0ED;
    border-style: solid;
    border-color: black;
    border-width: 1px;
    color: white;
    border-radius: 5px;
}

#btnTakeProfit {
    background-color: #33AB37;
}

#btnPlaceOrder {
    background-color: #6DA0ED;
}

.title {
    display: block;
    text-align: center;
    color: #1D0F73;
    font-size: 22px;
    font-weight: bold;
    margin: 5px;
}
<div id="newOrderControl">
    <label class="title">New order</label>
    <div id="newOrderInputFields">
        <div class="inputField">
            <label>Action:</label>
            <select id="txtOrderAction">
                <option value="">-</option>
                <option value="Buy">Buy</option>
                <option value="Sell">Sell</option>
            </select>
        </div>
        
        /* more input field elements... */
        
        <div id="newOrderButtons">
            <button id="btnTakeProfit">Take profit</button>
            <button id="btnPlaceOrder">Place order</button>
        </div>
    </div>
</div>

Existing Challenges:

  • The All button is not positioned correctly next to the input field
  • The two buttons are placed outside the newOrderControl div
  • The labels are breaking onto new lines
  • Need all inputs and labels to align in a grid-like format

Tried switching to a grid layout, but struggled with positioning input and button elements side by side. When wrapped in a div, they vanished completely.

Any suggestions on how to achieve the desired output?

Answer №1

(1) If we remove the enclosing <div> in the code below, the input and buttons will automatically align using the power of flexbox.


    <input type="text" id="txtOrderQuantity">
    <button class="btnAll" onclick="setAllQuantity()">All</button>

(2) Remember this rule: Avoid using float for layout purposes.

I have incorporated:

#newOrderControl {
  display: flex;
  flex-direction: column;

This change allows the buttons to be aligned on the right with:

#newOrderInputFields label {
  width: auto;
}

(3) To prevent wrapping of the <label>, we applied flex-shrink: 0;. The container's width was modified to width: max-content; while reducing the gap to accommodate it.


(4) By adding justify-content: flex-end;, the elements are now aligned properly:

#newOrderInputFields .inputField {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;

#newOrderControl {
  display: flex;
  align-items: center;
  flex-direction: column;
  border-style: solid;
  border-color: black;
  border-width: 1px;
  width: max-content;
  font-size: 14px;
}

#newOrderInputFields {
  display: grid;
  grid-template-columns: auto auto auto auto;
  column-gap: 5px;
}

#newOrderInputFields .inputField {
  display: flex;
  flex-direction: row;
  justify-content: flex-end;
  align-items: center;
}

#newOrderInputFields label {
  width: auto;
  flex-shrink: 0;
}

#newOrderButtons {
  align-self: flex-end;
}

#newOrderButtons button {
  float: right;
  border-style: solid;
  border-color: black;
  border-width: 2px;
  color: white;
  height: 30px;
  width: auto;
  padding: 0px 15px;
  text-align: center;
  text-decoration: none;
  font-size: 16px;
  border-radius: 10px;
  margin: 0px 5px;
}

.btnAll {
  background-color: #6DA0ED;
  border-style: solid;
  border-color: black;
  border-width: 1px;
  color: white;
  border-radius: 5px;
}

#btnTakeProfit {
  background-color: #33AB37;
}

#btnPlaceOrder {
  background-color: #6DA0ED;
}

.title {
  display: block;
  text-align: center;
  color: #1D0F73;
  font-size: 22px;
  font-weight: bold;
  margin: 5px;
}
<div id="newOrderControl">
  <label class="title">New order</label>
  <div id="newOrderInputFields">
    <div class="inputField">
      <label>Action:</label>
      <select id="txtOrderAction">
        <option value="">-</option>
        <option value="Buy">Buy</option>
        <option value="Sell">Sell</option>
      </select>
    </div>
    <div class="inputField">
      <label>Quantity:</label>
      <input type="text" id="txtOrderQuantity">
      <button class="btnAll" onclick="setAllQuantity()">All</button>
    </div>
    <div class="inputField">
      <label>Target price:</label>
      <input type="text" id="txtOrderTargetPrice">
    </div>
    <div class="inputField">
      <label>Target perc:</label>
      <input type="text" id="txtOrderTargetPerc">
    </div>
    <div class="inputField">
      <label>Coin:</label>
      <select id="txtOrderCoin">
        <option value="">-</option>
      </select>
    </div>
    <div class="inputField">
      <label>Amount:</label>
      <input type="text" id="txtOrderAmount">
      <button class="btnAll" onclick="setAllAmount()">All</button>
    </div>
    <div class="inputField">
      <label>Limit price:</label>
      <input type="text" id="txtOrderLimitPrice">
    </div>
    <div class="inputField">
      <label>Limit perc:</label>
      <input type="text" id="txtOrderLimitPerc">
    </div>
  </div>
  <div id="newOrderButtons">
    <button id="btnTakeProfit">Take profit</button>
    <button id="btnPlaceOrder">Place order</button>
  </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

Design HTML dropdown menu

I have a sleek menu design with rounded buttons, and I am looking to extend the same style to the dropdown list. I have experimented with various approaches but there are two specific issues that I need assistance with: 1. Achieving rounded corners simil ...

Background Patterns on Webpages

My website has a lovely gradient background on the html tag in css, while the body tag showcases a seamless pattern repeating on both the x and y axes. Everything was looking great until I checked the website on an iPad/iPhone in portrait mode, where the ...

Ensure the browser back button navigates to the login page seamlessly, without displaying any error

A scenario I am working on involves a Login jsp that accepts a user email and sends it to a servlet. If the provided email is not found in the database, the servlet redirects back to Login.jsp with an attribute "error". Within the header of Login.jsp, ther ...

What is the best way to transfer a user-generated PNG file to my backend server?

I'm in the process of developing a website that enables users to generate personalized graphics and easily download them directly from the platform. My approach involves allowing users to customize an svg using a javascript-powered form, which is the ...

Triangle-shaped image mapping and interactive hover effects

I am attempting to create a simple hover effect using two triangles. One triangle should start below the other, and upon hovering, it should appear above the first triangle. The problem I am encountering is that the surrounding boxes are obstructing the e ...

Guide to transforming a random hash into a visually appealing HTML table

After exploring HTML::QuickTable, I've noticed that it seems to only support a one-level-deep hash. I couldn't find a way within this module to specify the colspan and rowspan for certain headers when dealing with a multi-level hash structure. Is ...

Extract the chosen document from an input within the Electron application form

Need help with this form <form> <input type="file" name="idp" id="idp" onchange="uploadFiles();"/> </form> Once a user selects an image, I want to move it to a specific folder and save its full name in a variable for database storage. ...

Learn how to incorporate a 'Select All' feature as the primary choice in DevExtreme SelectBox with Angular 15 through Html coding

We are looking to replicate the following basic HTML select in Angular 15 using a dropdown control from DevExpress known as dx-select-box <select ng-model="selectedVal" ng-change="selectedValueChanged()"> <option value=&q ...

Issue with the div elements not aligning next to each other

I am facing an issue with my code in blade.php where I need to display images. The ideal layout would be to have the images stacked side by side within a div element, but unfortunately, it's not working as expected. I have tried using flex, bootstrap ...

Selected jQuery plugin is not updating HTML select option

I've been attempting to update the select tag in my HTML file to match the style used in the example on the Chosen v1.8.7 website, but despite following the instructions, the select element remains unchanged: test.html <head> <link rel=& ...

Tips for adjusting column sizes in react-mui's DataGrid based on screen size

I would like the title column to occupy 3/4 of the full row width and the amount column to take up 1/4 of the full row width on all screens sizes (md, sx...). Here is the updated code snippet: import React from 'react' const MyComponent = () =&g ...

Using setTimeout to click and hold on an element in Javascript

I am in need of adding a feature to my web app where a specific action is triggered when the user clicks and holds on an element, similar to the long press on Android. Here is the HTML code for my div: <div id="myDiv" onmousedown="press()" onmouse ...

I am looking to have three rows of input text area comments instead of just one

Utilizing Bootstrap 4.3.1 with React Components I currently have this image: https://i.sstatic.net/rudsE.png And I am aiming for this design: https://i.sstatic.net/VmyTW.png comments: { elementLabel: "Comments", elementType: 'textar ...

How can one choose an HTML element and then select another nested element using XPath?

Is there a way to target the div element "fc-event-time", and then select the next nested div element that is located at the same level? After identifying the text 1p - 2p within this snippet of HTML, how can I access the users' names? Here's t ...

What is the best way to call a JavaScript function with multiple arguments from a Silverlight project?

I encountered an issue when trying to invoke a JavaScript function with multiple arguments from an HTML page. Here is what I am attempting to do: wbNavigator.Navigate(new Uri("http://localhost:56433/route.html", UriKind.Absolute)); object results = wbNavi ...

Problems arising from positioning elements with CSS and organizing list items

I've been working on creating a navigation sidebar using Angular and Bootstrap, but I'm having trouble getting my navigation items to display correctly. APP Component HTML <div class="container-fluid"> <div class="row" id="header"&g ...

How to style HTML li elements to wrap like sentences within a paragraph

I am looking to format text list elements in a way that they wrap like sentences within a paragraph. Here is the accompanying HTML & CSS code snippet that demonstrates how li element behaves when it exceeds the width of the ul container. ul { ...

I am in search of a container that has full height, along with unique footer and content characteristics

I have set up a jsfiddle to demonstrate the scenario I am facing: There is a basic header, content, footer layout. Within the content-container is a messenger that should expand to a maximum of 100% height. Beyond 100% height, it should become scrollable. ...

Place a div in a location where it seems to be positioned beyond the main wrapper's width of 980 pixels

Is there a way to position a div outside the main wrapper (980px) of my website without causing horizontal scrollbars in the 1024px screen size? I'm open to solutions using CSS or jQuery. Any suggestions? The segment that needs to be outside should ...

Error: The property 'classList' of null is unreachable for HTMLImageElement

I'm facing an issue on my website that's causing the following error message to pop up: Uncaught TypeError: Cannot read property 'classList' of null at HTMLImageElement. Here's the code I've been using: https://jsfiddle. ...