英文:
CSS: cropped Box Shadow
问题
可以在左右两侧裁剪 box-shadow 以使其看起来像这样吗?
或者我需要使用两个独立的 div 元素来创建它?
我想要它具备响应式设计,并且应该保持它们之间的关系。
谢谢提前。
问候
Lars
英文:
is it somehow possible to crop a box-shadow on the left and right to make it look like this:
Or do I need to create this using two independent divs?
I would like to make it responsive, and it should keep the relations to each other.
Thanks in advance.
Regards
Lars
答案1
得分: 1
以下是您要翻译的内容:
"使用 ::after 可以通过 CSS 创建背景形状。
使用百分比 % 可以使方框具有响应性,同时保持宽高比。
.box {
  margin: 10% 0%;
  width: 100%;
  background-color: blue;
  position: relative;
  text-align: center;
  &::after {
    content: '';
    position: absolute;
    left: 10%;
    top: -20%;
    width: 80%;
    height: 140%;
    background-color: red;
    z-index: -1;
  }
}
button {
  width: 25%;
  height: 2rem;
  border-radius: 20%;
  margin: 2rem;
  padding: 0.5rem;
  border-radius: 1rem;
}
<div class="box">
  <button>Button</button>
</div>
英文:
It is possible to create the background shape with css by using ::after
Using % should allow the box to be responsive yet maintain the aspect ratios.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.box {
  margin: 10% 0%;
  width: 100%;
  background-color: blue;
  position: relative;
  text-align: center;
  &::after {
    content: '';
    position: absolute;
    left: 10%;
    top: -20%;
    width: 80%;
    height: 140%;
    background-color: red;
    z-index: -1
  }
}
button {
  width: 25%;
  height: 2rem;
  border-radius: 20%;
  margin: 2rem;
  padding: 0.5rem;
  border-radius: 1rem;
}
<!-- language: lang-html -->
<div class="box">
  <button>Button</button>
</div>
<!-- end snippet -->
答案2
得分: 1
你可以在单个元素上设置多个阴影。如果增加偏移并设置负的扩散半径,你可以为期望的效果绘制两个阴影。
参见 https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow
可能的示例,使用你自己的尺寸和阴影数值:
div {
  aspect-ratio: 16/9;
  background: #377172;
  width: 90vmin;
  box-shadow: 0 15vmin 0 -8vmin #CCD5CB, 0 -15vmin 0 -8vmin #CCD5CB
}
clip-path也可能是一个选项:
div {
  aspect-ratio: 16/9;
  background: #377172;
  width: 90vmin;
  box-shadow: 0 0 0 8vmin #CCD5CB;
  clip-path: polygon(0 0, 8vmin  0, 8vmin -8vmin, calc(100% - 8vmin) -8vmin,  calc(100% - 8vmin) 0, 100% 0, 100% 100%,  calc(100% - 8vmin) 100%, calc(100% - 8vmin) calc(100% + 8vmin), 8vmin calc(100% + 8vmin), 8vmin 100%, 0% 100%);
}
请注意,这只是代码的翻译部分,不包括其他内容。
英文:
you can set many shadows on a single element. if increase offset and set negative spread radius, you can draw 2 shadows for your expected result.
see https://developer.mozilla.org/en-US/docs/Web/CSS/box-shadow
possible example, use your size and own values for your shadow(s)
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
div {
  aspect-ratio: 16/9;
  background: #377172;
  width: 90vmin;
  box-shadow: 0 15vmin 0 -8vmin #CCD5CB, 0 -15vmin 0 -8vmin #CCD5CB
}
/* demo purpose */
html {
  display: grid;
  min-height: 100vh;
  place-items: center
}
div {
  color: white;
  font-size: 10vmin;
  display: grid;
  place-items: center
}
<!-- language: lang-html -->
<div>div & 2 shadows</div>
<!-- end snippet -->
clip-path could also be an option
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
div {
  aspect-ratio: 16/9;
  background: #377172;
  width: 90vmin;
  box-shadow: 0 0 0 8vmin #CCD5CB;
  clip-path: polygon(0 0, 8vmin  0, 8vmin -8vmin, calc(100% - 8vmin) -8vmin,  calc(100% - 8vmin) 0, 100% 0, 100% 100%,  calc(100% - 8vmin) 100%, calc(100% - 8vmin) calc(100% + 8vmin), 8vmin calc(100% + 8vmin), 8vmin 100%, 0% 100%);
}
/* demo purpose */
html {
  display: grid;
  min-height: 100vh;
  place-items: center;
  /* gives a shadow to the div and its shadow 
  filter:drop-shadow(0 0 5px red); 
  ... for infos only not needed */
}
div {
  color: white;
  font-size: 10vmin;
  display: grid;
  place-items: center
}
<!-- language: lang-html -->
<div>div & 1 shadow</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论