background-color ignores transparent content? - html

I have the image which has filled content and transparent content. I want to get background-color but only for filled content (ignores transparent) Is it possible? Can I do something like that instead?
My project: can be seen here
$("#hairbutton").click(function() {
var haircolor = $('#haircolorinput').val();
$(".img-wrap#hair img").css("background-color", haircolor);
});
$("#facebutton").click(function() {
var facecolor = $('#facecolorinput').val();
$(".img-wrap#face img").css("background-color", facecolor);
});
#-webkit-keyframes head {
from {
top: 0px;
}
to {
top: 1px;
}
}
#keyframes head {
from {
top: 0px;
}
to {
top: 1px;
}
}
#head {
position: relative;
-webkit-animation: head 1.5s infinite;
animation: head 1.5s infinite;
}
.img-wrap {
width: 153px;
height: 78px;
overflow: hidden;
width: 90px;
height: 60px;
clear: both;
}
.img-wrap img {
width: 90px;
height: 60px;
background-color: green;
}
input {
padding: 5px;
border-radius: 5px;
border: 2px solid black;
float: left;
height: 18px;
}
input:focus {
outline: 0;
}
#go {
padding: 5px;
border: 2px solid black;
width: auto;
display: table;
border-radius: 5px;
float: left;
height: 10px;
margin-left: 5px;
cursor: pointer;
height: 33px;
}
#face {
position: relative;
bottom: 35px;
left: 35px;
}
.form {
float: left;
clear: both;
}
button {
position: relative;
left: 5px;
top: 5px;
}
.clear {
clear: both;
}
<div class="form">
<input id="haircolorinput" name="haircolor" type="text" placeholder="Hair HEX">
<button id="hairbutton">Change</button>
</div>
<div class="form">
<input style="margin-top: 5px; margin-bottom: 50px;" id="facecolorinput" name="facecolor" type="text" placeholder="Face HEX">
<button id="facebutton">Change</button>
</div>
<div class="clear"></div>
<div id="head">
<div class="img-wrap" id="hair" style="z-index: -100;">
<img src="http://i.imgur.com/mz3GRLl.png" alt="" />
</div>
<div class="img-wrap" id="face" style="z-index: 100; width: 36px; height: 36px;">
<img src="http://i.imgur.com/DiHXscR.png" alt="" style="width: 36px; height: 36px;" />
</div>
</div>

To answer your question No, but you can sett a background-color to and image so that the background shows on the transparent part of you image instead:
tl;dr There are multiple ways of changing parts of an image/object. I think the easiest way is with an svg embedded image/object. and setting the fill of that element with css.
Fidely dee
PNG example
CSS
#yourimage:hover {
background-color: blue;
}
The setback of using the png solution:
This will fill any background color. So to avoid having blue background around an image usually you have a white or any other solid background on the image. See fiddle for example.
Canvas
Its fully possible setting the the strokeStyle in canvas.
Yay super easy right? Well canvas does not have objects or elements it only renders what is given to it. So in the code that draws you shape you have to define what color it takes before rendering. Maybe one day there will be elements in canvas?
SvgRecommended
Svg elements can have an id! So we can simply make an svg:
html
<svg width="100" height="100">
<circle id="favcirc" cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow"/>
<svg/>
And access the fill value of the element!
css
#favcirc {
fill = "#F00"
}
ps if you lucky and only have one color you can hue-rotate it with filter:
Filter demo

Related

Change image icon appears on hover but disappears with every mouse move

