What is the best way to ensure that this encompassing div adjusts its width to match that of its child elements?

My goal is to keep fave_hold at 100% of its parent div, while limiting the width of .faves to fit its child elements. These elements are generated dynamically with predefined widths.

Below is the code snippet in React/JSX format:


<div id='fave_hold'>
  {this.props.MenuFave.current === '1' &&
    <div className='faves' id='fave_hold_arc'>
      {tags1}
    </div>
  }
  {this.props.MenuFave.current === '280' &&
    <div className='faves' id='fave_hold_news'>
      {tags280}
    </div>
  }
  {this.props.MenuFave.current === '268' &&
    <div className='faves' id='fave_hold_news'>
      {tags268}
    </div>
  }
</div>

And here is the corresponding CSS:


#fave_hold {
  width: 100%;
  height: 100%;
}

.faves {
  height: 100%;
}

I have experimented with different display properties without success.

For a visual reference, please see the screenshot below:

https://i.sstatic.net/ORH19.png

The ultimate objective is to centrally align the content using this approach.

To explore the interactive prototype, click on the following link:

Answer №1

Your kids have been styled with a float, causing them to be outside of the normal flow, resulting in their size being ignored by the parent element.
I suggest taking a different approach by removing the float and utilizing flex instead.

.faves {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
}

You should also eliminate the position:relative and 'float' from the .bookmark_page class.
This will leave you with something like this:

 .bookmark_page {
    border-radius: 4px;
    width: 250px;
    margin: 6px;
    border: 6px solid white;
    box-shadow: 0px 1px 3px rgba(34,25,25,0.4);
    -webkit-box-shadow: 0px 1px 3px rgba(34,25,25,0.4);
}

These adjustments should yield a result similar to this:
https://i.sstatic.net/kSD2m.jpg

For smaller screens: https://i.sstatic.net/VsHuA.jpg

Edit

How can I remove the clutter at the bottom of each child element now?

I see that you were referring to the "clutter" at the bottom of the children's elements.
To address this, modify .faves as follows:

.faves {
    display: flex;
    align-items: flex-start;
    flex-wrap: wrap;
}

Consider slightly increasing the width of the children's elements (.bookmark_page) to maybe 260px.

The outcome:

https://i.sstatic.net/IRNMx.jpg

Answer №2

Applying display: inline-flex to your favorites can be a game-changer.

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 sluggish rendering speed of AngularJS is causing a delay in displaying the loader

Within my application, I have tabs that load a form in each tab with varying numbers of controls. Some tabs may contain a large number of controls while others may have fewer. My goal is to display a loader while the contents are being rendered into the HT ...

Struggling with my jQuery Ajax call, need some help

