Struggling to position divs within a container div in a React component

Looking for help with aligning the divs with blue borders at the top? I need CSS code to achieve this. I tried using margin but it didn't work as expected. Also, when more content is added inside these divs, they seem to expand from the bottom to the top instead of the top to bottom.

I want the inner divs with blue borders to be aligned at the top and expand downwards when more text is inserted.

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

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

Below is the CSS code. The parent div with red border has a class of propertiesContainer, while the inner divs have a class of properties.

propertiesContainer: {
  paddingTop: "26px",
  background: " #EAEAFA",
  border: "1px solid red",
  opacity: 1,
},
properties: {
  bottom: "100px",
  margin: "19px",
  width: "auto",
  border: "2px solid blue",
  height: "262px",
},

Here is some Reactjs code related to this:

<section className={classes.propertiesContainer}>
  <HorizontalScroll
    itemClass="MuiGrid-root MuiGrid-item MuiGrid-grid-xs-12"
    data={
      mobile_footer_data &&
      mobile_footer_data.map((data, index) => {
        const { header, body } = data;
        console.log("dataa", data);

        const text = body.map((item, index) => {
          return <div key={index}>{item}</div>;
        });

        return (
          <div key={index} className={classes.properties}>
            <CommonHeaderText
              text={<span>{header}</span>}
              variant="h1"
              style={{
                fontSize: "16px",
                fontWeight: "bold",
                fontFamily: "Open Sans",
                color: "##363636",
                height: "19px",
                marginBottom: "25px",
              }}
            />

            <div>{text}</div>
          </div>
        );
      })
    }
  />
</section>;

Answer №1

When dealing with inline elements, the default value of vertical-align is set to baseline, causing them to align along the text baseline. To change this alignment within a child div, you can try applying the following CSS:

vertical-align: top;

        .outer {
            outline: 2px dashed #387779;
            padding: 10px;
        }

        .inner {
            width: 100px;
            height: 80px;
            display: inline-block;
            background-color: #a3af92;
            color: #26310a;
        }
        .vtop {
            vertical-align: top;
        }
    <div class="outer">
        <div class="inner">hello<br/>world<br/>abc</div>
        <div class="inner">abcd<br/>two</div>
        <div class="inner">123</div>
    </div>
    <br/>
    <div class="outer">
        <div class="inner vtop">hello<br/>world<br/>abc</div>
        <div class="inner vtop">abcd<br/>two</div>
        <div class="inner">123</div>
    </div>

Answer №2

Imagine your main div is labeled as "mainDiv" and it contains two sub-divs named "subDiv1" and "subDiv2" the css code for styling would be:

.mainDiv{
   display:flex;
   justify-content:space-between;
   align-items:flex-start;
}

Give this styling a try on the main div, hopefully it resolves your problem.

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 'Product' property is inaccessible because it is undefined and cannot be read

I've been having trouble figuring out what the issue is and where to find it. I can successfully send requests using Postman, but my model file is throwing an error for product.js while user.js is working fine. TypeError: Cannot read properties of u ...

Empty space encompassing the entire sheet

While developing my website, everything seemed normal until I added a CSS document to my HTML page. Now, when we open the website , there is a white space around the entire page. I attempted to set the body class to container-fluid since I am using Bootst ...

Utilizing Next.js for Image Optimization presents challenges when using the default loader with `next export` functionality

Exporting a Next.js web application to GitHub Pages is proving to be a challenge for me. When I try to run 'next export', I receive the following error message: "Nextjs: Image Optimization using Next.js' default loader is not compatible with ...

Utilizing the req.body object in a GET request with Node.js

I'm facing an issue with a form that utilizes the GET method. I have an input named 'a' and when handling the request on the server side (using Node.js), I need to access req.body.a to search for 'a' in the database. However, it se ...

Responsive Text Carousel in Bootstrap 4

