简体   繁体   English

CSS线性渐变:如何设置从底部开始计数的固定颜色停止位置

[英]CSS linear-gradient: How to set fixed color-stop location counting from bottom

I'm looking to create a gradient background for my content area. 我正在寻找为我的内容区域创建渐变背景。 The gradient would simply be solid white, fading in from zero opacity at the top and again fading back to zero opacity at the bottom. 渐变只是纯白色,从顶部的零不透明度渐渐消失,再次在底部消失回零不透明度。 As the content height is highly variable, relative color-stop locations don't fare well. 由于内容高度变化很大,相对的颜色停止位置并不好。

At the moment I have this CSS: 目前我有这个CSS:

background: linear-gradient(
  to bottom, 
  rgba(255,255,255,0) 0%,
  rgba(255,255,255,1) 500px,
  rgba(255,255,255,1) 90%,
  rgba(255,255,255,0) 100%
);

I'm looking to replace the 90% with something that would equal (content height) - 500px . 我希望用相同的东西(content height) - 500px替换90% (content height) - 500px Is this possible and how is it done? 这可能吗?它是如何完成的?

Thanks! 谢谢!

Simply use calc : 只需使用calc

 body { min-height:1500px; margin:0; background: linear-gradient( to bottom, rgba(255,255,255,0) 0%, rgba(255,255,255,1) 500px, rgba(255,255,255,1) calc(100% - 500px), rgba(255,255,255,0) 100% ); } html { background:pink; } 

Or consider multiple background where you can adjust background-size / background-position 或者考虑多个背景,您可以在其中调整background-size / background-position

 body { min-height: 1500px; margin: 0; background: /* Top part will take 500px*/ linear-gradient(to bottom, transparent, #fff) top/100% 500px, /*Bottom part will take 500px*/ linear-gradient(to top, transparent, #fff) bottom/100% 500px, /*Middle part will take (100% - 2*500px) */ linear-gradient(#fff,#fff) center/100% calc(100% - 1000px); background-repeat:no-repeat; } html { background: pink; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM