I been trying all day to find a nice simple short pure-css/html code only to make a div disappear when a button is clicked. I would prefer CSS but if not possible will take a JS solution instead.
Lot of solutions on overflow but most use js. please take a look at my code below and add a example or suggestion below. Thanks for any help, Stewy
body {
background-color: #6B6B6B;
font-family: Arial;
color: grey;
font-size: 12px;
margin: 0px;
}
/*............... bgcover ...............*/
.bgcover {
position: fixed;
top: 50px;
left: 50px;
width: 200px;
height: 200px;
background-color: rgba(0, 0, 0, .4);
}
.call {
position: fixed;
top: 100px;
left: 100px;
}
/*........ crossfade on buttons ........*/
.hover,.nohover{
transition:.3s;
position:absolute;
}
.nohover{
opacity:0;
}
a:hover .hover{
opacity:0;
}
a:hover .nohover{
opacity:1;
}
<div class="bgcover" align="center">
<p>Button turns off this bg cover div.</p>
</div>
<div class="call">
<a href="bgcoveroff.htm">
<img src="http://wizzfree.com/pix/call2.png" width="100" class="nohover">
<img src="http://wizzfree.com/pix/call.png" width="100" class="hover"></a>
</div>
Define your interactive node and your target node.
Create event listener to handle click event.
create a function which would hide your second element
const button = document.getElementById('button'); // or classname, whatever. it is your link or any node element instead of it.
const div = document.getElementById('targetDiv');
function toggleDiv(e) {
// e - event object - {button}.
e.preventDefault(); // only if you use <a> as node.
div.classList.add("my-classname-to-hide-div");
}
button.addEventListener('click', toggleDiv, false);
css
.my-classname-to-hide-div {
display: none;
// or
opacity: 0;
visibility: hidden;
transition: all 0.2s all;
}
ps. if you use JQuery the code could be much simpler and you can use built-in transition effects.
You have to pass the the inline JS in tag. than only possible to hide the element. onclick attribute should need to trigger.
Like this Click
Hi Stewy please check this code. It is example of the inline js.
body {
background-color: #6B6B6B;
font-family: Arial;
color: grey;
font-size: 12px;
margin: 0px;
}
/*............... bgcover ...............*/
.bgcover {
position: fixed;
top: 50px;
left: 50px;
width: 200px;
height: 200px;
background-color: rgba(0, 0, 0, .4);
}
.call {
position: fixed;
top: 100px;
left: 100px;
}
/*........ crossfade on buttons ........*/
.hover,.nohover{
transition:.3s;
position:absolute;
}
.nohover{
opacity:0;
}
a:hover .hover{
opacity:0;
}
a:hover .nohover{
opacity:1;
}
.hidden{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="bgcover" id=box align="center">
<p>Button turns off this bg cover div.</p>
</div>
<div class="call">
<a id="btn" onclick="document.getElementById('box').classList.toggle('hidden');" >
<img src="http://wizzfree.com/pix/call2.png" width="100" class="nohover">
<img src="http://wizzfree.com/pix/call.png" width="100" class="hover"></a>
</div>
Related
So I'm trying to use a hover effect on two elements. One of the elements has a absolute position, this element needs to have the background recolored once hovered. This seems to work, however I cannot get the back image to scale accordingly. I think it has to do with the absolute positioning of the first div. But I cannot find a way to fix this. I have been trying to fix it using pointer-event but I cannot get that to do the trick.
<style>
.master {
width: 1200px;
overflow: hidden;
position: relative;
}
img {
width: 100%;
}
img:hover {
transform: scale(1.2)
}
.hover {
position: absolute;
height: 100%;
width: 100%;
}
.hover:hover {
background: rgba(255, 0, 0, 0.5);
}
</style>
<div class="master">
<div class="hover">
</div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>
This is the code I have got right now
You can't get hover effects on two stacked elements at once. However, if you want to be able to hover the element which is behind another element, you can use pointer-events: none; on the element in the foreground, allowing mouse actions to "go through it" and therefore affect the element in the back:
<style>
.master {
width: 1200px;
overflow: hidden;
position: relative;
}
img {
width: 100%;
}
img:hover {
transform: scale(1.2)
}
.hover {
position: absolute;
height: 100%;
width: 100%;
pointer-events: none;
}
.hover:hover {
background: rgba(255, 0, 0, 0.5);
}
</style>
<div class="master">
<div class="hover">
</div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>
You can add pointers-event: none; to img and your pointer will ignore the image but that's only in case you don't have any plan to add some mouse actions on image later
You're correct that you're not getting the hover event on the image because the absolutely positioned <div> is covering it. But since the <img> element immediately follows the <div> element in your HTML hierarchy, you can use the Adjacent Sibling Combinator to target it.
<style>
.master {
width: 1200px;
overflow: hidden;
position: relative;
}
img {
width: 100%;
}
.hover {
position: absolute;
height: 100%;
width: 100%;
z-index: 1;
}
.hover:hover {
background: rgba(255, 0, 0, 0.5);
}
.hover:hover + img {
transform: scale(1.2)
}
</style>
<div class="master">
<div class="hover">
</div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>
I have found out that moving the hover effect to .master instead of the seperate elements seem to work.
<style>
.master {
width: 1200px;
overflow: hidden;
position: relative;
}
img {
width: 100%;
z-index: 50;
}
.master:hover img{
transform: scale(1.2)
}
.hover {
position: absolute;
height: 100%;
width: 100%;
opacity: 0.8;
z-index: 100;
}
.master:hover .hover {
background: rgba(255,0,0, 0.5);
}
</style>
<div class="master">
<div class="hover">
test
</div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>
There is no problem between hover and absolut position.
Your problem is that img tag is place on top on .hover cause of the absolute position.
So your hover div is behind your image and the mouse over is intercepted by image tag.
Hope it's help
You need to set the :hover to an absolute element with class .hover:
.hover:hover + img {
transform: scale(1.2);
}
Also, set z-index for the same class without a :hover.
In addition, I've added transition for transform and background.
.master {
width: 1200px;
overflow: hidden;
position: relative;
display: inline-flex;
}
.hover {
position: absolute;
height: 100%;
width: 100%;
z-index: 9999;
transition: background .5s;
}
img {
width: 100%;
transition: transform .5s;
}
.hover:hover + img {
transform: scale(1.2);
}
.hover:hover {
background: rgba(255, 0, 0, 0.5);
}
<div class="master">
<div class="hover"></div>
<img src="http://via.placeholder.com/1200x350" alt="">
</div>
I have added a My account link and a wishlist link to my header. It shows fine on the home page but when I go on to any other page, the images are no longer displayed, all I see is the Alt text.
Can someone help me tweak my code so that the same images are shown properly on all pages? :-)
I have tried to do it so that the pictures change color on hover by having one image below the other...
PHP:
<div id="myaccount">
<a href="wordpress/my-account"><img class="bottom" src="wp-
content/themes/mt_theme/images/my_account_hover.png" alt="My
Account"/>
<img class="top" src="wp-content/themes/my_theme/images/my_account.png" alt="My Account"/>
<p class="icotext">My Account</p>
</a>
</div>
<div id="wishlist">
<a href="wishlist"><img class="bottom2" src="wp-content/themes/smy_theme/images/wishlist_hover.ico" alt="Wishlist"/>
Wishlist
CSS:
#myaccount{
position: relative;
margin: 0 auto;
}
#myaccount img {
position:absolute;
left:900px;
top: -20px;
Height: 50px !important;
width: 50px !important;
}
a:hover img.top {
opacity:0;
}
.icotext{
position: absolute;
left: 890px;
top: 28px;
color: #000000
}
a:hover .icotext{
font-weight: bold;
left: 888px;
}
#wishlist{
position: relative;
margin: 0 auto;
}
#wishlist img {
position:absolute;
left:990px;
top: -20px;
Height: 50px !important;
width: 50px !important;
}
.icotext2{
position: absolute;
left: 992px;
top: 28px;
color: #000000
}
a:hover .icotext2{
font-weight: bold;
left: 990px;
}
a:hover img.top2 {
opacity:0;
}
img.bottom2 {
opacity:0;
}
a:hover img.bottom2 {
opacity:1;
}
img.bottom {
opacity:0;
}
a:hover img.bottom {
opacity:1;
I'm guessing the other pages are trying to get the images from another folder.
src="wp-content/themes/my_theme/images/my_account.png"
You are using a relative address that is interpreted as "{current folder}/wp-content/..."
Try using
src="/wp-content/themes/my_theme/images/my_account.png"
The / on the front will be interpreted as "{site_root}/wp-content/..."
I'm new in HTML, CSS, sorry if my question is not professional.
I tried to find information about delay after animation but without results. Only before. My question is:
I have two buttons and div, when mouse on div - buttons should be shown - I resolved it with visibility and hover. but I need delay (when I move mouse out of div, buttons should be visible 5 sec more).
As I saw animation-delay does not resolve this issue. Please help to understand how I can do it. My code is bellow.
#deleteRow {
width: 80px;
height: 80px;
background-color: #881013;
border: none;
background-image: url("minus.png");
background-repeat: no-repeat;
background-position: center;
left: -84px;
padding: 0px;
margin: 0;
position: absolute;
visibility:hidden;
}
#deleteCol {
width: 80px;
height: 80px;
background-color: #881013;
border: none;
background-image: url("minus.png");
background-repeat: no-repeat;
background-position: center;
top: -84px;
right: 0px;
padding: 0px;
margin: 0;
position: absolute;
visibility:hidden;
}
.container {
position: relative;
display: inline-block;
}
.container:hover #deleteRow {
visibility:visible;
}
.container:hover #deleteCol {
visibility:visible;
}
To select an element present after div as button over-here, so for that use CSS + selector, using transition-delay you can delay the transition of visibility on hover, as below,
div {
width: 200px;
height: 200px;
background: red;
margin-bottom: 20px;
}
button {
transition: 1s ease;
transition-delay: 5s;
}
div:hover + .btn1 {
visibility: hidden;
}
div:hover + .btn1 + .btn2 {
visibility: hidden;
}
<div></div>
<button class="btn1">Click - 1</button>
<button class="btn2">Click - 2</button>
You could use a transition on your hover properties, and put a transition-delay on it.
https://www.w3schools.com/cssref/css3_pr_transition-delay.asp
You also can use some JavaScript code (with jQuery library included).
$(".container").mouseenter(function(){
$("#deleteRow,#deleteCol").css("visibility","visible");
});
$(".container").mouseout(function(){
setTimeout(function(){
$("#deleteRow,#deleteCol").css("visibility","hidden");
}, 5000);
});
Im trying to make a block with overlay hover effect (default: weak black background color - Hover: none black background) and an icon and text in the middle that stays in the same state all the way.
How do i get the icon and text to stay in the same state (no hover effect)?
Ive tried several rules to the overlay div and the icon div without any luck.
Is there any css rule that provide some kind of exclusion?
I managed to get it to work by adding them outside the divs that has overlay background, but it didnt work out well as the hover effect breaks when you hover over the icon and text.
Here is the code: https://www.w3schools.com/code/tryit.asp?filename=FEMUM4N9T30Q
<style>
.media-front-top-picture{
background-image: url("");
height:500px;
}
.media-front-top-icon{
content: url(");
width: 130px;
margin: auto;
padding-top: 200px;
opacity: 1;
}
.media-front-txt{
font-size: 22px;
letter-spacing: 8px;
color: white;
margin-top: 15px;
}
.media-front-bottom-picture{
background-image: url("h");
height:500px;
}
.media-front-bottom-icon{
content: url("");
width:130px;
margin: auto;
padding-top: 200px;
}
.media-picture-container {
position: relative;
}
.media-picture-overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
transition: .5s ease;
background-color: rgba(0, 0, 0, 0.47);
}
.media-picture-overlay:hover {
opacity: 0;
cursor:pointer;
}
</style>
<div class="body-media">
<div class="media-picture-container">
<div class="media-front-top-picture" style="border-bottom:4px solid white;">
<div class="media-front-top-icon"></div>
<div class="media-front-txt">VIDEOS</div>
<div class="media-picture-overlay"></div>
</div>
</div>
<div class="media-picture-container">
<div class="media-front-bottom-picture" style="border-bottom:4px solid white;">
<div class="media-front-bottom-icon"></div>
<div class="media-front-txt">PICTURES</div>
<div class="media-picture-overlay"></div>
</div>
</div>
</div>
for the classes on your icons, add a z-index higher than a z-index you add to the overlay class. Also, make sure to make the icon classes have position:relative so the z-index is applied. Note, my example only applies this solution to one icon, its up to you to apply it elsewhere.
Example:
.media-front-top-icon{
content: url("example.com");
width: 130px;
margin: auto;
padding-top: 200px;
opacity: 1;
z-index:10;
position:relative;
}
.media-picture-overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
transition: .5s ease;
background-color: rgba(0, 0, 0, 0.47);
z-index:5;
}
i have following code
<div>
<img src="http://placehold.it/350x150"/>
<div class="link-cont">click here to see more info</div>
</div>
div {
width: 350px;
font-size:12px;
position: relative;
}
div img{
padding:0 10px;
}
.link-cont {
background: red;
position: absolute;
bottom: 0;
width: 370px;
height: 210px;
opacity: 0;
transition: all 0.4s;
z-index: -1
}
div:hover .link-cont {
opacity: 1;
bottom:-40px;
}
.link-cont a{
opacity: 0;
}
div:hover .link-cont a{
position: relative;
opacity: 1;
bottom:-175px;
left:10px;
background:#fff;
color:red;
text-decoration:none;
padding:0 10px;
}
A link is wrapped inside a div which appears on hover. how do i make this touch device friendly.
jsfidd--> http://jsfiddle.net/yeyene/Nnd7w/17/
Several solutions:
skip hover effects in touch device stylesheets
use JavaScript to turn hover into click interactions
use JavaScript to simulate hover interactions on the touch device (see this Question on StackOverflow
Try this:
<script>
document.addEventListener("touchstart", function(){}, true);
</script>