svg width css animation from left to right instead right to left - html

Is there a way to make the green boxes behave like the red ones but in opposite direction? The width should shrink from left to right and not right to left. I know its possible if i replace the boxes with div's and give those a background color, but it tried it and it just doesnt look as good. Also the hover is not doing what it should do, cant seem to fix that either.
https://jsfiddle.net/v72tjcae/
.block rect{
transition: 200ms;
transition-timing-function: ease-in-out;
-webkit-transition: 200ms;
-webkit-transition-timing-function: ease-in-out;
}
.block:hover rect{
width: 0px;
}
<svg class="emblem" version="1.1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="500px"
height="500px" viewBox="0 0 553 553" enable-background="new 0 0 553 553"
xml:space="preserve">
<g id="Layer_2">
<g class="block">
<rect x="277" y="126" fill="#960B2C" width="156" height="88"/>
<rect x="120" y="126" fill="#004D44" width="157" height="88"/>
</g>
<g class="block">
<rect x="277" y="232" fill="#960B2C" width="156" height="88"/>
<rect x="120" y="232" fill="#004D44" width="157" height="88"/>
</g>
<g class="block">
<rect x="277" y="339" fill="#960B2C" width="156" height="88"/>
<rect x="120" y="339" fill="#004D44" width="157" height="88"/>
</g>
</g>
</svg>

Related

How to make right arc in a circle using SVG?

I am trying to make an arc like cut at the right of svg circle.
Current Result:
.ant-avatar-group {
display: inline-flex;
}
.ant-avatar {
width: 38px;
height: 38px;
line-height: 38px;
font-size: 19px;
background: #ccc;
border-radius: 50%;
}
<div class="ant-avatar-group">
<svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle opacity="0.15" cx="20" cy="20" r="19" fill="#F6BB43" stroke="white" stroke-width="2">
</circle>
</svg>
<span class="ant-avatar"> </span>
</div>
Expected Result:
Code Tried:
Changed, cx="20" to cx="30"
Also adding,
margin-left: -8px; and border-left: 4px solid #fff to .ant-avatar makes the avatar icon to distort (loose its original size) of the right avatar circle.
.ant-avatar-group {
display: inline-flex;
}
.ant-avatar {
width: 38px;
height: 38px;
line-height: 38px;
font-size: 19px;
background: #ccc;
border-radius: 50%;
}
<div class="ant-avatar-group">
<svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle opacity="0.15" cx="30" cy="20" r="19" fill="#F6BB43" stroke="white" stroke-width="2">
</circle>
</svg>
<span class="ant-avatar"> </span>
</div>
But this doesn't give the expected output as there needs to be arc like cut. Kindly help me to achieve the result making the svg circle with right arc as like the given expected result.
Big thanks in advance.
As I've commented I would put both circles in svg. For the image you can use clipPath to cut it in a circle shape.
For the other circle I'm usinga mask so that you can see through, since the mask is cutting a circular hole in it.
In CSS I've added a background to the svg. You can remove it
svg{width:300px; background:#efefef}
<svg viewBox="0 0 60 40">
<defs>
<mask id="m">
<rect fill="white" x="0" y="0" width="100" height="40" />
<circle cx="40" cy="20" r="18" fill="blabk" />
</mask>
<clipPath id="clip">
<circle cx="40" cy="20" r="16" fill="00f" />
</clipPath>
</defs>
<circle opacity="1" cx="20" cy="20" r="16" fill="#F6BB43" mask="url(#m)" />
<image href="https://assets.codepen.io/222579/darwin300.jpg" x="21" y="2" width="35" height="35" clip-path="url(#clip)" />
</svg>
Taking enxanetas'answer
Problem with inline SVGs is the ID values need to be unique per SVG,
or any following SVG will use the first defined mask/clip-path IDs
You can create those IDs dynamically:
svg {
width: 250px;
background: #efefef
}
<svg-avatar fill="#4267B2" href="https://i.imgur.com/iCKbSvQ.png"></svg-avatar>
<svg-avatar href="https://i.imgur.com/zTUDE6c.png"></svg-avatar>
<script>
customElements.define("svg-avatar", class extends HTMLElement {
connectedCallback() {
let id = n => n + Math.random() * 1e18; // create a unique id
let maskid = id("mask");
let clipid = id("clip");
this.innerHTML =
`<svg viewBox="0 0 60 40">
<defs>
<mask id="${maskid}"><rect fill="white"x="0"y="0"width="100"height="40"/><circle cx="40"cy="20"r="18"/></mask>
<clipPath id="${clipid}"><circle cx="40"cy="20"r="16"fill="00f"/></clipPath>
</defs>
<circle mask="url(#${maskid})"fill="${this.getAttribute("fill")||"#F6BB43"}"opacity="1"cx="20"cy="20"r="16"/>
<image href="${this.getAttribute("href")}"clip-path="url(#${clipid})"x="21"y="2"width="35"height="35"/>
</svg>`
}});
</script>
If you don't like the alternative you could just add another white circle that overlaps the existing one.... look here
<div class="ant-avatar-group">
<svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle opacity="0.15" cx="20" cy="20" r="19" fill="#F6BB43" stroke="white" stroke-width="2">
</circle>
<circle opacity="1" cx="47" cy="20" r="19" fill="#FFFFFF" stroke="white" stroke-width="2">
</circle>
</svg>
<span class="ant-avatar"> </span>
</div>
That will generate the arc but you still to set the negative margin-left to .ant-avatar to overlap the svg...
.ant-avatar-group {
display: inline-flex;
position:relative;
}
.ant-avatar {
position:absolute;
left:25px;
width: 38px;
height: 38px;
line-height: 38px;
font-size: 19px;
background: #ccc;
border:3px solid white;
border-radius: 50%;
}
<div class="ant-avatar-group">
<svg width="40" height="40" viewBox="0 0 40 40" fill="none" xmlns="http://www.w3.org/2000/svg">
<circle opacity="0.15" cx="20" cy="20" r="19" fill="#F6BB43" stroke="white" stroke-width="2">
</circle>
</svg>
<span class="ant-avatar"> </span>
</div>
circle in svg should have cx="20" because the circle can't go out of it's parent... you should overlap the .ant-avatar over the svg using something like
.ant-avatar {
margin-left: -10px;
}

Change Color of SVG mobile hamburger icon on Hover

I am trying to change the mobile hamburger menu icon to fit the color of my logo when the mouse is over -
.menu-hamburger:hover {
fill: #FFB400!important;
}
<a href="#">
<svg class="menu-hamburger">
<use xlink:href="#menu-hamburger"></use>
</svg>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<symbol id="menu-hamburger" class="menu-hamburger" viewBox="0 0 16 16" style="width: 16px; height: 16px; color:#FFF;">
<rect y="1" width="16" height="2"></rect>
<rect y="7" width="16" height="2"></rect>
<rect y="13" width="16" height="2"></rect>
</symbol>
</defs>
</svg>
</a>
but still no luck
When using <use xlink:href="#" , the SVG content falls into the DOM shadow and the styling result depends on the style hierarchy of the external, internal CSS style sheet, SVG presentation styles
What will be the result of the struggle of these styles is not always easy to foresee..
Therefore, in the adjacent answer, the rule fill: #FFB400!important;
But this is a very bad practice that can break the rest of the layout.
The style hierarchy can be seen in the image below from the article: By Sara Soueidan Styling SVG Content with CSS
Computed Styles has the biggest weight.
Therefore, it is most reliable to use CSS variables for CSS styling.
.menu-hamburger {
margin:1em;
}
#u1,#u2 {
fill:black;
}
#u1:hover {
--primary-color: red;
cursor:pointer;
}
#u2:hover {
--primary-color: green;
}
<a href="#">
<svg class="menu-hamburger" width="64" height="64" viewBox="0 0 16 16" >
<use id="u1" xlink:href="#menu-hamburger" ></use>
</svg>
<svg class="menu-hamburger" width="64" height="64" viewBox="0 0 16 16" >
<use id="u2" xlink:href="#menu-hamburger" ></use>
</svg>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0" viewBox="0 0 16 16">
<symbol class="menu-hamburger">
<g id="menu-hamburger" style="fill: var(--primary-color, black)">
<rect width="100%" height="100%" fill="transparent" />
<rect y="1" width="16" height="2"></rect>
<rect y="7" width="16" height="2"></rect>
<rect y="13" width="16" height="2"></rect>
</g>
</symbol>
</svg>
UPDATE
By applying CSS variables you can implement multi-color icons
.menu-hamburger {
margin:1em;
}
#u1,#u2 {
fill:black;
}
#u1:hover {
--primary-color: green;
--second-color: gold;
--third-color: red;
cursor:pointer;
}
#u2:hover {
--primary-color: purple;
--second-color: greenyellow;
--third-color: dodgerblue;
}
<a href="#">
<svg class="menu-hamburger" width="96" height="96" viewBox="0 0 16 16">
<use id="u1" xlink:href="#menu-hamburger" ></use>
</svg>
</a>
<a href="#">
<svg class="menu-hamburger" width="64" height="64" viewBox="0 0 16 16" >
<use id="u2" xlink:href="#menu-hamburger" ></use>
</svg>
</a>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="0" height="0" viewBox="0 0 16 16">
<defs>
<symbol class="menu-hamburger">
<g id="menu-hamburger" style="fill: var(--primary-color, black)">
<rect width="100%" height="100%" fill="transparent" />
<rect y="1" width="16" height="2" style="fill: var(--primary-color)"></rect>
<rect y="7" width="16" height="2" style="fill: var(--second-color)"></rect>
<rect y="13" width="16" height="2" style="fill: var(--third-color)"></rect>
</g>
</symbol>
</defs>
</svg>
Try this.
svg.menu-hamburger :hover {
fill: #FFB400!important;
}
<a href="#">
<svg class="menu-hamburger">
<use xlink:href="#menu-hamburger"></use>
</svg>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<symbol id="menu-hamburger" class="menu-hamburger" viewBox="0 0 16 16" style="width: 16px; height: 16px; color:#FFF;">
<rect y="1" width="16" height="2"></rect>
<rect y="7" width="16" height="2"></rect>
<rect y="13" width="16" height="2"></rect>
</symbol>
</defs>
</svg>
</a>
In this example I changed the <a> to a <button> because the action of clicking it is not navigating somewhere but opening a menu. It has the same properties. Controlling the width and the height of the menu is done using the button#menu-hamburger svg selector together with the fill color. So, now using a similar selector (button#menu-hamburger:hover svg) for hover the menu changes color without problem. I added the hover to the button so that you also "hover" when the pointer is in between the bars in the hamburger menu.
button#menu-hamburger {
display: block;
border: none;
background: transparent;
}
button#menu-hamburger svg {
fill: #000;
width: 16px;
height: 16px;
}
button#menu-hamburger:hover svg {
fill: #FFB400;
}
<svg xmlns="http://www.w3.org/2000/svg" width="0" height="0" display="none">
<defs>
<symbol id="menu-hamburger" viewBox="0 0 16 16">
<rect y="1" width="16" height="2"></rect>
<rect y="7" width="16" height="2"></rect>
<rect y="13" width="16" height="2"></rect>
</symbol>
</defs>
</svg>
<button id="menu-hamburger">
<svg xmlns="http://www.w3.org/2000/svg">
<use href="#menu-hamburger"/>
</svg>
</button>

