首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >固定或绝对时叠加CSS元素

固定或绝对时叠加CSS元素
EN

Stack Overflow用户
提问于 2019-12-20 19:03:06
回答 3查看 73关注 0票数 1

代码语言:javascript
复制
#snackbar {
      min-width: 350px; /* Set a default minimum width */
      margin-left: -125px; /* Divide value of min-width by 2 */
      background-color: #333; /* Black background color */
      color: #fff; /* White text color */
      text-align: center; /* Centered text */
      border-radius: 2px; /* Rounded borders */
      padding: 10px; /* Padding */
      position: fixed; /* Sit on top of the screen */
      z-index: 1; /* Add a z-index if needed */
      left: 85%; /* Center the snackbar */
      bottom: 85%; /* 30px from the bottom */
}
代码语言:javascript
复制
<div id="snackbar">
    Message 1
</div> 
<div id="snackbar">
    Message 2
</div> 
<div id="snackbar">
    Message 3
</div>

所有这些元素都堆叠成一个元素,它只显示消息3,但我希望它们堆在另一个元素的1下面。

我试图修复的方法是:

代码语言:javascript
复制
    #snackbar ~ #snackbar {
    margin-top: 10%;
    }

我想这可能会把它们堆在一起,但是没有运气。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2019-12-20 19:09:29

首先要做的是:独具特色

通过将它们封装在父元素中,您有了正确的想法。

代码语言:javascript
复制
#container{
  position: fixed;
  z-index: 1; 
  right: 3%; 
}

.snackbar {
  margin: 5px 0;
  min-width: 350px; 
  background-color: #333; 
  color: #fff; 
  text-align: center;
  border-radius: 2px;
  padding: 10px;
}
代码语言:javascript
复制
<div id="container">

  <div class="snackbar">
   Message 1
  </div>

  <div class="snackbar">
    Message 2
  </div>

  <div class="snackbar">
    Message 3
  </div>
  
</div>

票数 3
EN

Stack Overflow用户

发布于 2019-12-20 19:09:32

我认为下面是能解决你的问题.我改变了两件事。我把你的“id”改为“class”。类通常保留用于将样式应用于多个元素,而“id”则保留给单个元素。我改变的第二件事是我删除了你指定的“职位”。希望这能有所帮助!

HTML:

代码语言:javascript
复制
<div class="snackbar">
  Message 1
</div>

<div class="snackbar">
  Message 2
</div>

<div class="snackbar">
  Message 3
</div>

CSS:

代码语言:javascript
复制
.snackbar {
  min-width: 350px; /* Set a default minimum width */
  margin-left: -125px; /* Divide value of min-width by 2 */
  background-color: #333; /* Black background color */
  color: #fff; /* White text color */
  text-align: center; /* Centered text */
  border-radius: 2px; /* Rounded borders */
  padding: 10px; /* Padding */
  z-index: 1; /* Add a z-index if needed */
  left: 85%; /* Center the snackbar */
  bottom: 85%; /* 30px from the bottom */
}
票数 0
EN

Stack Overflow用户

发布于 2019-12-20 19:10:58

我添加了一个父div并更改了css,使其使用顶部15%而不是底部85%,尝试如下:

代码语言:javascript
复制
#snackbar {
      min-width: 350px; /* Set a default minimum width */
      margin-left: -125px; /* Divide value of min-width by 2 */
      background-color: #333; /* Black background color */
      color: #fff; /* White text color */
      text-align: center; /* Centered text */
      border-radius: 2px; /* Rounded borders */
      padding: 10px; /* Padding */
      position: fixed;  /*Sit on top of the screen */
      z-index: 1; /* Add a z-index if needed */
      left: 85%; /* Center the snackbar */
      top: 15%;
    }
代码语言:javascript
复制
   <div id="snackbar">
       <div>Message 1
      </div>

      <div>
        Message 2
      </div>

      <div>
        Message 3
      </div>
      </div>

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59430199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档