Arrange the computed item under a flexbox element

I am seeking guidance on a specific issue I am facing.

My goal is to arrange multiple elements inside a flexbox in such a way that they can adapt to their environment. Underneath one of these items, I want to place a calculated element whose size will depend on the element above it - essentially having a fixed (yet calculated) size.

I attempted to address this problem by placing the calculated element inside the flexbox, but this disrupted the overall behavior.

The desired layout should resemble something like this:

<Label without shrink> <input element, can grow>             <Label without shrink>
                   <some rectangle, size based on calculation> 

View the desired layout here as an image link

In the provided example on JSFiddle, I have utilized an input element along with two labels and a computed rectangle to illustrate the issue. Consider the rectangle as a generic representation of the problem.

Resize operations enlarging the window work smoothly. However, issues arise when reducing the window size.

Flex can only reduce the size of elements with variable sizes, such as the input element. The fixed size of my computed rectangle prevents the container from shrinking.

Idea 1: Adjust the size of the calculated item slightly smaller than the related input element so there is room for the container to shrink. This allows both the input and the rectangle to resize accordingly. Click here to see JSFiddle with a smaller rectangle.

While this solution works to some extent, rapid resizing may cause the rendering to fail to keep up leading to the previously described issue.

Idea 2: Ignore the calculated element and position it absolutely. Check out this implementation: JSFiddle with relative and absolute positioning.

I set the outer element to be relatively positioned and the inner (containing the calculated element with constant width) to be absolutely positioned. Although this approach removes the rectangle from the flow allowing things to function properly, the rest of the layout gets disrupted. This is problematic since this feature is intended to be used within a larger context. In the provided example, the last label is misplaced due to the div not considering the rectangle.

Now, I seek further advice. Ideally, I would prefer a CSS-based solution without relying on JavaScript calculations. Alternatively, leveraging more information from getBoundingClientRect() like the X position could potentially resolve the issue (?) - enabling me to remove the rectangle completely from the flexbox and manually position it at the desired location.

I appreciate any additional insights or ideas you may have.

Answer №1

It seems like the issue stems from relying on the width of the input element someInput to determine the width of the computed-element-container. Instead, consider using the width of the flex item for more accurate results. I have made adjustments in your jsfiddle example by adding a second flex container above the original one with three items. The width calculation now depends on an element identified by the id: content-box. Please check out the updated version below for a potential solution:

https://jsfiddle.net/jamarmstrong/h9rz1t4b/

Answer №2

In case someone is facing a similar issue, I wanted to share an alternative approach that I developed while attempting to modify James' solution.

Take a look at this jfiddle example: jsfiddle demonstration using overflow and min-width

What I revised in my initial solution: The problem with the shrinkage stems from a tricky situation where the flexbox cannot shrink beyond the limits set by its inner elements, creating a cycle of constraints preventing proper resizing. This led me to experiment with positioning the element absolutely in order to make the flexbox disregard it - disrupting the overall layout.

To enable shrinkage, I devised a method allowing certain elements to overflow outside the flexbox and have a defined min-width rather than just being set to auto.

This strategy involves letting the calculated element exceed the boundaries of the flexbox, effectively making the flexbox ignore it as if it were not there. It's important to adjust the min-width of specific elements accordingly.

Ultimately, I introduced a secondary flexbox to envelop the computed element, permitting both the container and its contents to tolerate overflow and shrinkage (potentially streamlining the elements further).

This setup enables the computed element to breach its confinement and allows the entire structure to contract. As a result, any temporary overflow is promptly corrected by resizing the computed element without delay.

You can compare this technique with James' idea and select the one that best addresses your specific challenges.

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

The Angular material autocomplete feature fails to close automatically when the document loses focus

Normally, an input field will blur when you click on the 'body' of the document. But when using the autocomplete directive on desktop, the menu never closes on mobile unless you select an item or refresh the page. Initially, I thought this issue ...

Exploring the Power of Arrays, For-Loops, and Functions in JavaScript

Currently, I have a functional code implementation, but I'm struggling with creating a proper loop. I'm seeking a solution to develop a function that changes the URL source of #mainImage to the one in the imgUrl array when hovering over an #id i ...

Utilize JavaScript to parse and retrieve specific data from an SVG file in XML format

I am attempting to retrieve an svg file using jquery or javascript. Here is an example of the svg code: <svg width="111" height="123" xmlns="http://www.w3.org/2000/svg"> <g> <title>Layer 1</title> <rect fill="#ffffff" strok ...