.profile__change-image-button should appear on hover (while the image changes opacity to 0.8) and clicking on it is supposed to open a form. With my current code, opacity is applied on hover and the icon does appear, but it disappears with every move of the mouse, which it makes it difficult to open the popup form. The end result should be- opacity changed to 0.8 on hover + icon appear and disappear only when the mouse moves outside the borders of the image. Thank you very much in advance!
.profile__user {
max-width: 583px;
display: flex;
position: relative;
}
.profile__image {
background-size: cover;
background-position: center;
width: 120px;
height: 120px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
}
.profile__change-image-button {
width: 26px;
height: 26px;
background: transparent;
border: none;
position: absolute;
left: 47px;
bottom: 56px;
}
.profile__change-image-button-hide {
display: none;
}
.profile__image:hover {
opacity: 0.8;
}
.profile__image:hover + .profile__change-image-button-hide {
display: block;
}
<div class="profile__user">
<div
class="profile__image"
title="An image of the French oceanographer- Jacques Cousteau"
></div>
<div class="profile__change-image-button-hide">
<button type="button" class="profile__change-image-button">
<img
src="<%=require('./images/edit-profile-icon.svg')%>"
alt="A vector image of a pen inside the profile image"
class="profile__change-image-icon"
/>
</button>
</div>
<div class="profile__info">
<h1 class="profile__title"></h1>
<button type="button" class="profile__edit-button">
<img
src="<%=require('./images/button-edit.svg')%>"
alt="A vector image of a pen inside the edit button"
class="profile__edit-icon"
/>
</button>
<p class="profile__subtitle"></p>
</div>
</div>
There is a lot of html and css rules going on so I simplified the code to give what I believe you are looking to do. Also I've imported an icon font to get the button image you described. It's not a vector graphic but it can be with a simple change.
#import url("https://cdn.jsdelivr.net/npm/bootstrap-icons#1.9.1/font/bootstrap-icons.css");
.profile__user {
position: relative;
display: inline-block;
}
.profile__image {
background-image: url("https://picsum.photos/id/1027/120");
height: 120px;
width: 120px;
border-radius: 50%;
border: none;
opacity: 1;
transition: opacity .5s;
}
.profile__change-image-button {
position: absolute;
height: 24px;
width: 24px;
bottom: 16px;
right: 16px;
padding: 4px;
border-radius: 5px;
border: none;
opacity: 0;
transition: opacity .5s;
}
.profile__image:hover {
opacity: .8;
}
.profile__image:hover .profile__change-image-button {
opacity: 1;
}
<div class="profile__user">
<div class="profile__image" title="An image of the French oceanographer- Jacques Cousteau">
<button type="button" class="profile__change-image-button">
<i class="bi bi-pencil-square"></i>
</button>
</div>
</div>

Add a Link to an Overlay Image

I would like to add a link to an image that has an overlay applied to it. When users hover over the image they get an opaque overlay and text appear. This works great, however I also need users to be able to click that image and taken to a link.
I have pasted my code below - the overlay works but the link portion does not. I believe it's because the overlay is interfering. I tried changing the placement of the link but was unable to do so. I am able to make the '+' be the link, but ideally the entire image should link.
Any thoughts?
JSFiddle
.roomphoto:hover .img__description { transform: translateY(0); }
.roomphoto .img__description_layer { top: 30px; }
.roomphoto:hover .img__description_layer { visibility: visible; opacity: 1; }
.img__description_layer {
position: absolute;
bottom: 0;
left: 0;
right: 0;
color: #cdcbca;
visibility: hidden;
opacity: 0;
display: flex;
align-items: center;
justify-content: center;
transition: opacity .2s, visibility .2s;
}
.roomphoto {
height: 166px;
}
.img__description {
transition: .2s;
transform: translateY(1em);
z-index: 999;
}
.overlay {
position: relative;
}
.overlay:after {
position: absolute;
content:"";
top:0;
left:0;
width:100%;
height:100%;
opacity:0;
}
.overlay:hover:after {
opacity: .8;
}
.blue:after {
background-color: #1a3761;
}
.img__description a {
color: #fff;
}
.roomselect {
clear: both;
float: none;
width: 100%;
max-width: 1000px;
margin: 0px auto;
padding-top: 20px;
}
.roomselect img {
width: 100%;
margin-bottom: -10px;
}
.room3 {
width: 32%;
text-align: left;
float:left;
}
<div class="roomselect"><div class="room3">
<div class="roomphoto overlay blue">
<a href="http://www.google.com">
<img src="https://cdn.mos.cms.futurecdn.net/J9KeYkEZf4HHD5LRGf799N-650-80.jpg" alt="Image Text" />
</a>
<div class="img__description_layer">
<p class="img__description">
<span style="border-radius: 50%; width: 30px; height: 30px; padding: 0px 14px; border: 1px solid #fff; text-align: center; font-size: 36px;">+</span>
</p>
</div>
</div>
</div>
You need to rearrange your html in such a way that when you click on image, you are actually clicking on the anchor tag. I have made some changes to your html, let me know if these work. You might have to increase roomphoto height.
<div class="roomselect">
<div class="room3">
<a href="http://www.google.com">
<div class="roomphoto overlay blue">
<img src="https://cdn.mos.cms.futurecdn.net/J9KeYkEZf4HHD5LRGf799N-650-80.jpg" alt="Image Text" />
<div class="img__description_layer">
<p class="img__description"><span style="border-radius: 50%; width: 30px; height: 30px; padding: 0px 14px; border: 1px solid #fff; text-align: center; font-size: 36px;">+</span></p>
</div>
</div>
</a>
</div>
</div>

