Well, I'm trying to create an SVG section separator. It worked like this:
<section id="section1">
</section>
<svg width="100%" height="100" viewBox="0 0 100 102" preserveAspectRatio="none">
<path d="M0 0 L50 100 L100 0 Z" />
</svg>
<section id="section2">
</section>
So far, so good. But now, I want to add a background to section1, including the SVG "pick", in example:
All I've accomplished is (really bad results):
Adding a
background: url(img)
to the element
And:
Justing adding a BG to section1
Here is an approach using the same code as your example but the svg path is changed to an inverted triangle and absolutely positioned to the bottom of the section:
#section1{
position:relative;
background:url('http://i.imgur.com/k8BtMvj.jpg');
background-size:cover;
height:200px;
}
svg{
position:absolute;
bottom:-10px; left:0;
width:100%; height:100px;
display:block;
}
<section id="section1">
<svg width="100%" height="100" viewBox="0 0 100 102" preserveAspectRatio="none">
<path d="M0 0 L50 90 L100 0 V100 H0" fill="#2A80B9" />
</svg>
</section>
Variant with a gradient:
.element {
display: block;
position: relative;
width: 100%;
height: 200px;
background: linear-gradient(-164deg, transparent 75%, #2A80B9 75%, #2A80B9 100%), linear-gradient(164deg, transparent 75%, #2A80B9 75%, #2A80B9 100%), url(http://i.imgur.com/k8BtMvj.jpg);
background-size: auto, auto, cover;
overflow: hidden;
}
<div class="element"></div>
First of all, I'm well aware this doesn't answer the question directly, however the questioner stated in the comments that they're interested in a non-SVG solution as well, and for reasons explained later in the post, it's a much better way to tackle this problem.
section {
background: url('http://i.imgur.com/k8BtMvj.jpg');
background-size: cover;
height: 200px;
position: relative;
width: 600px;
}
section:after {
border-color: transparent #2a80b9;
border-style: solid;
border-width: 90px 300px 0; /* the first value is the height of the triangles, the second is half the width of the parent container */
content: '';
height: 10px; /* this is the height of the solid color underneath the triangles */
position: absolute;
bottom: 0;
}
<section></section>
This solution works by absolutely placing an element at the end of every section, overlaying that and rendering the required shapes by use of borders - by giving the top border a transparent color.
This has the following qualities compared to an SVG solution:
there's no need for extra markup in every section because of the universally applying rule
that also means it's easier to maintain, because you don't have to go through multiple html files, looking for stray SVGs (which is why style should go in CSS and not markup in the first place)
changing the shape of the SVG requires changing several values, while you only need to change a single CSS value for anything you want to do. The CSS rules are also much more human-readable than the SVG multi-line anchor points (this might be subjective)
Variant with two triangles
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.element {
position: relative;
width: 100%;
height: 200px;
background: url(http://i.imgur.com/k8BtMvj.jpg) no-repeat center top;
background-size: cover;
}
.element:before,
.element:after{
content: '';
position: absolute; bottom: 0;
width: 0;
height: 0;
border-style: solid;
}
.element:before{
left: 0;
border-width: 100px 0 0 55vw;
border-color: transparent transparent transparent #00f;
}
.element:after{
right: 0;
border-width: 0 0 100px 55vw;
border-color: transparent transparent #00f transparent;
}
<div class="element"></div>
Variant clip-path
*{
padding: 0;
margin: 0;
box-sizing: border-box;
}
.element {
position: relative;
width: 100%;
height: 200px;
background: url(http://i.imgur.com/k8BtMvj.jpg) no-repeat center top;
background-size: cover;
}
.element:before{
content: '';
position: absolute; bottom: 0; left: 0;
width: 100%;
height: 100px;
background: #00f;
-webkit-clip-path: polygon(50% 95%, 100% 40%, 100% 100%, 0 100%, 0 40%);
clip-path: polygon(50% 95%, 100% 40%, 100% 100%, 0 100%, 0 40%);
}
<div class="element"></div>
Related
I'm trying to make a corner block, to create a one-page as in the photo below. But I ran into a problem.
I tried to make div slopes, but when looking at different resolutions it looked crooked.
What i need screen
(there was also a problem in that before these inclined divs there was a background image and some holes that left this div, the picture showed through.)My Fail Screen
.tri-index-right {
background: #fff;
height: 150px;
width: 100%;
transform: skewY(4deg);
overflow: hidden;
position: relative;
z-index: 2; /*fail method*/
}
I can not understand how to extend this angle using the CSS method at width 100%.
.1 {
min-width: 500px;
}
#triangle-left {
width: 0;
height: 0;
border-top: 10px solid transparent;
border-right: 100px solid red;
border-bottom: 100% solid transparent;
}
<div class="1">
<div id="triangle-left"></div>
</div>
I will be very grateful for the help
You can use vw for full width or use svg
.one {
width: 100%;
height: 200px;
background: url('https://imgur.com/a/kA3XA') center center no-repeat;
background-size: cover;
}
#triangle-left {
width: 0;
height: 0;
border-top: 100px solid transparent;
border-right: 100vw solid red;
}
<div class="one">
<div id="triangle-left"></div>
</div>
you can also use svg for this
.main {
position: relative;
min-height: 200px;
}
.svg-container svg {
width: 100%;
height: 150px;
fill: #333; /* change color to white since */
}
<div class="main">
<!-- main image -->
</div>
<div class="svg-container">
<svg xmlns="https://www.w3.org/2000/svg" version="1.1" class="separator" viewBox="0 0 100 100" preserveAspectRatio="none">
<path d="M0 100 L100 0 L100 100 Z"></path>
</svg>
</div>
you can also use multiple background image including a sharp gradient :
header {
min-height: 4em;
background:
/* first the mask */
linear-gradient(to bottom right, transparent 49.5%, white 50.5%) bottom left no-repeat,
/* then the background image */
url(http://lorempixel.com/400/300/abstract/1) 0 0;
/* finally resize each image, in particular the mask */
background-size: 100% 4em, cover;
padding: 1em 2em 4em;
color: white;
}
body {
margin: 0;
}
div {
padding: 1em;
}
<header>
<h1>whatever</h1>
</header>
<div>next content</div>
For a website I'm developing I need to include some diagonal shaped borders to a div. These are the main examples which I need to recreate.
double diagonal top border, triangle shaped
Now been scouting the web on how to achieve this, and my first thought as well would be by using ::before. However I can't get it to work without it being positioned absolute which messes up the entire page.
This is my code I have tried to achieve something like this:
.slider-container{
background-color: $blue;
width: 100%;
overflow: hidden;
position: relative;
.col-md-3{
img{
padding: 40px;
width: 100%;
max-width: 400px;
margin: auto;
}
}
&::before {
background: red;
bottom: 100%;
content: '';
display: block;
height: 100%;
position: absolute;
right: 0;
transform-origin: 100% 100%;
transform: rotate(-15deg);
width: 150%;
}
}
<section id="slider">
<div class="container-fluid">
<div class="row slider-container">
<div class="col-md-3">
<p>imgae 1</p>
</div>
<div class="col-md-3">
<p>imgae 2</p>
</div>
<div class="col-md-3">
<p>imgae 3</p>
</div>
<div class="col-md-3">
<p>imgae 4</p>
</div>
</div>
</div>
</section>
Note: it won't work in here but this is the result I get result
With just css and a bit tweaking based on your divs size you could create something like this:
.myclass {
width: 100px;
height: 100px;
background: linear-gradient(45deg, black 0%, black 26%, transparent 26%), linear-gradient(-45deg, black 0%, black 27%, transparent 27%)
}
.myclass2 {
width: 100px;
height: 100px;
background: linear-gradient(-45deg, blue 0%, blue 27%, transparent 27%), linear-gradient(45deg, blue 0%, blue 26%, red 26%)
}
With transparency:
<div class="myclass">My content here</div>
<br/>
Not as easy with transparent:
<div class="myclass2">My content here</div>
Edit: Just tested this in chrome, you might need special linear-gradients for older/other browsers.
The most simple way to achieve this would probably be to use a background image, though the effect may prove to be inconsistent on smaller devices. For this reason, you may want to consider using a hard-stop gradient.
.grad {
background: lightblue; /* For browsers that don't support gradients */
background: -webkit-linear-gradient(170deg, white 0%, white, 15%, lightblue 15%, lightblue 100%);
background: -o-linear-gradient(170deg, white 0%, white, 15%, lightblue 15%, lightblue 100%);
background: -moz-linear-gradient(170deg, white 0%, white, 15%, lightblue 15%, lightblue 100%);
background: linear-gradient(170deg, white 0%, white, 15%, lightblue 15%, lightblue 100%);
width: 100%;
padding: 20px;
}
<div class="grad">
<h1>Hard-stop gradient</h1>
<p>Using this type of gradient, you can create an angled background without using a background image.</p>
</div>
Using this, you can create a gradient from 0% to 15% that is white on both ends, followed by a gradient from 15% to 100% that's fully black. This completely removes the fading effect, giving you your angled background. It's probably the most efficient way as well since it only requires one line of CSS.
Something like this?
div {
background: yellow;
height: 150px;
overflow: hidden;
position: relative;
width: 300px;
}
div::before {
background: red;
bottom: 100%;
content: '';
display: block;
height: 100%;
position: absolute;
right: 0;
transform-origin: 100% 100%;
transform: rotate(-15deg);
width: 150%;
}
<div></div>
You can use clip-path.
body {
margin: 0;
padding: 0;
color: #ffffff;
}
.wrapper {
min-height: 100vh;
min-width: 100vw;
max-width: 100vw;
width: 100vw;
background-color: red;
}
.bg {
min-height: 100vh;
min-width: 100vw;
background-color: blue;
clip-path: polygon(80% 0, 100% 0, 100% 100%, 50% 100%);
}
<div class="wrapper">
<div class="bg"></div>
</div>
For me, the linear-gradient is not smooth ...
I would suggest either clip-path or svg:
svg {
display: block;
width: 100%;
height: 55px;
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 10" preserveAspectRatio="none">
<polygon points="100 0 100 10 0 10" fill="white" />
</svg>
.arrow-right {
width: 0;
height: 0;
border-top: 60px solid green;
border-bottom: 60px solid transparent;
border-left: 60px solid green;
}
How can we create the shape below in CSS?
I've searched everywhere but no luck.
Here are other possible ways to create this shape. Let us divide them in 2 broad categories:
CSS Based Approaches:
Below are few pure CSS based methods to create this shape:
1- Triangular Borders:
Create two overlays with ::before and ::after pseudo elements.
Apply CSS border properties to create triangular effect.
Place them in both bottom corners of the element with position: absolute.
Working Example:
.shape {
background: linear-gradient(skyblue, skyblue) no-repeat;
background-position: left bottom 30px;
position: relative;
min-height: 100px;
overflow: hidden;
}
.shape::before,
.shape::after {
border-left: 30vw solid skyblue;
border-top: 30px solid skyblue;
border-right: 30px solid transparent;
position: absolute;
content: '';
bottom: 0;
height: 0;
width: 0;
left: 0;
}
.shape::after {
border-right: 30vw solid skyblue;
border-top: 30px solid skyblue;
border-left: 30px solid transparent;
left: auto;
right: 0;
}
<div class="shape"></div>
2- Skewed Transformations:
Create two overlays with ::before and ::after pseudo elements.
Apply skewX() transformation to create the skewed corners.
Place them on each corner of the parent with position: absolute.
Working Example:
.shape {
background: linear-gradient(skyblue, skyblue) no-repeat;
background-position: left bottom 30px;
position: relative;
min-height: 100px;
overflow: hidden;
}
.shape::before,
.shape::after {
transform-origin: left bottom;
transform: skewX(-45deg);
position: absolute;
background: skyblue;
height: 30px;
left: -45px;
content: '';
width: 30%;
bottom: 0;
}
.shape::after {
transform-origin: right bottom;
transform: skewX(45deg);
right: -45px;
left: auto;
}
<div class="shape"></div>
3 - Linear-Gradient:
In this approach we will use CSS linear-gradient() function to draw this shape on the element as a background. As we can apply multiple background images on an element so we will divide this shape in small parts and draw them on the element with precisely controlled sizes and positions.
We can divide this shape in 3 parts and draw them each having specific size and position. I've tried to highlight each part in below image:
Working Example:
.shape {
background-image: linear-gradient(-45deg, transparent 25px, skyblue 25px),
linear-gradient(45deg, transparent 25px, skyblue 25px),
linear-gradient(skyblue, skyblue);
background-repeat: no-repeat;
background-size: 30% 100%, 30% 100%, 100% 100%;
background-position: left bottom, right bottom, left bottom 30px;
min-height: 100px;
}
<div class="shape"></div>
4 - Clip-Path:
Clipping means removing or hiding some parts of an element.
clip-path CSS property can be used to display a certain area of an element rather than displaying complete area. Any area that is outside the clipping region defined by this property will be hidden.
We can use polygon() basic shape to define the clipping region:
div.shape {
clip-path: polygon(0 0, 0 100%, 25% 100%, 28% 80%, 72% 80%, 75% 100%, 100% 100%, 100% 0);
}
Working Example:
.shape {
clip-path: polygon(0 0, 0 100%, 25% 100%, 28% 80%, 72% 80%, 75% 100%, 100% 100%, 100% 0);
background: green;
min-height: 100px;
}
<div class="shape"></div>
Note: This approach may not work in all browsers. Check for its Browser Support.
SVG Based Approaches:
1 - Polygon Shape:
In this approach we will use SVG's polygon element to create this shape and fill it with the required background color using fill property.
polygon element required one attribute points which contains a list of points that are joined together to draw a closed shpae.
Below is the necessary code:
<polygon points="0,0 0,30 25,30 28,22 72,22 75,30 100,30 100,0" />
Working Example:
.shape {
fill: skyblue;
}
<svg height="100px" width="100%" viewBox="0 0 100 30" preserveAspectRatio="none" xmlns="http://www/w3.org/2000/svg">
<polygon class="shape" points="0,0 0,30 25,30 28,22 72,22 75,30 100,30 100,0" />
</svg>
2 - Clip-Path:
This is the same concept as discussed in above section. However SVG has its own syntax to define a clipping region.
Working Example:
.shape {
fill: skyblue;
}
<svg height="100px" width="100%" viewBox="0 0 100 30" preserveAspectRatio="none" xmlns="http://www/w3.org/2000/svg">
<defs>
<clipPath id="shape">
<polygon points="0,0 0,30 25,30 28,23 72,23 75,30 100,30 100,0" />
</clipPath>
</defs>
<rect x="0" y="0" width="100" height="30" class="shape" clip-path="url(#shape)" />
</svg>
I think it is same as your picture
body{
background:black;
}
.outer{
margin:40px auto;
width:1200px;
height:100px;
background:#37FFF7;
}
.inner {
margin: 0 auto;
border-bottom: 30px solid white;
border-left: 70px solid transparent;
border-right: 70px solid transparent;
height: 0;
width: 700px;
position: relative;
top:70px;
}
<div class="outer">
<div class="inner"></div>
</div>
You Can use this code:
<!DOCTYPE html>
<html lang="en">
<head>
<title> Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/font-awesome-animation.min.css">
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<style type="text/css">
#trapezium {
height: 0;
width: 100%;
border-bottom: 60px solid white;
border-left: 50px solid transparent;
border-right: 50px solid transparent;
margin-top: 4%
}
</style>
</head>
<body>
<br>
<br><br><br>
<div class="container" style="background-color: rgb(55,255,247);height: 60px;">
<div class="col-lg-2"></div>
<div class="col-lg-8">
<div id="trapezium"></div>
</div>
<div class="col-lg-2"></div>
</div>
</body>
</html>
It may help you.
it can be done simply using the border property :
#inner{
position:relative;
margin:0 auto;
width:50%;
background:lightblue;
border-top:40px solid transparent;
border-bottom:40px solid white;
border-left:60px solid transparent;
border-right:60px solid transparent;
}
#parent{
width:100%;
height:80px;
background:lightblue;
margin-top:100px;
}
body{background:#000;}
<div id="parent">
<div id="inner"></div>
</div>
I was trying to accomplish this border for two divs with CSS:
I tried just using border-radius, but the two partial circles aren't pressed together: http://jsfiddle.net/uwz6L79w/
.left {
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 100px;
border-width: 4px;
border-color: black white black black;
border-style: solid;
border-radius: 60px
}
.right {
position: absolute;
left: 104px;
top: 0;
width: 100px;
height: 100px;
border-width: 4px;
border-color: black black black white;
border-style: solid;
border-radius: 60px;
}
<div class="left"></div>
<div class="right"></div>
I could just press them together further, but I'd have to have one div overlap the other, like this: http://jsfiddle.net/uwz6L79w/1/.
.left {
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 100px;
border-width: 4px;
border-color: black white black black;
border-style: solid;
border-radius: 60px
}
.right {
position: absolute;
left: 70px;
top: 0;
width: 100px;
height: 100px;
border-width: 4px;
border-color: black black black white;
border-style: solid;
border-radius: 60px;
background: #f2f2f2;
}
<div class="left"></div>
<div class="right"></div>
Does anyone know how I could accomplish this without having the divs overlap?
SVG
This is also possible using SVG.
The SVG version is very short as it mainly only requires an Arc command to control its shape, size and position.
<svg width="50%" viewbox="0 0 100 50">
<path d="M50,35
a20,20 0 1,0 0,-20
a20,20 0 1,0 0,20z"
fill="white"
stroke="black">
</path>
</svg>
SVG stands for Scalable Vector Graphic. The web browser views it as an image but you can add text and normal HTML elements within an SVG.
It is well supported across all browsers as viewable here: CanIUse
SVG | MDN
Using Borders: Recommended
You could do it the same way as in your second snippet and use positioning like in the below snippet to avoid the two div elements from overlapping. Here the circles are produced by pseudo-elements and the overlapping part is cut out using overflow: hidden on their parents.
One thing to note here is that any hover effect should be added on the pseudo-elements and not the parent elements. This is because if the :hover is attached to parent then it would be triggered even when hovering outside the circle (because the parent is still a square).
Out of all the three solutions provided in this answer, this is the one that has the best browser support and would work even in IE8. Hence, this is the recommended one.
.left, .right {
position: relative;
float: left;
height: 200px;
width: 200px;
/* border: 1px solid; uncomment to see that they aren't overlapped */
overflow: hidden;
}
.left:after, .right:after {
position: absolute;
content: '';
height: calc(100% - 12px); /* 12px because of 6px border on either side */
width: calc(100% - 12px); /* 12px because of 6px border on either side */
border-radius: 50%;
border: 6px solid gray;
}
.left:after { right: -20px; }
.right:after { left: -20px; }
<div class='left'></div>
<div class='right'></div>
Using Radial Gradients:
If you don't want to use pseudo-elements and a overflow: hidden on the parent then you could also make use of radial-gradient background images to produce the circle and position them such that they end up producing the required effect. Below is a sample snippet for this approach.
The downside of this approach is the low browser support for radial-gradient. It would not work in IE9 and lower. Plus, the circles produced by radial gradients are generally jagged (rough edges) and when we modify the color stop positions to make it smoother, it gives a slightly blurred appearance.
.left, .right {
float: left;
height: 200px;
width: 200px;
/*border: 1px solid; uncomment to see that they aren't overlapped */
}
/* generally the below code should be enough to produce 6px thick circular border
.left {
background: radial-gradient(circle at 70% 50%, transparent calc(50% - 3px), gray calc(50% - 3px), gray calc(50% + 3px), transparent calc(50% + 3px));
}
.right {
background: radial-gradient(circle at 30% 50%, transparent calc(50% - 3px), gray calc(50% - 3px), gray calc(50% + 3px), transparent calc(50% + 3px));
}
*/
/* but it produces jagged edges and so we can change the color stops a bit like below
this produces smoother circles but the disadvantage is that they'd look a bit blurred */
.left {
background: radial-gradient(circle at 70% 50%, transparent calc(50% - 4px), gray calc(50% - 2px), gray calc(50% + 2px), transparent calc(50% + 4px));
}
.right {
background: radial-gradient(circle at 30% 50%, transparent calc(50% - 4px), gray calc(50% - 2px), gray calc(50% + 2px), transparent calc(50% + 4px));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<div class='left'></div>
<div class='right'></div>
Using Clip Paths (CSS/SVG):
Another approach that could be used is to use clip-path. The advantage of this approach is that the hover effects would be triggered only when the cursor is within the circle (as can be seen in snippet). This is because the unnecessary portions are clipped.
Downside is again the poor browser support. CSS version of clip-path is supported only in Webkit but not in Firefox, IE whereas the SVG version (using inline SVG) is supported in Webkit, Firefox but not IE.
.left, .right {
float: left;
height: 200px;
width: 200px;
border-radius: 50%;
border: 6px solid gray;
}
/* CSS Clip Path - not supported by FF and IE */
.left.css-clip {
clip-path: polygon(0% 0%, 80% 0%, 80% 100%, 0% 100%);
}
.right.css-clip {
margin-left: -86px; /* 20% width * 2 (which is the clipped space) - border width */
clip-path: polygon(20% 0%, 100% 0%, 100% 100%, 20% 100%);
}
/* SVG Clip Path - supported by Webkit, FF but not IE */
.left.svg-clip {
clip-path: url(#clipper-left);
}
.right.svg-clip {
margin-left: -86px; /* 20% width * 2 (which is the clipped space) - border width */
clip-path: url(#clipper-right);
}
/* Just for demo */
h3{ clear: both; }
.left:hover, .right:hover{ background: red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<h3>CSS Clip Path</h3>
<div class='left css-clip'></div>
<div class='right css-clip'></div>
<h3>SVG Clip Path</h3>
<div class='left svg-clip'></div>
<div class='right svg-clip'></div>
<!-- Inline SVG for SVG Clip Path -->
<svg width='0' height='0'>
<defs>
<clipPath id='clipper-left' clipPathUnits='objectBoundingBox'>
<path d='M0,0 .8,0 .8,1 0,1z' />
</clipPath>
<clipPath id='clipper-right' clipPathUnits='objectBoundingBox'>
<path d='M.2,0 1,0 1,1 .2,1z' />
</clipPath>
</defs>
</svg>
Here's a solution using just a single <div>.
.shape is a transparent circle with a 10px red border.
.shape::before is an opaque white circle with a 10px red border.
.shape::after is an opaque white circle (no border).
.shape {
margin: 6px auto;
}
.shape, .shape::before, .shape::after {
display: block;
position: relative;
width: 160px;
height: 160px;
border-radius: 160px;
}
.shape, .shape::before {
border: 10px solid #f00;
}
.shape::before, .shape::after {
content: "";
background-color: rgba(255, 255, 255, 1);
}
.shape::before {
top: -10px;
left: -150px;
}
.shape::after {
top: -180px;
}
<div class="shape"></div>
Here's a quick example I came up with. I haven't tested it in different browsers but it should be fairly well-supported.
HTML:
<div class="one"></div>
<div class="two"></div>
CSS:
div {
background: #fff;
border-radius: 50%;
float: left;
height: 100px;
position: relative;
width: 100px;
}
.one:after,
.two:after{
/* adjust this to set the border color */
background: #666;
border-radius: 50%;
content: "";
position: absolute;
z-index: -1;
/* adjust these to set the border width */
top: -5px;
right: -5px;
bottom: -5px;
left: -5px;
}
.two {
/* adjust this to set the overlap of the circles */
margin-left: -20px;
}
Live Demo
I'm coming back to this question (after 6 weeks), purely because the top-voted answer piqued my academic curiosity in svg, which I've rarely come across and never taken the time to learn.
Since I'm now learning svg, this question (which sent me off on my quest to learn it in the first place) seemed like the ideal challenge against which to try out some new skills.
So here is an alternative svg solution, the equivalent of my single <div> css solution above:
svg {
width: 310px;
height: 180px;
}
svg circle {
stroke: rgb(255,0,0);
stroke-width: 10;
fill: rgb(255,255,255);
}
svg circle:nth-of-type(3) {
stroke: rgb(255,255,255);
}
<svg viewbox="0 0 310 180">
<circle cx="90" cy="90" r="80" />
<circle cx="220" cy="90" r="80" />
<circle cx="90" cy="90" r="70" />
</svg>
I was wondering if it is possible to create a background effect like in the image below using just one div.
I know how to achieve it with two divs, but my situation would make it really easy if it could be done in one div (maybe using :after ???). The code for the gradients is:
.bg-green {
background-image: linear-gradient(-180deg, #95D428 0%, #20560B 100%);
}
.bg-red {
background-image: linear-gradient(-180deg, #F5515F 0%, #8E0A1E 100%;
}
Thanks :)
Yes, this is possible using a single div with a :pseudo-element.
What you could do is add the second linear-gradient to its :after :pseudo-element. Notice the use of rgba(r, b, g, a) instead of hex colors. This way you could control the opacity of the second linear-gradient by changing its alpha value.
body, html {
height: 100%;
margin: 0;
}
div {
width: 100%;
height: 100%;
position: relative;
background: linear-gradient(110deg, #5EDC29 45%, #FF0058 45%, #FF0058 100%);
z-index: -1;
}
div:after {
content: '';
position: absolute;
width: 100%;
height: 100%;
background-image: linear-gradient(-180deg, transparent 0%, rgba(0,0,0,0.6) 100%);
}
<div></div>
If you want the exact same gradient colors that you've posted in your question, you'll need clipPath.
body {
background: #222222;
margin: 0;
}
.bg {
width: 500px;
height: 300px;
background: linear-gradient(-180deg, #F5515F 0%, #8E0A1E 100%);
}
.bg-2 {
position: absolute;
width: 500px;
height: 300px;
top: 0;
z-index: -1;
background-image: linear-gradient(-180deg, #95D428 0%, #20560B 100%);
}
<svg width="500" height="300">
<defs>
<clipPath id="shape">
<path d="M300,0 L501,0 L501,301 L175,301z" />
</clipPath>
</defs>
<foreignObject clip-path="url(#shape)" width="500" height="300">
<div class="bg"></div>
</foreignObject>
</svg>
<div class="bg-2"></div>
You can get this effect, but you will need to set overflow hidden on the div and to set the background in a after pseudo class
.test {
width: 400px;
height: 300px;
border: solid 1px black;
position: relative;
overflow: hidden;
}
.test:after {
content: "";
position: absolute;
width: 160%;
height: 160%;
top: -30%;
left: -30%;
background-image: linear-gradient(-210deg, #95D428 0%, #20560B 100%), linear-gradient(-210deg, #F5515F 0%, #8E0A1E 100%);
background-position: top left, top right;
background-size: 50% 100%;
background-repeat: no-repeat;
-webkit-transform: rotate(30deg);
}
<div class="test"></div>
The after is rotated to get the inclined separation. The dimensions need to be bigger so as not to show missing corners.
And then, you assign the 2 linear gradients as 2 separate background-images,inclined an additional 30deg to compensate for the base inclination