【问题标题】:How to create a box with cut off corner and shadow? (Boxed Website design)如何创建一个带有切角和阴影的盒子? (盒装网站设计)
【发布时间】:2020-02-02 15:52:06
【问题描述】:

我正在做一个网站,我需要剪掉主体的左上角。 之后我想在主体上应用阴影。这个阴影不应该绕过原来的盒子,它应该跟随主体并带有新的切角 - 我为此使用了阴影。

我尝试使用渐变背景,但无论我尝试什么,我的标题要么与主体重叠,要么阴影不起作用

我目前的尝试是这样的:https://codepen.io/Sophvy/pen/MWgMZzG

HTML:

<div id ="main1">
  <div id ="wrap"></div>
  <header>

  </header>
  <main>
  </main>

</div>

CSS:


#main1 {
  height:500px;
  width:500px;
  position:relative;
}
#wrap {
  width:500px;
  height:500px;
  background: linear-gradient(135deg, transparent 70px,green 0);
  filter: drop-shadow(0px 0px 10px blue);
  position:absolute;
}

header {
  height:55px;
  max-width:100%;
  background-color:#eee;
  position: relative;
}

我的问题是标题没有被切断,所以它只是重叠。 我尝试使用 z-index 但即使在每个元素上使用 position:absolute / relative 也无法使其工作。我查看了很多不同的想法,但没有发现任何可以处理与我的标题相同的问题。

我需要改变什么来切断主体和头部的角落,然后得到一个工作阴影?

编辑:我的解决方案

HTML:


<div id="background">
  <div id ="main1">
    <div id ="wrap">
      <header>
        header
      </header>
      <main>
      main
      </main>
    </div>
  </div>
</div>

CSS:

#background {
  height:500px;
  width:600px;
  padding-top: 5px;
  background-color:#bbb;
  padding-bottom: 5px;
}

#main1 {
  margin: 10px auto;
  width: 90%;
  height:400px;
  text-align:right;
  filter: drop-shadow(0px 0px 10px blue); 
}
#wrap {
  width:500px;
  height:500px;
  background: linear-gradient(135deg, transparent 70px,green 0);
  clip-path: polygon(25% 0, 100% 0, 100% 100%, 0 100%, 0 25%);
  position:absolute;
}

header {
  height:55px;
  max-width:100%;
  background-color:#eee;
  position: relative;
}

【问题讨论】:

  • 使用剪辑路径而不是渐变......渐变不会被剪切,它会用透明颜色着色

标签: html css shadow


【解决方案1】:

离你很近!

如果您使用剪辑路径,则可以剪切框的标题和主要部分。 然后在主元素上设置阴影过滤器时,您应该会获得所需的样式。

#main1 {
  height:500px;
  width:500px;
  filter: drop-shadow(0px 0px 10px blue);
}
#wrap {
  width:500px;
  height:500px;
  background: linear-gradient(135deg, transparent 70px,green 0);
  clip-path: polygon(25% 0, 100% 0, 100% 100%, 0 100%, 0 25%);
  position:absolute;
}

header {
  height:55px;
  max-width:100%;
  background-color:#eee;
  position: relative;
}
<div id ="main1">
  <div id ="wrap">
    <header>
    </header>

    <main>
    </main>
  </div> 
</div>

【讨论】:

  • 感谢您的回答。我可以看到它是如何工作的,但遗憾的是我无法将它用于我的网站。我制作了一个新的测试代码,更清楚地代表了我的实际网站布局:codepen.io/Sophvy/pen/yLLBGba 我有点困惑为什么这不起作用。因为唯一真正改变的是我有一个背景,然后是中间的框?
  • @Sophvy 是因为“main1”是您的主要容器吗?我真的不明白你为什么不在你的代码中使用这个方法,对不起。这种方法唯一需要做的就是在你的标题和主目录周围有两个 div。外层 div 需要有过滤器,内层需要有 clip-path 属性。
  • oki 我发现这只是一些小的逻辑错误。感谢您的帮助:)
猜你喜欢
  • 2019-12-03
  • 1970-01-01
  • 2020-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
相关资源
最近更新 更多