Pseudo element goes on top and not on the bottom of element

I have a :after pseudo element to create a border bottom animation (border coming in from left to right), I used this technique several times however this time the border comes on top and not on the bottom for some reason, which I cant figure out. I tried using float and chaning the display type but it makes no different.
Html:
<div class="search">
<svg viewBox="0 0 485.213 485.213">
<path d="M471.882,407.567L360.567,296.243c-16.586,25.795-38.536,47.734-64.331,64.321l111.324,111.324
c17.772,17.768,46.587,17.768,64.321,0C489.654,454.149,489.654,425.334,471.882,407.567z"/>
<path d="M363.909,181.955C363.909,81.473,282.44,0,181.956,0C81.474,0,0.001,81.473,0.001,181.955s81.473,181.951,181.955,181.951
C282.44,363.906,363.909,282.437,363.909,181.955z M181.956,318.416c-75.252,0-136.465-61.208-136.465-136.46
c0-75.252,61.213-136.465,136.465-136.465c75.25,0,136.468,61.213,136.468,136.465
C318.424,257.208,257.206,318.416,181.956,318.416z"/>
<path d="M75.817,181.955h30.322c0-41.803,34.014-75.814,75.816-75.814V75.816C123.438,75.816,75.817,123.437,75.817,181.955z"/>
</svg>
<span>Zoeken</span>
</div>
Css:
.search {
transition: 0.5s ease;
border-bottom: 2px solid transparent;
white-space: nowrap;
width: 120px;
height: 60px;
float: left;
display: block;
}
.search:after {
content: '';
display: block;
height: 2px;
width: 0;
background: $main-color;
transition: width .5s ease, background-color .5s ease;
float: none;
}
.search:hover:after {
width: 100%;
}
Here is a visual of the problem. The red line should be on the botttom.
In these cases I normally use position: absolute
.search {
white-space: nowrap;
width: 120px;
height: 60px;
position: relative;
}
.search svg {
height: 100%;
}
.search:after {
content: '';
position: absolute;
left: 0;
bottom: -4px;
height: 2px;
width: 0;
background: red;
transition: width .5s ease;
}
.search:hover:after {
width: 100%;
}
<div class="search">
<svg viewBox="0 0 485.213 485.213">
<path d="M471.882,407.567L360.567,296.243c-16.586,25.795-38.536,47.734-64.331,64.321l111.324,111.324
c17.772,17.768,46.587,17.768,64.321,0C489.654,454.149,489.654,425.334,471.882,407.567z"/>
<path d="M363.909,181.955C363.909,81.473,282.44,0,181.956,0C81.474,0,0.001,81.473,0.001,181.955s81.473,181.951,181.955,181.951
C282.44,363.906,363.909,282.437,363.909,181.955z M181.956,318.416c-75.252,0-136.465-61.208-136.465-136.46
c0-75.252,61.213-136.465,136.465-136.465c75.25,0,136.468,61.213,136.468,136.465
C318.424,257.208,257.206,318.416,181.956,318.416z"/>
<path d="M75.817,181.955h30.322c0-41.803,34.014-75.814,75.816-75.814V75.816C123.438,75.816,75.817,123.437,75.817,181.955z"/>
</svg>
<span>Zoeken</span>
</div>
I had to remove the float of the span element inside.

How to make a circular button out of a PNG image?

