Currently working on a web design project for a client where I designed a multi-layered diagonal background. I solved a single diagonal with;
background-color: #dbebde;
background-image: -webkit-linear-gradient(120deg, #dbebde 50%, #f8f8f8 45%);
min-height: 400px;
However, as seen in the image below, I need to add a smaller diagonal on the left side.
Does anyone have an idea on how to solve this specific issue?
You can use a single HTML element, let's say a <div>, and use pseudo-elements, particularly ::before and ::after, to create those shapes, without writing additional HTML elements.
You would draw the red one first:
body {
margin: 0;
}
.fullBox {
position: relative;
height: 100vh;
}
.diagonalBox {
background: #FFF;
overflow: hidden;
}
.diagonalBox::before,
.diagonalBox::after {
content: '';
position: absolute;
width: 200%;
height: 200%;
left: 0;
}
.diagonalBox::before {
background: #D00;
top: 10%;
transform: rotate(30deg);
transform-origin: top left;
}
<div class="fullBox diagonalBox"></div>
And then add the light mint green one on top of that:
body {
margin: 0;
}
.fullBox {
position: relative;
height: 100vh;
}
.diagonalBox {
background: #FFF;
overflow: hidden;
}
.diagonalBox::before,
.diagonalBox::after {
content: '';
position: absolute;
width: 200%;
height: 200%;
left: 0;
}
.diagonalBox::before {
background: #D00;
top: 10%;
transform: rotate(30deg);
transform-origin: top left;
}
.diagonalBox::after {
background: #DFD;
top: 100%;
transform: rotate(-30deg);
transform-origin: bottom left;
}
<div class="fullBox diagonalBox"></div>
Keep in mind that your may need to adjust the dimensions and positions of the pseudo-elements.
I suggest you using 2 DIVs and give one of them a gradient with transparent color.
HTML :
<div class="outer">
<div class="inner"></div>
</div>
CSS:
.outer,.inner{
position:fixed;
top:0;
bottom:0;
left:0;
right:0;
}
.outer {
background-color: #dbebde;
background-image: -webkit-linear-gradient(50deg, red 70%, #f8f8f8 65%);
background-image: -moz-linear-gradient(50deg, red 70%, #f8f8f8 65%);
background-image: -o-linear-gradient(50deg, red 70%, #f8f8f8 65%);
background-image: -ms-linear-gradient(50deg, red 70%, #f8f8f8 65%);
}
.inner{
background-color: transparent;
background-image: -webkit-linear-gradient(120deg, #dbebde 60%, transparent 55%);
background-image: -moz-linear-gradient(120deg, #dbebde 60%, transparent 55%);
background-image: -o-linear-gradient(120deg, #dbebde 60%, transparent 55%);
background-image: -ms-linear-gradient(120deg, #dbebde 60%, transparent 55%);
}
You can see it in action:
https://codepen.io/FaridNaderi/pen/LLBVqw
Hope at least it helps you.
Related
I was asked with making such a background on a responsive site. I thought about preparing two divs using gradient, but it is highly problematic. Is it even possible to do it? Using this as a background-image is cumbersome for higher and lower resolutions.
Any ideas?
some clip-path and pseudo element can approximate this:
.box {
width: 300px;
aspect-ratio: .8;
position: relative;
z-index: 0;
}
.box:before,
.box:after {
content: "";
position: absolute;
z-index: -1;
inset: 0;
}
.box:before {
clip-path: polygon(0 0, 100% 50%, 10% 100%,0 100%);
background: linear-gradient(40deg, #3185c5, #0ea1b1);
}
.box:after {
clip-path: polygon(100% 30%, 100% 50%, 10% 100%,0% 100%, 0 80%);
background: linear-gradient(40deg, #3185c5, #f55778);
}
<div class="box"></div>
I'm trying to create a repeated background existing out of two parts. Each part is a gradient and while the one moves up, the other moves down.
The best I got is this:
html {
background: black;
color: #4c4c4c;
}
body {
margin: 30vh auto;
max-width: 80vw;
}
.wave {
background: none;
height: 1rem;
width: 50%;
position: absolute;
z-index: 2;
animation: move 700ms 0ms steps(2) infinite both;
}
.color::after,
.color::before {
content: "";
position: absolute;
left: 0;
right: 0;
height: 100%;
top: 0;
}
.color {
background-image: linear-gradient(#fe0000 50%, #6531ff 0 100%);
}
.color::after {
background: linear-gradient(to bottom, #f4e04d, #3bceac 20%, rgba(22, 22, 22, 0) 100%), linear-gradient(to right, #042a2b 3rem, transparent 3rem, transparent 6rem);
}
.wave,
.color::after,
.color::before {
background-size: 5rem 1rem;
background-repeat: repeat-x;
}
#keyframes move {
0% {
margin-top: -3rem;
}
100% {
margin-top: -3.25rem;
}
}
<div class="color wave"></div>
I get why this doesn't work, but not sure how to proceed.
Since it is difficult to describe, here is an image of what I'm looking for:
At first (position 1), all odd blocks are higher than the even blocks. After the first animation, it's the other way around (position 2) and so on.
Maybe like below:
.box {
height:100px;
background:linear-gradient(red,blue,yellow,red) 0 0/100% 200%;
animation:y 2s linear infinite;
}
.box::after {
content:"";
display:block;
height:100%;
background:linear-gradient(green,lightblue,pink,green) 0 0/100% 200%;
animation:inherit;
animation-direction: reverse;
-webkit-mask:linear-gradient(90deg,#fff 50%,transparent 0) 0 0/20% 100%;
}
#keyframes y {
to {
background-position:0 -200%;
}
}
<div class="box"></div>
UPDATE: This is an interesting problem. I'm surprised to find that I don't have an obvious or particularly elegant solution to having a gradient running vertically while repeating with horizontal gaps.
Far more elusive than I initially expected.
Best I could come up with is to put one of the gradients in a pseudo element and apply a mask-image. This won't work in IE, but it appears to be supported everywhere else.
See updated demo below.
If I understand what you're trying to do, I think you could accomplish it by animating the background positions:
.demo {
height: 200px;
background-image:
linear-gradient(#f4e04d, #3bceac 20%, rgba(22, 22, 22, 0) 100%);
animation: move 0.7s infinite alternate;
background-size: 3rem;
position: relative;
}
.demo::before {
content: '';
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: linear-gradient(#042a2b, transparent);
/* This is the magic part: using a horizontal repeating-linear-gradient
to mask out "columns", allowing the container's background gradient to
show through */
-webkit-mask-image: repeating-linear-gradient(to right, black 0 3rem, transparent 3rem 6rem);
background-size: 3rem;
/* run the same animation in reverse to animate up instead of down */
animation: move 0.7s infinite alternate-reverse;
}
#keyframes move {
from {
background-position: 0 0;
}
to {
background-position:
0 200px;
}
}
<div class="demo"></div>
It's difficult to infer exactly what you're trying to do, but here's another sample (very similar to #ray hatfield's answer) that will move the first background down while the second background moves up:
.sample {
width: 250px;
height: 50px;
position: relative;
background-image: linear-gradient(to bottom, #f4e04d, #3bceac 20%, rgba(22, 22, 22, 0) 100%), linear-gradient(to right, #042a2b 3rem, transparent 3rem, transparent 6rem);
animation: move 1s infinite linear;
}
#keyframes move {
0%, 100% {
background-position: 0 -75px, 0 0;
}
50% {
background-position: 0 0, 0 -75px;
}
}
<div class="sample"></div>
How can I generate a gradient like this image?
This is the closest I've got so far, but it is not yet the same:
.background {
position: relative;
background-image: radial-gradient(circle at top right, #ff9bb4, #ff507b, #ff7a2d, #f55a00 ) ;
width: 100%;
height: 300px;
-webkit-transform: skewY(-1.5deg);
-moz-transform: skewY(-1.5deg);
-ms-transform: skewY(-1.5deg);
-o-transform: skewY(-1.5deg);
transform: skewY(-1.5deg);
}
<div class="background"></div>
Maybe you could go at this by layering semi transparent gradients:
CSS
.grad {
width: 100%;
height: 100vh;
position: absolute;
top: 0;
}
.outer {
background: linear-gradient(45deg, rgba(255,83,21,0.21) 0%,rgba(254,55,112,0.87) 100%);
}
.inner {
background: linear-gradient(45deg, rgba(255,83,21,0.5) 0%,rgba(254,55,112,0.87) 100%);
}
HTML
<div class='outer grad'>
<div class='inner grad'></div>
</div>
If you need it, the rotation in the image above can be achieved with transform: rotate(10deg) rather than the skew.
To create the gradient, this could be helpful for you:
Gradient generator
If you would like to have the border as well, I would set the gradient as the background of a div and set the border of the div the way you want.
Created a pen: https://codepen.io/Raphael_Bucher/pen/wyQRYr
HTML
<div class="gradient-wrapper">
<div>
CSS
.gradient-wrapper {
height: 250px;
width: 500px;
margin-top: 50px;
background-image: linear-gradient(to right bottom, #fa9049, #ff8153,
#ff7260, #ff656e, #fe597e);
transform: skewY(-4deg);
-moz-transform: skewY(-4deg);
-webkit-transform: skewY(-4deg);
}
What I am trying to achieve is that I have gradient on the div background, and I am trying to add a background image over the gradient(image is just a pattern) but either only gradient is being applied or only the pattern, not both
I get a white background with this code:
#box{
padding:50px;
background:linear-gradient(45deg, #A50F06 0%, #51A351 100%) repeat scroll 0% 0% transparent url('bs-docs-masthead-pattern.png') repeat scroll center center transparent;
}
only gradient with this:
#box{
background:linear-gradient(45deg, #A50F06 0%, #51A351 100%) repeat scroll 0% 0%
}
#box:after{
background: transparent url('pattern.png') repeat scroll center center transparent;
}
what m I doing wrong?
Update your CSS like below.
#box{
background:url('https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSXwTmM2R_vmBHFpn720_8bGOaegnP5Kawh0wb4JggN5rUALGwGvw') no-repeat center center, linear-gradient(45deg, #A50F06 0%, #51A351 100%) repeat scroll 0% 0%;
height:500px;
}
FIDDLE DEMO
add content:"" in pseudo element
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
#box {
background: linear-gradient(45deg, #A50F06 0%, #51A351 100%) repeat scroll 0% 0%;
width: 100%;
height: 100%;
}
#box::after {
content: "";
background: url('http://s28.postimg.org/rfznph7ul/pattern.png');
width: 100%;
height: 100%;
display: block;
}
<div id="box"></div>
Example: http://cdpn.io/EaDdx
Next to the "Sample Title" you can see the arrowhead I'm trying to create. I'm trying to get this to work in the same manner as the rectangles and the circle, allowing it to act as a 'porthole' to it's own fixed background image.
I've tried everything from using borders to create a triangle (which blocked out the body's background image), to using various rotations, alternations of use between an actual background image and background gradient.
Right now I'm using basically using this method, as so:
.gradient-triangle {
width: 50px;
height: 50px;
position: absolute;
top: 0px;
left: -25px;
clip: rect(auto 25px 50px auto);
}
.gradient-triangle:after {
content: '';
position: absolute;
top: 4px;
bottom: 4px;
left: 9px;
right: 0px;
-webkit-transform:rotate(-45deg);
background-attachment: fixed;
background: -moz-linear-gradient(45deg, #000000 0%, #000000 50%, #ffffff 50%, #ffffff 100%);
background: -webkit-gradient(linear, left bottom, right top, color-stop(0%,#000000), color-stop(49%,#000000), color-stop(50%,#ffffff), color-stop(100%,#ffffff));
background: -webkit-linear-gradient(45deg, #000000 0%,#000000 49%,#ffffff 50%,#ffffff 100%);
background: -o-linear-gradient(45deg, #000000 0%,#000000 50%,#ffffff 50%,#ffffff 100%);
background: -ms-linear-gradient(45deg, #000000 0%,#000000 50%,#ffffff 50%,#ffffff 100%);
background: linear-gradient(45deg, #000000 0%,#000000 50%,#ffffff 50%,#ffffff 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#000000', endColorstr='#ffffff',GradientType=1 );
background-position: center center;
background-attachment: fixed;
border: 1px solid #fff;
}
This is mainly a chrome issue right now, though as you'll see, it does something different in each browser, which is pretty odd.
I haven't seen any other websites that do this sort of thing, using fixed background images, within elements, to offer the "porthole" style view. I'd be interested to see any that anyone may know of, to checkout how they handle things similar to this.
Edit: Just to clarify what I think is causing this. Usually a fixed background image is relative to the window, not the element it's assigned to. As soon as that element is rotated, the background image becomes relative to it. There's a good chance this is a browser bug, I'd just like to be sure.
I could get the following to work in Chrome:
CSS
.filler {
height: 2000px;
}
.test2 {
position: absolute;
width: 400px;
height: 370px;
left: 219px;
top: 10px;
background-attachment: fixed;
background-repeat: no-repeat;
background-position-x: -266px;
background-position-y: -107px;
}
.container {
-webkit-transform: rotate(45deg);
position: absolute;
margin-left: -275px;
margin-top: -116px;
clip: rect(-265px, 693px, 0px, 428px);
}
.container2 {
-webkit-transform: rotate(-45deg);
position: absolute;
}
.test {
position: absolute;
width: 186px;
height: 400px;
left: 300px;
top: 85px;
background-attachment: fixed;
background-repeat: no-repeat;
}
.test, .test2 {
background-image: url(http://placekitten.com/900/600);
}
fiddle
There is alot of hand-set positions; probably that could be set easier playing with transform-origins.
Also, I have a difference between positions in Chrome (on one side) and Firefox - IE (on the other). Still trying to understand why ..