the remaining width content box with input labels

My custom content box styling is causing some issues for me. Inside the content box, there are several label and input fields. The box itself has a set width of `500px.

The challenge is that some of the labels are longer than others, but I want the input fields to always stretch out to fit the window size with a bit of white space around them.

This is the current HTML structure:

<div class="content">
    <div class="row">
        <label for="test1">This is a test #1</label>
        <input type="text" name="test1">
    </div>
    <div class="row">
        <label for="test2">This is a test #2</label>
        <input type="text" name="test2">
    </div>
    <div class="row">
        <label for="test3">But this is a test with a much longer label</label>
        <input type="text" name="test3">
    </div>
</div>

And here's the CSS code:

.content {
    border: 1px solid #000;
    width: 500px;
    height: 500px;
}

.row {
    margin-top: 5px;
}

input {
    width: 385px;
}

You can view the issue in action on this fiddle: https://jsfiddle.net/4ga3533o/. The first two rows display correctly, but in the third row, the input field drops below the label because it exceeds the width of the content box. How can I adjust the setup so that labels can vary in length while the input fields only take up remaining width?

Answer №1

You have the ability to utilize flex properties

.content {
  border: 1px solid #000;
  width: 500px;
  height: 500px;
}
.row {
  margin-top: 5px;
  display: flex;
}
input {
  flex: 1;
}
label {
  margin-right: 10px;
}
<div class="content">
  <div class="row">
    <label for="test1">This is a test #1</label>
    <input type="text" name="test1">
  </div>
  <div class="row">
    <label for="test2">This is a test #2</label>
    <input type="text" name="test2">
  </div>
  <div class="row">
    <label for="test3">But this is a test with a much longer label</label>
    <input type="text" name="test3">
  </div>
</div>

Answer №2

Implementing flexbox is the ideal solution to resolve your problem. Give this code snippet a try.

<div class="content">
  <div class="form-wrap">
    <label for="test1">Test #1</label>
    <input type="text" name="test1">
  </div>
  <div class="form-wrap">
    <label for="test2">Test #2</label>
    <input type="text" name="test2">
  </div>
  <div class="form-wrap">
    <label for="test3">Another test with lengthy label description</label>
    <input type="text" name="test3">
  </div>
</div>

.content {
  border: 1px solid #000;
  width: 500px;
  height: 500px;
}

.row {
  margin-top: 5px;
}

input {
  width: 385px;
}

.form-wrap {
  margin-bottom: 10px;
  display: flex;
  align-items: flex-start;
}

.form-wrap label {
  width: 40%;
}

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

How to create a responsive image that appears over a button when hovering in Bootstrap?

My goal is to display an image over a button when hovering. I've tried adding the .img-responsive class in Bootstrap while including the image in the HTML, but it's not working as expected with my current method of loading images. The buttons the ...

Adjust the element based on hover over the pseudo element

Looking to modify an element based on the hover state of a pseudo-element. Here's what I've tried so far, but it isn't working as expected: nav .logo { display: none; } nav:before{} nav:hover:before .logo { display: block; } I want th ...

Tips for aligning placeholder and text at the center in a React Material UI TextField

Existing Layout: https://i.stack.imgur.com/Zv4Tg.png Desired Layout: https://i.stack.imgur.com/Xuj6O.png The TextField element is displayed as follows: <TextField multiline={false} autoFocus placeholder={props.defaultAmt} ...

Leveraging information from a single controller for another

I am currently developing a website using MEAN stack. I am facing an issue with passing data from one controller to the next. My goal is to transfer a selected option value to the next page. Here is the section with the select box: <div class="plumber ...

Stop the entire page from scrolling in Next JS, while still enabling individual components to scroll

My ultimate goal is to have a fixed, sticky menu bar at the top of my app, followed by a div/component that houses the remaining content and does not scroll itself, while still allowing sub-components the ability to scroll when necessary. I plan to build t ...

How to align an unordered list horizontally in HTML without knowing the number of items

I'm currently developing a web page that needs to display an unknown number of items using the ul/li HTML tag. Here are my requirements: The list should utilize as much horizontal space as possible The list must be horizontally centered, even if lin ...

Using jQuery to dynamically render unordered lists from JSON data

Struggling to create a dynamic unordered list with multiple nested levels? Finding it difficult to render the necessary markup for future functionality? Take a look at the HTML structure I've created on this jsfiddle link: <ul class="nav nav-sideb ...

Exploring ng-view navigation in AngularJS

I have come across an SEO issue while working on a static website in AngularJS. Google Webmasters Tools report no crawl errors, but when I attempt to fetch different routes, it consistently returns the same home page result. It seems to be ignoring what ...

Icon dropdown in Bootstrap

Looking for a solution to modify this dropdown using only bootstrap classes. Instead of the current text "Dropleft" and left arrow icon, how can I replace it with an icon fas fa-ellipsis-h? <button type="button" class="btn btn-secondary ...

Where is the best place to insert Javascript code in an HTML file - the head or body section?

I am currently developing a search engine and have implemented code to automatically redirect users from HTTP to HTTPS when they visit my site. The only question I have is whether I should place this code in the head or body section of my webpage. if(wind ...

Generate a text input field within a dropdown menu

Below is an example of my basic HTML code for a drop-down list: <span id="Div_List"> <label for="Gender">For:</label> <select name="Gender" id="Sex"> <option value="1">1000mtr</option> <option val ...

Modify the default navbar from Bootstrap to display as a fixed navbar

Is there a way to change my default navbar into a fixed navbar when I scroll? I have tried adding data-spy="affix" data-offset-top="200", which works but doesn't look quite right. Should I create a new CSS class or is there an easier solution availab ...

IE10 struggling with loading CSS files

Upon visiting my website in Internet Explorer, I encountered the following error in the console: SEC7113: CSS was ignored due to mime type mismatch It appears that the CSS is not loading properly. After extensive research, I found numerous discussions o ...

What is the best way to choose the initial and final <td> that appears within a <tr>?

I am working with an html table that has the following structure: <table> <tbody> <tr> <input type="hidden" name="a" value="x"> <input type="hidden" name="b" value="y"> <td>First Ce ...

Causes for the display of the text within the alt attribute

Recently, I attempted to view an HTML page in text browsers and noticed that the alt attribute value of the img tag was visible. I decided to omit the alt attribute and instead utilized CSS: .headerImg:after, .headerImg::after { conte ...

Issue with uploading files in Internet Explorer using Ajax not resolved

I've been encountering an issue while uploading images using Ajax and PHP. Surprisingly, everything runs smoothly on Firefox but fails to work on Internet Explorer (I.E). Take a look at my HTML code below: <!doctype html> <head> <titl ...

Is there a way to activate the autoplay feature for this?

I'm really new to Jquery and most of this code isn't mine, I'm just using it as a learning tool for creating sliders. If someone could give me some guidance on how to make this slider work automatically when the page loads, that would be gre ...

The code @media (max-width: 767px) { /*content*/} is malfunctioning

While working on making my webpage responsive, I discovered that the key CSS modification for responsiveness is the @media rule. However, I am facing an issue with one specific part of the code. @media (min-width: 768px) and (max-width: 979px) { .drop ...

Leveraging the Angular (2) routerLinkActive directive to handle dynamic routes

Although my current approach works, I believe there may be a more efficient way to implement this in Angular. The situation is as follows: Imagine nested, inflected paths like /logos and /logo/:id The markup below functions as intended: <li class ...

Using HTML or JavaScript to generate a multitude of identical HTML elements on a webpage

I am currently designing a page that requires the presence of numerous tree illustrations with leaves, spanning across the width at the bottom of the page. I have considered two methods to achieve this. One option is to create a single set of trees and lea ...