【问题标题】:CSS3 linear gradient for top and bottom of element元素顶部和底部的 CSS3 线性渐变
【发布时间】:2012-11-27 03:31:18
【问题描述】:

我正在尝试为元素的顶部和底部获取线性渐变。在谷歌上找到的教程到处都是,所以我希望这里有人能提供帮助。

这是 HTML 和 CSS 代码:

CSS:

.content {background: -webkit-gradient(linear, center bottom, center top, from(#f5f5f5), to(#fff));  }
.separator {
    height:1px;
    border-bottom:1px solid #ebebeb;
    }

HTML:

<div class="content">

</div>

<div class="separator"></div>

<div class="content">

</div>

<div class="separator"></div>

<!-- etc... ->

我希望它产生这个:

当然,我使用的 CSS 和 HTML 在内容底部显示渐变。如何以最少的 HTML 同时使用顶部和底部来显示它?

我将在.content 中包含内容,所以我希望它的渐变成为背景图像。我可以为.separator 添加一个渐变,但渐变不会出现在下一个元素的后面。

我知道我可以使用背景图片,但我想避免这条路线,因为我有响应式设计。 (是的,我知道我可以使用响应式背景图像,但我想只使用 CSS,不使用图像。)

【问题讨论】:

  • 不知道我得到了你想要的。您在 .content div 中有内容,并且希望在每个内容 div 中都有一个从灰色到白色的整个渐变?还是您想要在所有内容区域后面有一个连续的?或者你想要一个白色到灰色到白色的渐变?
  • 我想要每个内容后面的渐变。我刚刚意识到我想我可以添加奇偶 CSS 序列。偶数元素从白色(顶部)到 whiteSmoke(底部)的渐变,奇数元素从 whiteSmoke(顶部)到白色(底部)的渐变。
  • 啊,所以它是每个内容区域的交替渐变,那么我同意,需要两个不同的渐变(即使它们是另一个相反的渐变),然后应用于偶数或根据需要奇数元素
  • 或不更改 html - jsfiddle.net/nYDAb/32

标签: html css


【解决方案1】:

我认为这对你有帮助

html

<div class="contentTop">

</div>

<div class="separator"></div>

<div class="contentBottom">

</div>

css

.contentTop{
    height:100px;
    background:#ffffff;
    /* For WebKit (Safari, Google Chrome etc) */
    background:-webkit-gradient(linear, left top, left bottom, from(#fff), to(#f1f1f1));
    /* For Mozilla/Gecko (Firefox etc) */
    background:-moz-linear-gradient(top, #fff, #f1f1f1);
    /* For Internet Explorer 5.5 - 7 */
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#fff, endColorstr=#f1f1f1);
    /* For Internet Explorer 8 */
    -ms-filter:"progid:DXImageTransform.Microsoft.gradient(startColorstr=#fff, endColorstr=#f1f1f1)";   
}
.contentBottom{
    height:100px;
    background:#f5f5f5;
    /* For WebKit (Safari, Google Chrome etc) */
    background:-webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#fff));
    /* For Mozilla/Gecko (Firefox etc) */
    background:-moz-linear-gradient(top, #f5f5f5, #fff);
    /* For Internet Explorer 5.5 - 7 */
    filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#f5f5f5, endColorstr=#fff);
    /* For Internet Explorer 8 */
    -ms-filter:"progid:DXImageTransform.Microsoft.gradient(startColorstr=#f5f5f5, endColorstr=#fff)";   
}
.separator{
    border-top:solid 1px #eaeaea;
    height:1px;
    font-size:0;
    line-height:0;
}

这里是jsFiddle File

【讨论】:

  • 感谢您的努力。唯一的问题是内容将被包含在 100px 的高度。
  • 如果您在 div 中添加内容,您可以删除高度...我添加高度只是因为我没有任何内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 2015-08-11
  • 1970-01-01
  • 2012-02-24
相关资源
最近更新 更多