我只想显示从子形状开始的父渐变.

我试着给白色背景的父母加::after来隐藏父母的背景 colored颜色 ,但是子元素的背景也变成了白色.

目前:

#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>

current

预期:

expected

此外,子元素的位置也是可变的.

推荐答案

这是我的解决方案:

.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;
}
<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>

另一种背景不随滚动变化的解决方案:

.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: '\fe3b';
  color: white;
  display: block;
  top: -60px;
  position: absolute;
  left: -20px;
  font-size: 90px;
}

.child:after {
  content: '\fe3c';
  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;
}
<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>

Html相关问答推荐

我需要主页按钮出现在中间

如何在不影响子列表的情况下在HTML(OL Li)上同时加数字和列表?

Font Awesome 6插入符号未显示

试图让三个Divs与下面的另外三个对齐

悬停时跳转的内容

子元素以元素X开始和结束的元素的XPath?

为所有必填字段添加所需的占位符文本(Angular Material)

有没有办法根据另一个项目调整FlexBox项目的大小?

如何从多个TO中获取一个范围标记以溢出其父标记

div居中碰撞问题

R 中的网页抓取数字?

如何从 div 内的属性中提取 href 和文本

文本不显示在 div 下方

CSS 网格跨度行到所有行

如何让一个 div 始终贴在容器的边缘?

图像比预期宽的网格列

弹出窗口溢出溢出:自动,我不明白为什么

HTML 标记未在页面上展开

我怎么能有 :not(.class):nth-of-type(even)?

列宽等于最宽列宽度的无界容器