css - 相对定位父级的溢出绝对位置伪元素设置为溢出 :hidden

标签 css html google-chrome css-position absolute

我正在通过 CSS ( jolly roger ) 制作一个戴着帽子的头骨,可以在这个 codepen 找到.

在帽子部分,它的下边缘必须从父 div 溢出,而帽子中的条纹必须有 overflow:hidden
您可以在 firefox 和 chrome 中打开 codepen 链接以发现差异。

虽然我的代码在 Firefox 中运行良好,但这在 chrome 中似乎不起作用。我已经尝试了很多解决方案,这些解决方案讲述了如何在相对父项中定位绝对项并溢出:隐藏但这些似乎都不起作用。也许我错过了一些重要的东西。

HTML

<div class="skull">
  <div class="skull__face skull__face--animate">
    <div class="skull__upper">
      <div class="skull__hat"></div>
      <div class="skull__nose"></div>
    </div>
    <div class="skull__lower">
      <div class="skull__jaw"></div>
    </div>
  </div>
  <div class="skull__bone skull__bone--left"></div>
  <div class="skull__bone skull__bone--right"></div>
</div>

CSS

.skull__upper, .skull__lower {
  background-color: white;
  border: 7px solid black;
  display: flex;
}

.skull__lower::before, .skull__jaw::before, .skull__jaw::after {
  content: "";
  width: 7px;
  height: 6rem;
  background-color: black;
  display: inline-block;
  position: absolute;
  bottom: -1.6rem;
  left: 0px;
  right: 0px;
  margin: auto;
}

@-webkit-keyframes boneDance {
  0% {
    -webkit-transform-origin: 49% 0%;
            transform-origin: 49% 0%;
  }
  50% {
    -webkit-transform-origin: 50% 0%;
            transform-origin: 50% 0%;
  }
  100% {
    -webkit-transform-origin: 51% 0%;
            transform-origin: 51% 0%;
  }
}

@keyframes boneDance {
  0% {
    -webkit-transform-origin: 49% 0%;
            transform-origin: 49% 0%;
  }
  50% {
    -webkit-transform-origin: 50% 0%;
            transform-origin: 50% 0%;
  }
  100% {
    -webkit-transform-origin: 51% 0%;
            transform-origin: 51% 0%;
  }
}
@-webkit-keyframes skullDance {
  0% {
    -webkit-transform: rotate(1deg);
            transform: rotate(1deg);
  }
  50% {
    -webkit-transform: rotate(0deg);
            transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(-1deg);
            transform: rotate(-1deg);
  }
}
@keyframes skullDance {
  0% {
    -webkit-transform: rotate(1deg);
            transform: rotate(1deg);
  }
  50% {
    -webkit-transform: rotate(0deg);
            transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(-1deg);
            transform: rotate(-1deg);
  }
}
@-webkit-keyframes blink {
  from {
    opacity: 0.4;
  }
  to {
    opacity: 1;
  }
}
@keyframes blink {
  from {
    opacity: 0.4;
  }
  to {
    opacity: 1;
  }
}
body {
  background-color: #191919;
}

