HTML5/CSS3: Hover Image Replacement - html

I've read multiple solutions here on Stack Overflow, but I still haven't found my solution. I've tried adding image goes here and manipulating #one:hover with CSS, but nothing happened. Using image goes here and manipulating #one:hover with CSS I get limited results, but they're all wrong. The original image never gets replaced, and a very thin line of the replacement image appears underneath my original image. Here 's my code:
HTML5:
<nav>
<p/><p/>
<a id="one" href="form.html"><img src="nav.email.gif" alt="E-Mail Form" width="256px" height="100px"/></a>
</nav>
CSS3:
#one:hover
{
background-image: url('nav.email.ani.gif');
width:256px;
height:100px;
}
Using "background" instead of "background-image" gives me nothing, and using a div tag gives me nothing. I tried absolute placement of the replacement image, but again all I got was a thin line instead of a full size image (even when using width and height). Thanks in advance.

One solution is suggest is to use background-image to img element like:
#imgOne:hover {
background-image: url('http://placehold.it/200x100');
width: 256px;
height: 100px;
}
#imgOne {
background-image: url('http://placehold.it/400x200');
}
<nav>
<p/>
<p/>
<a id="one" href="form.html">
<img id="imgOne" src="" alt="E-Mail Form" width="256px" height="100px" />
</a>
</nav>

Here is another alternative.
<a id="one" href="form.html>
<img title="email" src="nav.email.gif" onmouseover="this.src='nav.email.ani.gif'" onmouseout="this.src='nav.email.gif'" />
</a>

Option 1: Using Css 'Hover'
Rather than using an img tag, why don't you use a 'normal' div element? That way, you can do something like the snippet below, which sets the background for both the 'normal' and 'hovered' version within the css itself?
div {
height: 200px;
width: 200px;
background: url(http://placekitten.com/g/200/200);
}
div:hover {
background: url(http://placekitten.com/g/200/500);
}
<div></div>
Option 2: Replace Using Javascript/JQuery
You could use javascript at this point to replace the image's src, but I (personally) feel this would be a slower/less efficient approach than using css, Although, it would go along the lines of:
$("#myImageID").hover(function(){
$(this)...
});

The problem you have, is that the backgroung image is assigned to the a tag, but then have a image overlaying it.
I'd try this:
#one {
display: inline-block; /* keep it as inline nide but make it aware of width and height */
width: 256px;
height: 100px;
background-image: url('nav.email.gif');
background-repeat: no-repeat;
text-indent: -9999px; /* to hide the SEO text */
}
#one:hover {
background-image: url('nav.email.ani.gif')
}
Important: remove the img tag and put some text for SEO reasons in the a tag, if you like:
Some text for SEO reasons like Contact-Formular foo bar baz
*SEO text is optional.

Using Pure css content is fast and easy
#one:hover img{ content:url("http://placehold.it/400x200") }
<nav>
<p/><p/>
<a id="one" href="form.html">
<img src="http://placehold.it/200x100" alt="E-Mail Form" width="256px" height="100px"/>
</a>
</nav>
or You can Use #Alex Char answer or use this alternative solution.
#one img:nth-child(2)
{
display: none
}
#one:hover img:nth-child(1)
{
display: none
}
#one:hover img:nth-child(2)
{
display: block
}
<nav>
<p/><p/>
<a id="one" href="form.html">
<img src="http://placehold.it/200x100" alt="E-Mail Form" width="256px" height="100px" />
<img src="http://placehold.it/400x200" alt="E-Mail Form" width="256px" height="100px"/>
</a>
</nav>

Related

Set the equivalent of a src attribute of an img tag

I am doing like this:
<img class="myimg" />
.myimg{
content:url('/images/user_logo.png') center no-repeat;
}
But the image is not getting displayed. I want to achieve it without using background image
You are almost good, keep only the url (but this is not supported by all the browser)
.myimg {
content: url('https://lorempixel.com/400/200/');
}
<img class="myimg">
This will also work if there is already an image defined:
.myimg {
content: url('https://lorempixel.com/400/200/');
}
<img class="myimg" src="https://lorempixel.com/g/400/200/">
For browser compatibility better rely on another method. Let's assume that your image is within a container then you can try something like this:
.myimg img {
display: none;
}
.myimg:before {
content: url('https://lorempixel.com/400/200/');
}
<span class="myimg">
<img src="https://lorempixel.com/g/400/200/">
</span>
Use background-image for that:
.myimg{
background-image: url('/images/user_logo.png');
}
Try using :before and :after method to achieve this. follow by the stack example here
.myimg:before {
content:url('../images/user_logo.png');
}

englarge image upon hover using html

