Multiple CSS linear-gradients for background - css

I am trying to make the entire background of a page look like the image below with CSS, and I'm having difficulty using multiple linear-gradients together. The background has to have thin diagonal stripes with a top-to-bottom color fade that is lighter in the middle and fades to a darker color at the top and bottom.
I have tried a bunch of things and what I have here looks the best so far, but it's not quite right. Here is a jsfiddle showing what I have.
I am okay with the stripes, but the top-to-bottom gradient is definitely off, as the gradient only shows on the transparent stripes. I think what is needed here, is two gradients that overlap somehow to get the effect below, but perhaps there is a better way.
Here is the code from the fiddle in case the link breaks in the future:
/* Stripes */
body {
background: linear-gradient(
-45deg,
#5BABCF 25%,
transparent 25%,
transparent 50%,
#5BABCF 50%,
#5BABCF 75%,
transparent 75%,
transparent
);
background-size: 6px 6px;
height: 100vh;
margin: 0;
padding: 0;
}
/* Color Fade */
html {
background: repeating-linear-gradient(
to bottom,
#051219,
#91B7CA 25%,
transparent 50%,
#91B7CA 75%,
#051219 100%
);
}
Any idea how to go about doing this?

Added opacity to the diagonal stripes in body
body {
background: linear-gradient(
-45deg,
#5BABCF 25%,
transparent 25%,
transparent 50%,
#5BABCF 50%,
#5BABCF 75%,
transparent 75%,
transparent
);
background-size: 6px 6px;
height: 100vh;
margin: 0;
padding: 0;
opacity: 0.2;
}
/* Color Fade */
html {
background: repeating-linear-gradient(
to bottom,
#051219,
#91B7CA 25%,
transparent 50%,
#91B7CA 75%,
#051219 100%
);
}

Related

How to emulate styled HTML meter with two linear-gradient backgrounds, while avoiding the gradients horizontally compressing?

It seems to be damn-near impossible to style a <meter> HTML element to any interesting degree, so I am emulating a meter using CSS.
I have a step-wise gray linear gradient I want to use for the "unfilled" right-hand portion of the meter
background-image: linear-gradient(
to right,
#ddd 20%,
#ccc 20%,
#ccc 40%,
#bbb 40%,
#bbb 60%,
#aaa 60%,
#aaa 80%,
#999 80%,
#999 100%
);
and a step-wise green-ish gradient I want to use for the "filled" left-hand portion of the meter.
background-image: linear-gradient(
to right,
#70f600 20%,
#0e0 20%,
#0e0 40%,
#0d0 40%,
#0d0 60%,
#0c0 60%,
#0c0 80%,
#0b0 80%,
#0b0 100%
);
The effect I want is that
at 0% full meter, the styled meter will be the gray step gradient alone;
at 100% full meter, the styled meter will be the green step gradient alone;
at some intermediate percent (0% < X < 100%) full meter, the leftmost X% of the styled meter will be the leftmost X% of the green step gradient, and the remaining rightmost space of the styled meter will be the corresponding rightmost space of the gray step gradient. For example:
at ~36% fill
at ~82% fill
crucially, neither step gradient should be horizontally compressed to fit into the available space.
This last bulletpoint is what I am struggling to achieve.
My current best effort is the following HTML and CSS (to produce, in this case, a 36% filled meter):
HTML
<div class="meter-gauge">
<div class="negative-space" style="width: calc(100% - 36%)"/>
</div>
CSS
.meter-gauge {
position: relative;
display: inline-block;
height: 1em;
min-width: 10em;
background-image: linear-gradient(
to right,
#70f600 20%,
#0e0 20%,
#0e0 40%,
#0d0 40%,
#0d0 60%,
#0c0 60%,
#0c0 80%,
#0b0 80%,
#0b0 100%
);
}
.negative-space {
position: absolute;
top: 0;
right: 0;
height: inherit;
width: 0; /* Overridden by style attribute */
background-image: linear-gradient(
to right,
#ddd 20%,
#ccc 20%,
#ccc 40%,
#bbb 40%,
#bbb 60%,
#aaa 60%,
#aaa 80%,
#999 80%,
#999 100%
);
z-index: 1;
}
Here, unlike the desired meter styling, displayed earlier, we get a version where the gray step gradient is horizontally compressed to fit 100% of the gradient into 64% of the space.
For comparison, an 82% filled meter with the above CSS looks like this, where the issue is even more obvious:
How can I achieve the look I want, and avoid one of the two gradients being included in its entirety but horizontally squashed into the available space?
I have noted that the effect I want would have been possible to achieve if the two gradients were instead two image files, as demonstrated by this image comparison slider demo. This seems to be because the image files are defined with absolute widths, and are then scaled as necessary. The gradients on the other hand are defined only using percentages, which relate only to the width of the containing block, not that block's parent block width.
Note: I don't want to use absolute CSS size units, as I want to be able to plug this styled meter in anywhere, at any size.
How about using clip-path?
Example code
.gauge {
width: 30em;
height: 2em;
position: relative;
background-color: #ccc;
}
.gauge > * {
width: 100%;
height: 100%;
position: absolute;
}
.meter-gauge {
background-image: linear-gradient(to right,
#70f600 20%,
#0e0 20%,
#0e0 40%,
#0d0 40%,
#0d0 60%,
#0c0 60%,
#0c0 80%,
#0b0 80%,
#0b0 100%);
}
.negative-space {
background-image: linear-gradient(to right,
#ddd 20%,
#ccc 20%,
#ccc 40%,
#bbb 40%,
#bbb 60%,
#aaa 60%,
#aaa 80%,
#999 80%,
#999 100%);
clip-path: inset(0 0 0 30%);
}
<div class="gauge">
<div class="meter-gauge"></div>
<div class="negative-space"></div>
</div>
How it works
clip-path: inset(top right bottom left)
Just have a couple of elements, or pseudo elements, with the green on top of the gray.
Green one has clip-path:
clip-path: polygon(0 0, var(—pc) 0, var(—pc) 100%, 0 100%);
Where —pc is percentage required e.g 36%
Sorry I can’t give a proper snippet as am stuck on an iOS device.

Weird error message on Codepen

i have this code on Codepen:
body {
width: 100px;
height: 100px;
background-color: #eee;
background-image: linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black),
linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black);
background-size: 60px 60px;
background-position:0 0, 30px 30px;
}
for some reason I get this message:
Invalid CSS after "100px": expected expression (e.g. 1px, bold), was
";"
Any idea what's going on?
see it on Codepen
When using SASS, you don't need any curly brackets or semicolons. You also have to get rid of any line breaks in comma separated values.
I don't know about the backwards-capability of SASS to CSS, but see the Codepen working with SASS enabled:
http://codepen.io/anon/pen/ygxKvK
body
width: 100px
height: 100px
background-color: #eee
background-image: linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black), linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black)
background-size: 60px 60px
background-position: 0 0, 30px 30px
http://codepen.io/isaacalves/pen/OWoQGP - This works now.
Just set the CSS pre-processor settings as SCSS
I was just confusing Sass with Scss.
http://thesassway.com/editorial/sass-vs-scss-which-syntax-is-better

