HTML/CSS渐变,在特定高度停止并继续使用纯色

30 html css gradient

我想在HTML/CSS中使用渐变.

假设一些DIV总是超过400px高.我想添加渐变,使其在顶部为#FFFFFF,在300px时为#EEEEEE.所以第一个300px(高度方面)是一个很好的'白色到灰色'渐变.在300px之后,无论DIV有多高,我都希望背景颜色保持#EEEEEE.

我想这与渐变停止有关(?)

我该怎么做?

PS如果在IE中不可能我不在乎.如果gecko和webkit浏览器能够正确显示,我会很好.

小智 31

background-color: #eee;
background-image:         linear-gradient(top, #fff 0%, #eee 300px); /* W3C */
background-image:    -moz-linear-gradient(top, #fff 0%, #eee 300px); /* FF3.6+ */
background-image: -webkit-linear-gradient(top, #fff 0%, #eee 300px); /* Chrome10+,Safari5.1+ */
Run Code Online (Sandbox Code Playgroud)

这是根据当前的Mozilla文档:https://developer.mozilla.org/en/CSS/-moz-linear-gradient.

我已经确认它适用于Firefox 3.6和Chrome 15.


sea*_*cob 11

替代方式

background-color: #eee;

background-image: -webkit-gradient(linear, 0 0, 0 100%, from(#fff), to(transparent));
background-image: -webkit-linear-gradient(top, #fff, transparent);
background-image: -moz-linear-gradient(top, #fff, transparent);
background-image: -o-linear-gradient(top, #fff, transparent);
background-image: linear-gradient(to bottom, #fff, transparent);

background-repeat:no-repeat;
background-size:100% 300px;
Run Code Online (Sandbox Code Playgroud)


TNC*_*TNC 10

height: 400px;    
background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#eee), color-stop(0.75, #eee));
Run Code Online (Sandbox Code Playgroud)

您可能需要使用0.75,因为它是您身高的百分比,但这应该可以解决问题.

  • 在这里你去:`身高:800px; background:-webkit-gradient(linear,left top,0 300,from(#fff),to(#eee)); }`那总是停在300px. (8认同)
  • 这应该被投票的另一个原因是因为它只适用于webkit,但是提问者说gecko和webkit是通缉的. (2认同)