How to center a div with a overlay in CSS?

I have created an overlay with a div on top of it, and I am currently struggling to center the div over the overlay. I have tried adjusting the left and right properties, but nothing seems to be working.

.request-estimate {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 99999;
        background-color: rgba(0,0,0,0.8);
        display: none;
}

.request-estimate-box {
        display: none;
        height: 400px;
        width: 40%;
        margin: 0 auto;
        background-color: #FFF;
        z-index: 99999;
        position: fixed;
        top: 15%;
        padding: 20px;
        border-radius: 5px;
}

Here is the CSS snippet

<div class="request-estimate"></div>
                <div class="request-estimate-box">
                        <h1>Request Free Estimate</h1>
                        <form action="" method="post">
                                <p>
                                        <label for="name">Name</label>
                                        <input type="text" name="name" id="name" class="form-control" />
                                </p>
                                <p>
                                        <label for="email">Email</label>
                                        <input type="email" name="email" id="email" class="form-control" />
                                </p>
                                <p>
                                        <label for="phone">Phone</label>
                                        <input type="phone" name="phone" id="phone" class="form-control" />
                                </p>
                                <p>
                                        <input type="submit" name="submit" id="submit" value="Submit" class="btn btn-default" />
                                </p>
                        </form>
                </div>

Answer №1

.request-estimate {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 99999;
  background-color: rgba(0,0,0,0.8);
  /*display: none;*/
}

.request-estimate-box {
  /*display: none;*/
  height: 400px;
  width: 40%;
  margin-left:-20%; /*add this = width/2*/
  background-color: #FFF;
  z-index: 99999;
  position: fixed;
  top: 15%;
  left:50%; /*add this*/
  padding: 20px;
  border-radius: 5px;
}
<div class="request-estimate"></div>
<div class="request-estimate-box">
  <h1>Request Free Estimate</h1>
  <form action="" method="post">
    <p>
      <label for="name">Name</label>
      <input type="text" name="name" id="name" class="form-control" />
    </p>
    <p>
      <label for="email">Email</label>
      <input type="email" name="email" id="email" class="form-control" />
    </p>
    <p>
      <label for="phone">Phone</label>
      <input type="phone" name="phone" id="phone" class="form-control" />
    </p>
    <p>
      <input type="submit" name="submit" id="submit" value="Submit" class="btn btn-default" />
    </p>
  </form>
</div>

Answer №2

Resolution: https://jsfiddle.net/gqqqeLfL/

request-estimate-box now fits within request-estimate.

Initially:

<div class="request-estimate"></div>
  <div class="request-estimate-box"></div>

Adjusted to:

<div class="request-estimate">
  <div class="request-estimate-box"></div>
</div>

Additionally, the position:fixed was removed, allowing margin-auto to function properly with a default position:relative.

Answer №3

Consider two possible solutions:

.request-estimate-box {
height: 400px;
width: 40%;
background-color: #FFF;
z-index: 99999;
position: fixed;
top: 15%;
left: 0;
right: 0;
margin: auto;
padding: 20px;
border-radius: 5px;
}

http://codepen.io/anon/pen/dPLxPJ?editors=110

Alternatively:

.request-estimate-box {
height: 400px;
width: 40%;
background-color: #FFF;
z-index: 99999;
position: fixed;
top: 15%;
left: 50%;
-webkit-transform: translateX(-50%);
transform: translateX(-50%);
padding: 20px;
border-radius: 5px;
}

http://codepen.io/anon/pen/ByEXyM?editors=110

Add a margin of half its width from the center, as suggested by Tambo.

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

Expanding CSS Grid to Occupy Remaining Area

I'm currently utilizing CSS grid to construct a basic 3 column layout. Here's a snippet of my code... .container { display:grid; grid-template-columns:1fr 1fr 1fr; } ...

Why am I unable to attach this to JQuery?

$("input").keydown(function(event){ if(event.keyCode === 13){ return false; } }); I need to prevent the default action when the "enter" key is pressed in any of my text input fie ...

Having trouble with Python Selenium CSS Selector? If your element is not visible, it

My goal is to search for all hyperlinks on a webpage that contain an XML download link and download them in a continuous loop. When I click on these hyperlinks, a form pops up that needs to be completed before I can proceed with the download. However, I ...

Position the image slider uniformly at the bottom using CSS (utilizing the Slick Carousel library)

