Does CSS give preference to max width specifications?

Within a div, I have two child div elements. The parent container utilizes a flexbox layout, with preset maximum widths for the children as shown below:

<div class="wrap">
    <div class="one">Hi</div>
    <div class="two">my second div</div>
</div>

Accompanied by the SCSS code:

.wrap {
    display:flex;
    .one{
        max-width:100px;
    }
    .two {
        max-width: 50px;
    }
}

Initially, I assumed that resizing the .wrap container would proportionally shrink both children by equal amounts (e.g., -50px each). However, this turned out not to be the case.

I then speculated whether the behavior was determined by the highest or lowest assigned maximum width, but this hypothesis was debunked.

Upon observation, it became apparent that one of the child elements consistently shrinks significantly before adjustments are made to the other. What factors dictate this behavior exactly? My inquiry revolves around what influences which child reaches its maximum limit first? I conducted an experiment by removing the flex attribute, and the outcome remained unchanged, leading me to believe that the issue is solely related to the max-width property.

Answer №1

In a flex container, if the children do not specify a flex-basis, it will default to the item's width, specifically the computed item width in your case.

The flex property can be used to set the "shrink" / "grow" ratio.

For example, I have demonstrated a ratio of 1/3 - 2/3 below:

.wrap {
  display: flex;
}

.one {
  flex: 2;
  background-color: rgba(0, 0, 0, .5);
}

.two {
  flex: 1;
  background-color: rgba(0, 0, 0, .3);
}
<div class="wrap">
  <div class="one">Hi</div>
  <div class="two">my second div</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

Extracting Text from a Span Element Using XPath in Selenium

Here is the HTML code snippet: <div class="a-row a-spacing-small a-size-small"> <div class="a-row"> <a class="a-link-normal a-declarative g-visible-js reviewStarsPopoverLink" href="#" data-action="a-popover" data-a-popover="{"closeButton":" ...

"Is there a virtual keyboard available that supports multiple inputs and automatically switches to the next input when the maximum length

Looking to build a virtual keyboard with multiple inputs that automatically switch to the next input field after reaching the maximum length. Currently having an issue where I can only fill the first input and not the second one. Any assistance in resolvin ...

What is the best way to animate an element to slide down when it is hidden with

I'm working on an html tag called "loginBox" (<loginBox> ... </loginBox>) that is initially hidden with display:none. When a user selects an option, I want it to smoothly slide down instead of just appearing and disappearing. How can I ach ...

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; ...

Design a table featuring button groups using Bootstrap styling

I'm currently using Bootstrap 4 and facing some issues while attempting to create a button group with multiple rows, similar to the design shown in the image provided below. The default button groups in Bootstrap appear to only support arranging butto ...

The Vue component that was added dynamically is failing to render

When I have a section of HTML with Vue components inside it, coming from a server, and then insert it on the page by clicking a button, the components are not rendering or showing up. Here is the HTML that gets inserted when the button is clicked: <sec ...

Angular Components unexpectedly lose background transparency when using Transparent-Background-PNG in the foreground

Hey there! I'm currently working on a landing page and I have this amazing idea to use a stunning picture of clouds with a transparent background layered over a beautiful landscape. The only problem is that when I try to implement it, the transparency ...

Aligning Text to the Right Using CSS

HTML <body> <div class="menu_items"> <h1>My Heading</h1> <nav> <a>Item 1</a> <a>Item 2</a> <a>Item 3</a ...

Transforming HTML produced by an outdated ASP system to generate modern ASP.NET 2.0 web pages

I have a friend who is currently working on developing a solution to convert aspx pages from html pages generated by an older asp application. The plan involves running the legacy app, capturing the html output, cleaning the html with a tool like HtmlTidy ...

Tips on how to update the styling of an active link

http://jsfiddle.net/G8djC/2/ Looking to create a tabbed area where content changes based on the tab clicked. The Javascript function switches the link class to active upon clicking. However, struggling to change the color of the active tab beyond the firs ...

How to centrally align tabs in Material-UI AppBar using ReactJS

I'm developing a React Application using Material-UI and facing difficulty in centering the 3 tabs within the blue AppBar. Currently, they are aligned to the left like this: (unable to embed images at the moment, apologies) This is the code snippet ...

The background image fails to show up on the mobile device display

Currently, I am working on an app with Ionic, but unfortunately, the background image is not showing despite all my efforts. I have gone through various solutions provided in previous questions, but none of them seem to work for me. Here is the CSS I am u ...

Is there a method to pre-load a CSS background image so that it can still be displayed even without an internet connection?

Situation: Imagine having a web app that shows an error message when trying to perform an action but fails due to a connectivity problem. The error dialogue has a CSS class with a background image that can't load because of the connection issue, res ...

Shifting annotations on a Bar Graph featuring Negative Values on Google's Chart Maker

Incorporating google charts into my MVC project. Looking to create a bar chart that accommodates negative values. Desire annotations on the same side as the end of the bar for negative values (similar to positive values shown in the green box below). ht ...

What could be causing my .load() function to fail when attempting to target a specific div element?

My goal is to retrieve and display a div tag from another aspx page within my container div. Here is the jQuery code: function GetDocumentInfo(url) { $('MyContainer').load(url + ".otherdiv"); } This operation is triggered by a click event. ...

Troubleshooting Hierarchical Ordering in Django-MPTT's recursetree Function

I am currently utilizing the django-mptt package within my Django application, and I have encountered an issue when trying to use the .order_by() method in conjunction with a filter. Despite attempting different criteria for ordering, the results remain un ...

How come my dimensions are not returning correctly when I use offset().top and scrollTop() inside a scrolling element?

Currently in the process of developing a web app at , I am looking to implement a feature that will fade elements in and out as they enter and leave the visible part of a scrolling parent element. Taking inspiration from myfunkyside's response on this ...

Using JavaScript (without jQuery), take away the CSS class from an element

Seeking assistance from experts on the process of removing a class from an element solely using JavaScript. Kindly refrain from suggesting solutions involving jQuery as I am unable to utilize it, and have little knowledge about its functionalities. ...

How come when I click on the edit button, the selected data's model doesn't appear in the dropdown menu as I would like it to?

this is the function in my controller public function getModel($make_id = null) { $make_id = request()->make_id; $carsmodel = CarModel::where('make_id', $make_id)->orderBy('model', 'ASC')->get(); return re ...

In search of a render function that can effectively apply styles to HTML strings using React JS

I have been struggling to convert the result field value, including HTML attributes string, into respective styles for one column in my antd table. Below is the string I am trying to convert: The exhaust air requirements for fume hoods will be based on m ...