SVG: how to round stroke and rectangle corners?

Is it possible to round a rectangle in SVG while ensuring the stroke obeys the roundedness of corners? The code below isn't working.
No Stroke:
stroke-width="0px"
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" stroke="red" stroke-width="0px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
With Stroke:
stroke-width="10px"
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
The stroke seems to follow the original sharp corners instead of the rounded corners.
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
A wide string extends beyond the svg border of the canvas SVG. Therefore, the string is partially cropped.
You must reduce the size of the rectangle so that the line is visible and shift the upper left corner of the rectangle right and down x="5" and y="5"
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<rect x="5" y="5" width="90" height="90" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
Update
ViewBox added. The coordinates of the rectangle x andy of the are increased, SVG wrapped in a container and can be embedded in an HTML page as a separate block. Adaptive application
.container {
width:30%;
height:30%
}
<div class="container">
<svg viewBox="0 0 110 110" xmlns="http://www.w3.org/2000/svg">
<rect x="10" y="10" width="90" height="90" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
</div>
As you can see from the picture, a square with a wide stroke is completely inside the SVG canvas
The first trivial solution is to make the overflow visible and add some margin to rectify this
svg {
overflow:visible;
margin:5px; /*half the stroke*/
}
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
<svg width="150" height="80" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
<svg width="100" height="200" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
Or you use calc() like below:
svg rect{
height:calc(100% - 10px);
width:calc(100% - 10px);
x:5px;
y:5px;
}
<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
<svg width="150" height="80" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
<svg width="100" height="200" xmlns="http://www.w3.org/2000/svg">
<rect width="100%" height="100%" stroke="red" stroke-width="10px" rx="10px" ry="10px" stroke-linejoin="round" />
</svg>
That can be used as background too:
.box {
background:url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" ><rect x="5" y="5" width="100%" height="100%" style="height:calc(100% - 10px);width:calc(100% - 10px)" stroke="red" stroke-width="10" rx="10" ry="10" stroke-linejoin="round" /></svg>');
color: #fff;
padding:25px;
display:inline-block;
margin: 75px 0;
}
<div class="box"> Some text here</div>
<div class="box"> text very loooooooooooong here</div>
<div class="box"> a text <br> two line here</div>
<svg width="400" height="180">
<rect x="50" y="20" rx="20" ry="20" width="150" height="150"
style="fill:red;stroke:black;stroke-width:5;opacity:0.5" />
</svg>

