How can I ensure that my content stays fixed at the bottom of a scrolling div in Material UI React?

I have developed a React JS component using Material UI that includes a chat input feature. However, I am facing an issue where the width of the chat input is always half the screen size on desktop or mobile devices. When I try to change the width to 100%, it goes off-screen.

<> <Box style={{backgroundColor:'red'}}> {relativeUserinfo.name}

                    </Grid>
                </Grid>
            </Box>
            <Box sx={{ overflowY: "scroll",backgroundColor:'blue' }}>
                <List sx={{ width: '100%', bgcolor: 'background.paper' }}>
                    {Array.isArray(messageDetails) && messageDetails.map((message) =>
                        <>
                            <ListItem key={message._id}>
                                <ListItemAvatar>
                                    <Avatar>
                                        <PersonOutline />
                                    </Avatar>
                                </ListItemAvatar>
                                <ListItemText primary={getChatUsername(user, message.relativeUserId)} secondary={message.messageText} />
                                <span style={{ color: "grey", fontSize: '10px' }}>{formatDistanceToNow(new Date(message.createdAt))} ago</span>
                            </ListItem>
                        </>)}
                </List>
                <Input
                    style={{ backgroundColor: '#EEEEEE', padding: '10px' , width: 'inherit',position: 'fixed', bottom: '0'}}
                    value={message_input}
                    onChange={(e) => setMessageInput(e.target.value)}
                    type="text"
                    inputProps={{
                        style: { padding: "10px" },
                    }}
                    disableUnderline
                    fullWidth
                    placeholder="Type message...."
                    startAdornment={
                        <TagFacesIcon
                            sx={{
                                paddingLeft: "20px",
                                color: "#777",
                            }}
                        />
                    }
                    endAdornment={
                        <IconButton sx={{ paddingRight: "20px", }} onClick={handleMessageSubmit}>
                            <SendIcon
                                sx={{ color: "#777" }}
                            />

                        </IconButton>

                    }
                />
                
            </Box> */}
            </Box>
            

        </Box>
    </>

when I am changing width to 100% this is happening,and chrome mobile screen it disappears,event though I did not any fix height to any of div

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

Answer №1

The issue with using width: 100% is due to the element having a fixed position. Since it is not relative to the container <Box/>, setting width: 100% will make it take up 100% of the body's width instead of the Box/container. This is usually around the size of the screen. It is recommended not to use fixed positioning in this situation.

To solve this problem, you can either utilize margins:

.box-class, .input-class { position: relative; width: 100%; }
.list-class { margin-bottom: auto; } /* Move following elements to the bottom */

/* or.. */
.input-class { margin-top: auto; } /* Move it and following elements to the bottom */

or better yet, implement Flexbox:

.box-class { display: flex; flex-direction: column; height: 100%; }
.list-class { flex-grow: 1; } /* Expand to fill available space */
.input-class { position: relative; } /* Width will automatically adjust to the full width of the box, add width: 100% if needed */

Ensure that your containers have the desired height/width, for example:

html { height: 100%; width: 100%; }
body { min-height: 100%; width: 100% }
/* repeat for other containers as needed, preferably with position: relative */

I hope this information proves useful!

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

Mastering the Art of Displaying Every Side of a Spinning Cube Using HTML and CSS Animations

As I navigate the online world, my goal is to create a dynamic animation of a rotating cube with an image on each face. Despite my efforts, I am struggling to display all faces simultaneously, especially the front and back faces. I have explored various so ...

Designing an interactive HTML table that adapts to various screen

Currently utilizing Bootstrap to create a responsive HTML table on smaller devices like the iPad, but seeking a more polished and professional alternative. Searching for a JQuery/JavaScript or CSS solution without relying on plugins. Would appreciate any ...

Create an array of objects in JavaScript using JSON data

