代码之家  ›  专栏  ›  技术社区  ›  snazzybouche

使用CSS在Z轴上创建平滑倾斜的边界(“午餐托盘”效果)

css
  •  0
  • snazzybouche  · 技术社区  · 5 年前

    我想创建一个具有类似3D效果的元素:它看起来应该凹进背景中。

    https://neumorphism.io

    div {
      padding: 1rem;
      width: 10rem;
      box-shadow: inset 0px 3px 10px #bbb;
      background-color: #bbb1;
    }
    <div>Hello!</div>

    使用neumorphism.io的Z轴图,上面的图如下所示:

    diagram

    垂直线表示图元内部和外部之间的硬视觉边界。

    通过在元素外部添加进一步的阴影,我可以平滑Z轴图的上边缘:

    div {
      padding: 1rem;
      width: 10rem;
      box-shadow: inset 0px 3px 10px #bbb, 0px -3px 10px #bbb;
      background-color: #bbb1;
      border-radius: 1rem;
    }
    <

    smoother diagram

    但这并不是我想要的,因为这些硬边仍然存在(事实上,元素开始类似于放置在页面上的3D对象,并从下方照亮,而不是3D衰退)。

    (image) ,衰退为其他对象创建3D空间,并由以下内容表示:

    very smooth diagram


    0 回复  |  直到 5 年前
        1
  •  2
  •   dev sandbox    5 年前

    * {
      box-sizing: border-box; margin: 0; padding: 0;
    }
    html, body {
      height: 100%; font-family: Arial;
    }
    body, div {
      display: flex; justify-content: center; align-items: center;
      background-color: #ddc;
    }
    body::before, body::after, div {
      content: ''; position: absolute;
      border-radius: 1.5rem;
      width: 5rem; height: 5rem;
      background-color: rgba( 0,0,0,0.25 );
    }
    div {
      z-index: 1; box-shadow: 0rem 0rem 1rem 1rem #ddc;  
      background-color: #ddc; color: rgba( 0,0,0,0.05 );  
      font-size: 2rem; font-weight: bold;
    }
    div::after {
      content: 'x'; transform: scaleY( 0.875 );
    }
    body::before {
      transform: translate( -0.5rem, -0.5rem );
      box-shadow: 0rem 0rem 1rem 0.5rem rgba( 0,0,0,0.5 );
      background-color: rgba( 0,0,0,0.5 );    
    }
    body::after {
      transform: translate( 0.5rem, 0.5rem );
      box-shadow: 0rem 0rem 1rem 0.5rem #fff; background-color: #fff;    
    }
    <div></div>

    您应该能够根据您的项目需要缩小此范围。