How To Do Multiple Hover On List - html

Alright so i wanted to know how i can select multiple items on hover for example when i hover on list a Highlight a and on list b, give that list box shadow. ive tried to code it but for some reason im not able to do the multiple hover effect.
CSS Code:
#a:hover ~ #b {
background: #ccc
}
HTML Code:
<div><ul id="a"><li>Div A</li></ul></div>
<div>random elements</div>
<div>random elements</div>
<div>random elements</div>
<div><ul id="b"><li>Div B</li></ul></div>

The tilde ~ is for siblings.
But #b is not a sibling for #a
Change the id and it will work : (http://jsbin.com/AxUzOX/1/edit)
<div id="a">
<ul >
<li>Div A</li>
</ul>
</div>
<div>
random elements
</div>
<div>
random elements
</div>
<div>
random elements
</div>
<div id="b">
<ul >
<li>Div B</li>
</ul>
</div>
here is the jquery solution if you want http://jsbin.com/AxUzOX/4/edit

This is the jquery I have used
$('#a').hover(function(){
$('#a').css('background','#ccc');
$('#b').css('background','#ccc');
}, function(){
$('#a').css('background','#fff'); // Background of #a by default
$('#b').css('background','#fff'); // Background of #b by default
})
$('#b').hover(function(){
$('#a').css('background','#ccc');
$('#b').css('background','#ccc');
}, function(){
$('#a').css('background','#fff'); // Background of #a by default
$('#b').css('background','#fff'); // Background of #b by default
})
If you are changing the background of #a or #b,
specify the color to the places I had mentioned too. Simple :)

You should actually mention some class name or id name to the parent div which has the ul #a but as you don't want to assign it, you can use :first-child as shown below:
#a:hover {
background: #ccc
}
div:first-child:hover ~ div>ul#b{
color: red;
}
As you can see, I am hovering on the first child of div instead of ul because hovering on ul is same as hovering on parent div in your case.
I have did this way because there is no parent selector available in css not even in css3/css4.
Working Fiddle
Updated
for your previous sibling selector issue, use jquery
$('#b').hover(function() { $('#a').css('color','blue'); });
The other things you can do using just css as usual :)

If you add a wrapper div, you can accomplish it without any js.
The only issue with your example is the middle <div> that don't have any effects applied, so the jsfiddle solution will look a tad odd, but play with the effects and it might be what you want.
The idea is that you will hover the parent and apply a style to both, then when ALSO hovering over the child, apply different styles to that child.
Also try to not use ids if possible, they restrict you more than you often need.
.parent:hover .list-a, .parent:hover .list-b {
box-shadow: 3px 3px 5px 6px #ccc;
}
.parent .list-a:hover {
box-shadow: none;
background:#aaa;
}
.parent .list-b:hover {
box-shadow: none;
background:#4c4;
}
http://jsfiddle.net/tHTN4/
Now let's re-edit the jQuery part. Since you don't know any jQuery, or the knowledge isn't large. jQuery (a tool on top of Javascript) just helps you manipulate the HTML (DOM) in easy and efficient ways. To help you not edit css in multiple places, use the addClass and removeClass functions:
$(function(){
$('#a').hover(function(){
$('#a').addClass('.list-one-effect');
$('#b').addClass('.list-two-effect');
}, function(){
$('#a').removeClass('.list-one-effect');
$('#b').removeClass('.list-two-effect');
})
});
Once you remove the class, it will automatically revert to the original state (or the last altered state! from other effects). There is also a toggleClass function in jQuery, but I'd recommend you don't use that, as the above will always tell you what state you are in when. For completeness, this is wrapped in $(function(){}), which is one common way to make sure you have loaded jQuery AND the page is ready to run this code.

Related

How to exclusively style parent or child when hovering over either of them?

I have some very simple HTML that looks like
<div id="parent">
Child
</div>
I want to style the parent when hovering over the parent and I want to style the child when hovering over the child. I never want both to be styled at the same time.
I tried various combinations of :hover and :not() selectors in SCSS. Googling didn't bring me far; most solutions I found just tell me how to style the parent when hovering over the child, which is the opposite of what I want.
I found this and this workaround, both from 2013, but I was wondering whether there is a better, more modern way to do this.
If you only intend to support modern, evergreen browsers that support :has, then you can style #parent based on the following conditions:
is hovered
does not have a hovered child
That translates to a selector of such: #parent:hover:not(:has(#child:hover)).
In the example below, the #parent is red only when it is hovered and not its child:
#parent:hover:not(:has(#child:hover)) {
background: red;
}
#child:hover {
background: green;
}
<div id="parent">
Child
</div>

Undo hover effect of parent when hovering over child [duplicate]