@media only screen and (max-width: 600px) {
  body {
    font-size: 12px;
  }
}
.skull {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 10vh;
}
.skull__face {
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 50;
  position: relative;
}
.skull__face::after {
  content: "";
  width: 13rem;
  border-radius: 50%;
  box-shadow: 0px 0px 12rem 0 white;
  position: absolute;
  display: block;
  height: 100%;
  opacity: 0;
  transition: opacity 1s ease-in-out;
}
.skull__face--animate:hover {
  -webkit-animation: skullDance 1s steps(3) infinite alternate;
          animation: skullDance 1s steps(3) infinite alternate;
  cursor: none;
}
.skull__face--animate:hover::after {
  opacity: 1;
  -webkit-animation: blink 1s linear infinite alternate;
          animation: blink 1s linear infinite alternate;
}
.skull__face--animate:hover ~ .skull__bone {
  -webkit-animation: boneDance 1s steps(3) infinite alternate;
          animation: boneDance 1s steps(3) infinite alternate;
}
.skull__upper {
  width: 20rem;
  height: 20rem;
  border-radius: 50%;
  z-index: 50;
  position: relative;
}
.skull__upper::before, .skull__upper::after {
  content: "";
  display: block;
}
.skull__upper::before {
  position: absolute;
  border-radius: 50%;
  box-shadow: 5.5rem 13.5rem 0px 2.8rem black, 14.5rem 13.5rem 0px 2.8rem black;
  width: 1px;
  height: 1px;
  background-color: transparent;
}
.skull__hat {
  justify-content: center;
  background-color: #FFD020;
  width: 20rem;
  height: 10rem;
  z-index: 45;
  border-top-left-radius: calc(10rem + 7px);
  border-top-right-radius: calc(10rem + 7px);
  overflow: hidden;
}
.skull__hat::before, .skull__hat::after {
  content: "";
  display: block;
  left: 0px;
  right: 0px;
  margin: auto;
}
.skull__hat::after {
  height: 1rem;
  background-color: #FFD020;
  position: absolute;
  border: 7px solid black;
  border-radius: 1rem;
  width: 30rem;
  left: -5.5rem;
}
.skull__hat::before {
  width: 18.5rem;
  height: 0;
  position: relative;
  border-bottom: 3rem solid #FF0012;
  border-left: 0.75rem solid transparent;
  border-right: 0.75rem solid transparent;
  margin-top: 5.35rem;
  box-shadow: 0px 0px 0px 0px #191919, 0px -7px 0px 0px black;
}
.skull__lower {
  width: 14rem;
  height: 16rem;
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  z-index: 49;
  margin-top: -7rem;
  overflow: hidden;
  position: relative;
}
.skull__lower::before {
  bottom: 4.5rem;
  z-index: 40;
}
.skull__nose {
  width: 3rem;
  height: 2rem;
  background: black;
  border-radius: 50%;
  position: absolute;
  bottom: 2rem;
  left: 0px;
  right: 0px;
  margin: auto;
}
.skull__jaw {
  width: 21rem;
  height: 21rem;
  border-radius: 50%;
  border: 7px solid black;
  position: absolute;
  margin: auto;
  top: -12.5rem;
  left: -4rem;
  box-shadow: 0px 1.8rem 0px 0px white, 0px 2.2rem 0px 0px black;
}
.skull__jaw::before {
  margin-left: 6.6rem;
  -webkit-transform: rotate(10deg);
          transform: rotate(10deg);
}
.skull__jaw::after {
  margin-right: 6.6rem;
  -webkit-transform: rotate(-10deg);
          transform: rotate(-10deg);
}
.skull__bone {
  background: white;
  height: 3rem;
  width: 36rem;
  position: absolute;
  border: 7px solid black;
  -webkit-transform-origin: 50% 0%;
          transform-origin: 50% 0%;
}
.skull__bone::before, .skull__bone::after {
  content: "";
  position: absolute;
  border-radius: 50%;
  top: -1.5rem;
  width: 3rem;
  height: 3rem;
  background: white;
}
.skull__bone::before {
  left: -1.5rem;
}
.skull__bone::after {
  right: -1.5rem;
}
.skull__bone--left {
  -webkit-transform: rotate(-45deg);
          transform: rotate(-45deg);
}
.skull__bone--left::before {
  box-shadow: 0 3rem 0 white, -2px 3.1rem 0px 2px black, -2px -0.1rem 0px 2px black;
}
.skull__bone--left::after {
  box-shadow: 0 3rem 0 white, 3px 3.1rem 0px 1px black, 2px -0.1rem 0px 2px black;
}
.skull__bone--right {
  -webkit-transform: rotate(45deg);
          transform: rotate(45deg);
}
.skull__bone--right::before {
  box-shadow: 0 3rem 0 white, -2px 3.1rem 0px 2px black, -2px -0.1rem 0px 2px black;
}
.skull__bone--right::after {
  box-shadow: 0 3rem 0 white, 3px 3.1rem 0px 1px black, 2px -0.1rem 0px 2px black;
}

