Anchor hover with inline SVG and text - html

Not sure how to accomplish this, but I have an SVG inline with some text wrapper in an anchor tag. But the SVG isn't changing color when hovering over the anchor as a whole, just the text. Once you hover over the SVG, it will change color, but I'm wondering how get to to trigger the hover state as a whole.
CSS:
a, a svg {
color: #fff;
}
a:hover {
color: #111;
}
a svg:hover {
fill: #111;
}
HTML:
<a href="https://facebook.com/username" target="_blank">FACEBOOK
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="30px" height="30px" viewBox="0 0 30 30" enable-background="new 0 0 30 30" xml:space="preserve">
<path d="M17.252,11.106V8.65c0-0.922,0.611-1.138,1.041-1.138h2.643V3.459l-3.639-0.015
c-4.041,0-4.961,3.023-4.961,4.961v2.701H10v4.178h2.336v11.823h4.916V15.284h3.316l0.428-4.178H17.252z"/>
</svg>
</a>
I could have swore I've had this working somewhere else before not sure why it wouldn't work here.

Give the path an attribute fill="currentColor", and it will inherit the fill color from its parents.
a {
color: #fff;
}
a:hover {
color: #111;
}
<a href="https://facebook.com/username" target="_blank">FACEBOOK
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="30px" height="30px" viewBox="0 0 30 30" enable-background="new 0 0 30 30" xml:space="preserve">
<path fill="currentColor" d="M17.252,11.106V8.65c0-0.922,0.611-1.138,1.041-1.138h2.643V3.459l-3.639-0.015
c-4.041,0-4.961,3.023-4.961,4.961v2.701H10v4.178h2.336v11.823h4.916V15.284h3.316l0.428-4.178H17.252z"/>
</svg>
</a>

The fill color of your svg is showing by default #111 so you need to change the fill color of the svg normal state in your CSS. I don't know if you meant to have the text as white but you can obviously change that if you need to.
a, a svg {
color: #fff;
fill: #FF0000;
}
a:hover {
color: #111;
}
a:hover svg {
fill: #111;
}
<a href="https://facebook.com/username" target="_blank">FACEBOOK
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="30px" height="30px" viewBox="0 0 30 30" enable-background="new 0 0 30 30" xml:space="preserve">
<path d="M17.252,11.106V8.65c0-0.922,0.611-1.138,1.041-1.138h2.643V3.459l-3.639-0.015
c-4.041,0-4.961,3.023-4.961,4.961v2.701H10v4.178h2.336v11.823h4.916V15.284h3.316l0.428-4.178H17.252z"/>
</svg>
</a>

Related

Why doesn't my path element inherit fill attribute that was set in CSS?

