html - 如何使用 CSS 向图像添加多层透明叠加层

标签 html css svg flexbox

如何按下三 Angular 形并将内容包含在白色圆圈的顶部?我正在尝试找到一种解决方案来创建一个包含背景图像的英雄部分,其中包含三个叠加形状作为图像的一部分。覆盖层顶部将是 h1、p 和 btn。我在下面提供了一张屏幕截图,展示了设计的外观。

有以下三个叠加层:

  1. 直 Angular 形状,底部透明度为 0%。
  2. 具有透明度的外圆。
  3. 具有透明度的内圆。

这是我到目前为止所拥有的。我在下面包含了一个片段,并且在 Codepen 上也有一个工作版本。圆圈位于左下角的正确位置。

enter image description here

*{
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
svg {
  width: 628;
  height: 628:
}
.element {  
  position: relative;
  width: 100%;
  min-height: 628px;
  background: url(https://images-prod.healthline.com/hlcmsresource/images/AN_images/health-benefits-of-apples-1296x728-feature.jpg) no-repeat center top;
  background-size: cover;
}
.element:before{
  content: '';
  position: absolute; bottom: 0; left: 0;
  width: 100%;0
  -webkit-clip-path: polygon(0 0, 0% 100%, 100% 100%);
  clip-path: polygon(0 0, 0% 100%, 100% 100%);
}
.circle-outer {
  cx: 200;
  cy: 720;
  fill: #fff;
  fill-opacity: 0.6;
  r: 420;
  w: 628;
  h: 628;
}
.circle-inner {
  cx: 200;
  cy: 720;
  fill: #fff;
  fill-opacity: 0.6;
  r: 400;
}
.hero-triangle {
  content: '';
  position: relative; 
  width: 100%;
  height: 100px;
  background: #fff;
  -webkit-clip-path: polygon(0 8%, 0% 100%, 100% 100%);
  clip-path: polygon(0 80%, 0% 100%, 100% 100%);
  z-index: 99;
}
<div class="container">
  <div class="element">
    <div class="hero-content">
    <h1>This belongs in circle</h1>
    <p>This belongs in circle too.</p>
    <button class="btn btn-primary">Learn more</button>
    </div>
    <svg viewbox width="1000" height="580" viewBox="0 0 100 100">
      <circle class="circle-outer" />
      <circle class="circle-inner" />
      <polygon points="0,0 0,200 1000,200" style="fill:#fff;" />
    </svg>
  </div>
</div>
<div class="container">
  <h4>Body content must be positioned right underneath hero image for all widths.</h4>

最佳答案

由于圆圈只是装饰性的而不是增加意义,因此它们没有必要成为元素。它们作为背景图像就足够了。

这是一个简单的代码片段,它放置内容元素并为其提供两个背景图像,两者都具有一定的透明度,使用径向渐变使它们成为圆形。

.element {
  position: relative;
  width: 100%;
  rmin-height: 628px;
  background: url(https://images-prod.healthline.com/hlcmsresource/images/AN_images/health-benefits-of-apples-1296x728-feature.jpg) no-repeat center top;
  background-size: cover;
  clip-path: polygon(0 0, 0 80%, 100% 100%, 100% 0);
  aspect-ratio: 1296 / 728;
}

.hero-content {
  position: absolute;
  left: -12.5%;
  top: 50%;
  width: 70%;
  padding-top: 5%;
  box-sizing: border-box;
  aspect-ratio: 1 / 1;
  background-image: radial-gradient(circle, rgba(255, 255, 255, 0.8) 0 65%, transparent 65% 100%), radial-gradient(circle, rgba(255, 255, 255, 0.2) 0 70%, transparent 70% 100%);
  background-repeat: no-repeat;
  background-size: 100% 100%;
  display: flex;
  align-items: center;
  rjustify-content: center;
  flex-direction: column;
}

.hero-content h1 {
  font-size: 2vw;
}

.hero-content p {
  font-size: 1vw;
}

.hero-content button {
  font-size: 1vw;
}
<div class="container">
  <div class="element">
    <div class="hero-content">
      <h1>This belongs in circle</h1>
      <p>This belongs in circle too.</p>
      <button class="btn btn-primary">Learn more</button>
    </div>
    <!--<svg viewbox width="1000" height="580" viewBox="0 0 100 100">
      <circle class="circle-outer" />
      <circle class="circle-inner" />
      <polygon points="0,0 0,200 1000,200" style="fill:#fff;" />
    </svg>
    -->
  </div>
</div>
<div class="container">
  <h4>Body content must be positioned right

注意:显然您需要更改字体大小的设置以适合您的特定用例。我只是使它们相对于视口(viewport),以便它具有响应性。

此外,我认为英雄是否必须覆盖整个宽度或设置最小高度之间存在一些混淆。当然,如果这是所需要的,那么圆圈将位于相对于苹果的不同位置,并且一些图像可能会消失。

关于html - 如何使用 CSS 向图像添加多层透明叠加层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74166650/

相关文章:

svg - 更改行图中的标签文本颜色

css - 渐变服务器作为 SVG 中的外部文件

javascript - 绕另一个旋转圆旋转一个圆

javascript - CSS/SVG/动画 : Apply transform origin to a single element within svg not working

javascript:如何提取在此表单中输入的值?

html - 为什么背景大小为 :cover act differently on mobile?

html - jpg 图像未在 HTML 中呈现

css - 我应该使用 Div、Span、Nav 还是标题标签

html - 隐藏和显示文本 css

css - Accordion 组标题的样式(ngx-bootstrap)?