让它至少在 Firefox 和 Chrome 中运行的任何建议。

最佳答案

只需从 .skull__hat 类中删除 z-index 即可。

.skull__upper, .skull__lower {
  background-color: white;
  border: 7px solid black;
  display: flex;
}

.skull__lower::before, .skull__jaw::before, .skull__jaw::after {
  content: "";
  width: 7px;
  height: 6rem;
  background-color: black;
  display: inline-block;
  position: absolute;
  bottom: -1.6rem;
  left: 0px;
  right: 0px;
  margin: auto;
}

@-webkit-keyframes boneDance {
  0% {
    -webkit-transform-origin: 49% 0%;
            transform-origin: 49% 0%;
  }
  50% {
    -webkit-transform-origin: 50% 0%;
            transform-origin: 50% 0%;
  }
  100% {
    -webkit-transform-origin: 51% 0%;
            transform-origin: 51% 0%;
  }
}

@keyframes boneDance {
  0% {
    -webkit-transform-origin: 49% 0%;
            transform-origin: 49% 0%;
  }
  50% {
    -webkit-transform-origin: 50% 0%;
            transform-origin: 50% 0%;
  }
  100% {
    -webkit-transform-origin: 51% 0%;
            transform-origin: 51% 0%;
  }
}
@-webkit-keyframes skullDance {
  0% {
    -webkit-transform: rotate(1deg);
            transform: rotate(1deg);
  }
  50% {
    -webkit-transform: rotate(0deg);
            transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(-1deg);
            transform: rotate(-1deg);
  }
}
@keyframes skullDance {
  0% {
    -webkit-transform: rotate(1deg);
            transform: rotate(1deg);
  }
  50% {
    -webkit-transform: rotate(0deg);
            transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(-1deg);
            transform: rotate(-1deg);
  }
}
@-webkit-keyframes blink {
  from {
    opacity: 0.4;
  }
  to {
    opacity: 1;
  }
}
@keyframes blink {
  from {
    opacity: 0.4;
  }
  to {
    opacity: 1;
  }
}
body {
  background-color: #191919;
}

