Is it possible to apply CSS styling to a flex-item once it wraps onto a different row?

.wrapper {
  border: 5px solid pink;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.a-fc {
  background-color: purple;
  width: 300px;
  /*height: 100px;*/
}

.b-fc {
    background-color: orange;
    display: flex;
    flex-direction: column;
    /*flex-wrap: wrap;*/
    flex-basis:70px;
    flex-grow:1;
}

.b-fc > * {
  flex-grow: 1;
  flex-basis: 100px;
}

.b-fc > *:nth-child(1) {
  background-color: red;
}

.b-fc > *:nth-child(2) {
  background-color: blue;
}

.b-fc > *:nth-child(3) {
  background-color: green;
}
<div class="wrapper">
  <div class="a-fc">
   <div>a1</div>
  </div>
  <div class="b-fc">
  <div>b1</div><div>b2</div><div>b3</div>
  </div>
</div>

FC = flex-container. FI = flex-item.

I have successfully implemented a solution where .b-fc shifts to a new row when the available space on the initial row falls below 70px.

My current objective: I want the FIs of b-fc to stack vertically if they don't fit on one row without wrapping. However, if b-fc wraps, I want the FIs to align horizontally.


Potential Solution

I believe it would be more efficient to apply CSS specifically for when .b-fc wraps. Is this feasible?

Idea: Implementing media queries

An alternative approach could involve using media queries to detect when the second row is created and then applying CSS styles to .b-fc.


P.S. A similar query was raised in 2015, as seen here. Perhaps there have been advancements in techniques since then.

Answer №1

In this specific scenario, you can utilize the combination of max() function along with flex-basis property. The key is to set a value of either 0px for horizontal items or a significantly large value for vertical items.

It's important to note that this approach may not be universally applicable and depends on the structure of your HTML:

395px = 300px (width of a-fx) + 70px (flex-basis of b-fc) + 10px (border of wrapper) + 16px (default body margin) - 1px

.wrapper {
  border: 5px solid pink;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.a-fc {
  background-color: purple;
  width: 300px;
}

.b-fc {
  background-color: orange;
  display: flex;
  flex-wrap: wrap;
  flex-basis: 70px;
  flex-grow: 1;
}

.b-fc>* {
  flex-grow: 1;
  flex-basis: max(0px, (100vw - 395px)*100);
  height: 100px;
}

.b-fc>*:nth-child(1) {
  background-color: red;
}

.b-fc>*:nth-child(2) {
  background-color: blue;
}

.b-fc>*:nth-child(3) {
  background-color: green;
}
<div class="wrapper">
  <div class="a-fc">
    <div>a1</div>
  </div>
  <div class="b-fc">
    <div>b1</div>
    <div>b2</div>
    <div>b3</div>
  </div>
</div>

In response to your query: CSS cannot directly detect wrapping, but there are workarounds for specific cases like this one.

Related queries:

Achieving 3 column desktop to 1 column mobile layout without media queries

Determining maximum number of columns in CSS grid without media queries

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

Navigating with Nokia Here maps: plotting a path using GPS coordinates

I am currently developing a GPS tracking system and my goal is to visually represent the device's locations as a route. The challenge I'm facing is that there is a REST API available for this purpose, but my client-side application relies on soc ...

Toggle the visibility of content with jQuery or Ajax

As I work on designing a webpage at this link, I am facing an issue with the projects tab due to my lack of experience in proper protocol. The plan is to have a side menu where users can select different content options (CAD, FEA, MATLAB, etc.), which wi ...

Blog Writer: Picture quality compromised when adjusting size

Issue with blurry images after resizing. The problem is noticeable in the three main images located under the Header section. To preview the blog, click on this link: Click Here Your assistance in solving this issue would be greatly appreciated. Thank yo ...

Issue with retrieving duration XML data using Jquery

Could there be a specific explanation as to why the term duration might not function properly in JQuery? For instance, consider this XML snippet: <video description="description etc" duration="43306" id="1144537378001" name="Fashion" thumbnail="http:// ...

Is there a way to get the eventListener to function properly within a table that contains an image?

I need help with a JavaScript function that can add or remove an image from table cells when clicked. Here is my current code snippet: const table = document.querySelector("tbody") table.addEventListener("click", clickTile) function cl ...

What criteria need to be met for height: 100% to be displayed correctly?

Although I haven't had the need to use this often, there are times when it proves to be useful... when it decides to cooperate. It's interesting how sometimes the height: 100%; code works perfectly, while other times it seems to fail. It leaves ...

When the listbox value changes, add a new child element

Here is the code snippet I'm working with: //Header container - summary info var header = document.getElementById("Editsubmission"); var cont1 = document.createElement('div'); var cont2 = document.createElement('div'); cont1.text ...

The image sits on the edge of the container, not fully contained

.sub2 { background-color: #FFFFBF; margin-top: 30px; height: 410px; width: 100%; } h1.sub2 { font: bold 100px american captain; text-decoration: underline; float: right; } p.sub2- { font: italic 25px american captain; margin-top: -300px; ...

Exploring the World of Node.js Background Operations

What is the best approach for handling background processes in a NodeJS application? Situation: When a user posts something to an app, I need to process data, fetch additional information from external sources, etc. Due to the time-consuming nature of the ...

Examining the performance of a React application through the use of Google Chrome

Currently, while working on my react application, I have been utilizing the chrome developer tool to monitor performance. However, I am having difficulty identifying which specific function is causing a high level of computational intensity. Could anyone ...

Issue with Event.target not functioning properly when clicking outside the div to close it

I am facing an issue with my main div and sub div, which is referred to as a form because it is enclosed within the main div. I am trying to close the form when clicking outside of the main div, but unfortunately, this functionality is not working as expec ...

Using Javascript to focus on the value of a textfield

Criteria: Please only use Internet Explorer for testing purposes Requirements: There is an alert message in place to check if a user enters a value with less than 8 digits - the user should not be able to proceed to the next field and must remain on the c ...

Update the background color on the sidebar

Objective: I want to change the background of the class="col-sm-3 blog-sidebar" to match the color shown in the image below. Essentially, I am looking to update the sidebar background to reflect the picture's color. Challenge: How can this be ach ...

I'm having trouble getting FlowType.js to function properly

I have added the following code just before the closing </body> tag, but unfortunately, it seems like the type is not changing as expected. I am struggling to identify what mistake I might be making. Check out FlowType.JS on GitHub View the code on ...

Is there a way to utilize JavaScript for streaming a remote participant's screen sharing?

Check out this code snippet for enabling local participant screen sharing: const videoElem = document.getElementById("video"); const startElem = document.getElementById("start"); const stopElem = document.getElementById("stop" ...

Reading multiple files in NodeJS can be done in a blocking manner, and then the

When retrieving a route, my aim is to gather all the necessary json files from a directory. The task at hand involves consolidating these json files into a single json object, where the key corresponds to the file name and the value represents the content ...

What is the best way to successfully send an object through AJAX once all its updates are completed?

I am experiencing an issue with my JavaScript code within an event: var userData = tableWidget.grid('userData'); console.log(tableWidget.grid('userData')); $.ajax({ "url": "../../server/query.aspx?tableEvent=reordercolumns&tabl ...

Creating a countdown timer that is determined by the word count of a specific <div> element

What I have: A unique countdown timer that starts at 3 seconds and counts down to 0s. <div class="phrase"> This is a statement.</div> <p> <div style='font-family: Arial; font-size: 12px; color:gray'> <br><s ...

Is there a way to retrieve values from TextFields and Select elements by simply clicking on a button?

I am currently working on a project using react, redux, and material ui. I need to retrieve data from a TextField in order to make an order. The code snippet below showcases my current implementation: <Select value={product.color_set[0].title}> { ...

Bring in resources without specifying

Is there a way to directly import an image into a JSX tag in React without having to declare it at the top of the file using import img from './this/is/file.png'? I attempted to do so with <img src={import './this/is/file.png'} alt= ...