How to correctly position a modal in a React application using CSS

Currently, I am working on integrating the react-modal NPM library into a React Typescript project. The issue I am facing is that the modal occupies the entire width of the screen by default, with padding included. My goal is to have it display as a small popup in the center of the screen, yet I am struggling to achieve this as it seems to use absolute positioning by default.

Could someone please provide guidance on how I can modify the inline style here to ensure that the content section is positioned centrally within the overlay?

<ReactModal 
        isOpen={this.state.dialogOpen}
        contentLabel="Example Modal"
//        portalClassName="ReactModalPortal"
//        overlayClassName="ReactModal__Overlay"
//        className="ReactModal__Content"
//        bodyOpenClassName="ReactModal__Body--open"
        shouldCloseOnEsc={true}
        shouldReturnFocusAfterClose={true}
        role="dialog"
        onRequestClose={this.handleCloseModal}
        shouldCloseOnOverlayClick={false}
        // tslint:disable
        parentSelector={() => document.body}
        style={{
          overlay: {
            background: "rgba(0, 0, 0, 0.45)"
          },
          content: {
            width: 600,
            height:250,  
            position: "absolute",
            top: "30%",
            left: "30%",
            right: "40px",
            border: "1px solid #999",
            background: "rgba(54, 58, 67, 1)",
            overflow: "auto",
            WebkitOverflowScrolling: "touch",
            borderRadius: "6px",
            outline: "none",
            padding: "20px"          
          }
        }}

Answer №1

Here is the recommended CSS code to use:

.container{
  width: 600px;
  height: 250px; 
  position: absolute;
  z-index: 999;
  top: 30%;
  margin-left: calc(50% - 320px);
  border: 1px solid #999;
  background: rgba(54, 58, 67, 1);
  padding: 20px; 
}

Visit JsFiddle for demo

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 is the best way to utilize register in useForm with dynamic parameters?

I am currently struggling to create a form using react-hook-form due to the major changes in version 7. The code I referenced is no longer working, and I believe the issue lies with dynamically passing the name parameter for registering. Here is my main c ...

React Hook Form is experiencing an excessive amount of re-renders which can lead to an infinite loop. React sets a limit on the number

Currently, I am working on displaying a field named party. Once this field is selected, a list of products should be rendered. In my project, I am using React Hook Form along with the watch hook to keep track of changes. <FormProvider {...methods}> ...

Get rid of angular comments in an HTML document

Is it feasible to eliminate or deactivate the HTML comments generated by Angular? <ol class="items"> <!-- ngRepeat: item in list | orderBy:'time':true --> </ol> This is disrupting CSS rules that utilize the :empty pseudo c ...

How to upload images with React and Node.js

Whenever I upload an image using Postman, the API functions properly and successfully uploads the image. However, when attempting to send data from the front-end (React) to the backend (Node.js), I encounter this error: Cannot read property 'path&apos ...

Enhancing CSS to create a more visually appealing loading spinner

I am completely new to CSS and I've been attempting to customize the CSS in the angular-loading-bar module to create a more visually appealing loading spinner. Currently, I have a square spinner that rotates in the center of the page, with a black bor ...

I would like to apply my CSS styles to all the hyperlinks on my website

This is the CSS code I created: img{width:85%; height:auto;} #thumbwrap { position:relative; } .thumb img { border:1px solid #000; margin:3px; float:left; } .thumb span { position:absolute; visibility:hidden; } .thumb:hover, .thu ...

Enhancing ag-grid with alternating row group colors following row span

My data structure is shown below: https://i.stack.imgur.com/fy5tn.png The column spanning functionality in ag-grid is working for columns 1 and 2 as expected. However, I am looking to implement alternate row colors based on the values in column 2 (animal ...

Issue with Jest Testing: React Fragment Not Being Rendered

Currently, I am facing an issue while testing components related to rendering the rows within a material-ui table especially when using React Testing Library. To create the table rows of a material-ui table, I am utilizing react fragments to iterate throug ...

After updating the External SS, the background vanishes

There appears to be an issue with a page that I designed. I originally wrote the CSS code internally within the <style type="text/css>...</style> section, including a background image, and all was working fine initially. However, after transf ...

Learn how to implement media queries using CSS3 with an HTML5 form

Struggling to implement Media Query in my HTML5 form that was styled using CSS. Any assistance would be greatly appreciated. <div class="form"> <form action="Login.jsp" method="post" > <input type="text" name="username" placeholder="usernam ...

Ways to style specific dates on a datepicker?

Is there a way to customize the appearance of the first and last selected days in my ui datepicker for booking, similar to the design shown in the image linked below? var dateFormat = "DD/MM/YY", from = $("#checkin,. ...

What is the best way to retrieve props within a Component in NextJS?

I'm relatively new to NextJS and React, and I've set up a sample app to familiarize myself with the process. I'm encountering an issue where I am unable to access props that I passed to a component within the return() function. Below is an e ...

Tips for simulating the useState hook in RTL testing

This is a unique component. const AnalyticsComponent = () => { const [isLoading, setIsLoading] = useState(true); const handleAnnouncementsClick = () => { navigate('/applications/announcements'); }; return ( <DashboardLa ...

What is the best way to show a label and select box side by side using Angular Material?

I have been trying to keep input and label in the same line using Angular Material, but I haven't found a solution yet. Most of the available solutions are in HTML and CSS, but I specifically need a way using Angular Material. The method I tried invo ...

Shuffle contents of a Div randomly

I'm currently in the process of developing a new website and I need to randomly move the news feed. To achieve this, I have created an array containing the list of news items. The array is then shuffled and placed within the news feed section. Althou ...

Tips on aligning text to the left on mobile devices using CSS and HTML

When viewed on a standard 16:9 computer screen, everything looks perfect with text on the left and an image on the right. However, when it comes to smaller screens like phones, the picture appears on top of the paragraph instead. I am new to this, so any a ...

I aim to store a string as a variable by extracting it from an Axios response

Currently, I am developing a straightforward food ordering application using Dot Net 6 and React. Within the app, there is a table that displays a list of different foods along with their respective details, including the restaurant that serves them. I w ...

CSS Switchable Style Feature

I am in need of some assistance with the navigation on my website. You can find the current code at this link: http://jsfiddle.net/Sharon_J/cf2bm0vs/ Currently, when you click on the 'Plus' sign, the submenus under that category are displayed bu ...

Retrieve data from each URL listed in a JSON array using React

My json file structure was as follows: [ { "name": "google", "route": "/google", "url": "www.google.com" }, { "name": "bing", "route": " ...

What is the best approach to incorporate this feature in a React application?

I am planning to create a webzine website using React. I am looking to achieve the functionality where clicking on the index button will enlarge the box size and reveal a hidden menu bar, similar to the images below. https://i.stack.imgur.com/S4kUg.png ...