【问题标题】:CSS : Positioning background gradient with fixed attachment at bottom right cornerCSS:在右下角使用固定附件定位背景渐变
【发布时间】:2014-10-22 16:42:40
【问题描述】:

我遇到了一个 CSS 问题。

我想像 here 一样将 CSS 条纹作为我的页面背景,但我希望条纹位于页面的右下角。

此外,我希望它是一个固定的背景附件。

我尝试了这里的建议:How to position background image in bottom right corner? (CSS),但它似乎只适用于背景图像,不适用于背景渐变。

我尝试在渐变定义中更改偏移量,但它仍然是相对于左上角的,如果窗口大小发生变化,结果会有所不同。

这是我当前的代码:

body
{
    background: linear-gradient(
        150deg,
        rgba(180,214,14,0.0) ,
        rgba(180,214,14,0.0) 70px,
        rgba(180,214,14,0.4) 80px,
        rgba(152,197,10,0.5) 150px,
        rgba(0,0,0,0.4) 151px,
        rgba(0,0,0,0) 160px
    ), no-repeat 0 0 !important;

    background-attachment: fixed !important;    
    /* background-position: 80% 80% !important; */
    background-repeat: no-repeat !important;
}

欢迎任何建议!

【问题讨论】:

    标签: css background gradient


    【解决方案1】:

    我认为您是正确的,因为 background-position 属性仅适用于图像而不适用于渐变。至少这是我在玩弄它时发现的。

    因此,这不是“如何使background-position 用于渐变”的答案,而是建议将渐变放在不同的元素上并将 IT 放置在右下角。

    喜欢:

    div {
      position: absolute;
      right: 0;
      bottom: 0;
      width: 160px;
      height: 160px;
      background: linear-gradient(
          150deg,
            rgba(180,214,14,0.0) ,
            rgba(180,214,14,0.0) 70px,
            rgba(180,214,14,0.4) 80px,
            rgba(152,197,10,0.5) 150px,
            rgba(0,0,0,0.4) 151px,
            rgba(0,0,0,0) 160px
      ), no-repeat 0 0;
      background-position: center;
    }
    

    当然,您可能必须更改渐变以更好地适应该元素,但我认为这可能是实现您想要做的事情的唯一方法。

    此外,您需要确保 body 具有 position: relative(或任何包含元素)。

    【讨论】:

    • 我尝试了您的代码,但仍然缺少一些东西。首先,我将绝对位置更改为固定,这我希望 div 与页面一起滚动,但碰巧的是条纹仍然被 div 的左边框而不是下边框停止,如果我调整宽度在 div 中,条纹不会从右侧开始,而是从 div 的中间开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-05
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多