Arranging input elements horizontally

this issue is connected to this post: Flex align baseline content to input with label

I am grappling with the layout of my components, specifically trying to get all inputs and buttons aligned in a row with labels above the inputs and hints below. The CSS is scoped within the host and component, making CSS grid not the most ideal solution. I have flexibility to adjust styles inside the component, but changes must be contained within it.

<div style="display: flex; flex-direction: row; align-items: baseline; gap: 8px;">
  <div class="myComponent" style="width: 100px">
    <input id="myInput" placeholder="No label" style="width: 100%; box-sizing: border-box;"/>
  </div>
  <div class="myComponent"  style="width: 100px">
    <label for="myInput" style="display: block">Hint</label>
    <input id="myInput" placeholder="test" style="width: 100%; box-sizing: border-box;"/>
    <small style="display: block">hint</small>
  </div>
  <div class="myComponent"  style="width: 100px">
    <label for="myInput" style="display: block">Long Hint</label>
    <input id="myInput" placeholder="test" style="width: 100%; box-sizing: border-box;"/>
    <small style="display: block">A really long hint that wraps</small>
  </div>
  <div class="myComponent"  style="width: 100px">
    <label for="myInput" style="display: block">No Hint</label>
    <input id="myInput" placeholder="test" style="width: 100%; box-sizing: border-box;"/>
  </div>
  <button style="text-wrap: nowrap;">My Button</button>
</div>

Answer №1

To achieve this, you can utilize the CSS property grid-template-rows: subgrid;. Here is a sample code snippet that demonstrates how to implement it:

.my-layout {
  display: grid;
  grid-template-columns: repeat(5, 100px);
  grid-rows: repeat(3, auto);
  align-items: center;
}

.my-box {
  display: grid;
  grid-row: span 3;
  grid-template-rows: subgrid;
}

.my-box label {
  margin-top: auto;
}

.my-box input {
  width: 100%;
}

.my-box input:first-child,
.my-box button {
  grid-row-start: 2;
}
<div class="my-layout">
  <div class="my-box">
    <input placeholder="No label">
  </div>
  <div class="my-box">
    <label for="input-1">Hint</label>
    <input id="input-1" placeholder="test">
    <small>hint</small>
  </div>
  <div class="my-box">
    <label for="input-2">Long<br>Hint</label>
    <input id="input-2" placeholder="test">
    <small>A really long hint that wraps</small>
  </div>
  <div class="my-box">
    <label for="input-3">No Hint</label>
    <input id="input-3" placeholder="test">
  </div>
  <div class="my-box">
    <button>My Button</button>
  </div>
</div>


Please ensure that attributes like id and for are unique in your HTML code.

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

Discover the ultimate guide to creating an interactive website using solely JavaScript, without relying on any server-side

I'm facing a challenge: I have a desire to create a website that is mainly static but also includes some dynamic components, such as a small news blog. Unfortunately, my web server only supports static files and operates as a public dropbox directory. ...

SASS fails to recognize the variable

