【问题标题】:How do I make my modal cover the entire screen?如何让我的模态覆盖整个屏幕?
【发布时间】:2020-09-21 19:35:25
【问题描述】:

我正在构建一个排行榜网络应用程序,并且我正在尝试添加一个模式或“弹出窗口”,但我似乎无法正确处理。

这是它现在的样子: 如图所示,模态不会占据屏幕的整个宽度,导致屏幕的右侧部分没有被覆盖,模态本身也没有居中。 Screenshot of the problem

我的代码如下所示:

HTML:

<p>{{ num_matches_in_current_season }} matches have been played in season {{ current_season }} so far.</p>
<button type="button" name="end_season_btn" id="end-season_button" class="btn btn-secondary">End Season {{ current_season }}</button>

<div class="container confirm-window" id="myConfirmWindow">
  <div class="confirm-content">
    <p>End Season {{ current_season }} after {{ num_matches_in_current_season }} matches?</p>

    <form method="POST">
      {% csrf_token %}
      <input type="submit" name="end_season" class="btn btn-secondary" value="Yes. End Season {{ current_season }}">
    </form>
  </div>

</div>
</div>

CSS:

.confirm-window {
  display: none;
  position: fixed;
  z-index: 1;
  padding-top: 100px;
  left: 0;
  top: 0;
  width: 100%x;
  height: 100%;
  overflow: auto;
  background-color: rgb(0,0,0);
  background-color: rgba(0,0,0,0.4);
}

/* Modal Content */
.confirm-content {
  background-color: #fefefe;
  margin: auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

Javascript:

var modal = document.getElementById("myConfirmWindow");

var btn = document.getElementById("end-season_button");

btn.onclick = function() {
  modal.style.display = "block";
}

希望你们中的一些人能帮助我找出问题所在。提前致谢。

【问题讨论】:

    标签: css modal-dialog width


    【解决方案1】:

    position 属性指定用于元素的定位方法的类型(静态、相对、固定、绝对或粘性)。

    具有位置的元素:固定;相对于视口定位,这意味着即使页面滚动,它也始终保持在同一个位置。 top、right、bottom 和 left 属性用于定位元素。
    来源@https://www.w3schools.com/css/css_positioning.asp

    考虑到这一点,您应该将以下内容添加到您的 .confirm-window 类中

    .confirm-window {
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
    }
    

    更多@https://www.w3schools.com/css/css_positioning.asp关于位置属性

    【讨论】:

    • 感谢您的回答。您提出的解决方案使模态在屏幕上居中,但是我现在在页面的右侧和左侧都有一个边缘,该边缘没有被模态覆盖。
    • 在重现您的问题时,我没有遇到这个问题。如果您是 CSS 新手,您可能忘记重置默认浏览器样式行为。在 style.css 文件的 trop 处使用以下重置 *{margin:0;padding:0;box-sizing: border-box;} 这是我用来重现您的问题的当前代码笔的链接 codepen.io/amarinediary/pen/NWNEgJW
    • 嗯...谢谢。我会尽快调查。
    • 如果这回答了您的问题,请不要忘记投票并接受答案
    • 我发现了问题。建议的解决方案在我意识到我已将其放入限制 div 宽度的 Bootstrap 容器中之后就成功了。删除“容器”类并添加 right: 0;和底部:0;到 css 解决了它。谢谢@amarinediary
    猜你喜欢
    • 1970-01-01
    • 2016-09-14
    • 2018-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2021-05-09
    • 1970-01-01
    相关资源
    最近更新 更多