I am new to all of this and wanted to know how to enlarge my image when I hover over it.
So far I have tried this.
<ul class="enlarge">
<li>
<img src="http://bhushan.wcukdev.co.uk/wp_239/dev/wp-content/uploads/2015/02/Optimized-DSC_0077.jpg" width="150px" height="100px" alt="St John's" />
<span>
<img src="http://bhushan.wcukdev.co.uk/wp_239/dev/wp-content/uploads/2015/03/St-Johns-Pop-up.jpg" />
<br />St John's, Baldock
</span>
</li>
All this does is makes a small image and a large image. I don't know how to use css so if you respond please can it be in HTML code.
Also the HTML code that is coming up in the text box beneath is not what I have written and don't know how to change that.
Thanks for any help in advance.
Sarah
You should really look into CSS or Javascript as otherwise hovering is a near-impossible task. Heres what you can do:
First off, remove the span and use a class to identify the thumbnail.
<ul class="enlarge">
<li>
<img src="http://bhushan.wcukdev.co.uk/wp_239/dev/wp-content/uploads/2015/02/Optimized-DSC_0077.jpg" width="150px" height="100px" alt="St John’s" class="thumbnail" />
<img src="http://bhushan.wcukdev.co.uk/wp_239/dev/wp-content/uploads/2015/03/St-Johns-Pop-up.jpg" class="large-image" />
<br />St John’s, Baldock
</li>
</ul>
Now add some CSS, don't worry, it's rather simple. What we want to accomplish is that when you hover over the thumbnail, we display the larger image. So on hover, we hide the thumbnail and show the larger image. But since we're hiding the thumbnail, we can't hover on it, so we also want to keep displaying the larger image until our cursor moves away from it entirely.
<style type="text/css">
.enlarge .thumbnail + img {
display: none;
}
/* Hovering over the thumbnail, hide the thumbnail */
.enlarge .thumbnail:hover {
display: none;
}
/* Hovering over the thumbnail, show the large image and keep showing it when hovering over the image */
.enlarge .thumbnail:hover + img,
.enlarge .thumbnail + img:hover {
display: block;
}
</style>
The .enlarge select all elements with class="enlarge", the .thumbnail does the same for the class thumbnail. img selects every image element, and the + in the middle says to select any element that comes directly after the preceding, so the line simply reads: select any img element that comes after a .thumbnail element that is inside a .enlarge element. The :hover seems self-explanatory, but here goes anyway: a : selector is called a pseudo-selector and defines a state or meta element (meta elements are elements you can stylise but aren't really there, like ::before and ::after). Metas usually use a ::. There are other pseudo-states as well, like :active. The style that is defined here will only be invoked when that state is invoked. Its the easiest way to make a hover happen!
You can, however, do this with just one image as well:
<img src="http://bhushan.wcukdev.co.uk/wp_239/dev/wp-content/uploads/2015/02/Optimized-DSC_0077.jpg" width="150px" height="100px" alt="St John’s" class="enlarge-image" />
<br />St John’s, Baldock
It simplifies your styling a lot:
<style type="text/css">
.enlarge-image {
width: 150px;
height: auto;
}
/* Show full size on hover */
.enlarge-image:hover {
/* This can be any size you want it to be as well. */
width: auto;
}
</style>
A couple of notes on your code: first off, be aware you have typographic quotes (” compared to regular quotes: ") surrounding your image source. This can lead to issues. Second, an image size is always in pixels unless defined in %, so ommit px from your width and height.
.enlarge-image {
width: 50px;
height: auto;
}
.enlarge-image:hover {
width: auto;
}
<img src="http://bhushan.wcukdev.co.uk/wp_239/dev/wp-content/uploads/2015/02/Optimized-DSC_0077.jpg" width="150" height="100" alt="St John’s" class="enlarge-image" />
<br />St John’s, Baldock
You should start learning css. It is the only way to fix it.
<head>
<style type="text/css">
.picture{
width : 150px;
height : 100px;
}
.picture:hover{
width : 200px;
height : 150px;
}
</style>
</head>
<ul class="enlarge">
<li>
<img src="http://bhushan.wcukdev.co.uk/wp_239/dev/wp-content/uploads/2015/02/Optimized-DSC_0077.jpg" class="picture" alt="St John’s " />
</li>
</ul>
Figured it out, but now it looks rubbish. Anyone know how to hide the other photos when I hover and enlarge one photo as they just move around the larger photo.
That's what I have. I still cant post a photo of what it looks like. Also the last photo when you hover over it it flickers, is this my code or the size of the screen?
Above answer are correct. I am providing you some link which will help you.
http://cssdemos.tupence.co.uk/image-popup.htm
http://jsfiddle.net/4AM3S/

How to change background image change on hover

I would like to hover over div id-"RollOver1" and be able to change the background to a different image from the main one. Only pasted the HTML for the rollover div cant use jscript so is there a way in HTML or ....?
<div id="RollOver1" style="position:absolute;overflow:hidden;left:152px;top:397px;width:183px;height:183px;z-index:4">
<a href="./car.html">
<img class="hover" alt="" src="images/Enter_02.jpg" style="left: 0px; top: 0px; width: 183px; height: 183px; display: block;">
<span style="display: none;"><img alt="" src="images/index_01.jpg" style="left:0px;top:0px;width:183px;height:183px"></span>
</a>
</div>
You can do this with the following code:
#RollOver1 {
background:url(INITIAL_BACKGROUND);//here use the url of the background you want when is NOT on hover
}
#RollOver1:hover {
background:url(BACKGROUND_ON_HOVER);//here use the url of the bg you want when is on hover
}
You can use :hover pseudo class:
#RollOver1 {
background: url('img1.png');
}
#RollOver1:hover {
background: url('img2.png');
}
But you will usually see "glich" between changes of images, because second image will take some time to be loaded.
To avoid that, use image sprite. Put both images (normal and hover) to single image and than use css background-position
#RollOver1 {
background: url('sprite.png') no-repeat 0 0;
}
#RollOver1:hover {
background-position: -80px -90px;
}
It will be more efficient way to load small images (like buttons, icons and so on).
Check this link
Using JQuery you can try
$(document).on("mouseover", "#RollOver1", function(e) {
$(this).css("background", "url(sampleImage.png) no-repeat");
}
});
use the css pseudo class :hover
You can use below styles
.RollOver1:hover {
background-image: url('paper.gif');
}

