How to properly size a child div inside a parent container

I'm having trouble with sizing a child div inside a parent div. The problem is that the child div's size changes according to the number of elements it contains, but I want all the child divs to be the same size regardless. This issue arises within the .container element. When my array returns 6 items, everything works fine. However, if the array has less than 6 items, each {styles.object} gets resized based on the available space in the container. Typically, when fewer than 6 items are returned, each item becomes larger and takes up more space. But I need all the objects to be the same size even if there are less than 6 items.

    var textContainer = <div className={styles.text_container}>
    <div className = {styles.text}>Experts</div>
    <span className={styles.view}onClick={() => handleNextClick()} >View More</span>
    </div>
    var displayData = <div className = {styles.container}>
            {array.slice(0,6).map((item,i)=>{
                return (
                    <>
                        <div className ={styles.object} key={i}>
                            <img className ={styles.img} src={item.image}></img>
                            <div className={styles.name}>{item.name}</div> 
                        </div>
                    </>
                )
            })}
</div>
return (
<>  
    {textContainer}
    {displayData}
</>

.text_container{
    display: flex;
    margin: 50px;
    margin-left: 150px;
    margin-bottom: 10px;
    width: 80vw;
    height: 3vh;
    padding: 0;
    align-items: center;
}
.text {
    display: inline;
    font-size: 20px;
    font: url('../../public/fonts/Lato-Bold.ttf');
    letter-spacing: 0.24px;
    flex: 1;
}
.view{
    display: flex;
    font-size: 14px;
    font: url('../../public/fonts/Lato-Bold.ttf');
    cursor: pointer;
}
.container{
    display: flex;
    margin: 50px;
    margin-left: 150px;
    padding: 0;
    width: 80vw;
    height: 28vh;
    justify-content: flex-start;
}

.object{
    display: flex;
    margin-right: 30px;
    width: 22vw;
    height: 25vh;
    position: relative;
}
.name{
    margin-top: 0;
    font-size: 14px;
    width: 100%;
    position: absolute;
    top: 102%;
    text-align: center;
}
.img{
    width: 100%;
    height: 100%;
    border-radius: 50%;
    cursor: pointer;
}

Answer №1

To optimize the layout, consider setting both a minimum and maximum width for the child elements

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

Can the concept of partial class be used in an AngularJS controller?

Is it possible to organize code into groups using partials in angularjs? The issue is that my controller has become too cluttered with a lot of code for different methods and functionalities, making it difficult to read. I want to maintain the same contro ...

Selecting options from a pop-up menu

I've created a dropdown menu with what seems to be correct code. However, there are no errors showing up, but the selected item from the menu is not alerting as expected. Here is the jsfiddle link $(document).ready(function () { // This function ...

Is there a way to make my red div switch its background color from red to green when I press the swap button?

How can I make the red div change its background color to toggle between red and green when I click on the swap button in the following code? $(document).ready(onReady); var numberOfClicks = 0; function onReady() { console.log('inside on ready ...

The React application is being served behind an NGINX reverse proxy, causing the CSS to be displayed as

My react app is hosted with a CSS file and running behind nginx/1.23.0 in a nginx container. The application loads successfully, but the CSS styles are not being applied. I have ensured that the CSS file is loaded, as seen here: I also tried using "incl ...

The challenges with implementing makeStyles in React Material UI

const useStyles = makeStyles((theme) => ({ toolbarMargin: { ...theme.mixins.toolbar, marginBottom: "3em", }, logo: { height: "7em", }, tabContainer: { marginLeft: "auto", }, tab: { ...theme ...

What's the reason the element inside the <div> is not displayed when a value is selected in the dropdown box?

I am perplexed as to why the element inside the does not appear when a value is selected in the drop down box. However, it works perfectly fine in JSFiddle: JSFiddle Below is my HTML code and Jquery script that functions properly in my own system: <t ...

Expanding the properties of an object dynamically and 'directly' by utilizing `this` in JavaScript/TypeScript

Is it possible to directly add properties from an object "directly" to this of a class in JavaScript/TypeScript, bypassing the need to loop through the object properties and create them manually? I have attempted something like this but it doesn't se ...

Can we execute a function once a mandatory field in a form has been validated successfully?

Looking for a way to execute a function after validating required inputs in a form before submission. Any suggestions? And I'm specifically not using jQuery, but Angular ...

Managing the AJAX response from a remote CGI script

I'm currently working on a project that involves handling the response of a CGI script located on a remote machine within a PHP generated HTML page on an apache server. The challenge I am facing relates to user authentication and account creation for ...

The element type provided is incorrect: it should be a string or a class/function, but an object was given instead

Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. I'm currently working on an application using React JS with Spring Boot. I have successfully configured webpack, but ...

jsTree unable to locate node using the provided ID

Implementing the jsTree on the webpage is not a problem for me. I have experimented with numerous suggestions from different sources. $('#myTree').jstree({ .... }) .on('loaded.jstree', function (e, dta) { var t = $('#myTree&a ...

What is the process for creating a new web page? [Exploring web design]

When creating a new page, how exactly does it work? For instance, on a website that allows users to create posts, you may see a link like this: example.com/post/randomised123 I'm curious - does this process also involve server directories like pop ...

Can a webpage be redirected to another page after an animation using only HTML and CSS?

My goal is to design a landing page that has a button allowing users to trigger an animation before being redirected to another page. This concept involves adding a captivating element before transitioning between pages, similar to a link with a dynamic ...

Combining D3.js tooltip positioning with CSS overflow styling

I am encountering an issue with displaying tooltips in D3 when combined with CSS overflow. Is there a solution available for this particular problem? CSS code: #chart1 { overflow-x: auto; overflow-y: hidden; max-width: 800px; max-height: 22 ...

Am I making a mistake in my implementation of sending Socket.io messages to a designated room?

Upon a user joining, I alter their ID to a shortened random code. This change is made to utilize the id as a visible session ID on the front-end. I mention this to prevent confusion regarding non-standard socket IDs and their relation to potential issues. ...

Passing a parameter from AJAX to PHP

const studentNumber = $('#studno').val(); $(document).ready(function () { $('.go').click(function () { $('.inup').load('prochat.php', { studno: studentNumber }); }); }); I've ...

The response received from the server was a 404 status () indicating that the resource failed to load

I'm encountering an issue while loading from the web service, and receiving the following error: Failed to load resource: the server responded with a status of 404 () Your help would be greatly appreciated. You can also access the code on JSFiddl ...

Embed HTML code into a React/Next.js website

I've been given the task of enhancing the UI/UX for an external service built on React (Next.js). The company has informed me that they only allow customization through a JavaScript text editor and injecting changes there. However, I'm having tro ...

PHP/AJAX user action history manager

Is there a library available that offers undo/redo functionality with a complete history for a web application? One possible solution could be a system using php/javascript/ajax where you can record the opposite action and variable state for each user acti ...

The SWR library was called with an invalid hook

Attempting to make a delete request using the SWR library within a react hook. I understand that hooks cannot be called inside return(). How can I carry out a delete request using useSWR in my react hook project? Thank you. const page = () => { func ...