What techniques can be applied to keep a flexbox from expanding in width when it includes a text input field?

After implementing the following CSS/HTML code, I achieved my desired outcome - three boxes displayed in a row. The middle box has a fixed width while the outer boxes expand equally on either side:

#container {
  display: flex;
}
#container > * {
  background: #efefef;
}
#container .fixed-width-40px {
  width: 40px;
  background: #ccc;
}
.fill-available-horizontal-space {
  flex-grow: 1;
}
<div id="container">
    <div class="fill-available-horizontal-space">aaa</div>
    <div class="fixed-width-40px">bbb</div>
    <div class="fill-available-horizontal-space">ccc</div>
</div>

However, when I added a form (including a text input field) to the third box, the width of the third box increased and the first box decreased.

I am looking for a solution where both the first and third boxes maintain equal widths, despite adding the new content.

Is there a way to fix this issue?

<div id="container">
    <div class="fill-available-horizontal-space">aaa</div>
    <div class="fixed-width-40px">bbb</div>
    <div class="fill-available-horizontal-space">
        <form>
            <input type="text" name="test"/>
        </form>
    </div>
</div>

Answer №1

If you want the child elements to fill the available space, use the CSS rule "flex-grow: 1;" on those specific elements.

* {
  box-sizing: border-box;
}
#container {
  display: flex;
}
#container > * {
  background: #efefef;
}
#container .fixed-width-40px {
  width: 40px;
  background: #ccc;
}
.fill-available-horizontal-space {
  flex-grow: 1;
  width: 50%;
}
input {
  width: 100%;
  padding: .5rem;
}
<div id="container">
    <div class="fill-available-horizontal-space"></div>
    <div class="fixed-width-40px">bbb</div>
    <div class="fill-available-horizontal-space"><input></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

What could be causing issues with my CSS/HTML on Internet Explorer, including version 9?

Utilizing modernizer and html5boilerplate's CSS, I have encountered challenges in designing and debugging for Internet Explorer. It seems that my reliance on FireBug has hindered my progress. Can someone please review my work-in-progress at and provi ...

Is there a way to verify whether all elements (text, email, number, and radio) within a specific div have been selected or filled

Is there a way to activate the submit button on a form only when all inputs are checked or not empty? I've come across various snippets on Google and SO that achieve something similar, but they only work for a specific type of input, not taking into ...

The Limits of JavaScript Tables

Currently facing an issue with a webpage under development. To provide some context, here is the basic layout of the problematic section: The page features a form where users can select from four checkboxes and a dropdown menu. Once at least one checkbox ...

Restore original scale ratio to 1:1 following zoom

I am looking for a way to revert the image back to its original zoom level when a button is clicked using the onclick() event. I require the specific code for the onclick() event function. This is the div element in my HTML: div id="zoom"> ...

The alignment of the Bootstrap Radio white circle is off when adjusting the HTML font size

I'm currently working on a simple web application using Bootstrap for styling. While I really like the overall look of Bootstrap, I've run into an issue with the appearance of my radio buttons when I adjust the font size in my HTML. Here is a sn ...

Having trouble getting the smooth scroll-behavior to work properly in Tailwind CSS?

I've been working on my portfolio using next.js and tailwind CSS. I've added scroll-behavior: smooth in the globals.css file, and in the Navbar, I used https://i.stack.imgur.com/wqb4t.png I really want to achieve that smooth scrolling effect thr ...

issues with compass importing images

I'm struggling to understand how the directory structure functions. The setup I have is as follows: --compass --css --images --frontSprite Images -sass --_base.scss --advertiser.scss config.rb When ...

Can a webpage be redirected to another page while passing along the id from the original page?

https://i.sstatic.net/3LhYJ.png I have a page that displays shop names and addresses along with an edit function in views.py: def update_shop(request, id): context = {} # * fetch the object related to passed id obj_shop = get_object_or_404(VideoL ...

Tips for creating a clickable ::before image

I'm trying to create a hover effect where an image appears and is clickable when hovering over an element, similar to what is seen on many websites. For example, I want to display a bin icon when hovering over an element by using the ::before pseudo-e ...

How can I design unique navigation menus using HTML, CSS, JavaScript, and jQuery?

Is there a way to create menus where clicking on a holiday/seasonal category opens up all related lists automatically? For example: https://i.sstatic.net/Nctd1.png I've been searching online for submenu options but haven't found the right metho ...

Javascript editing enhancement for real-time changes

Are there any tools for in-place editing of Javascript code? I'm looking for something similar to Firebug, which is great for instant CSS editing and previewing but doesn't have the capability to edit JavaScript directly. Is there a tool or addon ...

Is there a way to extract text from a span element using selenium?

Below is my current code implementation: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import time as t P ...

Can you utilize CSS alone to divide a list into two vertical columns?

Here is some code that I am working with: <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> </ul> I am trying to ...

What are the different ways to programmatically change CSS rules using JavaScript in a Firefox Add-on, leveraging XUL, SDK, or WebExtensions methods?

Looking to dynamically alter a CSS rule set through JavaScript within a Firefox Add-on using XUL, SDK or WebExtensions techniques. Specifically targeting support for Firefox versions 29.0b1 through 49.0a1. The main issue I want to address is modifying the ...

Inserting pictures onto Bootstrap SVGs

Currently working on a website using Bootstrap 5 and incorporating an element from BS5 templates: <div class="col" data-aos="fade-up" data-aos-delay="200"> <div class="card shadow-sm"> ...

Trouble with HTML and CSS design layout

I'm struggling to create a responsive design for my webpage. The layout should consist of black blocks that display images. Ensuring the layout adapts to different screen sizes is proving challenging. It needs to maintain its shape on smaller screens ...

Is there a way to retrieve the disabled drop-down field value after submitting the form?

I'm struggling with a dropdown field that becomes disabled once an item is selected. After submitting the form, the dropdown field loses its value and I'm left with an empty field. Any suggestions on how to keep a value in the dropdown after subm ...

Discovering the art of incorporating various color palettes within a single stylesheet using HTML

I'm curious about incorporating multiple color schemes into a single stylesheet that can be activated by clicking, similar to the functionality found in platforms like WordPress and Magento. These platforms offer themes with various color options avai ...

Changing the styling frameworks for Components

Into my UI application developed using Angular, I have the option to select from various CSS frameworks such as: Bootstrap 4 Foundation Angular Material ng-bootstrap or ngx-bootstrap. A pre-built template available for purchase. And many o ...

Preventing access to drives and desktop until the mandatory HTML form is completed

Looking to develop a webpage in JSP that will automatically open whenever my system is started. Users will be required to fill out a form on this page before proceeding further. If they attempt to bypass filling out the form, they should not have access ...