CSS gradient checkerboard pattern

I want to create a checkerboard pattern using gradients. I've found an example and modified it to my needs, however it only works with -moz prefix. When I remove the -moz prefix, the pattern is completely different.
How can I make this -moz checkerboard pattern work with unprefixed linear-gradient?
body {
background-image:
linear-gradient(45deg, #808080 25%, transparent 25%),
linear-gradient(-45deg, #808080 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #808080 75%),
linear-gradient(-45deg, transparent 75%, #808080 75%);
background-size:20px 20px;
background-position:0 0, 10px 0, 10px -10px, 0px 10px;
}
Just modify the background-position like in the below snippet to get the required output. This works fine in Firefox, Chrome, Opera, IE11 and Edge.
body {
background-image: linear-gradient(45deg, #808080 25%, transparent 25%), linear-gradient(-45deg, #808080 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #808080 75%), linear-gradient(-45deg, transparent 75%, #808080 75%);
background-size: 20px 20px;
background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}
The problem seems to be happening because of a difference in the way the angles are handled by the -moz linear gradient and the standard one. -45deg in the -moz linear gradient seems to be equal to 135deg in the standard gradient (but changing the angle is resulting in a strange dot in the middle).
The below screenshots show the difference (both taken in the latest Firefox v44.0).
Output with -moz-linear-gradient:
Output with linear gradient:
It's 2020 and this can now be created with a single CSS gradient (if you don't need to support IE/ pre-Chromium Edge).
html {
background:
repeating-conic-gradient(#808080 0% 25%, transparent 0% 50%)
50% / 20px 20px
}
I wrote a detailed explanation on CSS Tricks for how this works.
The 45deg version works nicely, but can end up showing a line between the triangles at different zoom levels or on retina screens. Depending on what browsers you need to support you can also use background-blend-mode: difference (Caniuse currently shows support nearly everywhere except IE), you can tint the checks using an additional background image:
body {
background-image: /* tint image */
linear-gradient(to right, rgba(192, 192, 192, 0.75), rgba(192, 192, 192, 0.75)),
/* checkered effect */
linear-gradient(to right, black 50%, white 50%),
linear-gradient(to bottom, black 50%, white 50%);
background-blend-mode: normal, difference, normal;
background-size: 2em 2em;
}
This was Chrome's implementation for when you opened an image with transparency for a while (though they later removed it in favor of just using a solid background).
body {
background-position: 0px 0px, 10px 10px;
background-size: 20px 20px;
background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%),linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);
}
Thanks Harry for the inspiration - here's an scss mixin to do that
#mixin checkers($size: 50px, $contrast: 0.07) {
$checkerColor: rgba(#000, $contrast);
$angle: 45deg;
$tp: 25%;
background-image: linear-gradient($angle, $checkerColor $tp, transparent $tp),
linear-gradient(-$angle, $checkerColor $tp, transparent $tp),
linear-gradient($angle, transparent 3 * $tp, $checkerColor 3 * $tp),
linear-gradient(-$angle, transparent 3 * $tp, $checkerColor 3 * $tp);
background-size: $size $size;
background-position: 0 0, 0 $size/2, $size/2 -1 * $size/2, -1 * $size/2 0;
}

How to create straight line with same css gradient at both ends?

How would I create the below image using only CSS?
I'm attempting to draw a line with a transparent gradient at either end - here's what I've tried which does not work:
background-image: -webkit-linear-gradient(left, transparent, #8C8C8C),
-webkit-linear-gradient(right, transparent, #8C8C8C);
So at the left and right end of the line the gradient moves inwards.
You should just use a single gradient like in the below snippet with the start and end as transparent.
Explanation:
transparent 0% means the gradient starts with transparent color
#8C8C8C 15% means that between 0% to 15% the gradient's color gradually changes from transparent to #8C8C8C.
#8C8C8C 85% means that the gradient's color stays as #8C8C8C from 15% to 85%.
transparent 100% means that the gradient's color would again change gradually from #8C8C8C to transparent between 85% - 100%.
The color stops create the illusion as though the gradient is proceeding inwards from either direction. Equal splits make the change look equal on either side.
div {
background-image: -webkit-linear-gradient(left, transparent 0%, #8C8C8C 15%, #8C8C8C 85%, transparent 100%);
background-image: linear-gradient(left, transparent 0%, #8C8C8C 15%, #8C8C8C 85%, transparent 100%);
height: 2px;
}
<div></div>
The various color stop values can help achieve that effect.
Stop the white at 10% and prolong a mix of transparent and gray(increasing) up to 50% and then a mix of gray and transparent(increasing) up to 100%.
.gradient {
width: 600px;
height: 1px;
background: linear-gradient(to right, transparent 10%, gray 50%, transparent 100%);
}
<div class="gradient"></div>
Also, you can play around with the % values to get the exact gradient. For example, your image can be made as accurate as possible by increasing the stop points like below.
.gradient {
width: 600px;
height: 1px;
background: linear-gradient(to right, transparent 10%, gray 20%, gray 90%, transparent 98%, transparent 100%);
}
<div class="gradient"></div>

CSS - CSS3 pixelate dot background

Is it possible or is there a trick to make a background pixelated like the one in the image attached?
I use a background image, but as you can see it doesn't scale and it flashs on page scrolling.
Now I have CSS thanks to vlcekmi3:
background-color: white;
background-image: linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black),
linear-gradient(45deg, black 25%, transparent 25%, transparent 75%, black 75%, black);
background-size:100px 100px;
background-position: 0 0, 50px 50px;
But I'm unable to make it exactly like the image. Can someone check it?
Any code, resource, tutorial, and suggestion is appreciated.
From thirtydot's comment in the first post. Should have posted it as an answer - Brilliant. I almost missed it. Please rate his comment up :) I am only posting this as an answer so it might help others as it helped me.
Using a base64 encoded message:
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABZJREFUeNpi2r9//38gYGAEESAAEGAAasgJOgzOKCoAAAAASUVORK5CYII=);
http://jsfiddle.net/thirtydot/v7T98/3/
Here's the best I could come up with to match your image. It's adapted from the example here by Lea Verou What will be your fallback for non css3 browsers?
body {
background-image: -moz-linear-gradient(45deg, #666 25%, transparent 25%),
-moz-linear-gradient(-45deg, #666 25%, transparent 25%),
-moz-linear-gradient(45deg, transparent 75%, #666 75%),
-moz-linear-gradient(-45deg, transparent 75%, #666 75%);
background-image: -webkit-gradient(linear, 0 100%, 100% 0, color-stop(.25, #666), color-stop(.25, transparent)),
-webkit-gradient(linear, 0 0, 100% 100%, color-stop(.25, #666), color-stop(.25, transparent)),
-webkit-gradient(linear, 0 100%, 100% 0, color-stop(.75, transparent), color-stop(.75, #666)),
-webkit-gradient(linear, 0 0, 100% 100%, color-stop(.75, transparent), color-stop(.75, #666));
background-image: -webkit-linear-gradient(45deg, #666 25%, transparent 25%),
-webkit-linear-gradient(-45deg, #666 25%, transparent 25%),
-webkit-linear-gradient(45deg, transparent 75%, #666 75%),
-webkit-linear-gradient(-45deg, transparent 75%, #666 75%);
background-image: -o-linear-gradient(45deg, #666 25%, transparent 25%),
-o-linear-gradient(-45deg, #666 25%, transparent 25%),
-o-linear-gradient(45deg, transparent 75%, #666 75%),
-o-linear-gradient(-45deg, transparent 75%, #666 75%);
background-image: linear-gradient(45deg, #666 25%, transparent 25%),
linear-gradient(-45deg, #666 25%, transparent 25%),
linear-gradient(45deg, transparent 75%, #666 75%),
linear-gradient(-45deg, transparent 75%, #666 75%);
-moz-background-size: 2px 2px;
background-size: 2px 2px;
-webkit-background-size: 2px 2.1px; /* override value for webkit */
background-position: 0 0, 1px 0, 1px -1px, 0px 1px;
}
jsfiddle example
The "flickering" you observe is is not a software issue, but a hardware one. Basically, it's caused by that fact that the pixels on your screen can't change color instantly. Since your dotted background consists of alternating rows of pixels, any time you scroll down by an odd number of pixels, there will be a brief moment when your screen is switching between two shifted copies of the pattern, and this will appear as flicker.
This thread on Graphic Design Stack Exchange features an even more dramatic example of the same effect, and also explains why it happens in more detail. Just for a quick demonstration, let me borrow one of the images from Volker Siegel's answer:
Note how, on most screens, this image will show a noticeable "pulsing" effect when scrolled. (It may also appear to flicker a bit even while just looking at it, simply because the photoreceptors in your eyes also have some response delay and adaptation effects.)
Anyway, the only way you can stop your dotted background from flickering while scrolling is to make it not scroll. Fortunately, there's a CSS property just for that:
background-attachment: fixed;
Other than that, there's not much else to it. The best way to actually render the background is almost certainly with a simple two-color PNG image. You can even make the image semitransparent, so that you can layer it on top of different colored backgrounds. See the snippet below for a demonstration:
body {
background-color: white;
background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGUlEQVQ4y2NgoBJwoJAedcGoC0ZdMOAuAABF0hABJ/8lyQAAAABJRU5ErkJggg==);
background-attachment: fixed;
}
<p>blah</p><p>blah</p><p>blah</p><p>blah</p><p>blah</p><p>blah</p>
<p>blah</p><p>blah</p><p>blah</p><p>blah</p><p>blah</p><p>blah</p>
<p>blah</p><p>blah</p><p>blah</p><p>blah</p><p>blah</p><p>blah</p>
<p>blah</p><p>blah</p><p>blah</p><p>blah</p><p>blah</p><p>blah</p>
Note how the pattern does not flicker when you scroll it with the inner scroll bar. (It does flicker when you scroll the whole SO page, because the pattern is attached to the <iframe> it's displayed in, and will scroll along with it.)
(BTW, the inline image I've used in the snippet above is 16 × 16 pixels, even though the actual pattern is just 2 × 2 pixels. Repeating it a few times doesn't cost much in terms of file size, though, and might be slightly safer, as I seem to recall some older browsers having issues with very small background images.)
How about this one?
.card {
background: linear-gradient(90deg, #fff 2px, transparent 1%) center, linear-gradient(#fff 2px, transparent 1%) center, #ccc;
background-size: 5px 5px;
height: 10em;
width: 30em;
position: relative;
}
.text {
font-size: 2em;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<div class="card">
<div class="text">
Hello world!
</div>
</div>
In general the formula is
// color
$bg-color: #fff;
$dot-color: $gray-darker;
// Dimensions
$dot-size: 3px;
$dot-space: 5px;
background: linear-gradient(90deg, $bg-color ($dot-space - $dot-size), transparent 1%) center,
linear-gradient($bg-color ($dot-space - $dot-size), transparent 1%) center, $dot-color;
background-size: $dot-space $dot-space;
as seen # https://codepen.io/edmundojr/pen/xOYJGw
This is because of background-size, so just try this:
background-size:2px 2px;
Without all the browser prefixes:
background: linear-gradient(
45deg,
#fff,
#fff 50%,
#000 50%,
#000
);
background-size: 2px 2px;

Resources