I'm trying to make a path element in SVG icon inherit fill property from the parent.
When I try set : SVG path { fill: inherit} in CSS it not inherit it but choose his inner fill property. But I don't want delete his inner fill attribute because icon become black and I need set color for every icon. Is there is something wrong I do?
.contact__email-image {
fill: red;
path {
all: inherit;
}
&:hover {
fill: green;
}
}
<svg fill="none" viewBox="0 0 16 12" id="mail-black-envelope-symbol" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M14.953.252L8 6.336 1.047.252C1.332.096 1.653 0 2 0h12c.347 0 .668.096.953.252zm.79 10.71c.158-.286.257-.611.257-.962V2c0-.391-.117-.754-.312-1.062L10.691 5.31l5.052 5.652zM9.94 5.968l-1.61 1.409a.5.5 0 01-.658 0l-1.61-1.41-5.116 5.725c.307.193.666.308 1.055.308h12c.389 0 .748-.115 1.055-.308L9.939 5.968zM0 2c0-.392.117-.754.312-1.062l4.996 4.37-5.051 5.654A1.976 1.976 0 010 10V2z" fill="#87B0EE"/>
</svg>
my html:
<svg class="contact__email-image" viewBox="0 0 16 12" width="16" height="12"> <use xlink:href="img/icons/icons.svg#mail-black-envelope-symbol"></use> </svg>
What you want to do won't work because the selector cannot cross the shadow DOM. Rely on CSS variables to overcome this. Notice how I am defining the fill of the path using a variable with a fallback
.contact__email-image {
--color: red;
}
.contact__email-image:hover {
--color: green;
}
<svg class="contact__email-image" viewBox="0 0 16 12" width="40"> <use xlink:href="#mail-black-envelope-symbol"></use> </svg>
<svg viewBox="0 0 16 12" width="40"> <use xlink:href="#mail-black-envelope-symbol"></use> </svg>
<svg fill="none" viewBox="0 0 16 12" id="mail-black-envelope-symbol" xmlns="http://www.w3.org/2000/svg" style="width: 0">
<path fill-rule="evenodd" clip-rule="evenodd" d="M14.953.252L8 6.336 1.047.252C1.332.096 1.653 0 2 0h12c.347 0 .668.096.953.252zm.79 10.71c.158-.286.257-.611.257-.962V2c0-.391-.117-.754-.312-1.062L10.691 5.31l5.052 5.652zM9.94 5.968l-1.61 1.409a.5.5 0 01-.658 0l-1.61-1.41-5.116 5.725c.307.193.666.308 1.055.308h12c.389 0 .748-.115 1.055-.308L9.939 5.968zM0 2c0-.392.117-.754.312-1.062l4.996 4.37-5.051 5.654A1.976 1.976 0 010 10V2z" style="fill:var(--color,#87B0EE)"/>
</svg>
First things first, your styles are using SASS / SCSS syntax (the style nesting and &:hover). If you are not using a pre-processor for your styles you need to write in normal CSS.
In CSS that would look like:
.contact__email-image {
fill: red;
}
.contact__email-image:hover {
fill: green;
}
.contact__email-image path {
fill: inherit; /*avoid using all unless you need it*/
}
Next you are using the class contact__email-image to style the SVG, but it is not present. You need to add class="contact__email-image" inside the opening svg tag. I would replace the ID with it unless you are using it.
You also have some inline fill="" code on the SVG and the path, I would remove those since they may take priority over your styles if they are being loaded with an external stylesheet file.
Is this effect you're going for? It defaults to red, then changes to green on hover.
.contact__email-image svg {
fill: red;
}
.contact__email-image svg:hover {
fill: green;
}
.contact__email-image svg>path {
fill: inherit;
}
<div class="contact__email-image">
<svg fill="none" viewBox="0 0 16 12" id="mail-black-envelope-symbol" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M14.953.252L8 6.336 1.047.252C1.332.096 1.653 0 2 0h12c.347 0 .668.096.953.252zm.79 10.71c.158-.286.257-.611.257-.962V2c0-.391-.117-.754-.312-1.062L10.691 5.31l5.052 5.652zM9.94 5.968l-1.61 1.409a.5.5 0 01-.658 0l-1.61-1.41-5.116 5.725c.307.193.666.308 1.055.308h12c.389 0 .748-.115 1.055-.308L9.939 5.968zM0 2c0-.392.117-.754.312-1.062l4.996 4.37-5.051 5.654A1.976 1.976 0 010 10V2z"/>
</svg>
</div>

Background color of an svg

