Tips for aligning a div within another div using ReactJs

I'm having trouble positioning the children div at the end of the parent div in my code using reactJs, NextJs, and styled-components.

Here is the ReactJS code:

<a href={links[indexImg]} target="_blank" >
    <Carousel
        image={images[indexImg]}
    >
        <DescriptionText>
            <p>{descriptions[indexImg]}</p>
        </DescriptionText>
    </Carousel>
</a>

And here is the styled-components code:

export const DescriptionText = styled.div`
    color: white;
    background-color: black;
    background-position: 20px;
    opacity: 0.5;
`;
export const Carousel = styled.div`
    position: relative;
    border-radius: 50px;
    border: solid;
    border-width: 3px;
    border-color: #C2C2C2;
    margin: 0;
    height: 100%;
    width: 100%;
    justify-content: flex-end;
    background-size: cover;
    background-position: center;
    background-image:url(${props => props.image});
`;

Answer №1

When the .inner division is set to absolute position and the .outer division is set to relative position, the .inner division will adjust itself within the boundaries of the .outer division. For instance, if you want the .inner division to remain at the bottom of the .outer division, you can achieve this by using the following CSS:

.outer {
    position: relative
}
.inner {
    postion: absolute;
    bottom: 0
}

Answer №2

To start off, you can easily set up Bootstrap in your project by running a simple command in the terminal:

npm install react-bootstrap bootstrap

Assuming you have components like Leftsidebar.js, Rightsidebar.js, and Content.js that are imported into index.js,

you will need to install Bootstrap for your React.js application.

Here are the steps:

Below is the code snippet from index.js:

import React from 'react';
   import ReactDom from 'react-dom';
   import Leftsidebar from './Leftsidebar';
   import Rightsidebar from './Rightsidebar';
   import Content from './Content';
    
    ReactDom.render(
    
    // outer most div
    <div class='row'>
    
    <div class='col-4'>
    <Leftsidebar/>
    </div>
    
    
    <div class='col-4'>
    <Content/>
    </div>
    
    <div class='col-4'>
    <Rightsidebar/>
    </div>
    
    </div>
    
    
    ,document.getElementById('root');
    );

We utilized the grid system to split the screen into 4 parts out of 12. Additionally, it's worth noting that the above code is just an example typed in a text editor. The main goal here was to demonstrate how you can display data in different layouts or positions using React.js. I personally find using rows and columns the easiest method for arranging data in various positions.

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

How can state values be transferred between components?

I have come across various answers on different platforms but haven't been able to achieve the desired results. That's why I am reaching out with this question. So, my question is related to 3 files named: App.js, SignUp.js, and Welcome.js. My ...

Trigger the select dropdown when a key is clicked

I am currently working on a project in react where I am utilizing the Select component from material. When I open the dropdown and press a key on my keyboard, the option starting with that key gets automatically selected. This behavior is also observed in ...

Having trouble viewing the header in devtools for $http requests?

The data in the request header is not visible to me. Here's what I'm using: $http.post('http://localhost/api/validate', {}, { headers: key } ); https://i.sstatic.net/yioTT.png The form that gets passed during runtime is shown below: ...

Enhance the structure of information retrieved from the API

Recently I sought advice on formatting API data and received some excellent responses. However, I encountered an error when the API lacked data for certain assets: https://i.stack.imgur.com/HgJDd.png Here is an example without the highlighted code: http ...

Tips for implementing callback function in React hooks to handle submitted values

I am currently developing an app using React hooks. However, I encountered a problem where clicking the button twice intentionally can cause an error to occur. To prevent this issue, I want to disable the button while sending an API request using useStat ...

Unable to retrieve information beyond the scope of 'then' block

I am trying to work with a fetch request in my code where I POST to an Express server and then handle the response using '.then'. My goal is to pass the resulting JSON object to another component for export/import, but I am facing an issue as the ...

Is there a way to position two images using CSS, with one in the upper right corner and the other in the lower right corner?

I'm trying to use CSS to align two images, one in the upper right position and the other in the lower right position. Can you help me with this? Below is the HTML markup: <th class="X" scope="col"> Time <input id="ORAAsc" class="up" type="i ...

JavaScript organizes URLs based on their domain and directory

Is there a way to group URLs from a sorted list by domain and directory? When two URLs share the same directory (the first one after the domain), they should be grouped together in an array; If URLs have different first directories but the same domai ...

css how to style a table row with a specific identifier

Although this question has been asked millions of times on the internet, my English skills are not great and I struggle to find the right words to search. I have a table with a specific ID set up like this: <table class="tableClass"> <tr id="one ...

AngularJS mdDialog not supporting Tinymce functionality

I'm attempting to integrate the TinyMCE editor into an AngularJS mdDialog. Working Plunker: http://embed.plnkr.co/s3NsemdcDAtG7AoQRvLh/ Plunker with issues: http://embed.plnkr.co/fL8kGLl3b4TNdxW1AtKG/ All features are working fine except for the d ...

Are there any available tools for automatically creating CSS classes based on your HTML code?

Out of pure curiosity, I am wondering if there is a program or script that can automatically generate a style sheet (with empty values) based on the structure of your HTML document. Essentially, it would extract the IDs and classes you have set in your H ...

Utilizing jQuery's .slidedown alongside Prototype's .PeriodicalUpdater: A Comprehensive Guide

I am currently working on creating an activity stream that automatically updates with the latest entries from a database table every time a new row is added. Right now, I am using Prototype's Ajax.PeriodicalUpdater to continuously check for new entri ...

Set values for scope variables that are created dynamically

I am currently working on a solution to toggle dynamically generated rows of data. I have attempted using ng-init and passing it to a function, but I seem to be making a mistake somewhere and struggling to understand if this is even feasible. The issue see ...

Implementing full-height list elements using CSS

I'm still facing challenges with my lists... After receiving some assistance, I was able to create a horizontal list with perfect dimensions for my needs: each element taking up 33.33% width and adjusting its height based on the tallest element. I ac ...

Extract JSON data from a third-party website using JavaScript

I'm facing a challenge parsing JSON from an external website using JavaScript or jQuery for a Chrome extension. Specifically, I need to extract the number from an external URL with the JSON {"_visitor_alertsUnread":"0"} and assign that number to a var ...

Tips for preventing key/id issues in ReactJS and effectively passing props down from parent to child components

I keep encountering an obstacle when attempting to pass parent data down to the child component. Here is how I am approaching it: <%= react_component 'Items', { data: @items } %> Within my Items component, I make an ajax call, set state, ...

Converting a string to a date type within a dynamically generated mat-table

I am working on a mat-table that shows columns for Date, Before Time Period, and After Time Period. Here is the HTML code for it: <ng-container matColumnDef="{{ column }}" *ngFor="let column of columnsToDisplay" > ...

Deactivate the backface culling feature when loading an imported .obj file in three.js

I'm struggling with disabling wireframe rendering on the rear side of my vehicle. Any ideas on how to deactivate backface culling in wireframe mode for three.js? Check out this link for a helpful image ...

Managing the onChange event for a MaterialUI dropdown component

I'm encountering an issue with validating the MaterialUI TextField component (Country list) wrapped with Autocomplete. I am trying to use the onChange event to enable the Submit button when the country field is filled in. However, the problem arises w ...

Return ForwardRefExoticComponent and FunctionalComponent under certain conditions

I've developed a custom wrapper component called TextFieldWrapper for the MUI TextField component with unique styles and validations, which is utilized in multiple areas of the application. The issue arises when attempting to use this with the MUI To ...