简体   繁体   English

CSS响应横幅与斜影

[英]CSS Responsive banner with oblique shadow

I'm trying to create a background for a banner using css where one side has a color and on the other side has another one with a 45° cut like this 我正在尝试使用css为横幅创建背景,其中一侧有颜色,另一侧有另一个有45°切割的横幅,如下所示

例

I've been able to recreate the above image except for the drop shadow that doesn't stay in the right position. 我已经能够重新创建上面的图像,除了没有留在正确位置的投影。 Any advice would be greatly appreciated. 任何建议将不胜感激。

This is my code code: 这是我的代码:

 #container { height: 100px; width: 400px; overflow: hidden; background-color: #2962ff; } #triangle-topleft { width: 0; height: 0; border-top: 100px solid #2196f3; border-right: 400px solid transparent; -webkit-box-shadow: 5px 5px 20px 0px rgba(0,0,0,0.75); -moz-box-shadow: 5px 5px 20px 0px rgba(0,0,0,0.75); box-shadow: 5px 5px 20px 0px rgba(0,0,0,0.75); } 
 <div id="container"> <div id="triangle-topleft"></div> </div> 

The CSS triangle trick with border can not be used for this, as a shadow will still be applied to the box, and not only to the triangle. 带边框的CSS三角形技巧不能用于此,因为阴影仍将应用于框,而不仅仅应用于三角形。

You will have to create a pseudo element, rotate it and THEN apply shadow to it. 你将不得不创建一个伪元素,旋转它然后应用阴影。

 #container { position: relative; height: 200px; width: 200px; overflow: hidden; background-color: grey; } #container:before { content: ''; position: absolute; left: 20%; width: 100%; height: 200%; background-color: rgb(255, 255, 255); /* fallback */ background-color: rgba(255, 255, 255, 0.5); top: 0; -webkit-transform: rotate(45deg); -moz-transform: rotate(45deg); transform: rotate(45deg); box-shadow: inset 0 0 20px 10px #333; } 
 <div id="container"></div> 

Basically you create a rectangle which is larger than the parent, then rotate it and apply a shadow. 基本上,您创建一个大于父级的矩形,然后旋转它并应用阴影。 You can tweak the colors and rotation-degree for your needs 您可以根据需要调整颜色和旋转度

Demo: http://jsfiddle.net/b5TnZ/2032/ 演示: http//jsfiddle.net/b5TnZ/2032/

You can try gradient like below: 您可以尝试以下渐变:

 #container { height: 150px; background: linear-gradient(135deg,#2962ff 49.8%,rgba(0,0,0,0.75) 50%, #2196f3 calc(50% + 10px)); background-color:#2196f3; } 
 <div id="container"> </div> 

And simply replace the deg with to bottom right if you want the diagonal result: 如果您想要对角线结果,只需将deg替换to bottom right

 #container { height: 150px; width:50%; background: linear-gradient(to bottom right,#2962ff 50%,rgba(0,0,0,0.75) 50%, #2196f3 calc(50% + 10px)); background-color:#2196f3; } 
 <div id="container"> </div> 

You can add multiple color stops in Linear Gradients. 您可以在“线性渐变”中添加多个色标。 Use two color set. 使用两种颜色。

Gradient generated using Shapy 使用Shapy生成的渐变

 .canvas { display: flex; height: 100vh; align-items: center; justify-content: center; min-height: 100%; min-width: 100%; } .gradient-canvas { max-height: 100%; max-width: 100%; width: 100%; height: 100%; background: linear-gradient(127deg, rgb(31, 163, 209) 0%, rgb(31, 163, 209) 50%, rgb(25, 64, 208) 0%, rgb(46, 101, 223) 52%) 50% 50% / 100% 100% no-repeat; } 
 <div class="canvas"><div class="gradient-canvas"></div></div> 

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

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