I'm having an odd issue happen when trying to position an SVG at the bottom of a div:
http://jsfiddle.net/GsPhA/2/
The svg source is listed below:
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad" x1="0" y1="0" x2="100%" y2="0">
<stop offset="0" stop-color="#FFF" />
<stop offset="0.1" stop-color="#FFF" />
<stop offset="0.5" stop-color="#B1B1B1" />
<stop offset="0.9" stop-color="#FFF" />
<stop offset="1" stop-color="#FFF" />
</linearGradient>
</defs>
<rect x="0" y="0" width="100%" height="1px" style="fill:url(#grad)" />
</svg>
How can I get the svg to appear below the text (like a border-bottom)?
I'm not entirely fussed about earlier browsers, if it requires CSS 3 properties I'm happy!
EDIT: If there is no better way than absolute positioning, I will just add another div below to provide the effect.
EDIT 2: I'm not sure what I need to do differently with SVG, but positioning bottom with PNG works just fine: http://jsfiddle.net/YXRQX/1/
Is there anything I need to specify in the SVG code so it works properly?
EDIT 3: Final working jsFiddle here: http://jsfiddle.net/GsPhA/4/ Thank you To Ryan for the pointer!
Done it! Thanks to Ryan's pointer, I've added the background-size css property to fix the size to 1px high:
.ucp-controls {
text-align: center;
margin-top: 10px;
padding-bottom: 2px;
margin-bottom: 5px;
color: #242424;
cursor: default;
background: url('http://priddle.serveblog.net/sums2/images/header/line.png') bottom no-repeat;
background-size: 100% 1px; // The key line; preserves the width but locks the height.
font-family: Arial;
}
In the end, the SVG was resizing itself based on the size of the container it was being the background for.
As long as both are in the same container, the text should go above the SVG object like below:
<center>
<p>Sample Text Here</p>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad" x1="0" y1="0" x2="100%" y2="0">
<stop offset="0" stop-color="#FFF" />
<stop offset="0.1" stop-color="#FFF" />
<stop offset="0.5" stop-color="#B1B1B1" />
<stop offset="0.9" stop-color="#FFF" />
<stop offset="1" stop-color="#FFF" />
</linearGradient>
</defs>
<rect x="0" y="0" width="100%" height="1px" style="fill:url(#grad)" />
</svg>
</center>
Related
<svg width="300" height="500" version="1.1" xmlns="http://www.w3.org/2000/svg">
<style type="text/css">
path {fill:url(#MyGradient)}
</style>
<defs>
<linearGradient id="MyGradient" x1="0" x2="100%" y1="0" y2="100%">
<stop offset="20%" stop-color="red" />
<stop offset="20%" stop-color="yellow" />
<stop offset="40%" stop-color="yellow" />
<stop offset="40%" stop-color="black" />
<stop offset="60%" stop-color="black" />
<stop offset="60%" stop-color="blue" />
<stop offset="80%" stop-color="blue" />
<stop offset="100%" stop-color="pink" />
</linearGradient>
</defs>
<path d="M150 0 L75 200 L225 200 Z" />
</svg>
I am trying to implement shapes using svg & appling gradient colors on shapes based on some conditons. It works fine with Square,rectangle and circle. When i use triangle or any other irregular shapes, one or two colors from gradient are missing. Its because gradient colors are applied on rectangle so the area outside the path getting clipped. Any possible solution to apply gradient colors only on the path area? Red & pink colors are not applied on the svg.
I've been trying to solve this for a while now and can't get the SVGs to align correctly for some width and height. My viewBox has 16:9 aspect ratio and I would like to scale the SVG by the same aspect ratio. For some width and height, it does line correctly but for others it doesn't although both have the same aspect ratio.
Any hints on what is causing this?
I have a JSFiddle for you to try, https://jsfiddle.net/n90ty8ys/1/
<svg id="svg-main-panel" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 768 432" preserveAspectRatio="xMinYMin meet" width="1120" height="630">
<rect xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="200" height="200" fill="blue"></rect>
<svg width="20" height="40" x="20" y="20">
<linearGradient id="a" gradientUnits="userSpaceOnUse" x1="657.247" y1="172.823" x2="657.247" y2="152.907" gradientTransform="rotate(90 405.13 -232.117)">
<stop offset="0" stop-color="#BDCCD4"></stop>
<stop offset=".5" stop-color="#EBF0F2"></stop>
<stop offset="1" stop-color="#BDCCD4"></stop>
</linearGradient>
<path fill="url(#a)" d="M20 0H.034L0 40h19.966"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" x="20" y="60">
<linearGradient id="a" gradientUnits="userSpaceOnUse" x1="19.775" y1="30" x2="-.14" y2="30">
<stop offset="0" stop-color="#BDCCD4" />
<stop offset=".5" stop-color="#EBF0F2" />
<stop offset="1" stop-color="#BDCCD4" />
</linearGradient>
<path fill="url(#a)" d="M-.034 20h20v20h-20z" />
<linearGradient id="b" gradientUnits="userSpaceOnUse" x1="-1207.701" y1="556.747" x2="-1207.701" y2="576.663" gradientTransform="matrix(0 -1 -1 0 576.556 -1188.2)">
<stop offset="0" stop-color="#BDCCD4" />
<stop offset=".5" stop-color="#EBF0F2" />
<stop offset="1" stop-color="#BDCCD4" />
</linearGradient>
<path fill="url(#b)" d="M0 40l19.966-20L20-1H0" />
<linearGradient id="c" gradientUnits="userSpaceOnUse" x1="-847.237" y1="1808.924" x2="-847.237" y2="1828.84" gradientTransform="matrix(-1 0 0 1 -826.737 -1788.733)">
<stop offset="0" stop-color="#BDCCD4" />
<stop offset=".5" stop-color="#EBF0F2" />
<stop offset="1" stop-color="#BDCCD4" />
</linearGradient>
<path fill="url(#c)" d="M41 40V20H19.934L0 40h19.968" />
</svg>
</svg>
Updates
Originally I thought viewBox and svg had different ratio, which actually had the same. I updated #2 below accordingly.
I see two potential issues:
Your <path> elements have decimal numbers in d attribute. As browsers rendering unit is pixel (integer), it might misalign your SVG elements. In the below snippet, I changed all the decimal numbers in d attributes to integers. For example, I changed <path fill="url(#c)" d="M41 40V20H19.934L0 40h19.968" /> to <path fill="url(#c)" d="M41 40V20H20L0 40h20" />.
Your base <svg>’s viewBox size, especially ratio, is different from width and height attribute value. Your viewBox width and height is 768 and 432 respectively, while your <svg> has width="1120" height="630" for its size value. That difference makes the SVG skewed unexpectedly and might produce decimal-based rendering that causes pixel misalignment in the browser. As viewBox vs <svg> size ratio is 1:1.46 (1120/768), it could render misaligned elements. There is a great article by Sara Soueidan about how to configure your viewBox numbers.
I applied those two changes in the snippet below, and it seems to work fine. Good luck!
<svg id="svg-main-panel" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1120 630" preserveAspectRatio="xMinYMin meet" width="1120" height="630">
<rect xmlns="http://www.w3.org/2000/svg" x="0" y="0" width="200" height="200" fill="blue"></rect>
<svg width="20" height="40" x="20" y="20">
<linearGradient id="a" gradientUnits="userSpaceOnUse" x1="657.247" y1="172.823" x2="657.247" y2="152.907" gradientTransform="rotate(90 405.13 -232.117)">
<stop offset="0" stop-color="#BDCCD4"></stop>
<stop offset=".5" stop-color="#EBF0F2"></stop>
<stop offset="1" stop-color="#BDCCD4"></stop>
</linearGradient>
<path fill="url(#a)" d="M20 0H0L0 40h20"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40" x="20" y="60">
<linearGradient id="a" gradientUnits="userSpaceOnUse" x1="19.775" y1="30" x2="-.14" y2="30">
<stop offset="0" stop-color="#BDCCD4" />
<stop offset=".5" stop-color="#EBF0F2" />
<stop offset="1" stop-color="#BDCCD4" />
</linearGradient>
<path fill="url(#a)" d="M0 20h20v20h-20z" />
<linearGradient id="b" gradientUnits="userSpaceOnUse" x1="-1207.701" y1="556.747" x2="-1207.701" y2="576.663" gradientTransform="matrix(0 -1 -1 0 576.556 -1188.2)">
<stop offset="0" stop-color="#BDCCD4" />
<stop offset=".5" stop-color="#EBF0F2" />
<stop offset="1" stop-color="#BDCCD4" />
</linearGradient>
<path fill="url(#b)" d="M0 40l20-20L20-1H0" />
<linearGradient id="c" gradientUnits="userSpaceOnUse" x1="-847.237" y1="1808.924" x2="-847.237" y2="1828.84" gradientTransform="matrix(-1 0 0 1 -826.737 -1788.733)">
<stop offset="0" stop-color="#BDCCD4" />
<stop offset=".5" stop-color="#EBF0F2" />
<stop offset="1" stop-color="#BDCCD4" />
</linearGradient>
<path fill="url(#c)" d="M41 40V20H20L0 40h20" />
</svg>
</svg>
What is the correct way to fill an SVG rectangle with jet colour scheme? Using multiple stops in linearGradient does not seem to work.
Edit, I am trying to fill the a rectangle with one of the following colour gradient.
I edited the MDN code with a rainbow example
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Gradients -->
<svg width="120" height="240" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient2" x1="0" x2="0" y1="0" y2="1">
<stop offset="0%" stop-color="#d30000"/>
<stop offset="30%" stop-color="#ffff05"/>
<stop offset="50%" stop-color="#05ff05"/>
<stop offset="70%" stop-color="#05ffff"/>
<stop offset="100%" stop-color="#041ae0"/>
</linearGradient>
</defs>
<rect x="10" y="10" rx="15" ry="15" width="100" height="100" fill="url(#Gradient2)"/>
</svg>
in a fiddle: https://jsfiddle.net/9bmvr5hd/
The BbwrR gradient is the example used in Mozilla's SVG - Gradients documentation:
<svg width="120" height="240" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1">
<stop class="stop1" offset="25%"/>
<stop class="stop2" offset="50%"/>
<stop class="stop3" offset="75%"/>
</linearGradient>
<linearGradient id="Gradient2" x1="0" x2="0" y1="0" y2="1">
<stop offset="25%" stop-color="blue"/>
<stop offset="50%" stop-color="black" stop-opacity="0"/>
<stop offset="75%" stop-color="red"/>
</linearGradient>
<style type="text/css"><![CDATA[
#rect1 { fill: url(#Gradient1); }
.stop1 { stop-color: blue; }
.stop2 { stop-color: black; stop-opacity: 0; }
.stop3 { stop-color: red; }
]]></style>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="100" height="100"/>
<rect x="10" y="120" rx="15" ry="15" width="100" height="100" fill="url(#Gradient2)"/>
</svg>
I swapped the location of the red and blue and adjusted the offset percentages to try to make it look more like your image. You should be able to just change the colors and add/remove stops for the others.
I'm trying to apply a CSS Mask to fade a DIV horizontally in both direction.
I created the mask with an online editor and it works smoothly using -webkit-mask and base64 encoding:
#slicenter{-webkit-mask: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAAAUCAYAAAAeGxcbAAACWUlEQVR4Xu3bQU4DMQwFUHoH4P7ng94BGkSlyKQjRv6bqG8kVKVgd/rqjflwefl7vZWnxvly+/r6fXwt59XP31u8T3WjfnWeX672Olu/eDueIkCAAAECBAgQIECAAIEnEvgse+i1nMf352t1vu+/4/GovtZ+lH15dX742mPxrtfZpfhoqT6zzI/76Pwy4InmzVslQIAAAQIECBAgQIAAgQcC0aX59hpHS3dy2f9JxuslQTfnBAgQIECAAAECBAgQILCrgAR9+uTmBF6CvutIu28CBAgQIECAAAECBAjsKSBBnz63OYFP/rn8f/6Hfc/xcdcECBAgQIAAAQIECBAgkBKQoEvQU7OkDwECBAgQIECAAAECBAg0BCToEvTG+CglQIAAAQIECBAgQIAAgZSABF2CnpolfQgQIECAAAECBAgQIECgISBBl6A3xkcpAQIECBAgQIAAAQIECKQEJOgS9NQs6UOAAAECBAgQIECAAAECDQEJugS9MT5KCRAgQIAAAQIECBAgQCAlIEGXoKdmSR8CBAgQIECAAAECBAgQaAhI0CXojfFRSoAAAQIECBAgQIAAAQIpAQm6BD01S/oQIECAAAECBAgQIECAQENAgi5Bb4yPUgIECBAgQIAAAQIECBBICUjQJeipWdKHAAECBAgQIECAAAECBBoCEnQJemN8lBIgQIAAAQIECBAgQIBASkCCLkFPzZI+BAgQIECAAAECBAgQINAQkKBL0Bvjo5QAAQIECBAgQIAAAQIEUgISdAl6apb0IUCAAAECBAgQIECAAIGGwLYJ+jfSylgzQ2my8wAAAABJRU5ErkJggg==);}
The problem is: this works just on Chrome (and maybe Safari).
So I tried to export the same image to HTML/SVG. This is what I got:
<svg xmlns="http://www.w3.org/2000/svg" width="200px" height="200px" >
<defs>
<linearGradient id="lgrad" x1="0%" y1="50%" x2="100%" y2="50%" >
<stop offset="0%" style="stop-color:rgb(0,0,0);stop-opacity:0" />
<stop offset="5%" style="stop-color:rgb(0,0,0);stop-opacity:1" />
<stop offset="95%" style="stop-color:rgb(0,0,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(0,0,0);stop-opacity:0" />
</linearGradient>
</defs>
<rect x="0" y="0" width="100%" height="100%" fill="url(#lgrad)"/>
</svg>
And then I tried to convert this vector in a mask (but it's probably wrong):
<svg height="0" >
<defs>
<linearGradient id="g" gradientUnits="objectBoundingBox" x1="0%" y1="50%" x2="100%" y2="50%" >
<stop offset="0%" style="stop-color:rgb(0,0,0);stop-opacity:0" />
<stop offset="5%" style="stop-color:rgb(0,0,0);stop-opacity:1" />
<stop offset="95%" style="stop-color:rgb(0,0,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(0,0,0);stop-opacity:0" />
</linearGradient>
<mask id="lgrad" maskUnits="objectBoundingBox" maskContentUnits="objectBoundingBox">
<rect x="0" y="0" width="100%" height="100%" fill="url(#g)"/>
</mask>
</defs>
</svg>
I applied the mask to the same element with:
#slicenter{mask: url(#lgrad);}
But it doesn't works. Any Ideas?
Cheers
I love external stylesheets and want to be able to style any SVG graphics via an external sheet. I can declare single colour stroke and fill for my SVG logo but I want a gradient fill. I've tried a view things but can't get it to work right. Can someone help me figure out how to make it work?
I'm not sure how to put a code snippet considering I'm discussing external code, not inline, so here's a link to the SVG logo in question and its matching external stylesheet.
Actual logo I'm trying to recreate in SVG (PNG):
SVG logo: http://www.cafenocturne.com/testpage/images/svg/CafeLogoSVG.svg
Stylesheet: http://www.cafenocturne.com/testpage/css/CafeLogoSVG.css
There are some commented-out notes to make sure I don't lose the gradient code I'm trying to implement so I apologize that the CSS file is a mess. Once I can get it to work right, I won't need to keep notes there.
So how do I achieve this?
add the gradient to your SVG file, and change the stop-color from CSS:
#color1 {
stop-color: red
}
#color2 {
stop-color: blue
}
<svg>
<linearGradient id="lg">
<stop id="color1" offset="0" />
<stop id="color2" offset="1" />
</linearGradient>
<circle cx="50" cy="50" r="45" fill="url(#lg)" />
</svg>
if you need more control of your gradients, you could specify gradients in a seperate file (say 'myGradients.svg')
<svg xmlns="http://www.w3.org/2000/svg">
<linearGradient id="g1">
<stop offset="0" stop-color="red"/>
<stop offset="1" stop-color="blue"/>
</linearGradient>
<linearGradient id="g2">
<stop offset="0" stop-color="green"/>
<stop offset="1" stop-color="yellow"/>
</linearGradient>
</svg>
now in your css you can do
.logo {fill: url('myGradients.svg#g2');}
unfortunately this doesn't work in chrome :-(
alternatively you can have a copy of your gradient collection in your logo file or html, and still style it with css
.color1 {
stop-color: green
}
.color2 {
stop-color: yellow
}
#logo1 {
fill: url(#g1)
}
#logo2 {
fill: url(#g2)
}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="0" height="0">
<linearGradient id="g1">
<stop offset="0" class="color1" />
<stop offset="1" class="color2" />
</linearGradient>
<radialGradient id="g2">
<stop offset="0" class="color1" />
<stop offset="1" class="color2" />
</radialGradient>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100">
<circle id="logo1" cx="50" cy="50" r="45" />
</svg>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100">
<circle id="logo2" cx="50" cy="50" r="45" />
</svg>