I'm building a website with a lot of images. The concept is of a galaxy so you can imagine I have a number of round planets and I want to make them clickable buttons.
These planets are in PNG format with transparent background and I want the clickable area to only be the non-transparent area (which is the shape of a circle). However, I have not found a possible solution to do this.
I have also tried to put a transparent circle on top of the image, and put <a href> on the transparent circle instead of on the image, but this does not seem to work either.
What makes it worse is that I have overlapping images which might cause some of the solutions I found not working. For example I have two or three overlapping images and I want them all to be a button (linking to different pages) (and I have another image in its background) so I don't know what's going to happen if I click at the intersection of these buttons.
Some of the solutions I've tried are:
http://jsfiddle.net/josedvq/Jyjjx/45/
http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_areamap
http://jsfiddle.net/DsW9h/
http://bryanhadaway.com/how-to-create-circles-with-css/
A snippet of my code:
HTML
<div>
<a href="~/SomePage">
<img draggable="false" class="AIcon" src="~/Content/Aicon.png" id="AI">
</a>
</div>
CSS
.AIcon{
position:absolute; left: 50%; top: 40%; width: 2.5%; height:5%; opacity: 1;
-webkit-animation: AAAIcon .5s linear 0s 1 normal forwards running;
}
#-webkit-keyframes AAAIcon {
0% {left: 50%; top: 40%; width: 2.5%; height:5%; opacity: 0; z-index:4;}
100% {left: 78%; top: 20%; width: 32%; height:32%; opacity: 1; z-index:4;}
}
As it is now the image is clickable within the whole square of the image, including the transparent area, but not all of the area is clickable (there are some patches in the image where it's just not clickable).
This is driving me nuts. Any pointers would be extremely helpful.
You have three ways to do it:
1- In the following snippet, I have used a css circle inside an image div on the first moon.
2- Alternatively, got the same result on the second moon placing the circle on div:after.
3- A third method is simply the opposite of the second: create a transparent circle and let the moon image on :after.
The first and third methods allow you to use the moon as a link with onclick javascript mouse event. The red element is set with pointer-events: none; so it have no effect on the moons' hovers.
body {
margin:0px;
background: black;
overflow: hidden;
}
#circle1 {
width: 200px;
height: 200px;
background: purple;
-moz-border-radius: 100px;
-webkit-border-radius: 100px;
border-radius: 100px;
cursor: pointer;
opacity: 0.2;
}
#image1 {
display: inline-block;
width: 200px;
height: 200px;
position: relative;
background: url('http://i.imgur.com/YAWvTuu.png');
background-repeat: no-repeat;
background-size: 100% 100%;
}
#image2 {
display: inline-block;
width: 200px;
height: 200px;
position: relative;
background: url('http://i.imgur.com/YAWvTuu.png');
background-repeat: no-repeat;
background-size: 100% 100%;
}
#image2:after {
content:"";
display: inline-block;
width: 200px;
height: 200px;
background: orange;
-moz-border-radius: 100px;
-webkit-border-radius: 100px;
border-radius: 100px;
cursor: pointer;
opacity: 0.2;
}
#inactive {
background: tomato;
position:absolute;
top:50px;
left: 50px;
height:50px;
width: 400px;
pointer-events: none;
opacity: 0.9;
}
#third {
position:absolute;
display: inline-block;
width: 200px;
height: 200px;
background: transparent;
-moz-border-radius: 100px;
-webkit-border-radius: 100px;
border-radius: 100px;
cursor: pointer;
}
#third::after {
content: url('http://i.imgur.com/YAWvTuu.png');
cursor: auto;
pointer-events: none;
}
<div id="image1" alt=image><div id="circle1" onClick="window.location.href = 'http://www.google.com'"></div></div>
<div id="image2" alt=image></div><div id=third class="circle" alt=image onClick="window.location.href = 'http://www.google.com'"></div>
<div id=inactive></div>
I'm not sure if I've interpreted your question properly, but look into z-index. If there's elements overlapping each other, this will be a reason why they're not able to be clicked.
So, you can wrap the planet or circle in an <a> tag, border-radius that <a> element to be 100% which makes it a full circle and then hide the overflow.
See this: https://jsfiddle.net/xcqy7r14/2/
Markup:
<a href="#">
<canvas></canvas>
</a>
<br><br>
<a href="#">
<canvas></canvas>
</a>
CSS:
a {
border-radius: 100%;
overflow: hidden;
display: inline-block;
}
canvas {
width: 100px;
height: 100px;
display: inline-block;
background: #f00;
border-radius: 100%;
}

