CSS Gradient in BODY but not full length with fallback - css

I know that you can set a gradient for a specific height in a body tag.
So I have my body background that has a blue gradient for 300px height and then continues with plain white.
But I want to have a fallback for that gradient for older browsers, how can I make sure that these 300px will have a plain blue color and then white in older browsers that don't support css3 gradients?

Laurens' answer works, but if you don't want to introduce extra elements with no semantic meaning, you can make the html background white and give the body a height of 300 pixels.
html {
background: white}
body {
margin:0;
padding:8px;
max-height:300px;
overflow:visible;
background:blue;
background:linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
}

set the style for older browsers first, then add it for css3-capable browsers:
#div{
background: blue;
background: linear-gradient(top, #1e5799 0%,#2989d8 50%,#207cca 51%,#7db9e8 100%);
}
It will fall back to blue if gradients are not supported.
edit: Kinda misread your question.
I think the easiest solution is to add the white color to the body tag, and then add the blue plain/gradient to a div that is nested in that body tag.
Hope this helps

try this
.cssgradients
{
background: /* CSS Gradiant */
}
.no-cssgradients
{
background: /* link 1px by 300px blue background */ top left repeat-x;
/* more */
}

If you include modernizr on your page you'll be able to target css3 styles to browser that support them, your body tag would gain a class based on whether the browser supports css3-gradiants or not and you can style appropriately.
.cssgradients
{
background: /* CSS Gradiant */
}
.no-cssgradients
{
background: /* link to 1px by 300px blue background */ top left repeat-x;
}

Related

trying to use gradient to have center of my header color near white

