Expanding div contents not adapting to full width due to lack of flex grow property

My goal is to make the input elements fill the entire width they are allotted, but for some reason, they remain at their default size when initially rendered.

Check out the CodeSandbox

I found a workaround by adding the following:

. . .
<input style={{ width: '100%' }}
. . .

However, I thought using flex grow would achieve this automatically.

I also tried using flexBasis: '100%' instead of flexGrow: 1, but it didn't make a difference.

I'm not sure what I'm doing wrong here. Do I really have to manually set the width of the input element to get it to change?

Expected output: https://i.sstatic.net/4rz0m.png

Answer №1

To ensure that input elements occupy maximum space within the flex container, there are a couple of methods you can employ:

  1. Set a specific width for the input elements. When you define flex-basis or flex-grow, you are actually specifying those properties on the container of the input element, not directly on the input itself. This causes the container to expand in size, rather than the input.

const Input = ({ label }) => (
  <div className="inputContainer">
    <div>{label}</div>
    <input />
  </div>
);

function App() {
  return (
    <div className="container">
      <Input label="First Input" />
      <Input label="Second Input" />
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));
.container { display: flex; }
.inputContainer { flex-grow: 1; }

.inputContainer input {
  width: 100%;
  box-sizing: border-box;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
<div id="root"></div>

  1. Another approach to achieve the desired layout is by making the input container a flex container and then applying flex-grow or flex-basis to the div wrapping both the label and the input element.

    const Input = ({ label }) => (
      <div className="inputContainer">
        <div>
          <label>{label}</label>
          <input />
        </div>
      </div>
    );
    
    function App() {
      return (
        <div className="container">
          <Input label="First Input" />
          <Input label="Second Input" />
        </div>
      );
    }
    
    ReactDOM.render(<App />, document.getElementById("root"));
    .container,
    .inputContainer, 
    .inputContainer div {
      display: flex;
    }
    
    .inputContainer {
      flex-grow: 1;
    }
    
    .inputContainer div {
      flex-direction: column;
      flex-grow: 1;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script>
    <div id="root"></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

Updating the Navigation Bar and Theme in CRM Dynamics 2013 to Reflect Your Organization's Branding

In my CRM Dynamics 2013 setup, I am faced with a unique challenge. I need to customize the Organization navigation bar based on which organization is currently loaded. Specifically, I have two organizations - QA and PROD. When a user switches to the QA org ...

Problem with Bootstrap 4 layout in Firefox - works fine, but not displaying correctly in

Hey everyone, I've encountered an issue with my code. It's working perfectly fine in Firefox, but in Chrome and Opera, the layout is all messed up. Can anyone help me pinpoint what might be going wrong here? <div class="row broadband-block"&g ...

How can I combine an onkeypress and onclick event listener in one method?

I have two inquiries, somewhat intertwined. 1) Is it feasible to merge the 2 functions below into a more efficient function, or do I need to create a function and then call it in both event listeners? input.addEventListener("keyup", () => { if (eve ...

Having difficulty generating bcrypt password hashes in Node.js

I am facing an issue with the following code. I am attempting to hash my admin password when they register. The password is initially set to a default value via the mongoose schema. However, the hashing process seems to not be working correctly. Below ar ...

What could be causing "Unknown property" errors when using unicode property escapes?

The MDN website provides examples of matching patterns with unicode support, such as: const sentence = 'A ticket to 大阪 costs ¥2000 ...

Encountering a React error when attempting to generate a URL for an image in Sanity

Server Error Error: The asset reference 'e173af30-fd2d-42ed-a364-d92a2cddf32c' is malformed. It should be in the format of an id such as "image-Tb9Ew8CXIwaY6R1kjMvI0uRR-2000x3000-jpg".https://i.stack.imgur.com/koC26.png https://i.stack.imgur.com/ ...

Creating an array inside a Vue function

Currently, I am facing a challenge with restructuring an array in Vue.js. I am able to log the values within my method and push them into an array, but I'm struggling to format the array as required. The desired structure of the array should be like ...

Tips for showcasing information stored in a JSON file: Input a value into the field provided, then utilize AngularJS to retrieve and display the corresponding data based on that specific value

https://plnkr.co/edit/iZqalOkrpSnjIzwvGchO?p=preview Is there a way to retrieve the data associated with each serial number entered in the search box? What elements might be missing from the current code? { "SerialNumbers": { "451651": [{ ...

Resolving issues with JavaScript caused by Polymer updates

I am a novice when it comes to working with Polymer. From what I have gathered, there seems to be compatibility issues with Mozilla and Safari. After researching on StackOverflow, I found that adding addEventListener('WebComponentsReady', funct ...

Footer that sticks in a vuejs application

I am facing a challenge in implementing a sticky footer in my vuejs application. Vuejs and similar frameworks require the presence of a root element in the template. This requirement is causing complications when trying to use Flex to create a sticky foo ...

Storing values from a content script into textboxes using a button press: a simple guide

I am new to creating chrome extensions and currently utilizing a content script to fetch values. However, I am facing difficulty in loading these values into the popup.html. Here is the code snippet: popup.html <head> <script src ...

Filtering an array of map/key pairs can be done by using various methods to

I am searching for individuals above the age of 21. const people = [ {0: {name: 'john', age: 30}}, {1: {name: 'jay', age: 33}}, {2: {name: 'cris', age: 18}} ]; The code snippet provided below did not produce an ...

Is there a way to enable scrolling on the page upon loading, without moving the embedded PDF object itself?

After embedding a PDF object on my webpage, I noticed that when loading the page and scrolling using the mouse wheel, it actually scrolls up and down inside the PDF instead of moving through the entire page. To fix this issue, I have to first click in a bl ...

Limit the user to entering a maximum of (40) characters in the textarea

To enforce a character limit of 40 characters in a text area, I have implemented the following JavaScript function to trigger on the textarea's onKeyUp event: However, a problem arises when pasting content exceeding 40 characters into the textarea - ...

Is it possible to use Ajax post with localhost on Wamp server?

Looking to execute a basic POST function using Ajax on my localhost WAMP server. Here's the code I have: function fill_table() { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari ...

When trying to send a Vue to Laravel axios put/patch request, a 405 error occurred

I'm facing an issue with getting the update method to work using put/patch. Interestingly, I didn't have any problems when working with react, so it could be something specific to vue causing the trouble or maybe I'm overlooking a simple det ...

Unable to navigate using React Router DOM in useEffect hook for React versions 16 and 17

Recently, I encountered an issue in my project where using navigate('/a-path') with the useNavigate() hook in react-router-dom was not functioning as expected. Even though the navigate function did not throw any errors, it simply failed to naviga ...

Different ways to adjust the position of the popup dialog in Vuetify

<v-dialog v-model="dialog" max-width="250" class="pa-6" > <v-card class="scroll" min-height="300" max-height="700" color="rgb(255, 255, 255, 0.7)"> ...

Javascript API for Selenium WebDriverJS

Having difficulty grasping WebDriverJS and promises at the same time, especially since most of the available sample code is for Python or Java, not JavaScript. My main goal is to retrieve the full HTML content of a page. If you look at the WebDriverJS code ...

Trigger an alert using the Ajax response

Is there a way to display the value of imageX outside of the $.ajax() function? var imageX; $.ajax({ type: 'GET', url: 'php/my1.php', dataType: 'json', async: 'false', success: function(response) ...