I have this repeating linear gradient CSS design.
#div1{
background: repeating-linear-gradient(red, yellow 10%, green 20%);
}
I know what is meant by the color parameters but the percentage right after the color seems vague. What is it really meant for ? Is that identifies the position where the color must start? or what?
I already read the documentation, but I couldn't understand it. Can someone explain it to me in simpler words ?
Thank you.
OK, I'll give this a shot...but first the documentation is actual at w3.org...not W3Schools!
This is your code (assuming a div height of 100px):
#div1 {
background: repeating-linear-gradient(red, yellow 10%, green 20%);
height: 100px;
}
<div id="div1"></div>
So your final % figure is 20%, which means, in the case of a repeating gradient, that the gradient itself will end at 20% of the height of the element and then repeat.
So 100% / 20% = 5 so the gradient repeats 5 times.
See what happens when the last number is changed
#div1 {
background: repeating-linear-gradient(red, yellow 10%, green 33.33%);
height: 100px;
}
<div id="div1"></div>
Now as for each percentage, lets try this will a standard left to right gradient
#div1 {
background: linear-gradient(to right, red, yellow 10%, green 20%);
height: 100px;
}
<div id="div1"></div>
Here the color stops work like this.
The gradient starts out red, and changes gradually until it's yellow at 10% of the width. The color will start changing again, this time to green, immediately until it's fully green at 20% of the width...and then stays that color as no other color is mentioned after wards.
For a solid band of color (say yellow) the color has to be stated twice at different values
#div1{
background: linear-gradient(to right, red, yellow 10%, yellow 20%, green 30%);
height: 100px;
}
<div id="div1"></div>
You can get a hard line change by repeating the percentage values when changing from one color to another
#div1 {
background: linear-gradient(to right, red 10%, yellow 10%, yellow 20%, green 20%);
height: 100px;
}
<div id="div1"></div>
Related
I applied a css code to create a shadow on the initial part of the page (I have a social network and I would like to create the type of shadow created by Facebook on user profile pages, just to get an idea).
Everything would seem ok for some pages but not for others. That is, for short pages, the shading might be acceptable, but for long pages, the shading increases as the page length increases, almost encroaching on the page. I tried to set specific height values but without success, indeed if I set a "height" value an unwanted dividing line is created.
The code I'm using is as follows:
background: black;
background: -moz-linear-gradient(top, black 0%, #f0f2f5 10%);
background: -webkit-linear-gradient(top, black 0%, #f0f2f5 10%);
background: linear-gradient(to bottom, black 0%, #f0f2f5 10%);
Some idea?
Thank you!
If you use pixel values instead of percentage values you can keep the gradient height uniform.
div {
background: linear-gradient(to bottom, black 0px, #f0f2f5 50px);
width: 100px;
}
body {
display: flex;
}
<div style="height: 100px;"></div>
<div style="height: 400px;"></div>
I'm trying to make a gradient that, for branding purposes, must be (1) at a specific angle, and (2) the top of it must be inset by a specific amount:
The rest of the proportions don't matter. I created this gradient like this:
background: linear-gradient(75deg, white 0%, black 30%, blue 30%, white 100%);
This only produces the correct top inset at specific client area height/widths. At other sizes, offset can be different:
Again, for branding reasons, this is unacceptable. That top of that slope must be inset by a specific amount.
I tried use a pixel value for the inset, e.g.
background: linear-gradient(75deg, white 0%, black 125px, blue 30%, white 100%);
This works... for the bottom of the slope. The bottom will be offset by 125px at any shape/size:
I'd like to do the same thing but have the top offset fixed to 125px.
You can approximate it using pseudo element and rotation. You consider a straight gradient (90deg) then you rotate it by adjusting the transform-origin to have the distance you want on the top:
.box {
height:300px;
position:relative;
overflow:hidden;
}
.box:before {
content:"";
position:absolute;
/* a random big value for top bottom and left*/
top:-500px;
bottom:-500px;
right:0;
left:-500px;
/**/
/* in the below 625px = 125px + 500px and adjust the 350px to get close to the gradient you wnat*/
background: linear-gradient(90deg, white 350px , black 625px, blue 0, white);
transform:rotate(-15deg);
transform-origin:625px 500px;
}
<div class="box">
</div>
In this example I have a gradient of 2 colors, alignd to right.
background: linear-gradient(to right, #c4d7e6 50%, #66a5ad 50%, #66a5ad 50%);
Is there any way I can have more than 2 colors? For example may I add red color on the right of the second one?
Sure, just add color stops at every (100/numColors)%
div {
background:linear-gradient(to right, #c4d7e6 0, #c4d7e6 33%, #66a5ad 33%, #66a5ad 66%, #ff0000 66%, #ff0000 100%);
width: 100%;
height:64px;
}
<div></div>
You can use multiply background, like this:
background: linear-gradient(to right, #000, #66a5ad, #66a5ad, red);
Also see this codepen for much combinations.
Late answer but no doubt it will help someone else in the future...
I have found a good website called CSS Gradient that generates your gradient color with full control and allows you to copy the CSS code.
This gradient was generated by this website:
div{
width: 100%;
height: 200px;
background: rgb(255,0,0);
background: linear-gradient(90deg, rgba(255,0,0,1) 0%, rgba(30,250,0,1) 49%, rgba(4,0,255,1) 100%);
<div>
</div>
Assuming my linear CSS gradient looks like this:
background: linear-gradient(to right, red 0%, green 20%, blue 40%, purple 60%, yellow 80%, black 100%)
It will generate a CSS gradient that looks like this:
How do I make the same gradient but with solid colors without the transitioning between the colors? (using CSS)
Something similar to this:
Thanks
Like this
.gradient {
width: 500px;
height: 200px;
background: linear-gradient(to right,
red 20%,
green 20%,
green 40%,
blue 40%,
blue 60%,
purple 60%,
purple 80%,
yellow 80%,
yellow 100%
);
}
<div class="gradient"></div>
Gradient is created through blending of colors. You can control the blending effect of colors by specifying range for each color like in below example of .flag. In .flag:
the color #00ae00 will be applied till 33.3% of the div
from that point till 66.6%, white will be applied
in the end orange will start from 66.6% till the end of the box
This way you can add as many colors as you want.
But one thing keep in mind, it looks fine when the degree is straight, if you change the angle, in some cases edges of colors may not look smooth (depends upon color and screen density) as you can see in .pixeleted
.flag{
background: linear-gradient(to right, #00ae00 33.3%, white 33.3%, white 66.6%, orange 66.6%);
margin-right: 20px;
}
.pixeleted{
background: linear-gradient(30deg, red 33.3%, black 33.3%, black 66.6%, red 66.6%);
}
div {
width: 290px;
height: 145px;
border: 2px solid #999;
display: inline-block;
}
<div class="flag"></div>
<div class="pixeleted"></div>
you don't have to repeat the colors. It's also possible to write it like this:
background: linear-gradient(to right,
red 20%,
green 0 40%,
blue 0 60%,
purple 0 80%,
yellow 0 100%
);
So I have two adjacent fluid divs that can change their width when the device size changes, when the user changes the font-size of the browser, or when zooming the browser with Ctrl +.
I want to have a diagonal that crosses both divs, like this:
_____
|blah|
|blah|
|blah|
| /| <--- First div or section
|__/_|
| / | <--- Second div or section
|/ |
|blah|
|blah|
|blah|
|____|
<----> fluid width
I'm trying to do it with CSS to avoid downloading images and making extra http requests. So I am using linear gradients. The problem is that the angle of the diagonal must change when the width of its container increases or decreases, otherwise the diagonal breaks.
See a fiddle here: http://dabblet.com/gist/50db5e6220b5ba557b9e
For the record, the code of the above fiddle is:
/*
* Diagonal that crosses both divs without breaking
*/
div, p{margin:0;}
div{padding:0 1em;}
.one{
background: #f06;
background: linear-gradient(170deg, #f06 80%, yellow 80%, yellow);
font-size: 200%
}
.two{
background: yellow;
background: linear-gradient(170deg, #f06 20%, yellow 20%, yellow);
font-size: 200%
}
And the html:
<div class="one"><!-- foo --></div>
<div class="two"><!-- bar --></div>
I have tried changing the background-size, background-position, background-origin, the units from em to px, px to %, the color stops, etc. I run out of ideas.
My question is... is there a way to make the angle in the gradient dependent of the width of the container (in some indirect way), so that if it is resized the diagonal changes its angle? I would like a pure CSS solution (no JavaScript).
If this is a problem without solution, is ok =)
You could use this workaround:
<div class="wrapper">
<div class="bg"></div>
<div class="one"><!-- foo --></div>
<div class="two"><!-- bar --></div>
</div>
.wrapper {
position: relative;
}
.bg {
z-index: -1;
position: absolute;
top: 0; right: 0; bottom: 0; left: 0;
background: linear-gradient(to bottom right, #f06 50%, yellow 50%, yellow);
}
Demo
I guess you may do this using background-size and no-repeat. DEMO
.one{
background: #f06;
background: linear-gradient(to bottom right, #f06 50%, yellow 50%, yellow) right #f06 ;
background-size: 50% 100%; 100% 50% ;
background-repeat:no-repeat;
font-size: 200%
}
.two{
background: yellow;
background: linear-gradient(to bottom right, #f06 50%, yellow 50%, yellow) left yellow ;
background-size: 50% 100%; 100% 50% ;
background-repeat:no-repeat;
font-size: 200%
}
BUT, this will look funny if both div have different heights: DEMO
You better set the gradient within the parent container (body for your dabblet) in my own opinion, unless you do want to see the gradient angle breaking.