html - CSS 居中一个位于覆盖层之上的 div

标签 html css

我在上面创建了一个带有 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/

相关文章:

css - 文本输入响应宽度 - Bootstrap 3

html - 浏览器会在 SVG XML 中呈现命名空间 HTML 吗?

html - 页脚和页面底部之间的空间

javascript - 尝试实现类似于 jQuery 的东西

html - 可滚动 div 内的 div 不会扩展到内容宽度

html - Bootstrap 嵌入响应不适用于 css 列数?

html - 没有新的 css 加载

html - 响应式并排放置 6 个盒子

javascript - 根据 CSS 类更改 SVG 填充动画高度

css - 用于 NetBeans IDE 的代码 unminify 插件