I have fetched a collection of dictionaries from my API: The data can be retrieved in JSON format like this: [{"2020-03-11 14:00:00":10.765736766809729} ,{"2020-03-11 15:00:00":10.788090128755387}, {"2020-03-11 16:00:00":10.70594897472582}, {"2020-03-11 1 ...

Align title text to the right within the Material Design Card

I have implemented a Material Design (MDL) card to showcase numeric values in the title. However, I am facing an issue where the values are left aligned by default and I want them to be right aligned. Despite trying various styles, they are being ignored ...

Adjustable height within embedded object tag

I've been struggling to adjust the height of the content on my site automatically. I have attempted using iframes as well, but nothing seems to work despite trying numerous code examples from various sources including CSS and JS scripts. Any help wou ...

Integrating Flask with React for a cohesive frontend and backend connection

Recently, I decided to try my hand at setting up a webapp using Flask and React by following a YouTube tutorial. Despite encountering a few issues with virtual environments (which I ultimately resolved by not using one), I managed to closely follow the tut ...

If the HTML attribute "dir" is set to "rtl", then add a class to the body

I have a website that supports both English and Arabic languages. When the language is changed to Arabic, the "dir" attribute with a value of "rtl" is added to the HTML <html dir="rtl">. I want to add a class to the body element when the language i ...

Resize a div within another div using overflow scroll and centering techniques

Currently, I am working on implementing a small feature but am facing difficulties with the scroll functionality. My goal is to zoom in on a specific div by scaling it using CSS: transform: scale(X,Y) The issue I am encountering lies in determining the c ...

Consistently directing to a single page on the Node Js web server

I'm in the process of creating a website with multiple HTML pages. Currently, I have code that successfully links to the login page, but unfortunately, the localstores page also directs to the login page. const express = require('express') ...

Ways to change the CSS styles of components within App

When my app is in full screen mode, I need to increase the font size for certain components. Within my App.jsx file, I have a variable that adds the "fullscreen" class to the root DIV of the app when triggered. Instead of using a blanket approach like * ...

Unable to showcase array JSON values on HTML using ng-model

Utilizing ngTagInput for autocomplete textbox and successfully receiving suggestions. To display the values of data-ng-model (named "list") I am using: {{list}} and it is showing correctly. However, when selecting "list1", the display appears as: [{"lis ...

Method for transferring data to React frontend following activation of email link

Creating an application using ReactJS and Django REST Framework where users can register has been my latest project. The registration process involves sending the user an email activation link upon form submission. When clicked, this link redirects the use ...

Using react-scripts can create security vulnerabilities

I encountered an issue in the terminal and uninstalled react-scripts in an attempt to fix it. However, upon running npm install react-scripts, I received the following warnings: Cole@Coles-MacBook-Pro-4 client % npm i react-scripts npm WARN deprecated @ha ...

Pause the React rendering process to initiate a redirection

Currently, I am developing a React application using Next.js and facing a specific challenge that requires a solution: There are certain pages in my application that should only be accessible once a particular context has been established. To achieve this ...

What is causing my Card List Component to constantly re-render after every change?

Understanding the concept of useEffect has been a bit tricky for me. However, in order to address some warnings I was receiving, I decided to restructure my functions using useCallback. Surprisingly, this helped resolve the warning problem. The issue now i ...

jQuery counter no longer updates when scrolling

I'm having trouble with my jQuery counting function when using the scroll feature on a specific div ID. The numbers freeze if I keep scrolling before they finish updating on the screen. Whenever I scroll to the defined div ID, the counting function k ...

Removing the navigation button from the hamburger menu

I am working on creating a semi-progressive top navigation bar. For the mobile viewport, the navigation bar will only display the logo and a hamburger button. When the button is clicked, various navigation menu options as well as the Log In and Sign Up bu ...

jQuery offset(coords) behaves inconsistently when called multiple times

I am attempting to position a div using the jQuery offset() function. The goal is to have it placed at a fixed offset from another element within the DOM. This is taking place in a complex environment with nested divs. What's puzzling is that when I ...

The style from 'http://localhost:2000/cssFile/style.css' was rejected because its MIME type was 'text/html'

Currently, I am attempting to include my style.css file in the home.ejs file being rendered by express.js. However, I keep encountering the following error: Refused to apply style from 'http://localhost:2000/cssFile/style.css' because its MIME t ...

Tips for evaluating style modifications on mouseover using cypress

Trying to test hover styles on table rows but encountering issues: Here is the code snippet I am testing: it("Table rows should display correct hover styles", () => { cy.get("table>tbody>tr").each(($el, index, $list) =&g ...