繁体   English   中英

CSS 渐变方向仅一节

[英]CSS gradient direction for only one section

我有一个看起来像这样的 css 渐变

在此处输入图像描述

我用于此的代码如下

background: rgb(2,0,36);
background: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(243,249,252,1) 0%, rgba(243,249,252,1) 50%, rgba(231,244,249,1) 50%, rgba(255,255,255,1) 100%);

如您所见,左侧 50% 由单色组成,右侧 50% 具有从左到右的渐变。 我想让正确的渐变从下到上流动。 我怎样才能做到这一点?

默认情况下,从上到下应用渐变,在 class 中,您将 90 度旋转归因于渐变,通过将度数修改为 180,它会导致从下到上渐变。

background: rgb(2,0,36);
background: linear-gradient(180deg, rgba(2,0,36,1) 0%, rgba(243,249,252,1) 0%, rgba(243,249,252,1) 50%, rgba(231,244,249,1) 50%, rgba(255,255,255,1) 100%);

请参阅此处了解更多信息

为此,您必须对此类要求使用伪选择器,它非常适合您。 根据您的要求,您不可能想要垂直渐变并且宽度的前 50% 是正常颜色,为此我们将渐变应用到页面的整个宽度,然后使用伪选择器覆盖左侧 50% 宽度:before以获得更多理解按照片段代码。

 .bg_container { position: relative; background: rgb(2,0,36); background: linear-gradient(0deg, rgba(231,244,249,1) 0%, rgba(255,255,255,1) 100%); }.bg_container:before { position: absolute; top: 0; bottom: 0; left: 0; right: 50%; background: rgba(243,249,252,1); z-index: 0; content: ""; }.page_content{ position: relative; }
 <div class="bg_container" style="height: 400px;"> <div class="page_content"> hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii </div> </div>

将高度应用于 div 只是例如它取决于您的页面内容。

我希望这段代码对你有帮助。

谢谢你...

我尝试了 2 种方法,最适合您使用。

 #grad1 { background: linear-gradient(90deg, red 50%, transparent 50%), linear-gradient(0deg, red 30%, blue 70%); position: relative; z-index: 0; padding: 15px; } /*********************/ #grad2 { position: relative; z-index: 0; padding: 15px; } p { color: white; } #grad2:before { content: ""; background: linear-gradient(0deg, red 30%, blue 70%); position: absolute; top: 0; right: 0; width: 50%; height: 100%; z-index: -1; } #grad2:after { content: ""; background: linear-gradient(90deg, red 30%, green 70%); position: absolute; top: 0; left: 0; width: 50%; height: 100%; z-index: -1; }
 <div id="grad1"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div> <br> <br> <div id="grad2"> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> </div>

谢谢 !

您需要考虑多个背景层,使顶部(单色层)仅占宽度的50%并将其放在左侧:

 html { height:100%; background: linear-gradient(rgba(243,249,252,1),rgba(243,249,252,1)) left/50% 100%, linear-gradient(to top, rgba(231,244,249,1), rgba(255,255,255,1) ); background-repeat:no-repeat; }

暂无
暂无

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

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