html - 为什么模态背景与我的弹出模态重叠甚至设置了 !important 和 z-index?如何解决?

标签 html css

我有一个成功的消息弹出窗口。现在我想在 rgba(0,0,0,0.5) 下面添加一个半透明背景层,我使用了 CSS::before。但是背景 ::before { rgba(0,0,0,0.5) } 似乎与我的绿色弹出窗口重叠,即使我设置了 z-index背景:弹出窗口的 !important

这是我试过的代码

.alertMessageModal{ width:auto; height:auto; border-radius:10px; display:inline-block; position:fixed; top:20px; right:25px; color:#fff; padding:10px 15px; padding-bottom:15px; z-index:100; background:green !important;}
.alertMessageModal::before{content:''; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0, 0, 0, 0.5); z-index:-100;}
.alertMessageModal .modal-title{ font-size:15px; float:left; line-height:18px;}
.alertMessageModal .close{ float:right; margin-left:10px; line-height: 15px; color:#fff;}
<div class="alertMessageModal bg-success">
  <h4 class="modal-title">Your message has been successfully sent</h4>
  <button type="button" class="close">&times;</button>
</div>

最佳答案

您需要将模态框的内容包装在另一个额外的包装器 div .modal-content 中。 example from w3school .

您的代码没有按预期工作,因为 z index not working with fixed positioning

这是工作代码,我从你的代码中更改了几行:

/* renamed from .alertMessageModel::before */
.alertMessageModal {
  content: '';
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: -100;
}
/* renamed from .alertMessageModal */
.modal-content {
  width: auto;
  height: auto;
  border-radius: 10px;
  display: inline-block;
  position: fixed;
  top: 20px;
  right: 25px;
  color: #fff;
  padding: 10px 15px;
  padding-bottom: 15px;
  z-index: 100;
  background: green !important;
}
.alertMessageModal .modal-title {
  font-size: 15px;
  float: left;
  line-height: 18px;
}
.alertMessageModal .close {
  float: right;
  margin-left: 10px;
  line-height: 15px;
  color: #fff;
}
<div class="alertMessageModal bg-success">

  <!-- added an extra div -->
  <div class="modal-content">
    <h4 class="modal-title">Your message has been successfully sent</h4>
    <button type="button" class="close">&times;</button>
  </div>

</div>

关于html - 为什么模态背景与我的弹出模态重叠甚至设置了 !important 和 z-index?如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59442360/

相关文章:

html - 具有与 flex 相同高度的 div

html - style.css 在 Bootstrap 之前被包含

javascript - 使用 currentTime 时视频滞后

css - 奇怪的 CSS 和 HTML 下拉菜单故障重叠

javascript - 在 <form> 之外输入标签并提交表单

javascript - 如何从中心开始jquery .animation

css - 在选择器中的伪类之后包含元素是否有效 CSS?

jquery - 选择使用 jquery 单击的正确类

css,我无法像 Safari 中的其他站点一样设置字体 (mac)

html - CSS - 两个具有相同类的 CSS 文件无法正确加载