I know that there does not exist a CSS parent selector, but is it possible to style a parenting element when hovering a child element without such a selector?
To give an example: consider a delete button that when hovered will highlight the element that is about to become deleted:
<div>
<p>Lorem ipsum ...</p>
<button>Delete</button>
</div>
By means of pure CSS, how to change the background color of this section when the mouse is over the button?
I know it is an old question, but I just managed to do so without a pseudo child (but a pseudo wrapper).
If you set the parent to be with no pointer-events, and then a child div with pointer-events set to auto, it works:)
Note that <img> tag (for example) doesn't do the trick.
Also remember to set pointer-events to auto for other children which have their own event listener, or otherwise they will lose their click functionality.
div.parent {
pointer-events: none;
}
div.child {
pointer-events: auto;
}
div.parent:hover {
background: yellow;
}
<div class="parent">
parent - you can hover over here and it won't trigger
<div class="child">hover over the child instead!</div>
</div>
Edit:
As Shadow Wizard kindly noted: it's worth to mention this won't work for IE10 and below. (Old versions of FF and Chrome too, see here)
Well, this question is asked many times before, and the short typical answer is: It cannot be done by pure CSS. It's in the name: Cascading Style Sheets only supports styling in cascading direction, not up.
But in most circumstances where this effect is wished, like in the given example, there still is the possibility to use these cascading characteristics to reach the desired effect. Consider this pseudo markup:
<parent>
<sibling></sibling>
<child></child>
</parent>
The trick is to give the sibling the same size and position as the parent and to style the sibling instead of the parent. This will look like the parent is styled!
Now, how to style the sibling?
When the child is hovered, the parent is too, but the sibling is not. The same goes for the sibling. This concludes in three possible CSS selector paths for styling the sibling:
parent sibling { }
parent sibling:hover { }
parent:hover sibling { }
These different paths allow for some nice possibilities. For instance, unleashing this trick on the example in the question results in this fiddle:
div {position: relative}
div:hover {background: salmon}
div p:hover {background: white}
div p {padding-bottom: 26px}
div button {position: absolute; bottom: 0}
Obviously, in most cases this trick depends on the use of absolute positioning to give the sibling the same size as the parent, ánd still let the child appear within the parent.
Sometimes it is necessary to use a more qualified selector path in order to select a specific element, as shown in this fiddle which implements the trick multiple times in a tree menu. Quite nice really.
Another, simpler "alternate" approach (to an old question)..
would be to place elements as siblings and use:
Adjacent Sibling Selector (+)
or
General Sibling Selector (~)
<div id="parent">
<!-- control should come before the target... think "cascading" ! -->
<button id="control">Hover Me!</button>
<div id="target">I'm hovered too!</div>
</div>
#parent {
position: relative;
height: 100px;
}
/* Move button control to bottom. */
#control {
position: absolute;
bottom: 0;
}
#control:hover ~ #target {
background: red;
}
Demo Fiddle here.
there is no CSS selector for selecting a parent of a selected child.
you could do it with JavaScript
As mentioned previously "there is no CSS selector for selecting a parent of a selected child".
So you either:
use a CSS hack as described in NGLN's answer
use javascript - along with jQuery most likely
Here is the example for the javascript/jQuery solution
On the javascript side:
$('#my-id-selector-00').on('mouseover', function(){
$(this).parent().addClass('is-hover');
}).on('mouseout', function(){
$(this).parent().removeClass('is-hover');
})
And on the CSS side, you'd have something like this:
.is-hover {
background-color: red;
}
In 2022:
This can be now achieved with CSS only, using the :has pseudo-class and the following expression:
div:has(button:hover) {}
Here's a snippet showcasing the original proposition:
div:has(button:hover) {
background-color: cyan;
}
<div>
<p>Lorem ipsum ...</p>
<button>Delete</button>
</div>
See browser support here. At the time of writing, all major browser support it—except Firefox, which still has a flawed experimental implementation.
This solution depends fully on the design, but if you have a parent div that you want to change the background on when hovering a child you can try to mimic the parent with a ::after / ::before.
<div class="item">
design <span class="icon-cross">x</span>
</div>
CSS:
.item {
background: blue;
border-radius: 10px;
position: relative;
z-index: 1;
}
.item span.icon-cross:hover::after {
background: DodgerBlue;
border-radius: 10px;
display: block;
position: absolute;
z-index: -1;
top: 0;
left: 0;
right: 0;
bottom: 0;
content: "";
}
See a full fiddle example here
This is extremely easy to do in Sass! Don't delve into JavaScript for this. The & selector in sass does exactly this.
http://thesassway.com/intermediate/referencing-parent-selectors-using-ampersand

CSS selector to select the first element after n elements

