背景和z-index问题

时间:2015-10-20 09:02:35

标签: html css

只有从 .sign-up表单中删除背景:#a1a1a1; 时,才会以正确的方式运行代码。但我需要背景。更改 z-index 没有帮助,因为-1值太小而0很大。 如何解决这个问题? 谢谢。

.sign-up__form {
  width: 260px;
  padding: 20px;
  background: #fff;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  border-top: 5px solid #ff4e50;
  top: 80px;
  left: 0;
  right: 0;
  margin: auto;
  position: absolute;
}
.register-switch {
  height: 32px;
  margin-bottom: 20px;
  padding: 4px;
  position: relative;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
.register-switch-input {
  display: none;
}
.register-switch-label {
  float: left;
  width: 50%;
  line-height: 32px;
  color: #fff;
  text-align: center;
  cursor: pointer;
}
.register-switch-label:after {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: lightblue;
  left: 0;
  top: 0;
  z-index: -1;
}
.register-switch-input:checked + .register-switch-label:after {
  background: tomato;
}
.register-switch-input:checked + .register-switch-label {
  font-weight: normal;
  color: #666;
  background: #fff;
  border-radius: 2px;
  background-image: -webkit-linear-gradient(top, #fefefe, #eeeeee);
  background-image: -moz-linear-gradient(top, #fefefe, #eeeeee);
  background-image: -o-linear-gradient(top, #fefefe, #eeeeee);
  background-image: linear-gradient(to bottom, #fefefe, #eeeeee);
  -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
}
<form name="sign-up" class="sign-up__form">
  <div class="register-switch">
    <input type="radio" name="sex" value="Male" id="sex-m" class="register-switch-input" checked>
    <label for="sex-m" class="register-switch-label">Male</label>
    <input type="radio" name="sex" value="Female" id="sex-f" class="register-switch-input">
    <label for="sex-f" class="register-switch-label">Female</label>
  </div>
</form>

1 个答案:

答案 0 :(得分:2)

您还需要在z-index规则上设置.sign-up__form

.sign-up__form {
  width: 260px;
  padding: 20px;
  background: #fff;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  border-top: 5px solid #ff4e50;
  top: 80px;
  left: 0;
  right: 0;
  margin: auto;
  position: absolute;
  z-index: -1;
}
.register-switch {
  height: 32px;
  margin-bottom: 20px;
  padding: 4px;
  position: relative;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
}
.register-switch-input {
  display: none;
}
.register-switch-label {
  float: left;
  width: 50%;
  line-height: 32px;
  color: #fff;
  text-align: center;
  cursor: pointer;
}
.register-switch-label:after {
  position: absolute;
  content: "";
  width: 100%;
  height: 100%;
  background: lightblue;
  left: 0;
  top: 0;
  z-index: -1;
  border-radius: 5px;
}
.register-switch-input:checked + .register-switch-label:after {
  background: tomato;
}
.register-switch-input:checked + .register-switch-label {
  font-weight: normal;
  color: #666;
  background: #fff;
  border-radius: 2px;
  background-image: -webkit-linear-gradient(top, #fefefe, #eeeeee);
  background-image: -moz-linear-gradient(top, #fefefe, #eeeeee);
  background-image: -o-linear-gradient(top, #fefefe, #eeeeee);
  background-image: linear-gradient(to bottom, #fefefe, #eeeeee);
  -webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15), 0 0 0 1px rgba(0, 0, 0, 0.1);
}
<form name="sign-up" class="sign-up__form">
  <div class="register-switch">
    <input type="radio" name="sex" value="Male" id="sex-m" class="register-switch-input" checked>
    <label for="sex-m" class="register-switch-label">Male</label>
    <input type="radio" name="sex" value="Female" id="sex-f" class="register-switch-input">
    <label for="sex-f" class="register-switch-label">Female</label>
  </div>
</form>