I am trying to incorporate borders based on specific @media breakpoints. Here is the code I have written: @media(max-width:767px) { $height: auto; $grid: 1; } @media(min-width: 768px) and (max-width:991px) { $height: 370px; $grid: 2; } @media(min-width: 9 ...

Getting elements in order with bootstrap

Can you help me align the "Format Example" string with the textual labels of the input fields without using static width in AngularJS with Bootstrap? The current alignment is right-justified but I would like it to be more dynamic as shown in the screenshot ...

Is there a way to incorporate a JavaScript variable as the value for CSS width?

I created a scholarship donation progress bar that dynamically adjusts its width based on the amount donated. While I used CSS to handle the basic functionality, I am now trying to implement JavaScript for more advanced features. My goal is to calculate ...

Dividing image styles within css

I am trying to incorporate 4 arrow images into a CSS class (up, down, right, left). I started with a basic arrow css class: .arrow { background-repeat: no-repeat; background-position: center; } Next, I created subclasses like: .arrow .up { ...

What is the best way to show an HTML file in a WebBrowser using C++?

Currently, I'm facing the challenge of displaying an HTML file in a WebBrowser within a Windows application. The issue lies in the fact that I am dealing with a local file instead of a URL. My attempted solution with file://MyWeb.html proved unsuccess ...

Angular 4 is having trouble retrieving the JSON response

When I attempt to log errors in a specific scenario and use error, the following error message is displayed: { "errors" : [ { "entity" : "Movement", "property" : "direction", "invalidValue" : null, "message" : "Il campo non può essere v ...

Creating dynamic div containers on the fly

I'm dealing with an issue in my application where multiple overlapping div boxes are being dynamically generated and they all end up having the same content. When I add content to a box, it gets applied to all existing boxes instead of just the last o ...

Can an Angular 5 web application be developed without using Node.js/npm?

I want to develop an Angular 5 web application using Java, but my boss prefers not to use Node.js/npm. Is it possible to build an app without Node.js/npm and rely solely on Java? Most of the resources I've come across recommend using Node.js/npm, inc ...

HTML: Image not updating despite meta tag refresh

I am using the following HTML5 code to display an HTML page with an image (1.JPG). The setup is working correctly, but I am encountering an issue. The image at the specified path is supposed to be randomly replaced with a newer image within a certain tim ...

Unable to output value in console using eventListener

Hey everyone, I'm new to JavaScript and trying to deepen my understanding of it. Currently, I am working on an 8 ball project where I want to display the prediction in the console. However, I keep getting an 'undefined' message. const predi ...

How to style an HTML table with just a bottom border

Is there a way to recreate the Staff Directory section of this page using only HTML, without having to rely on CSS and Javascript? Specifically, I want to create a table with only bottom borders/dividers for each line. Any suggestions on how to achieve thi ...

most effective method for recycling dynamic content within Jquery mobile

My jQuery mobile app has a requirement to reuse the same content while keeping track of user selections each time it is displayed. I have successfully created the necessary html content and can append it to the page seamlessly. The process goes something ...

Utilizing CSS percentage height when the parent element is constrained by a defined minimum and

It is a common rule that when applying a percentage height to an element, the percentage is calculated based on its parent's height. Consider a scenario where you want a child element to be 40% of its parent's height. The parent element has both ...

Using @extend with numeric classes in Bootstrap 5: A step-by-step guide

I am looking to migrate some classes from Bootstrap 4 to 5 because some of the Bootstrap classes are no longer usable, especially in utilities. I am using SASS so I can utilize @extend for this process. https://i.sstatic.net/dEDCw.png For classes .text-l ...

Structural directives allow for the declaration of variables

In my Angular application, I've implemented a custom structural directive called `IfDataDirective` as follows: @Directive({ selector: '[appIfData]' }) export class IfDataDirective { private hasView = false; @Input() set appIfData( ...

Image two-way binding in Angular is not performing as anticipated

My Angular component is designed to display a base64 image string received from a websocket in the HTML using an image tag. I am polling the websocket every second and attempting to display the updated image on the page. Here is my code: HTML of the compo ...

Getting the value from a label and then setting it as the innerHTML of document.getElementById('label')

I have successfully implemented a JavaScript Google Maps functionality, but now I am facing an issue where I need to retrieve the type of HTML control and set it to JavaScript. Specifically, when attempting to extract the value from lblTitle, it is not f ...

Cross-Origin Resource Sharing (CORS) verification for WebSocket connections

I am currently utilizing expressjs and have implemented cors validation to allow all origins. const options = { origin: ['*'], credentials: true, exposedHeaders: false, preflightContinue: false, optionsSuccessStatus: 204, methods: [&a ...

Having trouble deleting element despite having the correct ID?

Whenever I click on the map, it adds an element to the map div using this line of code: $('#map').append('<label>test:</label><input type="hidden" name="map_coords" id="' + e.latlng.lat + '" value="' + e.latlng ...