Attempting to utilize Flexbox to populate vacant areas

https://i.stack.imgur.com/BhPU0.png

Can the yellow square smoothly transition to the empty grey square and beyond in this layout setup? Each flex item represents a component that can utilize either 50% or 100% of the container width, information only known at render time.

I was contemplating using solely flexbox for this task, but uncertain of its feasibility.

.flex-container {
  display: flex;
  width: 200px;
  height: 240px;
  background: grey;
  flex-wrap: wrap;
  align-content: flex-start;
}

.flex-item {
  width: 100px;
  height: 100px;
}

.red {
  background: red;
 
}

.blue {
  background: blue;
  width: 200px;
}

.yellow {
  background: yellow;
}

.green {
  background: green;
}
<div class="container">
  <div class="flex-container">
    <div class="flex-item red"></div>
    <div class="flex-item blue"></div>
    <div class="flex-item yellow"></div>
    <div class="flex-item green"></div>
  </div>
</div>

Answer №1

Utilizing the grid-auto-flow property in CSS-Grid, you can achieve a dense placement of items and automatic re-ordering by setting: grid-auto-flow: row dense;

Follow these steps to transition from using flexbox to CSS-Grid:

  1. Switch
    .flex-container { display: flex; }
    to:
    .flex-container { display: grid; }
    for CSS-Grid implementation
  2. Delete:
    .flex-container { height: 240px; flex-wrap: wrap; align-content: flex-start; }
    as it is unnecessary for CSS-Grid
  3. Change .flex-item { width: 100px; } to: .flex-item { min-width: 100px; } to enable wider elements exceeding 100px
  4. Update .blue { width: 200px; } to: .blue { grid-column: span 2; } to double the element's width

.flex-container {
  display: grid;
  width: 200px;
  background: grey;
  grid-auto-flow: row dense;
}

.flex-item {
  min-width: 100px;
  height: 100px;
}

.red {
  background: red;
 
}

.blue {
  background: blue;
  grid-column: span 2;
}

.yellow {
  background: yellow;
}

.green {
  background: green;
}
<div class="container">
  <div class="flex-container">
    <div class="flex-item red"></div>
    <div class="flex-item blue"></div>
    <div class="flex-item yellow"></div>
    <div class="flex-item green"></div>
  </div>
</div>

Answer №2

There have been some discussions regarding the use of grid versus flexbox, with @Tacoshy rightly pointing out that grid might be a more suitable option. However, for those specifically looking to implement this layout using flexbox, here is a way to achieve it by utilizing the order property:

