I'm trying to make a pure CSS image change on hover / rollover without using background images. So far I have one image and when you rollover that image, another image appears. Here is the CSS:
#hidden {
display: none;
}
#visible:hover + #hidden {
display: block;
}
So, when you rollover the #visible div, the #hidden div appears. Here is the jsFiddle: http://jsfiddle.net/MNyzd/1/
This works great, but it is not exactly what I want to accomplish. I would like the images to swap. So when you rollover #visible, it should disappear instead of remaining visible. My first initial idea was to make the #visible div to display:none on hover (#visible:hover display:none;), but this did not work.
So does anyone have any idea how I would successfully turn this into a traditional image hover / swap using this method? Any help would be appreciated and again, here is the jsFiddle... http://jsfiddle.net/MNyzd/1/
Use a container where you do the hover on:
http://jsfiddle.net/MNyzd/8/
.hidden {
display: none;
}
.container:hover .visible
{
display: none;
}
.container:hover .hidden {
display: block;
}
See also this answer: Hide / show content via CSS:hover (or JS if need be)
Like this?
jsFiddle
div {
cursor:pointer;
overflow:hidden;
width:300px;
}
div > img:nth-child(2), div:hover > img:nth-child(1) {
display:none;
}
div:hover > img:nth-child(2) {
display:block;
}
http://jsfiddle.net/MNyzd/4/
EDIT: The code:
#hidden {
display: none;
position: absolute;
top: 8px;
left: 8px;
}
#visible:hover + #hidden {
display: block;
}
#hidden, #visible {
width:300px;
height:300px;
}
<div id="visible"><img src="http://b.vimeocdn.com/ps/372/159/3721596_300.jpg"></div>
<div id="hidden"><img src="http://yuccan.com/wp-content/uploads/2013/04/cool-eez-spaced2.png"></div>
Related
I'm currently working on the feature in which whenever a user hovers over a link, instant preview is shown below the link. Like this -
What I wanted to do is, whenever a user hovers over a link, iframe should shift towards right, just like in Google Instant preview.
<div class="title" (mouseover)="overTitle()" (mouseleave)="overTitle()">
{{item.title}}
<div class="box">
<iframe width="400px" height="400px" [src]="myUrlList[i]"></iframe>
</div>
</div>
CSS file -
.box {
display: none;
width: 100%;
}
a:hover + .box, .box:hover{
display:block;
position: absolute;
z-index:100;
}
I tried using align="right" in div tag, but nothing happens. Also in .box class in CSS, I tried using float: right;, but nothing happens. It will be very much helpful if someone can help me. :)
Check something like that, without JS, pure CSS.
https://jsfiddle.net/fxdhcrxj/2/
I just use opacity and visibilityto animate fade in and out, you can use any other methods, like JS fade In out etc.
In CSS if you want catch next element use ~
CSS:
.title{
// for proper position absolute element inside
position: relative;
}
// We catch nexy element .box, and when some one hover over box
.title a:hover ~.box, .title .box:hover{
//display: inline;
visibility: visible;
opacity: 1;
}
// As i use element to bottom 0, I must translated it 100% below of his height.
.title .box{
//display: none;
visibility: hidden;
opacity: 0;
transition: visibility 0.2s ease-in-out, opacity 0.2s ease-in-out;
position: absolute;
z-index: 10;
background: #fff;
bottom:0;
left:0;
transform: translateY(100%);
}
HTML:
<div class="title" >
TITL0E
<div class="box">
<iframe width="400px" height="400px" src="https://www.w3schools.com"></iframe>
</div>
</div>
Important Note:
If you want put lot of Iframes, on page use Lazy load on them.
Simply just replace src attr when someone hover over title or use scripts e.g
http://dinbror.dk/blog/blazy/ otherwise your page will load really slow and make lot of freezes etc.
For better mobile support you may also use focus on element, not only hover
Here you go: http://codepen.io/ruchiccio/pen/vxVoKd
a:hover + .box, .box:hover {
display:block;
position: absolute;
right:0;
top:0;
z-index:100;
}
You need to have right:0; set for the box. Also, your .box was set for a 100% width which is why the iframe didn't know where to go. You have to options: Either set the box width to 400px, which is the same as the iframe (which is what I did in my codepen):
.box {
display: none;
width: 400px;
}
OR you may leave the 100% width but then add text-align:right; to the .box class. Both work.
.box {
display: none;
width: 100%;
text-align: right;
}
I have seen similar post on stack exchange where you can change css of another div when hovered over one div. However in my case it is not working as I am using this with a table cell.
Here, is the HTML code:
body {
padding: 5%;
}
.tbdata {
background-color: royalblue;
color: white;
cursor: pointer;
}
.tooltip {
color: white;
opacity: 0;
transition: opacity 1s;
background-color: red;
min-width: 50px;
min-height: 100px;
}
.tbdata:hover ~ .tooltip {
opacity: 1;
}
.tbdata:hover ~ .tooltip:after {
content: attr(data-);
}
<table>
<tr>
<td class="tbdata" data-="This is the tooltip text for col1">This is table data1</td>
<td class="tbdata" data-="This is the tooltip text for col2">This is table data2</td>
</tr>
</table>
<div class="tooltip">
</div>
I want to make the .tooltip div visible when hovered over the .tbdata and also change the content using attr() from the table cell.
Please suggest.
I used Jquery for this solution.
$('document').ready(function(){
$('.tbdata').hover(
function(){
$(".tooltip").css("visibility","visible");
$(".tooltip").attr("attrname","attrvalue"); //attribute
},
function(){
$(".tooltip").css("visibility","hidden");
$(".tooltip").attr("attrname","attrvalue"); //attribute
}
);
});
Here is example:
Fiddle
You cannot change the content property dynamically on hover, however, you can use some "trickery" to hide a span and show another.
This example below shows how you can do just that. This, as far as I know, is the best solution for a non-JS implementation of dynamically changing a div content on hover
.button{
background:lightblue;
width:200px;
}
.tooltip{
background:red;
width:200px;
}
.tooltip .before { display: block; }
.tooltip .after { display: none; }
.button:hover .tooltip{
background:green;
}
/* Hide the `.before` and show the `.after` */
.button:hover .tooltip .before{ display:none; }
.button:hover .tooltip .after { display:block; }
<div class="button">
Hover over me!
<div class="tooltip"><span class="before">Turn nothing</span><span class="after">Into something</span></div>
</div>
To hide/show the content, you would also apply display property changes to the .tooltip. I've left both div visible for this demonstration, however, to show the changing content.
Just been playing about with pointer-events property in CSS.
I have a div that I want to be invisible to all mouse events, except for :hover.
So all click commands go through the div to the one below it, but the div can report whether the mouse is above it or not still.
Can anyone tell me if this can be done?
HTML:
<div class="layer" style="z-index:20; pointer-events:none;">Top layer</div>
<div class="layer" style="z-index:10;">Bottom layer</div>
CSS:
.layer {
position:absolute;
top:0px;
left:0px;
height:400px;
width:400px;
}
Hover only. It is very easy. No JS... Prevent link default action too.
a:hover {
color: red;
}
a:active {
pointer-events: none;
}
Link here
Edit:
supported in IE 11 and above
http://caniuse.com/#search=pointer-events
"Stealing" Xanco's answer but without that ugly, ugly jQuery.
Snippet: Notice DIVs are in reverse order
.layer {
position: absolute;
top: 0px;
left: 0px;
height: 400px;
width: 400px;
}
#bottomlayer {
z-index: 10
}
#toplayer {
z-index: 20;
pointer-events: none;
background-color: white;
display: none
}
#bottomlayer:hover~#toplayer {
display: block
}
<div id="bottomlayer" class="layer">Bottom layer</div>
<div id="toplayer" class="layer">Top layer</div>
I don't think it's possible to achieve your aims in CSS alone. However, as other contributors have mentioned, it's easy enough to do in JQuery. Here's how I've done it:
HTML
<div
id="toplayer"
class="layer"
style="
z-index: 20;
pointer-events: none;
background-color: white;
display: none;
"
>
Top layer
</div>
<div id="bottomlayer" class="layer" style="z-index: 10">Bottom layer</div>
CSS (unchanged)
.layer {
position:absolute;
top:0px;
left:0px;
height:400px;
width:400px;
}
JQuery
$(document).ready(function(){
$("#bottomlayer").hover(
function() {
$("#toplayer").css("display", "block");
},
function() {
$("#toplayer").css("display", "none");
}
);
});
Here's the JSFiddle: http://www.jsfiddle.net/ReZ9M
You can also detect hover on different element and apply styles to it's child, or using other css selectors like adjacent children, etc.
It depends on your case though.
On parent element hover. I did this:
.child {
pointer-events: none;
background-color: white;
}
.parent:hover > .child {
background-color: black;
}
Pure CSS solution to your request (the opacity property is there just to illustrate the need for the transitions):
.hoverOnly:hover {
pointer-events: none;
opacity: 0.1;
transition-delay: 0s;
}
.hoverOnly {
transition: ,5s all;
opacity: 0.75;
transition-delay: 2s;
}
What it does:
When the mouse enters the box, it triggers the :hover state. However, in that state, the pointer-events are disabled.
But if you do not set the transitions timers, the div will cancel the hover state when the mouse moves; the hover state will flicker while the mouse is moving inside the element. You can perceive this by using the code above with the opacity properties.
Setting a delay to the transition out of the hover state fixes it. The 2s value can be tweaked to suit your needs.
Credits to transitions tweak: patad on this answer.
Just pure css, doesn't need jquery:
div:hover {pointer-events: none}
div {pointer-events: auto}
I use the :hover pseudo-element of an equal-sized parent/container to simulate a hover over my overlay div, then set the overlay's pointer-events to none to pass through clicks to elements below.
let button = document.getElementById('woohoo-button');
button.onclick = () => console.log('woohoo!');
let overlay = document.getElementById('overlay');
overlay.onclick = () => console.log(`Better change my pointer-events property back to 'none'`);
#container {
display: flex;
align-items: center;
justify-content: center;
position: relative;
background-color: green;
width: 300px;
height: 300px;
}
#overlay {
background-color: black;
width: 100%;
height: 100%;
opacity: 0;
z-index: 1;
/* Pass through clicks */
pointer-events: none;
}
/*
Set overlay hover style based on
:hover pseudo-element of its
container
*/
#container:hover #overlay {
opacity: 0.5;
}
#woohoo-button {
position: absolute;
width: 100px;
height: 100px;
background-color: red;
}
<div id="container">
<div id="overlay"></div>
<button id="woohoo-button">
Click Me
</button>
</div>
I'm very much a newbie, but I'm been trying to figure out how to get a hover effect for my thumbnails for the popular posts widget in Blogger.
I have relatively large thumbnails and I want to have the title of the post appear on hover.
Like this
Can anyone help me with this? I've been trying to find a solution online, but have so far found nothing.
Acutally the below can be simple achieved using display: block; and none but you won't be able to transit the element, hence, inorder to transit the element, we will hide it by using opacity: 0; and on hover we change it to 1, and you can make the background opaque using rgba() and the smoothness using transition. Also, don't forget to make the parent element position: relative; as it holds an absolute positioned child element.
I've made the below example from scratch, you can take a look...
Demo
div.wrap {
position: relative;
display: inline-block;
}
div.wrap img {
display: block;
}
.title {
opacity: 0;
background: rgba(255,255,255,.5);
padding: 5px;
-moz-transition: all .5s;
-webkit-transition: all .5s;
transition: all .5s;
position: absolute;
top: 50%;
right: 0;
}
div.wrap:hover .title {
opacity: 1;
}
try this:
suppose you have Html Markup like this:
<div class="thumbnail">
Iam the main content
<div class="onhover">
i am onhover content
</div>
</div>
css:
.thumbnail{
width:200px;
height:100px;
background-color:Red;
}
.thumbnail:hover .onhover {
display: block;
}
see the demo
so basically, what you want is to affect another element(with certain selector), when some other element is hovered, basic syntax is :
sourceSelector:hover targetSelector{ /*whatever css you want to apply*/}
I found this useful html as answer to a question about rollover image to text by renocor (rollover a text hyperlink to become an image)
<style>
.changeable img
{
display:none;
}
.changeable:hover span
{
display:none;
}
.changeable:hover img
{
display:inline-block;
}
</style>
<span>Hyper Text</span><img src="img.png" />
but i needed to achieve the opposite effect. So i inverted span with image it partially works. Meaning that instead of the image i see the text but the problem is that the image doesn't disappear but shifts just below the text.
Any suggestions?
Thanks
.changeable span
{
display: none;
}
.changeable:hover image
{
display: none;
}
.changeable:hover span
{
display: inline-block;
}
</style>
<span>Hyper Text</span><img src="img.png" />
Check the <style> and put img instead of image, worked for me.
.changeable:hover img
{
display: none;
}