Ensure the width of ancestor flexbox divs is limited to the width of its child by setting a specific width

I have a specific element that incorporates flex display and I've defined its width with the flex-basis property like this:

.flex-row input { flex: 0 1 450px; }

There is a flex-row division nested inside a flex-container division.

My goal is for the flex-row to match the width of the input element.

When I set the flex-container to have display: flex, it spans 100% of the width.

However, changing it to display: inline-flex causes the input to shrink, not maintaining the 450px width.

Although I'm not concerned about the width of the flex-container, it would be convenient for it to match its child's width. How can I ensure that the flex-row remains the same width as the input, particularly when there is enough space available (not exceeding 450px)?

.flex-container {
  display: flex;
}
.flex-row {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
}
.flex-row input {
  flex: 0 1 450px;
}
<div class="flex-container">
  <div class="flex-row">
    <input type="text" name="query" class="searchbar" />
  </div>
</div>

View the demo here.

Answer №1

Make sure to apply the same flex property to the flex-row that is used on the flex-row input.

.flex-row{
    display: flex;
    flex: 0 1 450px;
    align-items: center;
    justify-content: center;
}

To center the row, add the align and justify properties to the flex-container like this.

.flex-container{
   display: flex;
   align-items: center;
   justify-content: center;
}

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

Namespace SyndicationElementExtension refers to

I'm trying to achieve the following output: <someNSalias:full-text>Good news everyone!</someNSalias:full-text> This is the code I have written: var element = new SyndicationElementExtension(field.Name, field.HasNamespace ? RssNs : strin ...

What is the best way to create a number input field that also accepts text input?

The goal I have in mind is to create an input field that will contain text like "100%", "54vh", or "32px". I am attempting to utilize the number input's up and down arrows, while still allowing the user to edit the text. However, it should only allow ...

Utilize jQuery to serialize the HTML input elements that are dynamically generated outside of the form

A proof of concept is in progress for an application that involves the generation of HTML elements based on user-configured fields. Here is a sample configuration: // Customer SAM $response = array( array ( NAME => CUSTOMER, TYPE = ...

Enhance Your Website with HTML5 Video Transition Effects

Is it possible to recreate video transition effects using HTML5 similar to the ones shown in this example: Can this be achieved across all major browsers, including Safari, Firefox, Chrome, and IE9? ...

How to Properly Adjust the Material UI CircularProgress Size

Is there a way to display the Material UI CircularProgress component at a size that nearly fills the parent div, which has an unknown size? Ideally, I want the size to be around 0.8 * min(parent_height, parent_width). I've experimented with adjusting ...

Steps for accessing an image from the static folder in a Python-Django application

I am currently working on a portfolio website project where I am using html, css, js, and django. However, I am facing an issue with loading an image from the assets folder within the static directory. Here is a snippet of the template code causing the pr ...

Overflowing CSS grid issue

I've been working on this for a couple of hours, but I haven't been able to get it right. I want to add labels in a grid layout with a maximum of 3 columns. If the items don't fit, they should wrap to the next row. It would be great if the i ...

Generating DOM elements at specific intervals using Jquery

I am looking to dynamically create 1 div container every x seconds, and repeat this process n times. To achieve this, I have started with the following code: $(document).ready(function() { for (var i = 0; i < 5; i++) { createEle(i); } }); f ...

The jCapSLide Jquery Plugin is experiencing compatibility issues in Chrome browser

While this JQuery plugin works perfectly in Internet Explorer and Firefox, it seems to be malfunctioning in Chrome. The plugin is not being recognized at all by Chrome, and the captions are appearing below the image instead of on top with a sliding effect. ...

The parent div will not accommodate two nested divs

I'm having an issue with two divs inside a parent div not being contained within the container when I inspect the page, even though I've wrapped the div around the other 2 divs. My goal is to apply attributes to the parent div instead of each in ...

External CSS stylesheets

I have encountered the following HTML code: <div class="entry"> <p></p> //text-indent here <blockquote> <p></p> //no text-indent here </blockquote> </div> I am trying to apply a ...

Angular CSS Animation not functioning as expected

I have developed a system that retrieves stored messages from the server dynamically. The view for this system is structured as follows: <div id= "messages" data-ng-controller="MessageController"> <div class="message" data-ng-repeat="message ...

Utilizing Selenium to Click and Drag on the Browser Window in Order to Select Text on the Website

I am attempting to mimic the action of highlighting text on the screen by moving the cursor to a specific element and clicking and dragging. However, my current code does not seem to be replicating this behavior as expected. Despite defining the element, ...

The transition between backgrounds is malfunctioning

I want to create a smooth transition effect for changing the background of an element within a setInterval function. Currently, the background changes immediately, but I would like it to transition over a period of time. var act = true; setInterval(func ...

Is there a way to change TTF files into OTF format?

I am looking to utilize the @font-face feature and have my fonts stored in TrueType (TTF) format. Can anyone advise on how to convert TTF to OpenType (OTF) format? ...

Choosing the anchor text in the second td element of a table using jQuery

I have a webpage with the following HTML structure... <input type=text id="search"> <table> <tr> <td></td> <td><a href="">Some text...</a></td> <td></td> </tr> </table> This co ...

In Angular 4, the Bootstrap modal now only opens after a double click instead of opening on the first click

Working on an eCommerce application, there is a cart icon that triggers a modal screen displaying user-selected product data when clicked. However, the issue I'm facing is that upon initial page load, the modal screen opens only after double-clicking; ...

Every time I click the highlight button, my navbar link inexplicably shrinks

I am struggling with adjusting the list style in my bootstrap navbar. Whenever I click on the highlight button, it squeezes my navbar and I'm not sure how to fix it. <div id="links"> <a href="index.html">Home& ...

What is the purpose of using JSON.parse(decodeURIComponent(staticString))?

A specific approach is utilized by some dynamic web frameworks in the following code snippet <script> appSettings = JSON.parse( decodeURIComponent( "%7B%22setting1%22%3A%22foo%22%2C%22setting2%22%3A123%7D")); </script> Is there a part ...

Different position of navigation list-style visible in Chrome browser

Currently, I am working on a website where the main menu navigation is designed using the list-style property to separate each menu item. In Firefox, everything appears as intended with small circles between each menu item. However, when viewed in Chrome, ...