(I've also adjusted the height of the gray div to 300 from 240 for better visualization.)

.flex-container {
  display: flex;
  width: 200px;
  height: 300px;
  background: grey;
  flex-wrap: wrap;
  align-content: flex-start;
}

.flex-item {
  width: 100px;
  height: 100px;
}

.red {
  background: red;
  order: 0;
}

.blue {
  background: blue;
  width: 200px;
  order: 2
}

.yellow {
  background: yellow;
  order: 1;
}

.green {
  background: green;
  order: 3
}
<div class="container">
  <div class="flex-container">
    <div class="flex-item red"></div>
    <div class="flex-item blue"></div>
    <div class="flex-item yellow"></div>
    <div class="flex-item green"></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

PHP code that generates a drop-down list using a foreach iteration

all I'm encountering a slight issue with trying to create a dynamic drop down list. Here's the code I've written: <select name='categoryID' > <?php foreach( $categories as $category)?> <option value="<?p ...

Troubleshooting issues with CSS dropdown menu in Internet Explorer and Chrome

I am encountering an issue with my dropdown menu not functioning properly in Internet Explorer 10, Chrome, and compatibility mode. Surprisingly, it works flawlessly in the latest version of Firefox. CSS: #menu_items { float: left; width: 600px; } #me ...

Run a PHP function using <button onclick=""> tag

Is it possible to trigger the execution of a PHP script when clicking an HTML button? I am aware that simply calling a PHP function directly from the button's onclick event like this: <button onclick="myPhpFunction("testString")">Button</butt ...

jQuery not refreshing properly

I'm currently in the process of creating a script to switch the language on a website using PHP and Ajax/jQuery. I would like the page content to refresh without having to reload the entire page. So far, this is what I have come up with: $( "a[data-r ...

What is the most effective method to ensure this layout is functioning properly?

Seeking a solution to an unexpected challenge, what initially appeared as a simple task has transformed into a complex dilemma. Balancing the vertical alignment of green DIVs while preventing absolute positioned elements from overlapping following content ...

Centering text in a D3 donut chart

Struggling with centering text inside a d3 donut chart for a project. While trying to modify a piece of code, I find myself lost in the complexity of it. Despite my efforts, the text added to the center is not perfectly centered. I have attempted various ...

List the acceptable favicon formats for Internet Explorer version 10 and above

When I say type, I'm referring to the file extension. My favicon displays correctly in Firefox, Chrome, and Safari, but someone suggested that the issue may be related to the favicon type. Is there a reliable online resource where I can find informa ...

Tips for positioning a div between two vertically aligned input elements

I'm trying to figure out how to place a div between two inputs with the same height and vertically aligned. It seems like a simple task, but for some reason, the code below isn't working: input{ width: 35%; border: 1px solid #A7A7A7; ...

Guide to positioning text so the baseline aligns with a specific point in HTML/CSS

After developing an algorithm that converts text from SVG to HTML, I encountered a discrepancy in the positioning of the text. The issue stems from the difference in coordinate systems between SVG and HTML; SVG's origin is at the bottom left while HTM ...

Unveil as you scroll - ScrollMagic

I am working on a skill chart that dynamically fills when the document loads. I am exploring the possibility of using ScrollMagic to animate the chart filling as the user scrolls down. After trying various combinations of classes and pseudo-classes in the ...

What is the reason that PHP has the ability to set Cookies but Local Storage does not?

Let's take a step back to the era of cookies, not too far back since they are considered old but still quite relevant. PHP allows you to set and read them even though they are a client-side technology; however, JavaScript can also be used completely o ...

Ways to display or conceal various divs by employing an if/else statement?

I am aiming to conditionally display various divs based on certain criteria: Show the "x" div if it's not already visible Display the "y" div only if the "x" div is already being displayed Show the "z" div only if both the "x" and "y" divs are alrea ...

"Troubleshooting problems with jQuery click function during real-time search execution

In my code, I have an empty result div that dynamically fills up with item elements as search queries are entered (using jQuery ajax). If I use the following code: $(".result").click(function(){ alrert("click on whole result class captured") }); I r ...

A guide to sending epoch time data to a backend API using the owl-date-module in Angular

I have integrated the owl-date-time module into my application to collect date-time parameters in two separate fields. However, I am encountering an issue where the value is being returned in a list format with an extra null value in the payload. Additiona ...

When applying the OWASP ESAPI encodeForHTMLAttribute method, I noticed that symbols are being rendered as their corresponding HTML entity numbers instead of the actual symbols

I recently started exploring OWASP ESAPI for preventing XSS and integrating the JavaScript version into my application. As per Rule #2 in the XSS prevention cheat sheet, it is recommended to "Attribute Escape" before inserting untrusted data into attribut ...

Gain access to the iterable within the adjacent tag

I am looking to access an iterable from a sibling tag, but unfortunately it is within a table which complicates things. Simply wrapping the content in a div and moving the iteration outside is not possible due to the structure of table rows. Here is an exa ...

What is the reason that styled() does not apply styles to the Material-UI <TextField /> component?

I've managed to figure out a solution, but I'm curious about why this code is working: <TextField label='Zip Code' size='small' InputProps={{ style: { borderRadius: '.4rem' }, }} sx={{ width: & ...

HTML not being able to execute Javascript when using an SSL connection

While serving HTML via Jenkins HTTPS with an invalid security certificate, the following code is included: <!DOCTYPE html> <html> <head> <script type="text/javascript">alert(1);</script> <script type="text/javascri ...

.htacces Disables Styling on Page

I've been trying to understand the functionality of .htaccess RewriteRule. Here is a URL example where I have experimented with .htaccess: http://example.com/test/home This used to be: http://example.com/test/index.php?p=1 My goal is to redirect ...

Is Fullpage.js functioning only on local servers?

I have decided to create a personal website showcasing my portfolio using the fullpage.js library. Everything seems to be working fine during development, but once I upload the site to my github.io or another public domain via FTP, I encounter GET errors t ...