I am attempting to create an ajax request that will update the content of my select element. Below is the code for my request : $(function() { $("#client").change(function() { type: 'GET', url: "jsonContacts. ...

Dividing a collection of URLs into smaller chunks for efficient fetching in Angular2 using RxJS

Currently, I am using Angular 2.4.8 to fetch a collection of URLs (dozens of them) all at once. However, the server often struggles to handle so many requests simultaneously. Here is the current code snippet: let collectionsRequests = Array.from(collectio ...

Steps to retrieve the selected value from a set of Radio Buttons using ReactJS

I am currently working with a set of 4 radio buttons and my goal is to retrieve the value of the selected button. It is crucial that I validate whether or not the user has actually chosen one of the radio buttons. Below, you will see how I am approaching t ...

Unable to activate button click event using jQuery

I am using dot.js to enhance a specific webpage by adding a button that, when clicked, should insert text into a text field and then trigger another button to be clicked as well. To achieve this functionality, I have implemented a click handler for my butt ...

"Enhancing HTML table rows using jQuery for a dynamic user experience

I'm currently using a jQuery script to determine the number of rows in a table and then display that count within a <td> tag. <tr> <td id = "jquery"> </td> <td> <%= f.text_field :data1 %> </td> <td ...

Using Vue.js to pass an image URL as a prop for CSS animation by converting it to a CSS variable

Need help with a component that features a hover animation displaying 4 rotating images: animation: changeImg-1 2.5s linear infinite; @keyframes changeImg-1 { 0%, 100% { background-image: url('images/wel1.png'); } 25% { background-image: ur ...

Showing additional content in an alternative design

I'm currently facing an issue with the "load more" post button on my Wordpress site. I've designed a unique grid layout for the category page, with a load more button at the bottom. However, when I click the button to load more posts, they appear ...

"Is there a way to extract a value from a JSON object using

I have an object with the following key-value pairs: { 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier': '918312asdasc812', 'http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name': 'Login1&a ...

Placement: fresh column in absolute position

Could someone please help me understand this situation? I have posted my code on jsfiddle. I am using Bootstrap 4. In my design, I have text placed over an image with the parent div having position:relative. The left div has position:absolute and left:2%, ...

Struggling with Selenium as I attempt to click multiple "Remove" buttons, constantly encountering the StaleElementReferenceException error

I am facing a challenge when trying to remove multiple elements on a webpage. My current approach involves locating all the "Remove" buttons and clicking them one by one. However, I keep encountering a 'StaleElementReferenceException' as I remove ...

Having trouble setting up react-i18n with hooks and encountering a TypeError: Cannot read property '0' of undefined?

Encountering an error while setting up the react-i18n with hooks: TypeError: Cannot read property '0' of undefined Here's the content of i18n.js: import i18n from 'i18next'; import { initReactI18next } from 'react-i18next/h ...

Guide on assigning JSON response values to TypeScript variables in Angular 4

I'm just starting with Angular 4 and I'm attempting to retrieve a JSON value using http.post. The response I'm receiving is: {"status":"SUCCESS"} component onSubmit(value: any) { console.log("POST"); let url = `${this.posts_Url}`; t ...

Issue with a stationary directional light tracking the movement of a rotating object and/or changes in the camera perspective

I've been facing a challenge in implementing a day-night cycle with a directional light in an Earth model using custom shaders. Everything seems to work fine with the night and day maps, as well as the light, as long as I don't manipulate the cam ...

Is there a way to ensure that this daily countdown timer continues without resetting at midnight?

Important factors to remember: This is programmed to imitate the time of 11:00 PM. var bt = "23:00"; This is set to emulate 8:00 AM. var wt = "08:00"; The intended operation: The countdown timer kicks off each morning at 8:00 AM. It counts down unti ...

Using for loop in AJAX Done() function

I'm currently working on a for loop that loops through an array consisting of 3 different report numbers. For each value in the array, an AJAX request is sent out. What I want to achieve is having a unique behavior for the .done() function based on th ...

react-three fiber reveals: "Error: Div is not recognized within the THREE namespace. Ensure you have properly extended the object."

Trying to implement a scroll area in the app I am developing has been a challenge. I keep encountering the following error: Error: Div is not recognized within the THREE namespace! Have you forgotten to extend? Being new to three.js and incorporating Re ...

Patience is key when using JavaScript

I have a JavaScript function that is responsible for updating my data. When the user clicks multiple times, I need to wait for the second click until the first one has finished processing, and so on. $scope.isLastUpdateFinished = true; $ ...

Internet Explorer (on mobile devices) does not display submenus when touched

Hello, I have a query about displaying a sublist on touch. I have created a sublist that is supposed to show up when you hover over it. You can view the demo here: @import url(//fonts.googleapis.com/css?family=Titillium+Web:400,200,300,600,700); body { ...

Protecting URL Parameters in Next.js Version 14: A Guide

Is there a way to hide query parameters in the URL and display a custom URL during navigation in Next.js 14? For instance, changing /user/12 to just /user. Any assistance would be greatly appreciated. import { useRouter } from 'next/navigation'; ...