Unable to hover over rect on inline SVG [duplicate]

This question already has answers here:
Understanding CSS selector priority / specificity
(4 answers)
Closed 3 years ago.
https://jsfiddle.net/dr1vfymn/
I have an svg element with two boxes and I'm trying to change the fill color of one when the mouse hovers over it:
#room1:hover {
fill: #ffcccb;
}
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 150 100">
<rect id="room1" x="0.5" y="0.5" width="93" height="99" style="fill:#fff"/>
<path d="M128,44v98H36V44h92m1-1H35V143h94V43Z" transform="translate(-35 -43)"/>
<rect id="room2" x="93.5" y="0.5" width="56" height="99" style="fill:#fff"/>
<path d="M184,44v98H129V44h55m1-1H128V143h57V43Z" transform="translate(-35 -43)"/>
</svg>
However it is not working at all. What could be the reason?
Because your inline CSS is overriding it. One way to overcome that is to use !important
#room1:hover {
fill: #ffcccb !important;
}
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 150 100">
<rect id="room1" x="0.5" y="0.5" width="93" height="99" style="fill:#fff"/>
<path d="M128,44v98H36V44h92m1-1H35V143h94V43Z" transform="translate(-35 -43)"/>
<rect id="room2" x="93.5" y="0.5" width="56" height="99" style="fill:#fff"/>
<path d="M184,44v98H129V44h55m1-1H128V143h57V43Z" transform="translate(-35 -43)"/>
</svg>
Note that using !important is typically frowned upon so don't rely on inline styling:
#room1:hover {
fill: #ffcccb;
}
#room1,
#room2 {
fill: #fff;
}
<svg id="Layer_1" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 150 100">
<rect id="room1" x="0.5" y="0.5" width="93" height="99" />
<path d="M128,44v98H36V44h92m1-1H35V143h94V43Z" transform="translate(-35 -43)"/>
<rect id="room2" x="93.5" y="0.5" width="56" height="99"/>
<path d="M184,44v98H129V44h55m1-1H128V143h57V43Z" transform="translate(-35 -43)"/>
</svg>

