父元素背景只能从子元素中可见。

huangapple go评论61阅读模式
英文:

Parent background only be visible from child element

问题

我想展示仅从子形状中显示父渐变。

我尝试在父元素中添加::after,并设置白色背景来隐藏父元素的背景颜色,但这样子元素的背景也变为白色。

当前:

#parent {
  background: linear-gradient(#e66465, #9198e5);
  width: 100px;
  height: 300px;
}

.child {
  position: relative;
  width: 50px;
  height: 50px;
  border: 1px solid blue;
  border-radius: 50%;
  color: white;
}

.top-10 {
  top: 10px;
}

.top-20 {
  top: 20px;
}

.top-40 {
  top: 40px;
}

.top-60 {
  top: 60px;
}
<div id="parent">
  <div class="child top-10"></div>
  <div class="child top-20"></div>
  <div class="child top-40"></div>
  <div class="child top-60"></div>
</div>

父元素背景只能从子元素中可见。

期望:

父元素背景只能从子元素中可见。

子元素位置也是可更改的。

英文:

I wanted to show the parent gradient just from child shape.

I tried adding ::after to parent with white background to hide parent background color, but then child's background also becomes white.

Currently:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#parent {
  background: linear-gradient(#e66465, #9198e5);
  width: 100px;
  height: 300px;
}

.child {
  position: relative;
  width: 50px;
  height: 50px;
  border: 1px solid blue;
  border-radius: 50%;
  color: white;
}

.top-10 {
  top: 10px;
}

.top-20 {
  top: 20px;
}

.top-40 {
  top: 40px;
}

.top-60 {
  top: 60px;
}

<!-- language: lang-html -->

&lt;div id=&quot;parent&quot;&gt;
  &lt;div class=&quot;child top-10&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-20&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-40&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-60&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

父元素背景只能从子元素中可见。

Expected:

父元素背景只能从子元素中可见。

Also the child positions are changeable.

答案1

得分: 2

