简体   繁体   English

为什么线性渐变仅应用于页面的1%(CSS)?

[英]Why linear gradient is applied to just 1% of the page (CSS)?

When I tried to apply the linear gradient to a page in html using html tag in css the result is many tight strips, each one has it's own gradient. 当我尝试使用css中的html标签将线性渐变应用于html中的页面时,结果是许多紧条带,每个条带都有自己的渐变。

html

    {
    background-image:linear-gradient(to top, black, white);
    }

在此输入图像描述 but when I specified the height to be 100% it was applied to the hole page: 但当我指定高度为100%时,它被应用于孔页:

html
{background-image:linear-gradient(to top, black, white);
height:100%;
}

在此输入图像描述 and when I specified the height to be 1% the result was the same strips with same height. 当我指定高度为1%时,结果是相同的高度相同的条带。 在此输入图像描述

I'm confused and I don't know the reason behind that and why by default it's applied to just 1% despite I use the html tag (the root tag)? 我很困惑,我不知道背后的原因以及为什么默认情况下它只应用于1%,尽管我使用的是html标签(根标签)?

Don't use background-image but background only, and add background-attachment . 不要使用background-image ,但background而已,并添加background-attachment

If You use background-image and set height:100% for html while scrolling, You'll lose gradients. 如果在滚动时使用background-image并设置height:100%height:100% html ,则会丢失渐变。

Try this (just background and without height ): 试试这个(只是background ,没有height ):

html
{
    background:linear-gradient(to top, black, white);
    background-attachment:fixed;
}

The html is not full height. html不是全高。 It is probably something like eight pixels tall. 它可能像八像素高。 So setting the background image to a linear gradient executes that gradient over eight pixels. 因此,将背景图像设置为线性渐变会在八个像素上执行该渐变。 The default behavior for background images is to repeat indefinitely. 背景图像的默认行为是无限重复。 That is why the eight pixel gradient is repeated over and over. 这就是为什么一遍又一遍地重复八个像素梯度的原因。

If you add background-repeat: no-repeat you'll see just one small gradient, and if you set the height to 100% you'll see the gradient expand to fill the element. 如果你添加background-repeat: no-repeat你只会看到一个小渐变,如果你将高度设置为100%你会看到渐变展开以填充元素。

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

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