我创建了一个在其顶部具有div的叠加层,现在我正试图将div置于叠加层的顶部居中,我试图在左右方向上乱七八糟,但没有任何效果。

.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;
}


这是CSS

<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>

最佳答案

.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>

关于html - CSS将div居中放置在叠加层上方,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29442313/

10-12 13:15