Css 使用伪元素设置渐变图像背景

Css set gradient image background with pseudo element

我希望图像背景(之前是伪的)覆盖所有内容模态,此代码仅覆盖 200px 高度的内容。有可能吗?

.modal {
    width: 100%;
    max-width: 300px;
    max-height: 200px;
    overflow: scroll;
    background-color: #fff;
    padding: 25px 40px 15px; 
    box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.3);
    position: relative;
}
.modal:before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-image: linear-gradient(90deg, rgba(255, 79, 137, .3) 0%, rgba(255, 199, 150, .3) 100%);
    opacity: 0.3; 
}
 <div class="modal">
     <div class="content">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
      </div>
 </div>

为了实现您的目标,您应该插入另一个 div 作为包装内容。检查下面的代码:

.modal {
    width: 100%;
    max-width: 300px;
    height: 100%;
    position: relative;
}

.modal__wrap {
    max-height: 200px;
    overflow: auto;
    background-color: #fff;
    padding: 25px 40px 15px; 
    box-shadow: 0px 0px 5px 0px rgba(0,0,0,0.3);
}

.modal__wrap::before {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-image: linear-gradient(90deg, rgba(255, 79, 137, .3) 0%, rgba(255, 199, 150, .3) 100%);
    opacity: 0.3; 
  display:flex;
  flex-direction: column;
}
<div class="modal">
  <div class="modal__wrap">
       <div class="content">
          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum</p>
       </div>
  </div>
</div>

问题出在具有最大高度的父元素中的相对位置。