DEV Community

shethastha1
shethastha1

Posted on

How to create a modal from a div from scratch in Reactjs using CSS?

I have tried finding this solution online which shows to use position absolute, top, left, bottom and right 0, and some background color which gives overlay kind of effect, but position absolute will not work for my case.
Please find my code snippet below:

The JSX:

        Add new birthday
Enter fullscreen mode Exit fullscreen mode

The CSS:
.buttonContainer{
display: flex;
flex-direction: row;
}
For modal, using modalStyles.hidden and visible by toggling it through a useState
export const modalStyles = {
hidden: {
display: "none" /* Hidden by default /,
width: "100%" /
Full width /,
height: "100%" /
Full height /,
overflow: "auto" /
Enable scroll if needed /,
backgroundColor: "rgb(0,0,0)" /
Fallback color /,
backgroundColor: "rgba(0,0,0,0.4)" /
Black w/ opacity */,
},
visible: {
display: "flex",
zIndex: 1,
background: "rgba(0, 0, 0, 0.3)",
},
};

What can be done such that the modal remains at its current position and I can see that overlay, ie without using position: absolute

Top comments (1)

Collapse
 
markmatute profile image
Mark Matute

I suggest you format your code inside a code block for better readability.