Is it possible to use a gradient over a css background image? - css

I am trying to get a div to have a blue background image which is 500px wide. I then am trying to get the gradient to be white at the very left of the div and as it goes right the background image is slowly visible

This css code will be useful to make it gradient
.gradient {
width: 200px;
height: 200px;
background: #999; /* for non-css3 browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000'); /* for IE */
background: -webkit-gradient(linear, left top, left bottom, from(#ccc), to(#000)); /* for webkit browsers */
background: -moz-linear-gradient(top, #ccc, #000); /* for firefox 3.6+ */
}
Use the above css in html using class
<div class="gradient">
gradient box
</div>

I actually just posted something similar on another question, but it applies in this case as well. Here it is in action:
http://sassmeister.com/gist/3528cb23d3e831231949
And the CSS to achieve this effect:
.hero {
width: 100%;
height: 500px;
background: url("http://placesheen.com/1200/500") center center no-repeat;
background-size: cover;
}
.hero:before {
content: '';
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
height: 508px;
background-image: linear-gradient(rgba(255, 255, 255, 0.2), white);
}
Of course be sure to add the correct vendor-prefixes so that it is cross-browser compatible. And if you wanted to change gradient directions you would change the gradient values.
The html:
<div class="hero">
You could put content here if you want
</div>
More on gradients:
http://www.w3schools.com/css/css3_gradients.asp

Related

Only fade part of a background to white with CSS style sheet

I'm trying to use this code in a CSS stylesheet:
.layered-image {
background: linear-gradient( to right, transparent, white 40% ),
url("http://www.azlro.org/chad/images/IMG_3699-1920x1080.JPG");
}
...to fade the background image from the image itself to white from left to right. However, I want some of image (500 pixels) to not fade at all and then start fading from there. Is that possible?
This can be achieved by using the ::before selector.
The ::before selector inserts something before the content of each selected element(s), in your case, the linear-gradient 'layer'.
I'm not totally sure this is what you are after, but hopefully this will guide you to a solution for your project. You will have to play around with the opacity, width and possibly other factors to get it exactly how you want.
As the above commenter suggested, you can add values to each color inside your linear gradient to determine the amount that you want to persist, such as:
linear-gradient(to right, transparent 500px, white);
html, body {
width: 100%;
height: 100%;
}
.layered-image {
width: 100%;
height: 100%;
background: url('https://upload.wikimedia.org/wikipedia/commons/6/62/Starsinthesky.jpg') center center no-repeat;
background-size: cover;
}
.layered-image:before {
content: '';
position: absolute;
background-image: linear-gradient(to right, transparent, white);
opacity: 2.5;
height:100%;
width:100%;
}
<div class="layered-image">
</div>
Use opacity:
.layered-image {
opacity:0.8;
}
Simply adjust the gradient:
.layered-image {
height: 200px;
background: linear-gradient( to right, transparent 0,transparent 200px /*edit this value*/ ,white 60%),
url("https://lorempixel.com/1000/800/") center/cover;
}
<div class="layered-image">
</div>

Linear gradient is not working when used with "background-attachment: fixed"

I am trying to make a linear-gradient with percentage breakpoints while using "background-attachment: fixed". It is not working as I expected.
I've made a very simple fiddle about my question.
https://jsfiddle.net/f8v1h0ac/
HTML
<body>
<header>
Header
</header>
<main>
Main
</main>
</body>
CSS
body {
margin: 0;
padding: 0;
}
header {
width: 100%;
height: 300px;
background-attachment: fixed;
/* background-image: linear-gradient(to bottom, black 0px, white 300px); */
/* The code above is working but the code below is not. Note that the color at the bottom line of header is supposed to be white. */
background-image: linear-gradient(to bottom, black 0%, white 100%);
}
main {
height: 2000px;
}
PS: I guess this happens because the gradient's height is respective to the window height. But I don't have a clue how to solve this problem.
Would this work for you? I put the header content inside a div.
header {
height: 300px;
background-attachment: fixed;
}
div {
width: 100%;
height: 100%;
background-image: linear-gradient(to bottom, black 0%, white 100%);
}
https://jsfiddle.net/wazz/xkgkam74/

Hard linear-gradients (e.g. triangles) look jagged or blurry (Chrome vs IE) [duplicate]

This question already has answers here:
background image, linear gradient jagged edged result needs to be smooth edged
(2 answers)
Closed 5 years ago.
I'm building my content-triangles with linear-greadients. Why? Because I need them with a responsive width (not possible with fixed deg-rotation hacks etc.) but when it comes to cross browser testing IE and Chrome look different - I guess chrome has some antialiasing/interpolation missing here.
.box {
width: 100%;
color: white;
background: darkgreen;
padding: 20px;
position: relative;
text-align:center;
margin-bottom: 50px;
}
.box:before, .box:after, .box--darkorange:before, .box--darkorange:after {
content: "";
position: absolute;
width: 50%;
left: 0;
bottom: -30px;
height: 30px;
display: block;
background: linear-gradient(to right top, transparent 50%, darkgreen 50%);
}
.box:after {
left: 50%;
background: linear-gradient(to left top, transparent 50%, darkgreen 50%);
}
.box--darkorange {
background: darkorange;
}
.box--darkorange:before {
background: linear-gradient(to right top, transparent 49%, darkorange 50%);
}
.box--darkorange:after {
left: 50%;
background: linear-gradient(to left top, transparent 49%, darkorange 50%);
}
<div class="box">
The triangle looks jagged in Chrome but good in IE 10+ :-(
</div>
<div class="box box--darkorange">
The triangle below looks good in Chrome but horrible blurred in IE :-(
</div>
And here is an image of what I mean:
Jagged triangle
And here is my fiddler: https://jsfiddle.net/mnnhvgxz/
EDIT: Oh, and sure I could mess around with the values like 49.5 to 50 or so which would look nearly good in both worlds but maybe you guys no a besser solution or why IE is such blurry on this one or how to AA it in Chrome. Thanks!
when you have use gradient then you have to remember those things because your code will be run on different platform.
background: -webkit-linear-gradient(red,yellow,blue); /* Safari 5.1-6.0 */
background: -o-linear-gradient(red,yellow,blue); /* Opera 11.1-12.0 */
background: -moz-linear-gradient(red,yellow,blue); /* Firefox 3.6-15 */
background: linear-gradient(red,yellow,blue); /* Standard syntax */

How can I tint a background image with CSS?

I have a background image set up through CSS.
html {
background-image: url('../img/cello.jpg');
background-attachment: fixed;
background-size: 100%;
}
I plan on having a different background image for different pages of the website: so it's important that text is legible over it. Right now I've got a translucent black background to my #main content box in the middle like this in order to ensure legibility:
#main {
background: rgba(0, 0, 0, 0.5);
}
What I really want to do, though, is to have that kind of translucent background over the entire background image, because the black box looks a bit clunky. I've tried making a <div id=#tint> which includes the whole HTML document and giving rgba(0, 0, 0, 0.5) to #tint, but that doesn't work at all--I can either get nothing to change or I can get the entire background to become a simple grey with no background image visible at all. Is this simply not possible?
Use background-blend-mode for a simple tint
You can use the background-blend-mode css property:
.box {
width: 300px;
height: 300px;
background-size: cover;
background-image: url('https://placehold.co/300');
}
.background-tint {
background-color: rgba(200,100,0,.5);
background-blend-mode: multiply;
}
<div class="box background-tint"></div>
Place it on any element with a background image and you're good to go.
The property is well supported in modern browsers NOT including IE 11. For non supporting browsers you can use a polyfill.
Working demo
Other Options
Use filter for a complex tint
You can use the filter css property:
.box {
width: 300px; height: 300px;
background-size: cover;
background-image: url('https://placehold.co/300');
}
.background-tint {
filter: sepia(100%) saturate(200%) brightness(70%) hue-rotate(330deg);
}
<div class="box background-tint"></div>
Place it on any element with a background image and you're good to go.
In order to change the color change the hue-rotate value.
The property is well supported in modern browsers NOT including IE 11.
Working demo
Use a flat linear-gradient and a multiple background overlay
.background-tint {
background-image:
linear-gradient( rgba(0,0,0,.5), rgba(0,0,0,.5) ),
url('http://placehold.it/420')
}
I think this is the most widely used technique but it has the downside of being hardcoded i.e. you can't just take a class, stick it on an element and make a tint.
You could make this into a less or sass mixin, something like:
less
.background-tint(#tint-color, #image-url) {
background-image:
linear-gradient( #tint-color, #tint-color ),
url( #image-url )
}
sass
#mixin background-tint($tint_color, $image_url) {
background-image:
linear-gradient( $tint_color, $tint_color ),
url( $image_url )
}
Working demo
Use a transparent background
This method has the advantage of working on most browsers and is just a nice class you add to any element.
The downside is that if you have anything else inside of that element you will have to wrap it in a div with some kind of positioning position: relative would work best.
Example:
.box {
width: 300px; height: 300px;
background-size: cover;
background-image: url('http://placehold.it/300');
color: #facebc;
}
.background-tint { position: relative; }
.background-tint::after {
content: "";
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,.5);
margin: auto;
}
.u-relative { position: relative; z-index: 1; }
<div class="box background-tint">
<div class="u-relative">300 x 300</div>
</div>
Working Demo
I think you need to create an overlay element (potentially div) which has the sought translucent background. Something like:
.overlay {
z-index: 1;
height: 100%;
width: 100%;
position: fixed;
overflow: auto;
top: 0px;
left: 0px;
background: rgba(0, 0, 0, 0.7); /*can be anything, of course*/
}
And of course, a little demo: little link.
This worked great for me:
https://css-tricks.com/tinted-images-multiple-backgrounds/
.tinted-image {
background:
/* top, transparent red, faked with gradient */
linear-gradient(
rgba(255, 0, 0, 0.45),
rgba(255, 0, 0, 0.45)
),
/* bottom, image */
url(image.jpg);
}
And building on another answer, you can do this with existing colors in less like:
linear-gradient(
fade(#brand-primary, 50%),
fade(#brand-primary, 50%)
),
It would be the overlay property
https://dvcs.w3.org/hg/FXTF/rawfile/tip/compositing/index.html#blendingoverlay
But it's a draft. Don't rely on it
Try opacity:
opacity:0.4;
filter:alpha(opacity=40); /* For IE8 and earlier */
This is the simplest solution to the problem in my opinion.
.parent-div{
background-color : desired-color
}
#image-id{
opacity: dersired_value%
}
To increase readibity background-color: black and opacity percentages of range 50 to 60% seem to work nicely.

CSS fluid elements running into each other

I'm having some trouble with my css and I was gonna post on stackoverflow but I thought maybe this would be the right place to post seeing its strictly a css problem. I have a jquery cycle plugin tor rotate images and I want to have a block of text to the right of the the rotator but I don't want it to run into the rotator and I also would like it to not crop of when the page is shrunk within reason. right now if you pull the browser window in to the left it just slides under the rotator I would much rather the rotator move to the left as well until it can't anymore then the text should start expanding downward if that is even possible. but I cant seem to figure it out.
here is the site http://falconesuits.com/
hdere is the css (well at least the important part)
#story2 {
margin: 100px;
width:300px;
float:right;
color:#FFF;
}
.slideshow {
width:300px;
height:450px;
background: #cc9966; /* for non-css3 browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000'); /* for IE */
background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#cc9966)); /* for webkit browsers */
background: -moz-linear-gradient(top, #fff, #cc9966); /* for firefox 3.6+ */
min-height: 100%;
border:10px;
border-style:groove;
border-color:#939598;
margin-left: auto;
margin-right: auto;
top: 100px;
}
One approach that might help is to restructure your html by putting your story and slideshow in the same div container:
<div id="content">
<div id="story2">...</div>
<div class="slideshow">...</div>
</div>
Then styling #content so that it has the fluid margins instead.
#content {
overflow: hidden;
margin: 0 auto;
width: 650px;
margin-top: 100px;
}
#story2 {
width: 300px;
float: right;
color: #fff;
}
.slideshow {
float: left;
width:300px;
height:450px;
background: #cc9966; /* for non-css3 browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000'); /* for IE */
background: -webkit-gradient(linear, left top, left bottom, from(#fff), to(#cc9966)); /* for webkit browsers */
background: -moz-linear-gradient(top, #fff, #cc9966); /* for firefox 3.6+ */
min-height: 100%;
border:10px;
border-style:groove;
border-color:#939598;
}
As for expanding the text downwards, you could try setting your #story width to a percentage instead of a fixed pixel value, I guess.

Resources