I need to change the INSIDE background color of a heart-shaped svg but with normal background it overflows and with fill it changes the border not the inside, any tips on how i might achive what i am looking for?
.heart {
width: 50%;
margin-left: 25%;
cursor: pointer;
}
.heart:active {
background-color: red;
overflow: hidden;
}
<svg class="heart" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 471.701 471.701" style="enable-background:new 0 0 471.701 471.701;" xml:space="preserve">
<g>
<path d="M433.601,67.001c-24.7-24.7-57.4-38.2-92.3-38.2s-67.7,13.6-92.4,38.3l-12.9,12.9l-13.1-13.1 c-24.7-24.7-57.6-38.4-92.5-38.4c-34.8,0-67.6,13.6-92.2,38.2c-24.7,24.7-38.3,57.5-38.2,92.4c0,34.9,13.7,67.6,38.4,92.3 l187.8,187.8c2.6,2.6,6.1,4,9.5,4c3.4,0,6.9-1.3,9.5-3.9l188.2-187.5c24.7-24.7,38.3-57.5,38.3-92.4 C471.801,124.501,458.301,91.701,433.601,67.001z M414.401,232.701l-178.7,178l-178.3-178.3c-19.6-19.6-30.4-45.6-30.4-73.3 s10.7-53.7,30.3-73.2c19.5-19.5,45.5-30.3,73.1-30.3c27.7,0,53.8,10.8,73.4,30.4l22.6,22.6c5.3,5.3,13.8,5.3,19.1,0l22.4-22.4 c19.6-19.6,45.7-30.4,73.3-30.4c27.6,0,53.6,10.8,73.2,30.3c19.6,19.6,30.3,45.6,30.3,73.3 C444.801,187.101,434.001,213.101,414.401,232.701z"/>
</g>
</svg>
Issues & Solutions
svg's path is an outline only
apply fill to the path instead of svg
.heart {
width: 50%;
margin-left: 25%;
cursor: pointer;
}
.heart:active path {
fill: red;
overflow: hidden;
}
<svg class="heart" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" viewBox="0 0 471.701 471.701" style="enable-background:new 0 0 471.701 471.701;" xml:space="preserve">
<path d="m433.601,68.66767c-24.7,-24.7 -57.4,-38.2 -92.3,-38.2s-67.7,13.6 -92.4,38.3l-12.9,12.9l-13.1,-13.1c-24.7,-24.7 -57.6,-38.4 -92.5,-38.4c-34.8,0 -67.6,13.6 -92.2,38.2c-24.7,24.7 -38.3,57.5 -38.2,92.4c0,34.9 13.7,67.6 38.4,92.3l187.8,187.8c2.6,2.6 6.1,4 9.5,4c3.4,0 6.9,-1.3 9.5,-3.9l188.2,-187.5c24.7,-24.7 38.3,-57.5 38.3,-92.4c0.1,-34.9 -13.4,-67.7 -38.1,-92.4z" stroke="#000" fill="#fff"/>
</svg>

How to change position of svg on hover with a transition delay

I'm trying to move svg icons in my navbar to the left on hovering, which I'm able to do so, but I need the element to move smoothly through something like this transition : all .5s;
The issue is that svg tag doesn't accept transition property in css, so I tried to use the transition on the container, but that doesn't work, it just move instantly without transition effect.
HTML :
<div id="sidenav-icon-section">
<li>
<a href="/">
<img src="/assets/images/home.svg" alt="home" onload="SVGInject(this)">
</a>
</li>
</div>
I use the SVGInject library to transform my svg code in the browser, which results to this :
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" width="64px" height="64px" viewBox="0 0 64 64" enable-background="new 0 0 64 64" xml:space="preserve" data-inject-url="http://localhost:4200/assets/images/home.svg" _ngcontent-c1="">
<polygon fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" points="32,3 2,33 11,33 11,63 23,63 23,47 39,47 39,63 51,63 51,33 62,33 "></polygon></svg>
Also I have another svg icon that has a path tag instead of polygon
CSS :
#sidenav-icon-section {
top: 25%;
position: relative;
li {
position: relative;
transition: all .5s;
&:hover svg {
left: 7%;
}
}
}
I tried applying the transition to and the "left" attribute to path and polygon elements but nothing happens at that point, they don't even move.
You are trying to set the attribute left on an element which position is static. Try a negative margin instead. Also you are applying the transition to the wrong element.
svg { transition: margin-left .5s }
li:hover svg {
margin-left: -7px;
}
<ul id="sidenav-icon-section">
<li class="item">
<a href="/">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Layer_1" x="0px" y="0px" width="64px" height="64px" viewBox="0 0 64 64" enable-background="new 0 0 64 64" xml:space="preserve" data-inject-url="http://localhost:4200/assets/images/home.svg"
_ngcontent-c1="">
<polygon fill="none" stroke="#000000" stroke-width="2" stroke-miterlimit="10" points="32,3 2,33 11,33 11,63 23,63 23,47 39,47 39,63 51,63 51,33 62,33 "></polygon></svg>
</a>
</li>
</ul>
the scss equivalent:
li {
svg { transition: margin-left .5s; }
&:hover svg {
margin-left: -7px;
}
}

Why can't I add a border on my svg path?

