相对定位父级的绝对定位的psuedo元素的overflow设置为overflow:hidden

塔沙尔舒克拉

我正在通过CSS(jolly roger制作一个戴着帽子的骷髅,可以在这个Codepen中找到

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

虽然我的代码在Firefox中可以正常工作,但在chrome中似乎无法正常工作。我已经尝试了许多解决方案,这些解决方案说明了如何将绝对项目放置在带有Overflow:hidden的相对父级中,但是这些似乎都不起作用。也许我错过了一些重要的事情。

的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中运行的任何建议。

尼罗什·奈克(Nilesh Naik)

只需z-index.skull__hat班级中删除即可

.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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

绝对定位的psuedo元素与父级一起滚动

绝对定位元素不留在相对父级

为绝对定位元素设置高度 = "100% - 相对顶部"

填充设置时相对父级内部的绝对定位溢出div

绝对定位的 div 溢出相对父级的边界

将绝对定位的 div 展开并将其定位到相对定位的父级

子级和父级的绝对定位高度为0

DropDown 在相对父容器之外定位绝对元素

将绝对元素定位在相对父容器之外

CSS定位的绝对元素,自动宽度超出父级边界?

如何更改绝对定位的CSS元素的偏移父级?

相对定位与绝对定位

根据主体而不是相对父级定位元素

如何将绝对定位元素宽度设置为父节点宽度?

如何快速找到绝对定位的子对象的相对定位的HTML父元素?

允许绝对定位的元素比父绝对定位的元素宽

如果父元素是静态的,为什么绝对定位的元素自身不相对于父元素定位?

将绝对定位的DIV“完全”适合相对定位的父DIV?

在绝对定位的父级中滚动div

相对定位元素内部的绝对定位元素被裁剪

固定父级内的相对定位div

具有相对和绝对定位的元素

绝对定位元素扩展父元素

相对定位的HTML元素与父元素重叠

当父元素已定位时,如何相对于视口水平居中放置绝对定位的元素?

绝对定位的子元素溢出父容器

绝对/相对定位问题

为什么不能相对于“位置:静态”的父对象定位“位置:绝对”元素?

防止{overflow-y:auto} div中绝对定位的元素触发滚动条