I am utilizing the slick carousel library to create a unique and elegant slider. However, when using images with varying heights, I am encountering issues in aligning the images at the same bottom: https://i.stack.imgur.com/ab5s3.png Here is my code snip ...

Overriding Styles Set by an External CSS file

Let's assume that I have two different style sheets set up on my webpage: site.css: .modal { position: absolute; width: 200px; ... } ... reset.css: .reset * { position: static; width: auto; ... } Unfortunately, I don ...

What is the process for transforming a String into an HTML element within a Next JS Application?

I stored the product description as HTML elements in a Database, but when I try to render this data into a div, it displays as a String. I am looking to showcase all the data as HTML elements in my Next JS application. I attempted using JSON.parse, but unf ...

CSS PopUp positioning

Can anyone help me with applying the position top and bottom of header to the NewUser div? I've tried but it's not working. How can I add !important; to the CSS? Maybe something like $("header")!important} <div id="divNewUser" class="CreateUs ...

Catalog of items in Mustache

I am currently working on generating HTML content using the mustache template engine. However, I have encountered an issue when trying to render a list of objects. Below is an example of my object: var Prod={ "Object1": { "name": "name1", "cat ...

Assigning a class to an li element once the page has finished loading

I am facing an issue with my navigation bar where the links are dynamically loaded from a database using a foreach loop. Although the nav bar is static, I want to apply an 'Active' class to the link when it is currently active. Despite trying to ...

Tips for handling CSS loading delays with icons in OpenLayers markers

When using openlayers (v4.6.4) with font-awesome as marker icons, the icons do not display upon first load (even after clearing cache and hard reload). Instead, I see a rectangle resembling a broken character. It is only on the second load that they appear ...

Dynamically implementing event listeners in JavaScript to remove specific elements based on their

My website has a list of phones displayed as ul li. When a user clicks the "Buy" button, it should create a new li within another div called "ordersDiv". Users can delete their purchase from the cart by clicking "Remove", which should remove the li with ma ...

Hide the cursor when the text box is in focus

Is there a way to remove the cursor from a textbox when it is clicked on? I want the cursor to disappear after clicking on the textbox. jQuery(document).ready(function($) { $(function(){ $('#edit-field-date-value-value-datepicker-popup-0&ap ...

Why is the feedback section showing a horizontal scrollbar?

I'm puzzled as to why a horizontal scrollbar is appearing in the feedback section. I've examined the elements but can't seem to pinpoint the issue. ...

Cannot access mobile navigation due to expanded browser search bar

Greetings, all! Currently, as I am in the process of developing a website for my company, I have encountered an issue with the navigation menu, specifically on mobile devices, or more accurately, mobile browsers(most of them). The hamburger icon seems to ...

Having trouble assigning a value to ng-model following a mathematical operation

Currently, I am in the process of developing a filter that allows users to choose a property location based on specific parameters. The filter will allow users to select multiple criteria, and from there, I will perform arithmetic calculations to determine ...

What is the best way to extract the name from JSON data?

I have a list of items in my HTML with data attributes. When a user clicks on one of the items, I want to display or append a list of related suburbs for that selected item in another list. This is being achieved using ajax. <ul id="cities"> <l ...

Why does Next.js throw an error when using an additional <div></div>?

I'm new to exploring Next.js and I'm encountering an error when using the html tag twice. It works fine with just one container, but as soon as I add another one, this error pops up: Error: error: Unexpected token "div". Expected jsx i ...

when the window is scrolled a certain amount, use jQuery to scroll back in the opposite direction

Using jQuery for Background Image Position: $(window).scroll(function() { var scroll = $(window).scrollTop(); if (scroll >= 50) { $(".class").addClass("bgposi"); // $(".top").addClass("fixd"); // $(".logo").addClass ...

Issues with Iframe { height:70%;} in Internet Explorer 8 and Firefox causing display problems

In my website's design, I have a div structure with an iframe to load pages. The header remains at the top seamlessly The footer stays at the bottom perfectly The content is positioned in the middle flawlessly However, the iframe does not stret ...

Tips for fetching integer numbers in a string in JavaScript in sequential order starting from the left side

Within an input element, users are required to input the price of a product. <input size=10 type="text" id="prd_price" title="prd_price" name="prd_price"> Users have the ability to enter Currency Symbols along with other characters, and we cannot ...