Ensure that the width of the container holding two "text" input boxes is appropriately set when utilizing the display flex property

MRE:

.outer-container {
  width: 80%;
}

.form-container {
  display: flex;
}

.form-container input[type=text] {
  width: 50%;
  height: 40px;
}

.form-container label {
  margin-right: 10px;
}
<p>Type in your text (let an AI finish it):</p>
<div class="outer-container">
  <div class="form-container">
    <div>
      <label for="text-input-1">Recipient<br></label>
      <input type="text" id="text-input-1" name="text-input-1">
    </div>
    <div>
      <label for="text-input-2">Donor<br></label>
      <input type="text" id="text-input-2" name="text-input-2">
    </div>
  </div>
</div>
<div>
  <input type="submit" value="Submit">
</div>

CSS (I included outer-container to create a container that should occupy 80% of the page width, however, it seems not to have any effect - whether I use it or not):

I utilized display: flex to align the two boxes horizontally, rather than stacking vertically. Despite setting width: 80%, it appears that more than half of the page's width remains unutilized: https://i.sstatic.net/qqy9H.png

Answer №1

By default, Flex will shrink to the width of the content unless you specify the content width in absolute units. If you want the input to be 50% of the parent div's width, keep in mind that the parent div, as a flex item, will also adjust its width based on the content. To make the flex item equal width of the parent div, you can use flex:1. For more information, refer to this article on css tricks.

It's worth noting that your .outer-container is set to 80% of the full screen width, so even with this example, the input boxes won't span the entire viewport width. Some additional lines have been added to demonstrate this.

body {
  border: 2px solid purple;
}

.outer-container {
  width: 80%;
  border: 2px solid red;
}

.form-container {
  display: flex;
  border: 2px solid blue;
}

.form-container input[type=text] {
  width: 50%;
  height: 40px;
}

.form-container label {
  margin-right: 10px;
}

/* added this */
.form-container > div {
  flex:1;
  border: 2px dotted orange;
}
<p>Enter text (an AI will complete it):</p>
<div class="outer-container">
  <div class="form-container">
    <div>
      <label for="text-input-1">Recipient<br></label>
      <input type="text" id="text-input-1" name="text-input-1">
    </div>
    <div>
      <label for="text-input-2">Donor<br></label>
      <input type="text" id="text-input-2" name="text-input-2">
    </div>
  </div>
</div>
<div>
  <input type="submit" value="Submit">
</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

What steps can be taken to restrict access to the next page for users who are not logged in?

In my main JavaScript file, I am trying to implement a condition where users must be logged in to access the welcome page. If a user is not logged in, they should be redirected to the login page by default. However, in my current code, the form is submitti ...

Managing primary and secondary color schemes within Material Design Components for Web - a complete guide

Exploration I delved into the realm of Material Components (MDC) for Web and found myself utilizing CDN for hosted CSS and JavaScript libraries: https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css https://unpkg.com/mate ...

Scaling Flexbox divs based on images dimensions

Check out this cool example page When resizing the window in Firefox, Internet Explorer 10, and Opera, the images remain at their current sizes without any scaling. However, Chrome shrinks everything down proportionally as desired. It's unclear whet ...

Sending target information as a property argument

I have encountered an issue with my app's two Bootstrap modals. It seems that many people are facing problems with opening the second modal. Is it possible to pass data-target and modal id properties as props in this manner: data-target={props.da ...

What is the conventional method for organizing forms in ASP.Net?

Here's my forms layout: <body> <p>Please note that this form is solely for illustrating the Required Field Validator and does not perform any actual function.</p> <form id="frmValidator" method="post" runat=" ...

Experiencing difficulties replicating two auto-scrolling divs

I have implemented a script to automatically slide two different divs. Here is the code I am using: The HTML: <div id="gallery"> <div id="slider" style="width: 6000px; left: -500px;"> <div><aside class="widget widget_testimoni ...

Web page containing information from IPv6 and IPv4 sources exclusively

Is it possible to have an HTML5 page accessible through both IPv4 and IPv6, while only allowing CSS style and JavaScript from other domains via IPv4? Unfortunately, it seems that this setup does not function properly for pure IPv6 connections. Will th ...

Incorporate your weekly itinerary from Google Sheets directly onto your Webnode website

I have a weekly schedule arranged in a Google Sheet that I want to incorporate into my Webnode website. Unfortunately, using the embed HTML option resulted in a poor display when accessed on mobile phones. Additionally, exporting it to Google Calendar an ...

Ways to retrieve form data following a post request using JavaScript

Does anyone know how to read form data from testPage.html in testPageResult.html? I have created a html page called testPage.html with a form that has a post action like the following: <h2>HTML Forms</h2> <form action="http://test.com/tes ...

Using embedded js files in jQuery's .load() function does not function properly

In the index.html file, I have the code snippet below: <div id="page-insert"></div> function openPage(pageid) { $('#page-insert').load(pageid); } openPage('testpage.html'); Additionally, there are script files embedde ...

Bootstrap 4's compacted navigation bar

Is it possible to create a collapsible navbar in my code? I am facing an issue where the navbar does not collapse when viewed on mobile devices. I tried using a button to toggle the visibility but it did not work as expected – the navbar either disappear ...

Omit the element from the event listener

Is there a way to prevent the image from triggering a click event in this code snippet? $('#templtable .trhover *:not(#infoimg)').live('click', function(event) { event.stopPropagation(); $(this).toggleClass('selected&a ...

A guide on automating the html5 color picker using input type="color" in selenium webdriver

When interacting with an HTML color input element, a color picker window pops up that prevents viewing the DOM. I am looking for a solution to either select a color and close the window or enter a hex code in the popup's text box. Can anyone provide m ...

Overlap of Navigation and Logo Divs on Mobile Website

Trying to optimize my website for mobile at coveartschildcare.com, but the header divs keep overlapping and nothing I do seems to fix it. Here's the CSS code I'm currently using: @media only screen and (min-device-width: 320px) and (max-devi ...

Unexpected results can occur when using ngClass and CSS with all unset

Within my Angular 4 project, I am utilizing ngClass on an object that contains a CSS class applied with unset: all within it. Despite knowing that ngClass adds its properties, the expected result was for all values to be unset and the style elements from n ...

How can I make every tuple point to a unique link?

I am currently working on setting up different URLs for each tuple to display different department semesters when clicked within a table. After attempting to add an href tag before the echo in this code, I found that all tuples in the table were leading t ...

Validating Forms using Javascript

I'm struggling to understand why I'm not getting any error or success messages when I click the submit button. My goal is to validate strings in an HTML form to ensure they are not null or too long. Both the developer's tools and the Online ...

A guide to adjusting the width without affecting the td element

Currently, I am facing a challenge while coding in HTML. I am trying to create a table with a header (time range) that fits on a single line without affecting the width of the td elements. Below is the code snippet: <table style="border:1px solid #8c ...

Issues with div and hyperlinks/forms

I am currently working on creating a left sidebar in my HTML code, but I am still new to this. In the div for the sidebar, I have three buttons. However, Weebly keeps notifying me of an issue with the <a> tag, and the second button's link appear ...

Stopping videos in the JQuery Cycle Plugin triggers the stop event

Currently, I have a simple cycle set up with YouTube videos using the following code: <script type="text/javascript"> $(document).ready(function () { $('#cycle').cycle({ fx: 'fade', sp ...