Reputation:
So i've got a button on an image that i want to change the colour of when a user hovers over the image.
The css for the button itself
.prev{
cursor: pointer;
position: absolute;
top: 50%;
width: auto;
padding: 16px;
margin-top: -50px;
color: white;
font-weight: bold;
font-size: 20px;
transition: 0.6s ease;
border-radius: 0 3px 3px 0;
user-select: none;
-webkit-user-select: none;
This is the html
<body>
<h2 style="text-align:center">Lightbox</h2>
<div class="row">
<div class="column">
<img src="img_nature.jpg" style="width:100%" onclick="openModal();currentSlide(1)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="img_fjords.jpg" style="width:100%" onclick="openModal();currentSlide(2)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="img_mountains.jpg" style="width:100%" onclick="openModal();currentSlide(3)" class="hover-shadow cursor">
</div>
<div class="column">
<img src="img_lights.jpg" style="width:100%" onclick="openModal();currentSlide(4)" class="hover-shadow cursor">
</div>
</div>
<div id="myModal" class="modal">
<span class="close cursor" onclick="closeModal()">×</span>
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 4</div>
<img src="img_nature_wide.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">2 / 4</div>
<img src="img_fjords_wide.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">3 / 4</div>
<img src="img_mountains_wide.jpg" style="width:100%">
</div>
<div class="mySlides">
<div class="numbertext">4 / 4</div>
<img src="img_lights_wide.jpg" style="width:100%">
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
<div class="caption-container">
<p id="caption"></p>
</div>
<div class="column">
<img class="demo cursor" src="img_nature_wide.jpg" style="width:100%" onclick="currentSlide(1)" alt="Nature and sunrise">
</div>
<div class="column">
<img class="demo cursor" src="img_fjords_wide.jpg" style="width:100%" onclick="currentSlide(2)" alt="Trolltunga, Norway">
</div>
<div class="column">
<img class="demo cursor" src="img_mountains_wide.jpg" style="width:100%" onclick="currentSlide(3)" alt="Mountains and fjords">
</div>
<div class="column">
<img class="demo cursor" src="img_lights_wide.jpg" style="width:100%" onclick="currentSlide(4)" alt="Northern Lights">
</div>
</div>
</div>
This is the css i'm trying to use. div.mySlides is the div that contains my images.
.prev {
color:white;
}
div.mySlides:hover + .prev, .prev:hover {
color:red;
}
The javascript i'm using
<script>
function openModal() {
document.getElementById('myModal').style.display = "block";
}
function closeModal() {
document.getElementById('myModal').style.display = "none";
}
var slideIndex = 1;
showSlides(slideIndex);
function plusSlides(n) {
showSlides(slideIndex += n);
}
function currentSlide(n) {
showSlides(slideIndex = n);
}
function showSlides(n) {
var i;
var slides = document.getElementsByClassName("mySlides");
var dots = document.getElementsByClassName("demo");
var captionText = document.getElementById("caption");
if (n > slides.length) {slideIndex = 1}
if (n < 1) {slideIndex = slides.length}
for (i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
for (i = 0; i < dots.length; i++) {
dots[i].className = dots[i].className.replace(" active", "");
}
slides[slideIndex-1].style.display = "block";
dots[slideIndex-1].className += " active";
captionText.innerHTML = dots[slideIndex-1].alt;
}
</script>
At the moment the color will change only when a user hovers over the button location. I've tried changing the order about in the css only to find nothing works.
Upvotes: 0
Views: 1438
Reputation: 96281
So what you had basically works already, only for just the last .mySlides element, because you selected only the immediately following .prev sibling (using the adjacent sibling combinator +
), instead of all further siblings that have the class, no matter how "far" after the hovered div they come - that's the ~
, the general sibling combinator.
The following basic examle shows that it works in principle (without going through the bother of adding actual images or additional styling.)
.prev {
color:blue;
}
div.mySlides {
margin:.5em 0;
padding: .5em;
border: 1px solid;
cursor: pointer;
}
div.mySlides:hover ~ .prev, .prev:hover {
color:red;
}
<div id="myModal" class="modal">
<div class="modal-content">
<div class="mySlides">
<div class="numbertext">1 / 4</div>
</div>
<div class="mySlides">
<div class="numbertext">2 / 4</div>
</div>
<div class="mySlides">
<div class="numbertext">3 / 4</div>
</div>
<div class="mySlides">
<div class="numbertext">4 / 4</div>
</div>
<a class="prev" onclick="plusSlides(-1)">❮</a>
<a class="next" onclick="plusSlides(1)">❯</a>
</div>
Upvotes: 1
Reputation: 491
Change this
div.mySlides:hover + .prev, .prev:hover {
color:red;
}
to this
div.mySlides:hover ~ .prev, .prev:hover {
color:red;
}
See https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_selectors vs.https://developer.mozilla.org/en-US/docs/Web/CSS/General_sibling_selectors for more info.
Upvotes: 0
Reputation: 7923
button{
padding:30px;
color:white;
background-color: red;
}
img:hover + button{
background-color: black;
}
<img src="https://www.google.ca/logos/doodles/2018/har-gobind-khoranas-96th-birthday-4731112378073088-s.png" width="200" />
<button>my button</button>
Upvotes: 0
Reputation: 477
Try the code below
#a:hover ~ #b {
color: #ccc
}
<div id="a">Div A</div>
<div>elements</div>
<div>elements</div>
<div>elements</div>
<div id="b">Div B</div>
Upvotes: 0