这是我的解决方案:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-css --&gt;

    .child {
      position: relative;
      width: 50px;
      height: 50px;
      border: 1px solid blue;
      border-radius: 50%;
      color: white;
      background: linear-gradient(#e66465, #9198e5);
      background-attachment: fixed;
    }

    .top-10 {
      top: 10px;
    }

    .top-20 {
      top: 20px;
    }

    .top-40 {
      top: 40px;
    }

    .top-60 {
      top: 60px;
    }

&lt;!-- language: lang-html --&gt;

    &lt;div id=&quot;parent&quot;&gt;
      &lt;div class=&quot;child top-10&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;child top-20&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;child top-40&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;child top-60&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;

&lt;!-- end snippet --&gt;

另一个解决方案,背景不随滚动而改变:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-css --&gt;

    .child {
      position: relative;
      width: 50px;
      height: 50px;
      border: 1px solid blue;
      border-radius: 50%;
      color: white;
      outline: 10px solid white;
    }

    #parent {
      background: linear-gradient(#e66465, #9198e5);
      width: fit-content;
      height: 267px;
    }

    .child:before {
      content: &#39;\fe3b&#39;
      color: white;
      display: block;
      top: -60px;
      position: absolute;
      left: -20px;
      font-size: 90px;
    }

    .child:after {
      content: &#39;\fe3c&#39;
      width: 60px;
      height: 10px;
      color: white;
      display: block;
      top: 10px;
      position: absolute;
      left: -20px;
      font-size: 90px;
    }

    .top-10 {
      top: 10px;
    }

    .top-20 {
      top: 25px;
      /* 这里进行了修正 */
    }

    .top-40 {
      top: 40px;
    }

    .top-60 {
      top: 60px;
    }

&lt;!-- language: lang-html --&gt;

    &lt;div id=&quot;parent&quot;&gt;
      &lt;div class=&quot;child top-10&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;child top-20&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;child top-40&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;child top-60&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;

&lt;!-- end snippet --&gt;

Amir的解决方案:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-css --&gt;

    #parent {
      width: 100px;
      height: 300px;
    }

    .child {
      position: relative;
      width: 50px;
      height: 50px;
      border: 1px solid blue;
      border-radius: 50%;
      color: white;
      background: linear-gradient(#e66465, #9198e5);
      background-size: 50px 300px;
    }

    .top-10 {
      top: 10px;
      background-position: 0px -20px;
    }

    .top-20 {
      top: 20px;
      background-position: 0px -70px;
    }

    .top-40 {
      top: 40px;
      background-position: 0px -140px;
    }

    .top-60 {
      top: 60px;
      background-position: 0px -2000px;
    }

&lt;!-- language: lang-html --&gt;

    &lt;div id=&quot;parent&quot;&gt;
      &lt;div class=&quot;child top-10&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;child top-20&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;child top-40&quot;&gt;&lt;/div&gt;
      &lt;div class=&quot;child top-60&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;

&lt;!-- end snippet --&gt;
英文:

This is my solution:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.child {
  position: relative;
  width: 50px;
  height: 50px;
  border: 1px solid blue;
  border-radius: 50%;
  color: white;
  background: linear-gradient(#e66465, #9198e5);
  background-attachment: fixed;
}

.top-10 {
  top: 10px;
}

.top-20 {
  top: 20px;
}

.top-40 {
  top: 40px;
}

.top-60 {
  top: 60px;
}

<!-- language: lang-html -->

&lt;div id=&quot;parent&quot;&gt;
  &lt;div class=&quot;child top-10&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-20&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-40&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-60&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Another solution with background that is not changing on scroll:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.child {
  position: relative;
  width: 50px;
  height: 50px;
  border: 1px solid blue;
  border-radius: 50%;
  color: white;
  outline: 10px solid white;
}

#parent {
  background: linear-gradient(#e66465, #9198e5);
  width: fit-content;
  height: 267px;
}

.child:before {
  content: &#39;\fe3b&#39;;
  color: white;
  display: block;
  top: -60px;
  position: absolute;
  left: -20px;
  font-size: 90px;
}

.child:after {
  content: &#39;\fe3c&#39;;
  width: 60px;
  height: 10px;
  color: white;
  display: block;
  top: 10px;
  position: absolute;
  left: -20px;
  font-size: 90px;
}

.top-10 {
  top: 10px;
}

.top-20 {
  top: 25px;
  /* correction here*/
}

.top-40 {
  top: 40px;
}

.top-60 {
  top: 60px;
}

<!-- language: lang-html -->

&lt;div id=&quot;parent&quot;&gt;
  &lt;div class=&quot;child top-10&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-20&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-40&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-60&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Amir's solution:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#parent {
  width: 100px;
  height: 300px;
}

.child {
  position: relative;
  width: 50px;
  height: 50px;
  border: 1px solid blue;
  border-radius: 50%;
  color: white;
  background: linear-gradient(#e66465, #9198e5);
  background-size: 50px 300px;
}

.top-10 {
  top: 10px;
  background-position: 0px -20px;
}

.top-20 {
  top: 20px;
  background-position: 0px -70px;
}

.top-40 {
  top: 40px;
  background-position: 0px -140px;
}

.top-60 {
  top: 60px;
  background-position: 0px -2000px;
}

<!-- language: lang-html -->

&lt;div id=&quot;parent&quot;&gt;
  &lt;div class=&quot;child top-10&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-20&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-40&quot;&gt;&lt;/div&gt;
  &lt;div class=&quot;child top-60&quot;&gt;&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

.parent元素不设置其他样式。 子元素添加了两个重要的样式属性:background: linear gradient (#e66465, #9198e5); background-attachment: fixed;。 渐变用于分别绘制四个圆。

所有的样式代码:

.child {
  position: relative;
  width: 50px;
  height: 50px;
  border: 1px solid blue;
  border-radius: 50%;
  background: linear-gradient(#e66465, #9198e5);
  background-attachment: fixed;
  color: white;
}
.top-10 {
  top: 10px;
}
.top-20 {
  top: 20px;
}
.top-40 {
  top: 40px;
}
.top-60 {
  top: 60px;
}

注意:
background-attachment:fixed

参考链接

英文:

The parent element does not set other styles. The child element adds two important style attributes: background: linear gradient (# e66465, # 9198e5);background-attachment: fixed;. the gradient is used for four circles respectively.

all style codes:

.child {
  position: relative;
  width: 50px;
  height: 50px;
  border: 1px solid blue;
  border-radius: 50%;
  background: linear-gradient(#e66465, #9198e5);
  background-attachment: fixed;
  color: white;
}
.top-10 {
  top: 10px;
}
.top-20 {
  top: 20px;
}
.top-40 {
  top: 40px;
}
.top-60 {
  top: 60px;
}

be careful:
background-attachment:fixed

https://developer.mozilla.org/en-US/docs/web/css/background-attachment

huangapple
  • 本文由 发表于 2023年3月9日 21:15:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685135.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定