我想在HTML/CSS中添加渐变效果。
假设某个DIV的高度始终大于400px。我希望添加渐变效果,使其顶部为#FFFFFF颜色,到300px时为#EEEEEE颜色。因此前300px(高度)呈现出漂亮的“白到灰”的渐变效果。在超过300px后,无论DIV的高度如何,背景颜色都保持为#EEEEEE。
我猜这与梯度停止点有关(?)
我该如何实现呢?
P.S. 如果IE不支持,我不在意。只要gecko和webkit浏览器能够正确显示即可。
我想在HTML/CSS中添加渐变效果。
假设某个DIV的高度始终大于400px。我希望添加渐变效果,使其顶部为#FFFFFF颜色,到300px时为#EEEEEE颜色。因此前300px(高度)呈现出漂亮的“白到灰”的渐变效果。在超过300px后,无论DIV的高度如何,背景颜色都保持为#EEEEEE。
我猜这与梯度停止点有关(?)
我该如何实现呢?
P.S. 如果IE不支持,我不在意。只要gecko和webkit浏览器能够正确显示即可。
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+ */
根据Mozilla的最新文档:https://developer.mozilla.org/en/CSS/-moz-linear-gradient
我已确认它在Firefox 3.6和Chrome 15中可用。
另一种方式
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;
top
已经过时了。 - Volker E.height: 400px;
background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#eee), color-stop(0.75, #eee));
你可能需要尝试一下0.75,因为它是你高度的百分比,但那应该可以解决问题。
height: 800px; background: -webkit-gradient(linear, left top, 0 300, from(#fff), to(#eee));}
它的高度为800像素,背景渐变颜色从白色到灰色,但会在300像素处停止。 - TNC300px
(顺便说一下,我没有投反对票)。与旧的 -webkit-linear-gradient
相比,它可以吞噬各种值,而旧的 -webkit-linear-gradient
只知道 0.0-1.0
和 0-100%
,无法停留在像素值。 - Silviu-Marian...top, 0 300, ...
;它应该是 ...top, 0 300px, ...
。 - Silviu-Marian首先,需要了解的是在渐变中可以使用超过2个的颜色停止点,但不能使用固定像素作为坐标,必须是百分比。
在您的情况下,您可以将第一个颜色停止点定义为0%,第二个颜色停止点定义为50%左右。我建议您使用渐变生成器,因为实现取决于浏览器。
我想到了
background: #FFFFFF; /* old browsers*/
background: -moz-linear-gradient(top, #FFFFFF 0%, #EEEEEE 50%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#FFFFFF), color-stop(50%,#EEEEEE)); /* webkit */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#FFFFFF', endColorstr='#EEEEEE', GradientType=0); /* ie */
最简单的解决方案是使用多个背景,并为背景的渐变部分定义一个大小,可以使用百分比或像素。
body {
background: linear-gradient(to right, green 0%, blue 100%), green;
background-size: 100px 100%, 100%;
background-repeat: no-repeat;
background-position: right;
}
html,
body {
height: 100%;
margin: 0;
}
根据需要,与浏览器前缀混合使用。
background: -moz-linear-gradient(top, #d7d7d7 0px, #f3f3f3 178px);
background: -webkit-gradient(linear, left top, left bottom, color-stop(0px,#d7d7d7), color-stop(178px,#f3f3f3));
background: -webkit-linear-gradient(top, #d7d7d7 0px,#f3f3f3 178px);
background: -o-linear-gradient(top, #d7d7d7 0px,#f3f3f3 178px);
background: -ms-linear-gradient(top, #d7d7d7 0px,#f3f3f3 178px);
background: linear-gradient(top, #d7d7d7 0px,#f3f3f3 178px);
这对我有用
您可以执行以下操作:
<div id="bgGen"></div>
那么
#bgGen{
height: 400px;
background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#eee), color-stop(0.75, #eee));
margin-bottom:-400px;
}
这有点作弊,但它有效...
.main-container {
position: relative;
width: 100%;
}
.gradient-container {
/* gradient code from 0% to 100% -- from colorzilla.com */
height: 115px; /* sets the height of my gradient in pixels */
position: absolute; /* so that it doesn't ruin the flow of content */
width: 100%;
}
.content-container {
position: relative;
width: 100%;
}
HTML:
<div class="main-container">
<div class="gradient-container"></div> <!-- the only thing added for gradient -->
<div class="content-container">
<!-- the rest of my page content goes here -->
</div>
</div>
这对我有用
background: rgb(238, 239, 240) rgb(192, 193, 194) 400px;
background: -webkit-linear-gradient(rgba(192, 193, 194, 1), rgba(238, 239, 240, 1) 400px);
background: -moz-linear-gradient(rgba(192, 193, 194, 1), rgba(238, 239, 240, 1) 400px);
background: linear-gradient(rgba(192, 193, 194, 1), rgba(238, 239, 240, 1) 400px);
background-repeat:repeat-x; background-color:#eeeff0;
还有人评论说为什么不只是制作一个渐变图像并将其设置为背景。我现在更喜欢使用 CSS,因为移动设计和访问者的数据使用受限,尽量减少图像。如果可以用 CSS 完成,那就这样做。