Steer clear of using absolute positioning in Material-UI and React for better styling and layout

Is there a way to achieve the same layout without using position: absolute for the "Hello There" button? I want to remove that CSS property while keeping the design intact. Any better suggestions? Feel free to check out my codesandbox:

CODESANDBOX -----> CLICK HERE

<MainContainer>
  <Stack spacing={1}>
    {products?.map((product) => (
      <ProductItem
        key={product.id}
        name={product.name}
        description={product.description}
      />
    ))}
  </Stack>
  <Stack
    alignItems={"center"}
    sx={{
      marginTop: 2,
      display: "flex",
      left: 0,
      right: 0
    }}
  >
    <Button variant="outlined" sx={{ bgcolor: "white" }}>
      Hello There
    </Button>
  </Stack>
</MainContainer>

Answer №1

While it may seem like a non-answer, my suggestion would be to reconsider removing the absolute position in this situation. Creating a fully relative layout would require a more complex structure where your dotted border exists "behind" other elements. Even if you were to create this "layer," you would still need to use a div with absolute positioning.

Your current use of position absolute is actually quite appropriate - it allows for responsiveness, containment, and solves the specific issues that position absolute is designed for.

If you have a specific reason for wanting to remove the absolute position, consider if there are other ways to address any negative effects it may be causing. There may be alternative solutions that can achieve the desired outcome without removing the absolute positioning.

Answer №2

Perhaps consider utilizing position: relative in conjunction with top,, however, I'm uncertain if that aligns with your objective.

  <Stack alignItems={"center"}>
    <Button variant="outlined" sx={{ bgcolor: "white", position: 'relative', top: 50 }}>
      Hello There
    </Button>
  </Stack>

Could you provide further details on the desired outcome?

I suggest sticking with position: absolute as you originally planned. It seems to suit your purpose of layering something atop without affecting the underlying box model.

Answer №3

Swap that out with position: fixed.
In case you encounter issues with fixed positioning, consider using a relative position and adjusting its top and margin-top styles.

<Stack alignItems={"center"}>
  <Button
    variant="outlined"
    sx={{
      bgcolor: "white",
      position: "relative",
      top: 96,
      marginTop: -10,
    }}
  >
    Greetings
  </Button>
</Stack>

Link to Codesandbox

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

What are the steps to achieve form styling using Bootstrap?

I am trying to achieve a specific style using the Bootstrap framework. I have been able to achieve something similar to what I want, but the issue is that the save button is not aligned to the right and is not taking up the available width. There is a gap ...

Only add table heading if there is available space, but refrain from doing so if it will disrupt the layout

My current challenge involves a table where I have successfully used white-space nowrap to prevent the heading text from wrapping. The issue arises when viewing the layout on smaller screen widths, as this nowrap setting causes the table to expand beyond ...

Conditional requirements for Ant Design form items are essential

Within my form, there is a password input field intended for creating and editing user information. This form retrieves initial values from an internal state. The code snippet looks like this: <Form.Item label="Password" name="pas ...

I'm noticing that my style.css changes are only showing up after a hard refresh, but then they revert back to an older version when I refresh normally

Apologies for the lengthy title. Here's the issue: I have a WordPress website with a custom theme and its corresponding child theme. Previously, updating the style.css file of the child theme would show the changes immediately after a website refresh ...

Unable to update bootstrap table in React hooksifecycle

I am having an issue with rendering a table after adding new data to it using react hooks. Initially, the data in the state renders correctly, but when I add more items, they do not appear in the table after re-rendering. The inputs consist of categories ...

Transferring information to a different component

I am trying to figure out if there is a way to pass data directly from a row in my MUI Table to another page using useNavigate instead of relying on localStorage. Any suggestions? <TableBody> {data && ...

Tips on concealing the overflow content within a Material UI table cell, rather than allowing it to wrap around

I have been trying to use the material UI Table component in order to display a table. The issue I am facing is that when the content in a cell exceeds the allotted space, instead of showing ellipses (...), the text wraps within the cell. I attempted to ap ...

React-dnd enhances the functionality of the MUI tree view

I've been using a Material UI v4 Treeview with react-dnd and everything works smoothly. However, when I recently upgraded to MUI v5 Treeview, the drag functionality stopped working - the item is no longer draggable. After comparing the two TreeItem im ...

I created a customized version of jQuery tabs, but I am looking for an external link to display the tab content and to style the original navigation

I've been experimenting with modifying a tabs script in jQuery that I came across. It seems like my attempt to enhance it has made things more complex than necessary. While I know creating tabs with jQuery is simple, I wanted to create my own version ...

Issue with Material UI: Invalid element type provided. Expected a string for built-in components or a class/function for composite components, but received null instead

Error: Element type is invalid. Expected a string (for built-in components) or a class/function (for composite components), but received null. Verify the render method of TransitionGroup. This specific issue arises only when using material-ui and importin ...

Encountered an error while trying to create a React project using create-react-app

I am struggling to set up a React demo using create-react-app on my Mac M1 with node -v 18.17.1. I keep encountering errors and would appreciate any help! Creating a new React app in /Users/gengyinshan/gys/kwr-chrome-extension. Installing packages. This m ...

Encountering this error for the first time - Uncaught Error displayed in the Console

I have been working on a ToDo list and it is almost complete. However, I have encountered an unfamiliar error in the console that is preventing me from creating the list. The error message reads as follows: OPTIONS http://localhost:4000/cpds/add net::E ...

Troubleshooting typescript error in styled-components related to Material-UI component

When using typescript and trying to style Material UI components with styled-components, encountering a type error with StyledComponent displaying Type '{ children: string; }' is missing the following properties import React, { PureComponent } f ...

Troubleshooting Problems with CSS `transform` in JQuery UI's `Slide` Transition

While implementing JQueryUI's slide transition on an element with a CSS transform, I encountered an issue where the top half of the element gets hidden during the animation. Is there a way to tweak either my JQueryUI animation or CSS to avoid this pro ...

The People and Groups selection dialog is displaying incorrectly in IE 10

I've been working on a Sharepoint web application and have created a site column of type “People or Group”. This should be automatically associated with a user selection dialog as shown below: However, as you can see in the image above, the users ...

A dynamic and versatile solution for achieving uniform column heights across different web browsers using child elements

Get straight to the point. I'm looking for a solution to create a responsive two-column layout like the one shown in the image below. https://i.sstatic.net/mHhfc.png The width ratio of the .left and .right columns should always be 75/25% relative t ...

Please provide guidance on using NPM to install dependencies in this Visual Studio Code project. Let's

Currently learning React and exploring this repository. After cloning and running NPM install, a lengthy output is generated: (Note: Stackoverflow only allows max 30,000 characters, original output was 300,000) The majority of the output is highlighted i ...

Exploring i18nNext integration with antd table in React

Presently, this is the UI https://i.stack.imgur.com/CMvle.png App.tsx import "./styles.css"; import MyTable from "./MyTable"; export default function App() { const data = [ { key: "1", date: "2022-01- ...

Accessing the "src" attribute of image tags

Currently working on a project to create a product display feature that showcases the selected image as the main one. I'm struggling with getting the image source in useState. Is passing it as a parameter to the function the best approach? As a React ...

I don't understand why this error keeps popping up. It seems that there are several `InputBase` components nested within a

Issue: The error message indicates that there are multiple instances of the InputBase component within a FormControl, causing visual inconsistencies. Only one InputBase should be used. I have tried enclosing my forms within the FormControl, but the error ...