I have a svg where I am highlighting my path on hover. Here is my svg
<svg version="1.1" x="0px" y="0px" viewBox="0 0 2986 886" enable-background="new 0 0 2986 886">
<image display="block" overflow="visible" width="2986" height="886" xlink:href="/A-1.jpg">
</image>
<path fill="none" stroke="#000000" strokeWidth="0.25" stroke-miterlimit="10" points="2781.5,905 2986,905 2986,865.6
2842.7,634.6 2635.2,601.1 " id="1"></path>
....
</svg>
Here is my css:
svg path{
fill:none;
pointer-events:all;
}
svg path:hover {
fill: rgba(73,143,226,0.80);
border: 5px solid #31C6FF;
}
svg rect:hover {
fill: rgba(73,143,226,0.80);
border: 5px solid #31C6FF;
}
svg polygon:hover {
fill: rgba(73,143,226,0.80);
border: 5px solid #31C6FF;
}
When I hover, my path changes to the proper color, but I don't get a border. What am I misunderstanding?
For SVG, use the stroke property instead of border.
Edit: As the owner of the question points out in comments, stroke-opacity: 1 was needed as well as stroke and stroke-width.

class svg :hover css fill not working correctly [duplicate]

This question already has answers here:
How to modify the fill color of an SVG image when being served as background image?
(24 answers)
Closed 7 years ago.
How can I cause this svg to fill when I hover? I looked at a fair amount of documentation and tried several ways but was unsuccessfully.
Here is my html:
<a class="anchor-class" href="#"><svg class="svg-class"></svg></a>
And i have tried the following in my css
a svg:hover {
fill: #50c8e8;
}
as well as
.anchor-class svg:hover{
fill: #50c8e8;
}
and even
.svg-class :hover{
fill:#50c8e8;
}
What am I doing wrong? I should be able to accomplish this with css, right?
EDIT: It might help to note that this image is being used as a background image. a little more detail in the css below:
.svg-class {
background-repeat: no-repeat;
background-size: 20px 20px;
height: 20px;
width: 20px;
display: inline-block;
.background-image('path.svg')
}
EDIT 2: Here is the .svg code, not sure that it is important though
<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 100 100" style="enable-background:new 0 0 100 100;" xml:space="preserve">
<style type="text/css">
.st0{fill:#E4E4E4;}
</style>
<path id="XMLID_154_" class="st0" d="M85.5,15c-19.5-19.4-51-19.4-70.4,0c-19.5,19.4-19.5,51,0,70.4c19.5,19.5,51,19.4,70.4,0
C105,66,105,34.4,85.5,15z M49.5,9c5.3,0,9.5,4.3,9.5,9.5c0,5.3-4.3,9.6-9.5,9.6c-5.3,0-9.6-4.3-9.6-9.6C39.9,13.2,44.1,9,49.5,9z
M64.8,79.1c0,2-1,3-3,3H37.3c-2,0-3-1-3-3v-6.3c0-2,1-3,3-3h4.5V43.4h-4.6c-2,0-3-1-3-3v-6.3c0-2,1-3,3-3h17.2c2,0,3,1,3,3v35.7
h4.6c2,0,3,1,3,3V79.1z"/>
</svg>
UPDATE: with the svg code that you posted, you can do this behavior in this way.
Example:
a .svg-class:hover path {
fill: #50c8e8;
}
.svg-class {
height: 20px;
width: 20px;
display: inline-block;
}
<a class="anchor-class" href="#">
<svg class="svg-class" version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
viewBox="0 0 100 100" style="enable-background:new 0 0 100 100;" xml:space="preserve">
<style type="text/css">
.st0{fill:#E4E4E4;}
</style>
<path id="XMLID_154_" class="st0" d="M85.5,15c-19.5-19.4-51-19.4-70.4,0c-19.5,19.4-19.5,51,0,70.4c19.5,19.5,51,19.4,70.4,0
C105,66,105,34.4,85.5,15z M49.5,9c5.3,0,9.5,4.3,9.5,9.5c0,5.3-4.3,9.6-9.5,9.6c-5.3,0-9.6-4.3-9.6-9.6C39.9,13.2,44.1,9,49.5,9z
M64.8,79.1c0,2-1,3-3,3H37.3c-2,0-3-1-3-3v-6.3c0-2,1-3,3-3h4.5V43.4h-4.6c-2,0-3-1-3-3v-6.3c0-2,1-3,3-3h17.2c2,0,3,1,3,3v35.7
h4.6c2,0,3,1,3,3V79.1z"/>
</svg>
</a>