Achieving a proportional distribution of flex items within a parent container, even when they are of varying sizes

Here is the template I am working with:

<div id="row">
  <div id="longer"></div>
  <div id="shorter"></div>
</div>

In the same row, I want longer to occupy twice as much width as shorter. To achieve this, I have applied the following CSS:

#row {
  display: flex;
  flex-wrap: wrap;
}

#longer {
  flex: 1 0;
}

#shorter {
  flex: 0.5 0;
}

While this setup works fine, an issue arises when shorter wraps onto a new line and only takes up 50% of that row's width despite being the only item in the row.

I intend for shorter to fill the entire row if it's the lone item on the line. Does anyone know if this can be achieved without using media queries?

Plunker link

Answer №1

If the combined flex-grow value of children is less than 1, they will not occupy the entire width of the parent but only a proportionate fraction instead.

You can increase their values uniformly to ensure they are at least 1:

#row {
  display: flex;
  flex-wrap: wrap;
}

#longer {
  flex: 2 0;
}

#shorter {
  flex: 1 0;
}


/* For demonstration purposes */

#row div {
  white-space: nowrap;
}

#row div:nth-child(1) {
  background: green;
}

#row div:nth-child(2) {
  background: red;
}
<div id="row">
  <div id="longer">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
  <div id="shorter">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
</div>

Answer №2

Adjust the flex: 2 0 and flex: 1 0 values - using fractional flex-grow or flex-shrink will only extend a portion of the available space in the specified flex axis. Check out the demonstration below:

body {
  display: flex;
}

#row {
  flex: 1 0;
  display: flex;
  flex-wrap: wrap;
}

#longer {
  flex: 2 0;
  background-color: lightgreen;
}

#shorter {
  flex: 1 0;
  background-color: lightblue;
}
<div id="row">
  <div id="longer">
    this_is_the_longer_div_asdasdasdasdasdasdasdasdasdasdasdasdasdasdasd
  </div>
  <div id="shorter">
    this_should_fill_parent_width_when_wrapped
  </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 causes certain images to have unspecified height and width measurements?

Currently, I am in the process of extracting image sizes from a website using a module called scraperjs. Most images have their height and width attributes defined, but some do not, resulting in the returned value being "undefined". How can I retrieve the ...

PHP code fails to set a hidden element within a form array

My current project involves sifting through a modest PHP application to debug and enhance it. One snag I've come across is that what should be updates are instead treated as deletes and inserts. To rectify this, I've made some adjustments. Speci ...

Prevent using href for opening the browser when clicked

In the control, there is an href link that triggers a javascript function and passes a variable to it: <a href="<%#XPath("link").ToString()%>" onclick="return getLink(this)">Link</a> I'm looking for a way to prevent the browser fro ...

I would greatly appreciate your assistance in grasping the CSS code provided

Although I wouldn't consider myself a CSS expert, I usually can figure out how to manipulate code to achieve my desired result. However, the CSS code snippet below has me puzzled and I haven't been able to find any resources to help me make sens ...

A hyperlink will not float above

I can't seem to get the links in my navbar to change color when I hover over them, even though I've used the :hover selector. Can someone please assist me with this? <header> <nav class="navbar navbar-expand-lg navbar-dark sta ...

Develop your website layout with Flexbox (Bootstrap 4.2), using stacked grid designs similar to the float: left method

For my Wordpress theme, I am utilizing Bootstrap 4.2 to design a basic grid layout for a list of blog posts. In my design, I have a Card that is double the height of others and I am attempting to 'float' two single-height cards beside it. Previ ...

Ways to create a responsive Instagram iframe without using JavaScript

Currently, I am attempting to create a responsive grid featuring Instagram posts. Unfortunately, the padding-bottom hack isn't effective for my particular situation. After some experimentation, I discovered that the height could be calculated by addin ...

CSS transitioning can be tricky when trying to fade an image or video with a solid background alongside an HTML element that shares the same background color

When attempting to apply a fade in/out effect to an image or video with a solid background color and using the same transition for the opacity of an html element with a matching background color, a noticeable "square" appears around the image/video. Why d ...

Button from Material-UI vanishes upon being clicked

I encountered an issue with a button that disappears when clicked on. Additionally, clicking the button once does not trigger any actions associated with it. In order to execute the button actions, I have to click the area where the button was located afte ...

Unable to Load CSS File with Path in ClojureScript Project

Currently, I am working on a project in Electric Clojure that requires importing a CSS file to enhance the styling of my application. While I have been able to add styles using dom/element, the resulting code appears cluttered. What is the most effective m ...

Selenium allows for easy extraction of text within nested span tags

In the project I am working on, there is HTML code provided below: <button id="tab-oneWay" class="" data-initial-tab="true" type="button" data-section-id="#section-oneWay"> <span class="tab-label"> One Way <span class="visually ...

Align the content to the right and center it within the table

One common issue I face is working with tables that contain numbers requiring right alignment to ensure the ones/tens/hundreds/thousands places line up correctly. Here's an example: 2,343 1,000,000 43 43,394 232,111 In these tables, ...

What is the best way to convert a dynamic HTML table with input fields into an Excel spreadsheet?

I have developed a JavaScript function to convert an HTML table into an Excel sheet. However, I am facing an issue where some fields in the table are enclosed in input tags instead of td tags, causing them to not appear in the Excel sheet. function expo ...

Using JQuery to extract the unique identifiers of nodes within a tree view format for the purpose of storing data according to these identifiers

Using Angular to display data in the view, I have a sample code here. When a specific age group category is clicked, I want to populate a form with data and save the entire dataset, including the parent node IDs. I am facing an issue with fetching the pa ...

Tips for saving an ajax response in a class and retrieving the data later by clicking on the corresponding element

Received some values in data via ajax request Storing data in the class attribute as class="' + data + '" $.each(response.d, function (i, data) { var id = data.Soid; var title = data.Name; ...

Issue with radio button click event not triggering

Whenever I click on a radio button, I want to call a function, but for some reason, it's not working as expected. I found this fiddle that demonstrates exactly what I want, but when I implement it in my code, it doesn't work. Here's a snipp ...

What makes these two ul li a lists unique?

I am facing an issue with two inline unordered lists with list items as links. One list behaves as expected, while the other has a space between the link items. ul { list-style: none; margin: 0; padding: 0 } li { display: inline; } a { border: ...

Tips for implementing border-radius on Internet Explorer 8:

Similar Question: How to create rounded borders in IE8 using CSS? I typically use css border-radius for the design of my webpages. While they display well in Firefox, I am facing compatibility issues with IE8. Can anyone offer any assistance? Thank yo ...

The fixed position feature seems to be malfunctioning

I'm attempting to incorporate the persistent header feature demonstrated in this tutorial: http://css-tricks.com/persistent-headers/ Here's My Code: <div class="col-md-4 col-sm-4 col-xs-12 postRight persist-area"> <h2 class ...

Blank areas on a document

I've recently started learning HTML and CSS, and I've encountered a problem that has me stuck. HTML: <div class = "window"> <form> <input class="button" type="submit" value="Login" /> </form> ...