Is there a way to select the first element with a some class after n elements? For example in the following HTML, I want to be able to select only the first div that has CSS class apple after the 5th div, resulting in the 7th div element being selected.
<div>
<div class="data-class">1</div>
<div class="data-class">2</div>
<div class="data-class">3</div>
<div class="data-class apple">4</div>
<div class="data-class">5</div>
<div class="data-class">6</div>
<div class="data-class apple">7</div>
<div class="data-class apple">8</div>
<div class="data-class">9</div>
<div class="data-class apple">10</div>
</div>
This selector selects all the divs, but I only want the first: .data-class.apple:nth-child(n+5)
And this one doesn't even work: .data-class.apple:nth-child(n+5):first-child
I have put the HTML and CSS samples here.
UPDATE
Current CSS selectors
.data-class{
background-color: #0ea;
padding: 10px;
margin-bottom: 5px;
border: 1px solid #444;
}
.data-class:nth-child(n+5)+.apple{
background-color: #f0f;
}
To select an element appearing after some other element, use the ~ combinator:
.data-class:nth-child(5) ~ .data-class.apple {
background-color: #f0f;
}
You won't be able to match only the first .apple that occurs using just one selector. You will need to create another rule to undo the styles that you apply for subsequent .apple elements:
.data-class:nth-child(5) ~ .data-class.apple ~ .data-class.apple {
background-color: #0ea;
}
This technique is explained here.
it is better to say having css class, not having css.
I couldn't find the appropiate selector strictly.
Instead of this, you could use jquery and write javascript function which
use for loop from 5th child until it finds class containing apple. You may use jquery n-th-child to select child in loop and hasClass to determine if it contains apple.
Therefore select result by passing result to n-th-child function.
It uses this test:
div > .data-class.apple, .data-class{.....}
or another use:
div > .apple:not(.data-class){.....}

Using "div" as a css selector

I have a document structure that is similar across multiple pages.
HTML:
<ul>
<li>
<div>
<img src=""/>
</div>
</li>
</ul>
Currently, there is a border around all img elements. The client would like me to remove the border from around the image, because not all images are the same size and they want a uniform look with the borders. I noticed that there was a div wrapping the images, but the div does not have an id or class. How can I select this div in my css?
Thanks
For instance using
ul>li>div {
border: 1px solid blue;
margin: 5px;
padding: 5px;
}
From my point of view, this is the best way to avoid HTML manipulation.
However, if the structure ul>li>div is repeated elsewhere, this can be ambiguous.
Give it a class or ID... Then make the CSS for it.
If there’s no context anywhere, your recourse is to select it by the structure (as least specific as possible, if you like); for example,
li > div > img
But there usually is some kind of context. If your <li> had a class, for example, you could do:
li.contains-image > div > img
Or just
li.contains-image img
if there’s no other image. Does it or one of its parents have a sibling that identifies it somehow? Use one of the sibling combinators!
li.before-the-one-that-contains-the-image + li img
In case you only have these set or cascade of element in the page you can use
<style>
ul li div {
border: 1px solid red;
}
</style>
Otherwise this will add a border on all element matching on the page.
Best is to use an Id or a class on the element.

How to change one element while hovering over another

I have looked at several other questions but I can't seem to figure any of them out, so here is my problem: I would like to have a div or a span, when you hover over it an area would appear and would be like a drop down.
Such as I have an div, and I want to hover over it and have it show some info about the item I hovered over
<html>
<head>
<title>Question1</title>
<styles type="css/text">
#cheetah {
background-color: red;
color: yellow;
text-align: center;
}
a {
color: blue;
}
#hidden {
background-color: black;
}
a:hover > #hidden {
background-color: orange;
color: orange;
}
</styles>
</head>
<body>
<div id="cheetah">
<p>Cheetah</p>
</div>
<div id="hidden">
<p>A cheetah is a land mammal that can run up 2 60mph!!!</p>
</div>
</body>
</html>
But this ^ doesn't seem to work, I don't know why... and if there is a way to do that in CSS, I would like to know, but I want any and all suggestions.
You can achieve this in CSS only if the hidden div is a child of the element you use for hovering:
http://jsfiddle.net/LgKkU/
You cannot affect a non-child element using :hover from within CSS2, which is supported by all common browsers.
You can affect a sibling element using CSS2.1 selectors, like so:
a:hover + .sibling { ... }
However, this only works for direct siblings. This means you could have HTML like this:
<p>Cheetah <span class="sibling">Blah Blah Blah</span></p>
Notice that the a and the span are direct siblings.
Here's a fiddle showing the siblings working: http://jsfiddle.net/vUUxp/
However, not all browsers support the CSS2.1 sibling selectors, so you need to decide based on your target audience if you can use this or not.
Edit: Corrected my mistake on the CSS version for the + selector: it's 2.1 that defines it, not CSS3. I also added a link showing browser support. Otherwise, the answer is the same.
Or, if you're open to it, use jQuery.
Something like this would work:
$("#element") // select your element (supports CSS selectors)
.hover(function(){ // trigger the mouseover event
$("#otherElement") // select the element to show (can be anywhere)
.show(); // show the element
}, function(){ // trigger the mouseout event
$("#otherElement") // select the same element
.hide(); // hide it
});
And remember to wrap this in a DOM ready function ($(function(){...}); or $(document).ready(function(){...});).
You can absolutely do this in CSS3 now using the ~ adjacent sibling selector.
triggerSelector:hover ~ targetSelector {
display: block;
}
For example, if you want a tooltip to appear when hovering over an adjacent button:
.button:hover ~ .tooltip {
display: block;
}