Im trying to do a gradiente with css but Im not having sucess.
I have a purple logo at center of my header, and I like to see purple with white, but I dont want my whole header white.
So Im trying to have my header blue, and at center where I have my logo I want a gradient that allows me to have whiter center so I can put my logo there.
I want something like this:
Blue and at center where I want to put my logo I want to have something like white background.
Im trying to do this effect but Im not having the result that Im looking for.
You can see here what Im getting: http://jsfiddle.net/BA9Ry/2/
My html:
<header id="top">
<span id="logo">
<img src="http://i62.tinypic.com/11ls8li.png" />
</span>
</header>
css:
*
{
margin:0;
padding:0;
border:0;
outline:none;
}
#top
{
width:800;
height:auto;
margin:0 auto 0 auto;
background:#7088A8;
background: -webkit-gradient(linear, left center, right center, from(#7088A8), to(#fff));
}
#logo img {
width:200px;
height:100px;
display:block;
margin:0 auto;
}
Use this syntax:
background: -webkit-radial-gradient(#FFF, #7088A8);
this is the line you need
background: -webkit-radial-gradient(#ff, #7088a8);
Expanding on Miljan Puzović's comment answer, you'll need to use radial-gradient instead of gradient(linear, ...).
As you can see from the JSFiddle he provided, there are several different prefixes given to each radial-gradient. If for whatever reason you don't need to support older browsers, you can drop all of them except for -webkit-radial-gradient() and radial-gradient(). For IE 9 and below support, you'll need to user Microsoft's proprietary filter: progid:DXImageTransform.Microsoft.gradient().
Calling background: before all the radial-gradients will give old browsers a single colour background to use if they don't support radial backgrounds.
CSS from the JSFiddle:
#top {
background: #c5e0f4; /* Old browsers */
background: -moz-radial-gradient(center, ellipse cover, #c5e0f4 0%, #60abf8 78%, #6b98c4 100%); /* FF3.6+ */
background: -webkit-gradient(radial, center center, 0px, center center, 100%, color-stop(0%,#c5e0f4), color-stop(78%,#60abf8), color-stop(100%,#6b98c4)); /* Chrome,Safari4+ */
background: -webkit-radial-gradient(center, ellipse cover, #c5e0f4 0%,#60abf8 78%,#6b98c4 100%); /* Chrome10+,Safari5.1+ */
background: -o-radial-gradient(center, ellipse cover, #c5e0f4 0%,#60abf8 78%,#6b98c4 100%); /* Opera 12+ */
background: -ms-radial-gradient(center, ellipse cover, #c5e0f4 0%,#60abf8 78%,#6b98c4 100%); /* IE10+ */
background: radial-gradient(ellipse at center, #c5e0f4 0%,#60abf8 78%,#6b98c4 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#c5e0f4', endColorstr='#6b98c4',GradientType=1 ); /* IE6-9 fallback on horizontal gradient */
}
Browser Support:
You can read more on the support of browsers here. As you can see -webkit-radial-gradient is still required for most iOS/Android browsers.
Full Syntax:
You can see more about the full syntax of radial-background on Mozilla's Developer Network Website.

Combining a gradient and background-image in non-Webkit browsers?

I'm trying to combine a transparent CSS gradient and a background image, and fail gracefully in browsers that don't support the gradient.
I have this CSS, which works fine in Webkit browsers, but seems to be totally ignored by non-Webkit browsers (e.g. Firefox), which display a white background:
body {
height:100%;
-webkit-font-smoothing: subpixel-antialiased;
padding-top: 2%;
padding-bottom: 2%;
background: -webkit-gradient(linear, left top, right top,
from(rgba(0,0,0,0.4)), to(rgba(0,0,0,0.4)),
color-stop(0.03, rgba(0,0,0,0.2)),
color-stop(0.06, transparent),
color-stop(0.94, transparent),
color-stop(0.97, rgba(0,0,0,0.2))),
url(../img/myimg.jpg) repeat;
}
However, if I set background to:
background: url(../img/myimg.jpg) repeat;
instead, it works fine in Firefox. Shouldn't Firefox just ignore the -webkit-gradient part of the rule? How can I make this Firefox-friendly?
You should try to use the standard, unprefixed linear gradient syntax - this is now quite widely supported: IE10, chrome 26 (current is 27), firefox 16 (current is 20), opera 12.1 (the latest version). To support mobile browsers you'll additionally need the webkit-prefixed version.
Using your example gradient, the standard syntax is...
background: linear-gradient(to left,
rgba(0,0,0,0.4), rgba(0,0,0,0.0) 6%, rgba(0,0,0,0.0) 94%, rgba(0,0,0,0.4));
You can see this in a jsfiddle example.
If the value is invalid, firefox won't read anything after; here your background is ignored as -webkit is an unknown property value for firefox, so in your example, -webkit is an unknown value for firefox at first so it will skip that and move to next property in that class..Say for example
background: asadsa, url('http://images.google.co.in/intl/en_ALL/images/logos/images_logo_lg.gif');
/* asadsa is invalid here, so firefox will skip to next property */
Demo
CSS
div {
background: asadsa, url('http://images.google.co.in/intl/en_ALL/images/logos/images_logo_lg.gif');
---^---
/* Invalid Value For Property background */
height: 200px;
width: 300px;
border: 1px solid #f00;
}
Firefox doesn't just ignore "that part" of the rule. Firefox ignores the whole rule when it doesn't recognize a part of it.
This means you can specify several rules and Firefox will pick only those that it understands:
body {
height:100%;
-webkit-font-smoothing: subpixel-antialiased;
padding-top: 2%;
padding-bottom: 2%;
background: url(http://lorempixel.com/400/200/) repeat;
background: -webkit-gradient(linear, left top, right top,
from(rgba(0,0,0,0.4)), to(rgba(0,0,0,0.4)),
color-stop(0.03, rgba(0,0,0,0.2)),
color-stop(0.06, transparent),
color-stop(0.94, transparent),
color-stop(0.97, rgba(0,0,0,0.2))),
url(http://lorempixel.com/400/200/) repeat;
}
fiddle:
http://jsfiddle.net/yb5AE/
Firefox understands the first background rule, but not the second. Therefore the first one is used.
Webkit understands both and therefore the second one overwrites the first one, because it is declared "later", and so the second one is used.

How to mimic Photoshop "Color Overlay" in CSS

I have a button designed with Photoshop. The button has a gradient defined and I know how to generate it using Ultimate CSS Gradients Tool.
The problem is the button also has a 'Color Overlay' and I don't know how to convert it to css terms?!
UPDATE:
O.K, just to clarify, I know CSS quite well and i know how to set styles to elements.
I have this gradient:
/* IE9 SVG, needs conditional override of 'filter' to 'none' */
background: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/Pgo8c3ZnIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgdmlld0JveD0iMCAwIDEgMSIgcHJlc2VydmVBc3BlY3RSYXRpbz0ibm9uZSI+CiAgPGxpbmVhckdyYWRpZW50IGlkPSJncmFkLXVjZ2ctZ2VuZXJhdGVkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgeDE9IjAlIiB5MT0iMCUiIHgyPSIwJSIgeTI9IjEwMCUiPgogICAgPHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2IzYjdiZCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgICA8c3RvcCBvZmZzZXQ9Ijg1JSIgc3RvcC1jb2xvcj0iIzZhNzI3ZCIgc3RvcC1vcGFjaXR5PSIxIi8+CiAgPC9saW5lYXJHcmFkaWVudD4KICA8cmVjdCB4PSIwIiB5PSIwIiB3aWR0aD0iMSIgaGVpZ2h0PSIxIiBmaWxsPSJ1cmwoI2dyYWQtdWNnZy1nZW5lcmF0ZWQpIiAvPgo8L3N2Zz4=);
background: -moz-linear-gradient(top, rgba(179,183,189,1) 0%, rgba(106,114,125,1) 85%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(179,183,189,1)), color-stop(85%,rgba(106,114,125,1))); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, rgba(179,183,189,1) 0%,rgba(106,114,125,1) 85%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, rgba(179,183,189,1) 0%,rgba(106,114,125,1) 85%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, rgba(179,183,189,1) 0%,rgba(106,114,125,1) 85%); /* IE10+ */
background: linear-gradient(to bottom, rgba(179,183,189,1) 0%,rgba(106,114,125,1) 85%); /* W3C */
But like i mentioned, In photoshop there is also a Color Overlat defined which makes the button a bit darker, so in photoshop the button looks darker then in the browser, So my question is how do i combine the gradient and the Color Overlay in one CSS rule to make the button look exactly the same as in photoshop.
Update:
Demo
A little better button Demo
Demo with overlay
Just declare a class for button say .design
.design {
/* Gradient code goes here */
}
Now you can use this class as follows
<button class="design">Designed Button</button>
OR
<input type="button" class="design" value="Designed Button" />
For the COLOR OVERLAY, you can wrap the button inside a div with
display: inline-block and position: relative so that it will wrap
the button appropriately and use another div which is positioned
absolute inside the wrapper div with a height and width defined as
100% and assign opaque background color using rgba() and define
opacity as needed
Also if you want to be precise with your CSS declarations, you can particularly define .design class as input[type=button].design or button.design whatever you use, so that .design will not be applied to any other element

Adding gradient to element breaks CSS functionality in IE

I am working on a CSS-based drop-down menu. It works fine until I add gradient to elements. Than something breaks in IE and when I hover over <li> items in sub-menu the menu box disappears.
Here's the code I use to add gradient and make it cross-browser compatible:
background-color: #c1ddf4; /* for non-css3 browsers */
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#c1ddf4', endColorstr='#ffffff', GradientType=0); /* for IE */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #c1ddf4), color-stop(100%, #ffffff));/* for webkit browsers */
background: -moz-linear-gradient(top, #c1ddf4, #ffffff); /* for firefox 3.6+ */
background-image: -o-linear-gradient(#c1ddf4, #ffffff);
Please see the following examples:
OK (without gradient) vs. NOT OK (with gradient)
The IE filter will break certain functionality when applied to elements. My suggestion is to use a horizontally tiled gradient image for IE, either by using a CSS hack, an IE-only style sheet, or targeting it using Modernizr.js.
The truly proper way would be to use Modernizr, then write this CSS:
.no-cssgradients li {
background: url(gradient.png) repeat-y;
}
That way, any browser that doesn't support CSS gradients (not just IE) will get served the image instead.

How can I get IE Filter & CSS Transparent Background to display together?

I am trying to get a transparent PNG & Gradient to display at the same in IE. Right now, the filter dominates over the background image. If I take out the filter, the PNG does display. Ideally, I would like the PNG to be on top of the gradient.
CSS:
.defaultSelection {
border: 1px solid #bbb; color: #222222; outline: 0 none;
background: url('/img/dropdown-arrow.png') right center no-repeat;
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#e9e9e9', endColorstr='#ffffff' )
}
HTML:
<li class="defaultSelection">Current Selection</li>
Good news: This is possible with IE (despite what others have said). But it does need a little hack called CSS3Pie.
CSS3Pie is a hack for IE which allows it to support a variety of CSS3 features using ordinary CSS, rather than those horrible filter styles.
See here for its supported features: http://css3pie.com/documentation/supported-css3-features/
You'll note that this includes the ability to specify a background with an image and a gradient:
As described on the page linked above, simply specify your CSS with -pie-background in addition to the normal background style, and also with the Pie behavior style to run the Pie script.
#myElement {
background: url(bg-image.png) no-repeat #CCC; /*non-CSS3 browsers will use this*/
background: url(bg-image.png) no-repeat, -moz-linear-gradient(#CCC, #EEE); /*gecko*/
background: url(bg-image.png) no-repeat, -webkit-gradient(linear, 0 0, 0 100%, from(#CCC) to(#EEE)); /*webkit*/
background: url(bg-image.png) no-repeat, linear-gradient(#CCC, #EEE); /*future CSS3 browsers*/
-pie-background: url(bg-image.png) no-repeat, linear-gradient(#CCC, #EEE); /*PIE*/
behavior: url(PIE.htc);
}
Behind the scenes, CSS3Pie creates a VML element, and layers it with the real element to achieve the desired effects (VML is a vector graphics language which is supported by IE6 and up). But you don't need to know any of this, as Pie goes to some lengths to make itself completely transparent to the developer and to the user. It does have some bugs and known issues, but overall it's a very very good tool for pulling older versions of IE up to some sort of parity with more modern browsers.
Have you tried using the gradient on the li and then applying the image on an element within the li?
<li class="defaultSelection">Current Selection<span class='bg'> </span></li>
.defaultSelection {
border: 1px solid #bbb; color: #222222; outline: 0 none;
filter: progid:DXImageTransform.Microsoft.gradient(GradientType=0, startColorstr='#e9e9e9', endColorstr='#ffffff' )
}
.defaultSelection .bg{
display:inline-block;
width: 10px;
height:10px;
background: transparent url('/img/dropdown-arrow.png') right center no-repeat;
}
This is not possible with IE as a filter gradient is essentially another background image (it takes its place.) Try reversing the order to have the filter first and the bg image last in the CSS selector, you'll most likely see the image.
Your best bet is to go with layering, or make on PNG that has both the image and transparency.

Resources