简体   繁体   English

CSS 渐变方向仅一节

[英]CSS gradient direction for only one section

I have a css gradient that looks like this我有一个看起来像这样的 css 渐变

在此处输入图像描述

And the code i used for this is as follows我用于此的代码如下

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%);

As you can see left 50% is consist of single color and the right 50% has a gradient from left to right.如您所见,左侧 50% 由单色组成,右侧 50% 具有从左到右的渐变。 I want to make that right gradient flows from bottom to top.我想让正确的渐变从下到上流动。 How can i do this?我怎样才能做到这一点?

By default gradients are applied top to bottom, in your class you have attributed a 90deg rotation to gradient, which by modifying the degree to 180 it results in bottom to top gradient.默认情况下,从上到下应用渐变,在 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%);

Refer to Here for more info请参阅此处了解更多信息

for this you have to use pseudo selector for this type of requirement its works perfectly for you.为此,您必须对此类要求使用伪选择器,它非常适合您。 as per your requirement it is not possible that you want vertical gradient and first 50% of width is a normal color for this we apply gradient to the full width of your page and then cover left 50% width using pseudo selector :before for more understanding follow the snippet code.根据您的要求,您不可能想要垂直渐变并且宽度的前 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>

apply height to the div is just for example its depends on your page content.将高度应用于 div 只是例如它取决于您的页面内容。

I hope this code is helpful for you.我希望这段代码对你有帮助。

Thank You...谢谢你...

I tried 2 methods, which suits you best you can use.我尝试了 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>

Thank you !谢谢 !

You need to consider multiple background layer where you make the top one (the one color layer) take only 50% of the width and place it on the left:您需要考虑多个背景层,使顶部(单色层)仅占宽度的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