首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在div后面制作带有CSS的按钮

在div后面制作带有CSS的按钮
EN

Stack Overflow用户
提问于 2015-12-02 17:37:26
回答 3查看 2.1K关注 0票数 4

我有一些html,其中包含一些在div类中包装的按钮。

代码语言:javascript
复制
.addshade {
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100000;
  background: rgba(0, 0, 0, 0.75);
  display: block;
}
.cbutton {
  cursor: pointer;
  display: inline-block;
  font-family: Roboto;
  font-weight: 700;
  font-size: 16px;
  border-radius: 5px;
  padding: 5px 6px;
  min-width: 90px;
  text-decoration: none;
  -webkit-font-smoothing: antialiased;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  z-index: -1000;
}
代码语言:javascript
复制
<div class="addshade">
  <p class="description"></p>
  <div class="options">
    <a class="buy cbutton whiteonpurple" target="_blank" href="http://www.apple.com/shop/buy-watch/apple-watch">Buy</a>
    <a class="hint cbutton whiteonblack" target="_blank">Hint</a>
  </div>

  <div class="info" style="display: none;">
    <div class="bar"></div>
    <div class="rehints" id="rehint_55cd0ef28ead0e883c8b4567" style="visibility: hidden;">10 REHINTS</div>
    <div class="hinter" style="visibility: hidden;">
      <div class="picture monophoto">
        <div class="text" style="font-size: 37px; margin-top: 4.375px;">SO</div>
        <div class="img" style="background-image: url(http://graph.facebook.com/filler/picture?type=large);" onclick="location.href='/user/filler';"></div>
      </div>
      <div class="content">
        <div class="one">Hinted by:</div>
        <div class="two"><a href="/user/sean12oshea">Sean O'Shea</a>
        </div>
      </div>
    </div>
    <div class="partnertext">Partnered Hint</div>
    <div style="clear:both;"></div>
  </div>
</div>

即使使用z索引,按钮也显示在我想要的阴影前面:

然而,按钮在前面,可点击。我怎样才能得到想要的行为?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-02 17:44:34

z-index只适用于定位元素(位置:绝对、位置:相对或位置:固定)

因此,将position:relative;添加到.cbutton

代码语言:javascript
复制
.addshade {
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100000;
  background: rgba(0, 0, 0, 0.75);
  display: block;
}
.cbutton {
  cursor: pointer;
  display: inline-block;
  font-family: Roboto;
  font-weight: 700;
  font-size: 16px;
  border-radius: 5px;
  padding: 5px 6px;
  min-width: 90px;
  text-decoration: none;
  -webkit-font-smoothing: antialiased;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  z-index: -1000;
  position: relative;
}
代码语言:javascript
复制
<div class="addshade">
  <p class="description"></p>
  <div class="options">
    <a class="buy cbutton whiteonpurple" target="_blank" href="http://www.apple.com/shop/buy-watch/apple-watch">Buy</a>
    <a class="hint cbutton whiteonblack" target="_blank">Hint</a>
  </div>

  <div class="info" style="display: none;">
    <div class="bar"></div>
    <div class="rehints" id="rehint_55cd0ef28ead0e883c8b4567" style="visibility: hidden;">10 REHINTS</div>
    <div class="hinter" style="visibility: hidden;">
      <div class="picture monophoto">
        <div class="text" style="font-size: 37px; margin-top: 4.375px;">SO</div>
        <div class="img" style="background-image: url(http://graph.facebook.com/filler/picture?type=large);" onclick="location.href='/user/filler';"></div>
      </div>
      <div class="content">
        <div class="one">Hinted by:</div>
        <div class="two"><a href="/user/sean12oshea">Sean O'Shea</a>
        </div>
      </div>
    </div>
    <div class="partnertext">Partnered Hint</div>
    <div style="clear:both;"></div>
  </div>
</div>

票数 6
EN

Stack Overflow用户

发布于 2015-12-02 17:45:28

参见此小提琴

回答这里。上面写着,

Z-索引只适用于定位元素(位置:绝对,位置:相对,或位置:固定)。

从而添加

代码语言:javascript
复制
z-index: -1000;
position: relative;

转到.cbutton

票数 2
EN

Stack Overflow用户

发布于 2015-12-02 17:49:31

对于定位框,z-index属性指定: 1)当前堆叠上下文中框的堆栈级别。( 2)盒子是否建立了一个本地堆叠上下文。

Z-指数受堆叠上下文影响较大.我想强调一下

将z-索引值定位并分配给HTML元素将创建一个堆叠上下文。

因此,我个人认为在这两个元素上都有一个相对位置,以便将两者置于相同的堆叠上下文中。

代码语言:javascript
复制
.addshade, .cbutton {
    position: relative;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34049338

复制
相关文章

相似问题

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