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

Using two different typefaces to accommodate two distinct languages on a single webpage

Let me clarify my situation. I want to create a platform where users can input text in both Hebrew and English, regardless of the language of the website itself. This means that users could type in Hebrew on an English website and vice versa. My goal is to ...

What's the reason for text-alignment not functioning properly?

After some testing, I have come up with the following code: .Greetings { width:100%; display:block; text-align:center; font-family: "Times New Roman", Times, serif; font-size:75px; color:#208CB7; } The objective was to center the text on the ...

Issue with the overall layout in Bootstrap 4 involving the main content area and sidebar

I am encountering challenges with the overall design of my website, particularly with how the main content area and sidebar are positioned. The layout I have in mind is: Below is the code snippet I am using to implement this design (spread across multiple ...

adhesive section on the left side with a defined lower boundary

I have a contact div that I made sticky using Bootstrap affix. <div id="contactForm" data-spy="affix" data-offset-top="400"> However, I am unsure of how to limit its bottom to the top of the next div. In other words, I want it to stay in place when ...

A guide on styling JSON key values within HTML

I need help organizing my JSON Document on an HTML page. Here is the JavaScript code I am using: xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // Optionally, here you can update ...

Looking for a reliable tool to check your CSS classes and tidy up your code effectively?

It seems like a common issue with a straightforward solution. I have numerous CSS selectors scattered across various files, many of which may be unnecessary and a directory full of HTML files. My goal is to identify all redundant selectors in my CSS within ...

What steps should I take to address these styling challenges?

Recently, I came across some code that uses the hspace attribute which is now considered outdated. In order to create space between elements in a widget, I am looking for a solution that involves adding 10px of space above and between the items using CSS. ...

Update the FontSize in the HTML dropdown menu using JavaScript

I am having trouble filling my Selection using a script. For example, when I try to populate my FontSizeMenu, I use the following code snippet: function FillFontSizeMenu() { FillSelection(GetPossibleFontSizes(), "fontSizeMenu"); } function GetPossib ...

Confirming an authorization?

I have been working on this code and it is almost perfect, but I am struggling to understand why it needs to validate the 0 or 10 in order to work correctly. The issue I am facing is with a validation where the button should be deactivated when the counte ...

CSS navigation bar (background gradient problem)

I have encountered an issue with my multi-tier navigation menu where tiers below tier 1 are displaying an unexpected block to the left of the options. I suspect this problem is related to the background gradient applied, but I haven't been able to fin ...

ngSanitize continues to present plain text rather than rendering HTML code

When working with AngularJS scope, I encountered the need to display certain items as HTML. After some research, I realized that integrating ngSanitize was necessary for this functionality. Here is how I implemented it in my app: <script src="Scripts/a ...

Guide to incorporating a shiny application into an Rmarkdown HTML file

Currently, I am trying to craft an HTML document using RMarkdown that includes text, R code, and a Shiny application embedded within it. In my attempts, I considered using the asis=TRUE for the shinyApp(ui, server) block. However, I discovered that RStud ...

After running assets:install and assetic:dump, Font Awesome fonts are no longer functioning properly

Setting up a site on shared hosting has been going smoothly, except for the FontAwesome icons which Symfony is having trouble finding in their designated locations. The steps I followed to move the site to production on shared hosting are: To overcome th ...

Fade effects on hover with a changing background color

Here is the CSS code to be used: #onec-fourth:hover { background-color: #F36DE1; color: white; } I am looking to keep the background color and text color effects for 1 second after I move my mouse off the object (#onec-fourth). Currently, when I mov ...

On smaller screens in portrait mode, CSS automatically incorporates margin adjustments

Here is the code I am working with: <html> <head> <style> body { margin: auto; width: 720px; } </style> </head> <body> <p>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ip ...

HTML5 - Ajax - puzzling behavior that I am unable to comprehend

Need Help Clarifying My Issue: I currently have a Div with Page 1 content. Page 1 contains a button that transitions the div content to Page 2. Page 2 has a button that switches the div content back to Page 1. The Problem: If Page 1 is loaded first, t ...

`Loading CSS and JS files in node.js: A step-by-step guide`

I've searched through numerous similar questions without success, so I'm reaching out for help. My folder structure looks like this: Source Code Frontend Graphs.html Graphs.css Temperature.js Server_Backend server.js I aim ...

Obtain information about a div element while scrolling

Looking to enhance my social feed page by adding a view count feature for each post. The challenge is figuring out how to keep track of views as the user scrolls down the page. Any suggestions? ...

Creating an overlay button within various containing divs requires setting the position of the button

Tips for overlaying a button on each HTML element with the class WSEDIT. My approach involves using a JavaScript loop to identify elements with the CSS class WSEDIT, dynamically create a button within them, and prepend this button to each element. Below ...

Adjust image size based on the size of the screen

I am facing an issue with two images that are created for a 1024 x 768 resolution but I need to use them in a higher resolution. The challenge is to align these two images so that the circle from the first image falls between the second image. Although I ...