@media only screen and (max-width: 600px) {
  body {
    font-size: 12px;
  }
}
.skull {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 10vh;
}
.skull__face {
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 50;
  position: relative;
}
.skull__face::after {
  content: "";
  width: 13rem;
  border-radius: 50%;
  box-shadow: 0px 0px 12rem 0 white;
  position: absolute;
  display: block;
  height: 100%;
  opacity: 0;
  transition: opacity 1s ease-in-out;
}
.skull__face--animate:hover {
  -webkit-animation: skullDance 1s steps(3) infinite alternate;
          animation: skullDance 1s steps(3) infinite alternate;
  cursor: none;
}
.skull__face--animate:hover::after {
  opacity: 1;
  -webkit-animation: blink 1s linear infinite alternate;
          animation: blink 1s linear infinite alternate;
}
.skull__face--animate:hover ~ .skull__bone {
  -webkit-animation: boneDance 1s steps(3) infinite alternate;
          animation: boneDance 1s steps(3) infinite alternate;
}
.skull__upper {
  width: 20rem;
  height: 20rem;
  border-radius: 50%;
  z-index: 50;
  position: relative;
}
.skull__upper::before, .skull__upper::after {
  content: "";
  display: block;
}
.skull__upper::before {
  position: absolute;
  border-radius: 50%;
  box-shadow: 5.5rem 13.5rem 0px 2.8rem black, 14.5rem 13.5rem 0px 2.8rem black;
  width: 1px;
  height: 1px;
  background-color: transparent;
}
.skull__hat {
  justify-content: center;
  background-color: #FFD020;
  width: 20rem;
  height: 10rem;
  border-top-left-radius: calc(10rem + 7px);
  border-top-right-radius: calc(10rem + 7px);
  overflow: hidden;
}
.skull__hat::before, .skull__hat::after {
  content: "";
  display: block;
  left: 0px;
  right: 0px;
  margin: auto;
}
.skull__hat::after {
  height: 1rem;
  background-color: #FFD020;
  position: absolute;
  border: 7px solid black;
  border-radius: 1rem;
  width: 30rem;
  left: -5.5rem;
}
.skull__hat::before {
  width: 18.5rem;
  height: 0;
  position: relative;
  border-bottom: 3rem solid #FF0012;
  border-left: 0.75rem solid transparent;
  border-right: 0.75rem solid transparent;
  margin-top: 5.35rem;
  box-shadow: 0px 0px 0px 0px #191919, 0px -7px 0px 0px black;
}
.skull__lower {
  width: 14rem;
  height: 16rem;
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  z-index: 49;
  margin-top: -7rem;
  overflow: hidden;
  position: relative;
}
.skull__lower::before {
  bottom: 4.5rem;
  z-index: 40;
}
.skull__nose {
  width: 3rem;
  height: 2rem;
  background: black;
  border-radius: 50%;
  position: absolute;
  bottom: 2rem;
  left: 0px;
  right: 0px;
  margin: auto;
}
.skull__jaw {
  width: 21rem;
  height: 21rem;
  border-radius: 50%;
  border: 7px solid black;
  position: absolute;
  margin: auto;
  top: -12.5rem;
  left: -4rem;
  box-shadow: 0px 1.8rem 0px 0px white, 0px 2.2rem 0px 0px black;
}
.skull__jaw::before {
  margin-left: 6.6rem;
  -webkit-transform: rotate(10deg);
          transform: rotate(10deg);
}
.skull__jaw::after {
  margin-right: 6.6rem;
  -webkit-transform: rotate(-10deg);
          transform: rotate(-10deg);
}
.skull__bone {
  background: white;
  height: 3rem;
  width: 36rem;
  position: absolute;
  border: 7px solid black;
  -webkit-transform-origin: 50% 0%;
          transform-origin: 50% 0%;
}
.skull__bone::before, .skull__bone::after {
  content: "";
  position: absolute;
  border-radius: 50%;
  top: -1.5rem;
  width: 3rem;
  height: 3rem;
  background: white;
}
.skull__bone::before {
  left: -1.5rem;
}
.skull__bone::after {
  right: -1.5rem;
}
.skull__bone--left {
  -webkit-transform: rotate(-45deg);
          transform: rotate(-45deg);
}
.skull__bone--left::before {
  box-shadow: 0 3rem 0 white, -2px 3.1rem 0px 2px black, -2px -0.1rem 0px 2px black;
}
.skull__bone--left::after {
  box-shadow: 0 3rem 0 white, 3px 3.1rem 0px 1px black, 2px -0.1rem 0px 2px black;
}
.skull__bone--right {
  -webkit-transform: rotate(45deg);
          transform: rotate(45deg);
}
.skull__bone--right::before {
  box-shadow: 0 3rem 0 white, -2px 3.1rem 0px 2px black, -2px -0.1rem 0px 2px black;
}
.skull__bone--right::after {
  box-shadow: 0 3rem 0 white, 3px 3.1rem 0px 1px black, 2px -0.1rem 0px 2px black;
}
<div class="skull">
  <div class="skull__face skull__face--animate">
    <div class="skull__upper">
      <div class="skull__hat"></div>
      <div class="skull__nose"></div>
    </div>
    <div class="skull__lower">
      <div class="skull__jaw"></div>
    </div>
  </div>
  <div class="skull__bone skull__bone--left"></div>
  <div class="skull__bone skull__bone--right"></div>
</div>

关于css - 相对定位父级的溢出绝对位置伪元素设置为溢出 :hidden,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54567187/

相关文章:

html - 如何根据屏幕大小调整鼠标悬停在div上?

php - 不支持字体锐化效果

css - Chrome : Text blurry when skew back : skew(-10deg) -> skew(10deg)

CSS 缩进到最后一行

html - 为什么我的标题和导航在具有相同 CSS 的不同页面上表现不同

css - Joomla css 路径不正确

html - <ruby> 文本溢出 : ellipsis

javascript - 为什么textArea不更新?

python - 谷歌浏览器下载文件时如何自动重命名文件?

html - 并排放置 2 个 div