I have several svg rectangles with an hover effect, the background-color of the rectangles gets changed when the mouse is over them. The hover effect is set via css:
.myclass:hover {
fill: rgb(255,128,0);
}
Apart from that, text is placed above each of the rectangles. A pair of text and rectangle define a group.
<g>
<rect class="myclass" x="10" y="10" width="40" height="40" />
<text x="30" y="40" font-family="Verdana" font-size="10" fill="blue">ESC</text>
</g>
The hover effect works fine, but only if the cursor is not exactly above the text. If it is exactly above the text, then the hover effect vanishes.
How could I fix that?
Here a screenshot: on the left you can see the hover effect (background is orange), on the right you can see how the effect vanishes if the cursor hits the text on the rectangle:
Thank you
You need to make the text have pointer-events="none" so that it's ignored by the hover.
<g>
<rect class="myclass" x="10" y="10" width="40" height="40" />
<text x="30" y="40" font-family="Verdana" font-size="10" fill="blue" pointer-events="none">ESC</text>
</g>
The problem is the hover is assigned to element that sits behind the text. So when you're hovering over the text, you're technically NOT touching the background anymore.
My suggestion would be to apply the class to the parent element, and assign the hover to that.
<g class="myclass">
<rect x="10" y="10" width="40" height="40" />
<text x="30" y="40" font-family="Verdana" font-size="10" fill="blue">ESC</text>
</g>
Now your CSS would look like this, to target the child "rect" element:
.myclass:hover rect {
fill: rgb(255,128,0);
}
Related
I want to apply a single css rotation transformation to a set of elements in an SVG, such that each element is rotated independently, without having to calculate the centre of each element in the css. For example, I have an SVG that looks like the picture on the left, and want to apply css to achieve the effect on the right
I'm writing the svg myself, and am creating something like this
<svg baseProfile="full" height="200" version="1.1" width="200" xmlns="http://www.w3.org/2000/svg">
<text transform="translate(50 100)" text-anchor="middle">Text 1</text>
<text transform="translate(100 100)" text-anchor="middle">Text 2</text>
</svg>
When I apply a css rotation, e.g. by inserting <style>text {transform: rotate(10deg)}</style>, it seems to overwrite the first transformation, and the rotated text is placed in the top left corner:
I can modify the svg to use `x="X" y="Y" instead of a transform attribute, but that results in the transformation being applied around a different centre instead:
<svg baseProfile="full" height="200" version="1.1" width="200" xmlns="http://www.w3.org/2000/svg">
<style>text {transform: rotate(10deg)}</style>
<text x="50" y="100" text-anchor="middle">Text 1</text>
<text x="100" y="100" text-anchor="middle">Text 2</text>
</svg>
How can I structure the svg so that I can apply a rotation which works independently on each element without overwriting the initial transform?
This is a possible solution:
-The text has x="0" y="0" and is rotated with CSS.
-You put the text in the <defs>.
-You use the text and the <use> element has the x and y values you need.
text{transform:rotate(90deg)}
<svg baseProfile="full" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<defs>
<text id="a" text-anchor="middle" >Text 1</text>
<text id="b" text-anchor="middle" >Text 2</text>
</defs>
<use xlink:href="#a" x="50" y="50" />
<use xlink:href="#b" x="100" y="50" />
</svg>
Yet another solution (inspired by the comment of Robert Longson) would wrapping the rotated text in a g element and translate the g
text{transform:rotate(90deg)}
<svg baseProfile="full" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<g transform="translate(50,50)"><text text-anchor="middle" >Text 1</text></g>
<g transform="translate(100,50)"><text text-anchor="middle" >Text 2</text></g>
</svg>
I'm trying to put my text centered to triangle, I tried to make the position absolute and then top:0; right:0; left:0; bottom:0; nothing changed.
<svg class="intro go" viewbox="0 0 200 86">
<text text-anchor="start" x="10" y="30" class="text text-stroke" clip-path="url(#text1)">TEST</text>
<text text-anchor="start" x="10" y="30" class="text text-stroke text-stroke-2" clip-path="url(#text1)">TEST</text>
<defs>
<clipPath id="text1">
<text text-anchor="start" x="10" y="30" class="text">TEST</text>
</clipPath>
</defs>
</svg>
here is my code: https://codepen.io/anon/pen/ePEdEZ
I'm not sure what you meant by "centered to triangle"?
Did you mean centered to rectangle? If so:
Change the coordinates of the text from x="10" y="30" to x="100" y="43".
Then set text-anchor="start" to text-anchor="middle". And add alignment-baseline="middle" to your text. The text is now centered in the box.
Here is a JSFiddle: https://jsfiddle.net/1mLey8v4/
You could also set alignment-baseline and text-anchor by scss.
Looks cool btw!
How can I clip an image to text? I would prefer a method that is available in any browser. I tried background-clip but it not available in all modern browsers.
Add a clip-path to an image and you'll see a clipped image.
I've made it look more like your example by adding an additional copy of the image with low opacity behind the clipped image.
<svg width="100%" height="100%">
<defs>
<clipPath id="sample" clipPathUnits="userSpaceOnUse">
<text x="85" y="330" font-family="sans-serif" font-size="380" font-weight="bold">I CAM</text>
</clipPath>
</defs>
<image transform="scale(0.4)" xlink:href="http://netghost.narod.ru/gff/sample/images/png/marble24.png" x="20" y="20" width="1419" height="1001" opacity="0.3" />
<image transform="scale(0.4)" xlink:href="http://netghost.narod.ru/gff/sample/images/png/marble24.png" x="20" y="20" width="1419" height="1001" clip-path="url(#sample)" />
</svg>
I am trying to make a resizable window within SVG, in which I want to have little indicators where the window can be dragged to resize it. I've currently got the following:
<svg width="300" height="200">
<rect x="25" y="25" width="150" height="100" fill="none" stroke="black" stroke-width="2" stroke-dasharray="5,5" />
<g fill="white" stroke="black" stroke-width="1">
<rect x="23" y="23" width="5" height="5" style="cursor:nw-resize" />
<rect x="173" y="23" width="5" height="5" style="cursor:ne-resize" />
<rect x="173" y="123" width="5" height="5" style="cursor:se-resize" />
<rect x="23" y="123" width="5" height="5" style="cursor:sw-resize" />
</g>
</svg>
but if I want to resize that, I'd have to update three of the <rect>s whenever one of the corners is being resized, or if I would add squares in the middle of the line (like what MS Paint uses), I'd have to update 5 at a time.
So is it possible to position these four boxes relative to a parent <g>? That way I would only have to resize the one <g>'s width and height to automatically move all of the <rect>s. It seems to me this kind of thing (grouping SVG elements together) is what the <g> tag was invented for, but I couldn't find anything on how to do what I want to do here.
<svg> elements nest so stick what you want to resize in an inner <svg> element and alter its viewBox
The viewBox defines the area visible within an element so if you make it bigger the contents will appear smaller and vice versa.
I have a div that has the opacity set to 0.6 to make it a little transparent. Nested in this div, I'm adding lines and shapes using SVG that I want to be opaque. The fiddle shows a simple example:
JSFiddle:
The green circle in the div with the red background is inheriting the opacity of the div with the red background. The green circle below the red div shows it without any transparency - which is how I want to look on the red background.
<div style="width: 160px;height: 140px;background-color: red;opacity: 0.6;">
<svg width="160" height="140">
<circle cx="40" cy="40" r="15" fill="green" fill-opacity="1"> </circle>
</svg>
</div>
<svg width="160" height="140">
<circle cx="40" cy="40" r="15" fill="green" fill-opacity="1"> </circle>
</svg>
How can I make the green circle nested in the red div opaque?
You should use rgba(255,0,0,0.6) colors instead to set only background opacity.
The behavior you experiment is normal, opacity applies all the way through the element , childs & text included .
http://jsfiddle.net/vLV5h/7/
The 0.6 opacity applies to the whole of the div (including it's contents). There is no way for a child element to override the opacity of it's parent.
Your only choice is to move the red background into the SVG.
<div style="width: 160px;height: 140px;">
<svg width="160" height="140">
<rect width="100%" height="100%" fill="red" opacity="0.6"/>
<circle cx="40" cy="40" r="15" fill="green" fill-opacity="1"> </circle>
</svg>
</div>
<svg width="160" height="140">
<circle cx="40" cy="40" r="15" fill="green" fill-opacity="1"> </circle>
</svg>
Demo here