Incorporating unique HTML5/iframe widgets into your Facebook Timeline and Tab Pages

I have a unique widget that users can easily integrate into their websites using the HTML/iframe code generated by my app. Now, I am looking for a way to allow users to also add this widget to their Facebook Company Pages. The widgets are accessible th ...

Styling Challenges with CSS/AngularJS Accordion in Footer

I want to create a page layout as shown below: +---------------------------+ | Auto-fill / v scrollable ^| | || | || | v| +---------------------------+ | Fixed [][][] ...

The animation speed of the jQuery hashchange event is set to zero, causing the animation

I'm facing an issue with jQuery where my animation inside a hashchange event is not smooth; it happens instantly when triggered. I'm looking for a way to make the animation smoother. jQuery( document ).ready(function() { jQuery(window).on(&a ...

Using v-model with the Toast-UI editor in Vue.js adds a dynamic and

Is there a way to bind the input value while using toast-ui vue-editor since it doesn't support v-model? Here is my setup for toast-ui: import '@toast-ui/editor/dist/toastui-editor.css'; import { Editor } from '@toast-ui/vue-editor&apos ...

What are some methods to secure my API keys within my React application?

What steps can I take to secure my api keys in my react application? Should I incorporate something with express? My goal is to avoid creating any server-side components to handle the API calls. Currently, my backend is managed by firebase but I also uti ...

Searching for data in Ag grid Vue using a filter

Can someone help me create a search filter in ag grid vue like the one shown in this image? Image link: https://ibb.co/cwVq7DD For documentation, refer to: https://www.ag-grid.com/vue-data-grid/tool-panel-columns/#read-only-functions I attempted to impleme ...

Enhance your Bootstrap Progress Bar by incorporating the width value within the CSS using CoffeeScript or jQuery

I'm attempting to create a function where every time the button is clicked, the progress bar increases by an extra 12.5% out of the total 100%. (Therefore, eight clicks will complete the progress bar to 100%) Currently, it updates the text content bu ...

Ensuring the mobile menu button is always visible on Bootstrap 4, regardless of screen size

Is there a way to consistently display the mobile menu button regardless of screen size? I want the links to only appear when someone clicks on the mobile button. Thank you! I am aware of a solution for Bootstrap 3, but it does not seem to work with Boots ...

Updating the value of a component asynchronously in Quasar

I am attempting to change the alias field once a promise is fulfilled. However, when I attempt to await the promise, Quasar displays an error message: [Vue warn]: Component <MainLayout>: setup function returned a promise, but no <Suspense> bou ...

Ways to include ajax information in the attribute of a td component

Within my ajax datatable initialization, I currently have the following setup: "ajax": { "url": "{{ route('cust_continfo_data_table') }}", "dataType": "json", "type": "POST", "data": { _token: "{{ csrf_token() }}", ...

What could be causing my Express.js response object to come back empty?

My current challenge involves authenticating the Spotify API using the OAuth2.0 client credentials flow. Here is an overview of the flow: I send a GET request to the /authenticate endpoint with required parameters, including a callback endpoint set to htt ...

Create a string representing the path to access a property within a nested object

I am dealing with an object that has nested properties: { x: { y: { z: { min: 1, max: 2 }, w: 5 } } } The levels of nesting can vary. It can end with an object containing properties like 'min' ...

Steps for triggering mutation when the state changes

My web app features multiple components in App.Vue, each displaying one at a time based on the value of the 'Step' variable in Store. Here's an example: <div class="hero-body main-content-container"> <Welcome id="intro" v-if="ste ...

When attempting to trigger an error, the unit test will fail to pass

I am relatively new to Mocha, Chai, and Unit Testing. I'm currently attempting to create a basic test to verify the presence of Authorization headers in the request that passes through my middleware function. Despite trying various approaches such as ...

Utilizing Bootstrap's inline-block feature for card layouts

My current challenge involves using Bootstrap 4 cards within a modal. Regardless of the number of cards I implement, they all end up in a single row. Ideally, I would like to have a maximum of 3 cards per row. Another issue arises when resizing the window, ...

Unable to open file downloaded from Laravel with Vue.js support

I am facing an issue with my function in Laravel and vue.js. Even though it successfully downloads the desired file, when I try to open it, I consistently receive an error message stating that the file type is unsupported. Below is the vue.js function I a ...

Techniques for transferring PHP print_r arrays to Javascript

Array ( [0] => Array ( [sno] => 1 [name] => Sivamani [contact] => 750241378 [$city] => Madurai ) ) Array ( [1] => Array ( [sno] => 2 ...