My attempt to create a carousel with text inside is not turning out as expected. When I resize the screen or view the website on a smartphone, the text becomes misaligned. Can anyone advise on how to resolve this issue? I've searched numerous websites ...

Fixing the 'window is not defined' Error in Node JS through Command Line

Trying to incorporate Angular JS using 'require' in my code snippet: var ang = require('angular'); However, encountering an error: ReferenceError: window is not defined I am aware that the window object is not defined in the Node co ...

The jQuery click event will only trigger once the mouse has been moved, but the CSS hover effect will work properly even when the hover state is not active

I seem to be facing a similar issue like the one mentioned here: jQuery click event only working after moving the mouse in Chrome, but specifically on Mac OS X (tested on Chrome, Firefox, and Safari). The functionality works fine on Windows (skipping IE fo ...

Responsive Tabs with Material-UI

Can MUI's Tabs be made responsive? This is what I currently have: https://i.stack.imgur.com/KF8eO.png And this is what I aim to accomplish: https://i.stack.imgur.com/b3QLc.png ...

Numerous Material-UI Tooltip components that can be individually opened and closed with a click within a list

Looking for a solution: I'm working on a react component that contains a list of products. Each product has specific information that needs to be displayed in a tooltip when clicked. If you have any suggestions or solutions, please check out my code ...

Setting the thumbnail image for an HTML5 video: A step-by-step guide

Is it possible to set a thumbnail image for an HTML5 video? I would like to display an image before the video starts playing. Here is my current code: <video width="470" height="255" controls> <source src="video.mp4" type="video/mp4"> ...

Sharing data between parent and child components in React

I am facing an issue with passing a dynamically generated value from child components to my parent/web component. The buttons in the child component are assigned a `value="URL"` based on MongoDB data, and I want to pass that value to my parent component us ...

The API version header has not been accurately transmitted

Hey there, I've run into a strange problem. Everything seems to work fine when I make API requests using postman or insomnia. However, when I try the same code on my website or even locally on localhost, the leads request doesn't go through and t ...

The console is being flooded with API logging messages multiple times

My goal is to develop a search page for Pathfinder. I have crafted the following code in an attempt to retrieve data from the API. During the process of testing the fetch requests, I have noticed that when I console.log the fetched data, it appears multipl ...

Creating a new React Native project with specified versions of React and React Native can be done

I am trying to start a new react-native project using the [email protected] and [email protected] packages due to dependency issues. However, when I use the react-native command, it only allows me to specify the react-native version: react-nativ ...

Customizing the style of react-select is essential for creating a unique and visually appealing user experience

I am looking to maintain the visual style of my input fields to match that of a react-select component. I have been advised to use styled-components, yet I am uncertain about which styles need to be adjusted in order to achieve the desired outcome. Ideally ...

Styling the nested list items with text decoration

I have a scenario where I applied text-decoration: underline; to the parent li, which is being inherited by the child li. Despite trying to remove this style from the child elements using text-decoration: none !important;, it doesn't seem to work. The ...

Conceal the header on specific pages within react-router-dom

Is there a way to selectively hide the page header for certain routes in React Router? Currently, my issue lies in the fact that my App component renders my Main component, which includes my BrowserRouter, and my Header is rendered within the App component ...

Combining Bootstrap 3 buttons and input fields into a single row

My collection of buttons and input text box is currently misaligned, as shown in the code below. <div class="col-md-12 pull-right"> <div class="pull-right"> <button type="button" class="btn btn-default btn-xs"> &l ...

Tips for preventing the repetition of code blocks in node.js

Within my code block, I encountered a situation where the if and else blocks contain almost identical code. The only difference is due to one function call with a callback that requires repeated code in both blocks. While I am aware that creating a funct ...

Explore an associative array in a circular list format

I have a unique requirement to traverse through an associative array like a circular list. The associative array is structured as follows: array = {item1:array(...), item2:array(...), ...} When I reach the end of one array, I want it to seamlessly transi ...