Change SVG fill="none" to fill="#FFF" when Hover?

This SVG have 2 fill one is NONE and secound is color RED.
How I do so when it's not hover one stay in NONE, then when it's hover it change to color?
By the way it's right now, it only change the one that is red of the
.icon{
fill: red;
transition: all 200ms ease-out;
}
.icon:hover{
fill: blue;
}
<svg xmlns="http://www.w3.org/2000/svg" class="icon" width="24" height="22" viewBox="0 0 24 22">
<path fill="none" d="M17.73,3c-3.26,0-5,3.47-5.73,5-.75-1.54-2.48-5-5.72-5A4.05,4.05,0,0,0,2,7.19c0,3.44,4.74,7.85,10,13,5.26-5.15,10-9.56,10-13A4.06,4.06,0,0,0,17.73,3Z" transform="translate(0 -1)" />
<path fill="#ff0000" d="M17.73,1A6.53,6.53,0,0,0,12,4.25,6.51,6.51,0,0,0,6.28,1,6,6,0,0,0,0,7.19C0,11.85,5.57,16.62,12,23c6.43-6.38,12-11.15,12-15.81A6,6,0,0,0,17.73,1ZM12,20.19C6.74,15,2,10.63,2,7.19A4.05,4.05,0,0,1,6.28,3c3.24,0,5,3.49,5.72,5,.75-1.55,2.47-5,5.73-5A4.06,4.06,0,0,1,22,7.19C22,10.63,17.26,15,12,20.19Z" transform="translate(0 -1)"/>
</svg>
Try adding a wrapper class and giving css fill to the svg path. This is a sample code. Hope it helps
.svg-wrapper svg path{
fill: red;
transition: all 200ms ease-out;
}
.svg-wrapper svg path:hover{
fill: blue;
}
<div class="svg-wrapper">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="22" viewBox="0 0 24 22">
<path fill="none" d="M17.73,3c-3.26,0-5,3.47-5.73,5-.75-1.54-2.48-5-5.72-5A4.05,4.05,0,0,0,2,7.19c0,3.44,4.74,7.85,10,13,5.26-5.15,10-9.56,10-13A4.06,4.06,0,0,0,17.73,3Z" transform="translate(0 -1)" />
<path fill="#ff0000" d="M17.73,1A6.53,6.53,0,0,0,12,4.25,6.51,6.51,0,0,0,6.28,1,6,6,0,0,0,0,7.19C0,11.85,5.57,16.62,12,23c6.43-6.38,12-11.15,12-15.81A6,6,0,0,0,17.73,1ZM12,20.19C6.74,15,2,10.63,2,7.19A4.05,4.05,0,0,1,6.28,3c3.24,0,5,3.49,5.72,5,.75-1.55,2.47-5,5.73-5A4.06,4.06,0,0,1,22,7.19C22,10.63,17.26,15,12,20.19Z" transform="translate(0 -1)"/>
</svg>
</div>
You can animate it with an SVG tag. Here is how you would animate the fill property back and forth with an hover effect on a square:
<?xml version="1.0"?>
<svg width="120" height="120" viewBox="0 0 120 120" version="1.1"
xmlns="http://www.w3.org/2000/svg">
<rect x="10" y="10" width="100" height="100" fill='#000000'>
<animate attributeType="XML" attributeName="fill" from="#000000" to="#ff0000"
dur="0.5s" repeatCount="1" begin='mouseover' fill='freeze'/>
<animate attributeType="XML" attributeName="fill" from="#ff0000" to="#000000"
dur="0.5s" repeatCount="1" begin='mouseout' fill='freeze'/>
</rect>
</svg>
When this SVG is placed on a page it will turn red when you rollover it and go back to black when rolling out.