Hover event taking place outside of area I am wanting

I have a small bug that I can't seem to locate. In my snippet you can see how whenever you hover over the image, opactiy and an image scale takes place, as well as a box comes over the image. That is perfect, but my issue is whenever you hover over the text below it, the hover effect takes place over the image.
I can't seem to figure out how for the hover effect to only take place when the mouse is hovering over the image.
Any suggestions would be appreciated.
$('.home-img-block img').addClass(function() {
return (this.width / this.height > 1) ? 'wide' : 'tall';
});
#home-img-block-section {
width: 100%;
height: 900px;
}
#home-img-blocks {
width: 100%;
height: 750px;
}
.home-img-block {
width: 33.33%;
float: left;
display: block;
overflow: hidden;
position: relative;
}
.home-img-container {
position: relative;
overflow: hidden;
cursor: pointer;
}
.home-img-block:hover .overlay {
background: rgba(0, 0, 0, 0.6);
width: 100%;
height: 100%;
position: absolute;
top: 0;
left: 0;
}
.home-img-container:after {
content: attr(data-content);
color: #fff;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
opacity: 0;
transition: all 0.5s;
border: 1px solid #fff;
padding: 20px 25px;
text-align: center;
}
.home-img-container:hover:after {
opacity: 1;
}
.home-img-block img {
display: block;
transition: all 1s ease;
}
.home-img-block:hover img {
transform: scale(1.25);
background: rgba(0, 0, 0, 0.3);
width: 33.33%;
max-height: 100%;
overflow: hidden;
}
.home-img-block img.wide {
max-width: 100%;
max-height: 100%;
height: auto;
width: 100%;
}
.home-img-block img.tall {
max-width: 100%;
max-height: 100%;
width: auto;
}
#home-img-block-wording-container {
width: 100%;
height: 300px;
}
.home-img-wording-blocks {
width: 100%;
height: 100%;
text-align: center;
display: inline-block;
vertical-align: top;
}
.home-img-wording-block-title {
padding-top: 30px;
font-size: 1.7em;
}
.home-img-wording-block-description {
padding: 25px 50px 0 50px;
font-size: 1.1em;
color: #5d5d5d;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="home-img-block-section">
<div id="home-img-blocks">
<div class="home-img-block fadeBlock1">
<div data-content="FIND OUT MORE" class='home-img-container'>
<img src="http://optimumwebdesigns.com/images/test1.jpg">
<div class="overlay"></div>
</div>
<div class="home-img-wording-blocks">
<div class="home-img-wording-block-title">WEB DESIGN</div>
<div class="home-img-wording-block-description">The OD team can see your web design visions brought
to life, creating a site that promotes your uniqueness through specific functionalities and features.</div>
</div>
</div>
<div class="home-img-block fadeBlock2">
<div data-content="FIND OUT MORE" class='home-img-container'>
<img src="http://optimumwebdesigns.com/images/test2new.jpg">
<div class="overlay"></div>
</div>
<div class="home-img-wording-blocks">
<div class="home-img-wording-block-title">ECOMMERCE</div>
<div class="home-img-wording-block-description">Custom built solutions catered towards you end goal.
gfdgfdsg greg reg regrfesg fdsg gretswtgy tgreswt treswt trgegfd gsvbd fbgre greasgv drfdg greaag gredr</div>
</div>
</div>
<div class="home-img-block fadeBlock3">
<div data-content="FIND OUT MORE" class='home-img-container'>
<img src="http://optimumwebdesigns.com/images/test3new.jpg">
<div class="overlay"></div>
</div>
<div class="home-img-wording-blocks">
<div class="home-img-wording-block-title">MARKETING STRATEGIES</div>
<div class="home-img-wording-block-description">MARKETING STRATEGIES gfdgf fdggs gfsg gfsg gf sgf g
gfdsg sdfggfs gfdsgssdfg fdggfds gfdsg gfds gfdgs gf dsgfdsgfgs gfdgfs gtrg resg reg rgesgresrgrg</div>
</div>
</div>
</div>
</div>
.home-img-block:hover .overlay
and
.home-img-block:hover img
replace them with
.home-img-container:hover .overlay
.home-img-container:hover img
otherwise you're triggering when you hover over the whole container instead of only wheen hovering the img one.