CSS Background-Image not showing up

I am trying to setup background images using CSS but I can't seem to get the images to populate correctly.
Here is the CSS for what I want to do
a.fb {
background-image: url('img/Facebook.png');
}
a.fb:hover {
background-image: url('img/FacebookHover.png');
}
Here is the html code that I am using, I have tried a couple of different ways to populate the images with no luck
<div class="footer">
<a class="fb" href="http://www.facebook.com" target="_blank"></a>
</div>
Any help would be greatly appreciated
Okay added the following and still not go any other thoughts
a.fb {
display:block;
width: 33px;
height: 33px
background-image: url('img/Facebook.png');
}
EDIT: Yup got it working now forgot the ; after height, but no I get a white border around it and tried setting border: none; no luck
a.fb {
border: none;
display:block;
width: 33px;
height: 33px;
background-image: url('img/Facebook.png');
}
An anchor tag by default shows as an inline elements, so it depends on its content in order to get a height and width. To do what you want, you should add some styles: display:block; width: 20px; height: 20px.
You could also change the aproach completely and use html + mouseover and mouseout events:
<div class="footer">
<a class="fb" href="http://www.facebook.com" target="_blank">
<img src="http://www.facebook.com/images/fb_icon_325x325.png" alt="fb" name="fb" width="33px" height="33px" name="image_name" onmouseover="fb.src='http://goo.gl/cxiR7'; fb.width='38'; fb.height='38';" onmouseout="fb.src='http://www.facebook.com/images/fb_icon_325x325.png'; fb.width='33'; fb.height='33';" />
</a>
</div>
Here is a jsBin: http://jsbin.com/onehuw/1/edit
background-image only draws in the space that the element occupies. Your a tag has no content, and therefore it's width is 0. You'll not see any content (and background) until you give it at least some width (and height if needed).
You need to add padding to the <a> tag otherwise it has a width and height of 0 for example:
a.fb {
padding: 20px;
background-image: url('img/Facebook.png');
}
a.fb:hover {
background-image: url('img/FacebookHover.png');
}
You could also just set the width and height of the anchor

How to keep <img> over text, without hiding the text and using CSS background or text-indent?

See js fiddle here http://jsfiddle.net/Ws8ux/
Is it possible to keep the text under logo without hiding it using display:none or text-indent? I want to bring the image up and keep logo behind it. Like is PSD layers. And Don't want to use Logo Image as a CSS background
<a href="/" title="Return to the homepage" id="logo">
<img src="http://lorempixum.com/100/100" alt="Nike logo" />Logo Text
</a>
Like this (fiddle)?
HTML:
<a href="/" title="Return to the homepage" id="logo">
<img src="http://lorempixum.com/100/100" alt="Nike logo" /><span>Logo Text</span>
</a>
CSS:
a { display: block; position: relative; }
a span {
display: block;
position: absolute;
top: 40%;
}
What's the purpose of keeping the text if it's to be hidden? If your goal is to hide the text underneath the image for the purposes of accessibility, you may be interested to know that most search engines won't fault you if you just leave the text as an alt attribute on your image. In contrast, you might find some techniques for deliberately hiding content could prove detrimental to your cause.
If it's important to have both the image and text present, you may want to try wrapping the text in a <span>, using an accessible style on that and then disabling it in your print stylesheet.
#jitendra; may be you have to play with css:
CSS:
a { position:relative; }
img { position:absolute; top:0; left:0 }
HTML:
<a href="/" title="Return to the homepage" id="logo">
Logo Text<img src="http://lorempixum.com/100/100" alt="Nike logo" />
</a>
check the fiddle may that's help your http://jsfiddle.net/sandeep/Ws8ux/11/
You can do this by setting position: absolute for the image. You should probably also make sure the anchor is the same size as the image, so that it doesn't break the layout of other elements on the page.
img {
position: absolute;
}
a {
display: inline-block;
height: 100px;
position: relative;
width: 100px;
}
The updated fiddle: http://jsfiddle.net/Ws8ux/7/