How to affect other elements when one element is hovered - html

What I want to do is when a certain div is hovered, it'd affect the properties of another div.
For example, in this JSFiddle demo, when you hover over #cube it changes the background-color but what I want is that when I hover over #container, #cubeis affected.
div {
outline: 1px solid red;
}
#container {
width: 200px;
height: 30px;
}
#cube {
width: 30px;
height: 100%;
background-color: red;
}
#cube:hover {
width: 30px;
height: 100%;
background-color: blue;
}
<div id="container">
<div id="cube">
</div>
</div>

If the cube is directly inside the container:
#container:hover > #cube { background-color: yellow; }
If cube is next to (after containers closing tag) the container:
#container:hover + #cube { background-color: yellow; }
If the cube is somewhere inside the container:
#container:hover #cube { background-color: yellow; }
If the cube is a sibling of the container:
#container:hover ~ #cube { background-color: yellow; }

In this particular example, you can use:
#container:hover #cube {
background-color: yellow;
}
This example only works since cube is a child of container. For more complicated scenarios, you'd need to use different CSS, or use JavaScript.

Using the sibling selector is the general solution for styling other elements when hovering over a given one, but it works only if the other elements follow the given one in the DOM. What can we do when the other elements should actually be before the hovered one? Say we want to implement a signal bar rating widget like the one below:
This can actually be done easily using the CSS flexbox model, by setting flex-direction to reverse, so that the elements are displayed in the opposite order from the one they're in the DOM. The screenshot above is from such a widget, implemented with pure CSS.
Flexbox is very well supported by 95% of modern browsers.
.rating {
display: flex;
flex-direction: row-reverse;
width: 9rem;
}
.rating div {
flex: 1;
align-self: flex-end;
background-color: black;
border: 0.1rem solid white;
}
.rating div:hover {
background-color: lightblue;
}
.rating div[data-rating="1"] {
height: 5rem;
}
.rating div[data-rating="2"] {
height: 4rem;
}
.rating div[data-rating="3"] {
height: 3rem;
}
.rating div[data-rating="4"] {
height: 2rem;
}
.rating div[data-rating="5"] {
height: 1rem;
}
.rating div:hover ~ div {
background-color: lightblue;
}
<div class="rating">
<div data-rating="1"></div>
<div data-rating="2"></div>
<div data-rating="3"></div>
<div data-rating="4"></div>
<div data-rating="5"></div>
</div>

Only this worked for me:
#container:hover .cube { background-color: yellow; }
Where .cube is CssClass somewhere inside of the #container.
Tested in Firefox, Chrome and Edge.

Here is another idea that allow you to affect other elements without considering any specific selector and by only using the :hover state of the main element.
For this, I will rely on the use of custom properties (CSS variables). As we can read in the specification:
Custom properties are ordinary properties, so they can be declared on
any element, are resolved with the normal inheritance and cascade
rules ...
The idea is to define custom properties within the main element and use them to style child elements and since these properties are inherited we simply need to change them within the main element on hover.
Here is an example:
#container {
width: 200px;
height: 30px;
border: 1px solid var(--c);
--c:red;
}
#container:hover {
--c:blue;
}
#container > div {
width: 30px;
height: 100%;
background-color: var(--c);
}
<div id="container">
<div>
</div>
</div>
Why this can be better than using specific selector combined with hover?
I can provide at least 2 reasons that make this method a good one to consider:
If we have many nested elements that share the same styles, this will avoid us complex selector to target all of them on hover. Using Custom properties, we simply change the value when hovering on the parent element.
A custom property can be used to replace a value of any property and also a partial value of it. For example we can define a custom property for a color and we use it within a border, linear-gradient, background-color, box-shadow etc. This will avoid us reseting all these properties on hover.
Here is a more complex example:
.container {
--c:red;
width:400px;
display:flex;
border:1px solid var(--c);
justify-content:space-between;
padding:5px;
background:linear-gradient(var(--c),var(--c)) 0 50%/100% 3px no-repeat;
}
.box {
width:30%;
background:var(--c);
box-shadow:0px 0px 5px var(--c);
position:relative;
}
.box:before {
content:"A";
display:block;
width:15px;
margin:0 auto;
height:100%;
color:var(--c);
background:#fff;
}
/*Hover*/
.container:hover {
--c:blue;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
</div>
As we can see above, we only need one CSS declaration in order to change many properties of different elements.

Big thanks to Mike and Robertc for their helpful posts!
If you have two elements in your HTML and you want to :hover over one and target a style change in the other the two elements must be directly related--parents, children or siblings. This means that the two elements either must be one inside the other or must both be contained within the same larger element.
I wanted to display definitions in a box on the right side of the browser as my users read through my site and :hover over highlighted terms; therefore, I did not want the 'definition' element to be displayed inside the 'text' element.
I almost gave up and just added javascript to my page, but this is the future dang it! We should not have to put up with back sass from CSS and HTML telling us where we have to place our elements to achieve the effects we want! In the end we compromised.
While the actual HTML elements in the file must be either nested or contained in a single element to be valid :hover targets to each other, the css position attribute can be used to display any element where ever you want. I used position:fixed to place the target of my :hover action where I wanted it on the user's screen regardless to its location in the HTML document.
The html:
<div id="explainBox" class="explainBox"> /*Common parent*/
<a class="defP" id="light" href="http://en.wikipedia.or/wiki/Light">Light /*highlighted term in text*/
</a> is as ubiquitous as it is mysterious. /*plain text*/
<div id="definitions"> /*Container for :hover-displayed definitions*/
<p class="def" id="light"> /*example definition entry*/ Light:
<br/>Short Answer: The type of energy you see
</p>
</div>
</div>
The css:
/*read: "when user hovers over #light somewhere inside #explainBox
set display to inline-block for #light directly inside of #definitions.*/
#explainBox #light:hover~#definitions>#light {
display: inline-block;
}
.def {
display: none;
}
#definitions {
background-color: black;
position: fixed;
/*position attribute*/
top: 5em;
/*position attribute*/
right: 2em;
/*position attribute*/
width: 20em;
height: 30em;
border: 1px solid orange;
border-radius: 12px;
padding: 10px;
}
In this example the target of a :hover command from an element within #explainBox must either be #explainBox or also within #explainBox. The position attributes assigned to #definitions force it to appear in the desired location (outside #explainBox) even though it is technically located in an unwanted position within the HTML document.
I understand it is considered bad form to use the same #id for more than one HTML element; however, in this case the instances of #light can be described independently due to their respective positions in uniquely #id'd elements. Is there any reason not to repeat the id #light in this case?

WARNING: If you are able to use CSS: Use CSS without Javascript!
If you want to select an element with an unknown / or not reachable position you could use JavaScript.
const cube = document.getElementById('cube')
const container = document.getElementById('container')
cube.addEventListener('mouseover', () => container.style.backgroundColor = "blue")
cube.addEventListener('mouseleave', () => container.style.backgroundColor = "white")
div {
outline: 1px solid red;
width: 200px;
height: 30px;
}
#cube {
background-color: red;
}
<main>
<div id="container">
</div>
</main>
<div id="cube">
</div>

In addition to the common selectors already provided in the other answers, you can now rely on :has() in the situations where we don't have a parent/child or sibling relation.
Here is an example:
.box {
width: 200px;
aspect-ratio: 1;
display: inline-block;
border: 1px solid red;
}
.box > div {
margin: 5px;
height: 100%;
border: 2px solid green;
cursor: pointer;
}
/* when div1 is hovered make div2 blue */
body:has(#div1:hover) #div2 {
background: blue;
}
/* when div2 is hovered make div1 purple */
body:has(#div2:hover) #div1 {
background: purple;
}
<div class="box">
<div id="div1">
</div>
</div>
<div class="box">
<div id="div2">
</div>
</div>
As you can see, we can affect div1 by hovering div2 and vice versa.

#imageDiv:hover #detailDiv
{
z-index: -999 !important;
}
here
#imageDiv is first div that to be hover and
#detailDiv is second div on that css will apply on hover
so if i hover on first div then zindex will assign to second div
Works for me

Related

how to change different children at the same time of same parent? [duplicate]

What I want to do is when a certain div is hovered, it'd affect the properties of another div.
For example, in this JSFiddle demo, when you hover over #cube it changes the background-color but what I want is that when I hover over #container, #cubeis affected.
div {
outline: 1px solid red;
}
#container {
width: 200px;
height: 30px;
}
#cube {
width: 30px;
height: 100%;
background-color: red;
}
#cube:hover {
width: 30px;
height: 100%;
background-color: blue;
}
<div id="container">
<div id="cube">
</div>
</div>
If the cube is directly inside the container:
#container:hover > #cube { background-color: yellow; }
If cube is next to (after containers closing tag) the container:
#container:hover + #cube { background-color: yellow; }
If the cube is somewhere inside the container:
#container:hover #cube { background-color: yellow; }
If the cube is a sibling of the container:
#container:hover ~ #cube { background-color: yellow; }
In this particular example, you can use:
#container:hover #cube {
background-color: yellow;
}
This example only works since cube is a child of container. For more complicated scenarios, you'd need to use different CSS, or use JavaScript.
Using the sibling selector is the general solution for styling other elements when hovering over a given one, but it works only if the other elements follow the given one in the DOM. What can we do when the other elements should actually be before the hovered one? Say we want to implement a signal bar rating widget like the one below:
This can actually be done easily using the CSS flexbox model, by setting flex-direction to reverse, so that the elements are displayed in the opposite order from the one they're in the DOM. The screenshot above is from such a widget, implemented with pure CSS.
Flexbox is very well supported by 95% of modern browsers.
.rating {
display: flex;
flex-direction: row-reverse;
width: 9rem;
}
.rating div {
flex: 1;
align-self: flex-end;
background-color: black;
border: 0.1rem solid white;
}
.rating div:hover {
background-color: lightblue;
}
.rating div[data-rating="1"] {
height: 5rem;
}
.rating div[data-rating="2"] {
height: 4rem;
}
.rating div[data-rating="3"] {
height: 3rem;
}
.rating div[data-rating="4"] {
height: 2rem;
}
.rating div[data-rating="5"] {
height: 1rem;
}
.rating div:hover ~ div {
background-color: lightblue;
}
<div class="rating">
<div data-rating="1"></div>
<div data-rating="2"></div>
<div data-rating="3"></div>
<div data-rating="4"></div>
<div data-rating="5"></div>
</div>
Only this worked for me:
#container:hover .cube { background-color: yellow; }
Where .cube is CssClass somewhere inside of the #container.
Tested in Firefox, Chrome and Edge.
Here is another idea that allow you to affect other elements without considering any specific selector and by only using the :hover state of the main element.
For this, I will rely on the use of custom properties (CSS variables). As we can read in the specification:
Custom properties are ordinary properties, so they can be declared on
any element, are resolved with the normal inheritance and cascade
rules ...
The idea is to define custom properties within the main element and use them to style child elements and since these properties are inherited we simply need to change them within the main element on hover.
Here is an example:
#container {
width: 200px;
height: 30px;
border: 1px solid var(--c);
--c:red;
}
#container:hover {
--c:blue;
}
#container > div {
width: 30px;
height: 100%;
background-color: var(--c);
}
<div id="container">
<div>
</div>
</div>
Why this can be better than using specific selector combined with hover?
I can provide at least 2 reasons that make this method a good one to consider:
If we have many nested elements that share the same styles, this will avoid us complex selector to target all of them on hover. Using Custom properties, we simply change the value when hovering on the parent element.
A custom property can be used to replace a value of any property and also a partial value of it. For example we can define a custom property for a color and we use it within a border, linear-gradient, background-color, box-shadow etc. This will avoid us reseting all these properties on hover.
Here is a more complex example:
.container {
--c:red;
width:400px;
display:flex;
border:1px solid var(--c);
justify-content:space-between;
padding:5px;
background:linear-gradient(var(--c),var(--c)) 0 50%/100% 3px no-repeat;
}
.box {
width:30%;
background:var(--c);
box-shadow:0px 0px 5px var(--c);
position:relative;
}
.box:before {
content:"A";
display:block;
width:15px;
margin:0 auto;
height:100%;
color:var(--c);
background:#fff;
}
/*Hover*/
.container:hover {
--c:blue;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
</div>
As we can see above, we only need one CSS declaration in order to change many properties of different elements.
Big thanks to Mike and Robertc for their helpful posts!
If you have two elements in your HTML and you want to :hover over one and target a style change in the other the two elements must be directly related--parents, children or siblings. This means that the two elements either must be one inside the other or must both be contained within the same larger element.
I wanted to display definitions in a box on the right side of the browser as my users read through my site and :hover over highlighted terms; therefore, I did not want the 'definition' element to be displayed inside the 'text' element.
I almost gave up and just added javascript to my page, but this is the future dang it! We should not have to put up with back sass from CSS and HTML telling us where we have to place our elements to achieve the effects we want! In the end we compromised.
While the actual HTML elements in the file must be either nested or contained in a single element to be valid :hover targets to each other, the css position attribute can be used to display any element where ever you want. I used position:fixed to place the target of my :hover action where I wanted it on the user's screen regardless to its location in the HTML document.
The html:
<div id="explainBox" class="explainBox"> /*Common parent*/
<a class="defP" id="light" href="http://en.wikipedia.or/wiki/Light">Light /*highlighted term in text*/
</a> is as ubiquitous as it is mysterious. /*plain text*/
<div id="definitions"> /*Container for :hover-displayed definitions*/
<p class="def" id="light"> /*example definition entry*/ Light:
<br/>Short Answer: The type of energy you see
</p>
</div>
</div>
The css:
/*read: "when user hovers over #light somewhere inside #explainBox
set display to inline-block for #light directly inside of #definitions.*/
#explainBox #light:hover~#definitions>#light {
display: inline-block;
}
.def {
display: none;
}
#definitions {
background-color: black;
position: fixed;
/*position attribute*/
top: 5em;
/*position attribute*/
right: 2em;
/*position attribute*/
width: 20em;
height: 30em;
border: 1px solid orange;
border-radius: 12px;
padding: 10px;
}
In this example the target of a :hover command from an element within #explainBox must either be #explainBox or also within #explainBox. The position attributes assigned to #definitions force it to appear in the desired location (outside #explainBox) even though it is technically located in an unwanted position within the HTML document.
I understand it is considered bad form to use the same #id for more than one HTML element; however, in this case the instances of #light can be described independently due to their respective positions in uniquely #id'd elements. Is there any reason not to repeat the id #light in this case?
WARNING: If you are able to use CSS: Use CSS without Javascript!
If you want to select an element with an unknown / or not reachable position you could use JavaScript.
const cube = document.getElementById('cube')
const container = document.getElementById('container')
cube.addEventListener('mouseover', () => container.style.backgroundColor = "blue")
cube.addEventListener('mouseleave', () => container.style.backgroundColor = "white")
div {
outline: 1px solid red;
width: 200px;
height: 30px;
}
#cube {
background-color: red;
}
<main>
<div id="container">
</div>
</main>
<div id="cube">
</div>
In addition to the common selectors already provided in the other answers, you can now rely on :has() in the situations where we don't have a parent/child or sibling relation.
Here is an example:
.box {
width: 200px;
aspect-ratio: 1;
display: inline-block;
border: 1px solid red;
}
.box > div {
margin: 5px;
height: 100%;
border: 2px solid green;
cursor: pointer;
}
/* when div1 is hovered make div2 blue */
body:has(#div1:hover) #div2 {
background: blue;
}
/* when div2 is hovered make div1 purple */
body:has(#div2:hover) #div1 {
background: purple;
}
<div class="box">
<div id="div1">
</div>
</div>
<div class="box">
<div id="div2">
</div>
</div>
As you can see, we can affect div1 by hovering div2 and vice versa.
#imageDiv:hover #detailDiv
{
z-index: -999 !important;
}
here
#imageDiv is first div that to be hover and
#detailDiv is second div on that css will apply on hover
so if i hover on first div then zindex will assign to second div
Works for me

How can I affect one div's css if it is not child of the one I hover on? [duplicate]

What I want to do is when a certain div is hovered, it'd affect the properties of another div.
For example, in this JSFiddle demo, when you hover over #cube it changes the background-color but what I want is that when I hover over #container, #cubeis affected.
div {
outline: 1px solid red;
}
#container {
width: 200px;
height: 30px;
}
#cube {
width: 30px;
height: 100%;
background-color: red;
}
#cube:hover {
width: 30px;
height: 100%;
background-color: blue;
}
<div id="container">
<div id="cube">
</div>
</div>
If the cube is directly inside the container:
#container:hover > #cube { background-color: yellow; }
If cube is next to (after containers closing tag) the container:
#container:hover + #cube { background-color: yellow; }
If the cube is somewhere inside the container:
#container:hover #cube { background-color: yellow; }
If the cube is a sibling of the container:
#container:hover ~ #cube { background-color: yellow; }
In this particular example, you can use:
#container:hover #cube {
background-color: yellow;
}
This example only works since cube is a child of container. For more complicated scenarios, you'd need to use different CSS, or use JavaScript.
Using the sibling selector is the general solution for styling other elements when hovering over a given one, but it works only if the other elements follow the given one in the DOM. What can we do when the other elements should actually be before the hovered one? Say we want to implement a signal bar rating widget like the one below:
This can actually be done easily using the CSS flexbox model, by setting flex-direction to reverse, so that the elements are displayed in the opposite order from the one they're in the DOM. The screenshot above is from such a widget, implemented with pure CSS.
Flexbox is very well supported by 95% of modern browsers.
.rating {
display: flex;
flex-direction: row-reverse;
width: 9rem;
}
.rating div {
flex: 1;
align-self: flex-end;
background-color: black;
border: 0.1rem solid white;
}
.rating div:hover {
background-color: lightblue;
}
.rating div[data-rating="1"] {
height: 5rem;
}
.rating div[data-rating="2"] {
height: 4rem;
}
.rating div[data-rating="3"] {
height: 3rem;
}
.rating div[data-rating="4"] {
height: 2rem;
}
.rating div[data-rating="5"] {
height: 1rem;
}
.rating div:hover ~ div {
background-color: lightblue;
}
<div class="rating">
<div data-rating="1"></div>
<div data-rating="2"></div>
<div data-rating="3"></div>
<div data-rating="4"></div>
<div data-rating="5"></div>
</div>
Only this worked for me:
#container:hover .cube { background-color: yellow; }
Where .cube is CssClass somewhere inside of the #container.
Tested in Firefox, Chrome and Edge.
Here is another idea that allow you to affect other elements without considering any specific selector and by only using the :hover state of the main element.
For this, I will rely on the use of custom properties (CSS variables). As we can read in the specification:
Custom properties are ordinary properties, so they can be declared on
any element, are resolved with the normal inheritance and cascade
rules ...
The idea is to define custom properties within the main element and use them to style child elements and since these properties are inherited we simply need to change them within the main element on hover.
Here is an example:
#container {
width: 200px;
height: 30px;
border: 1px solid var(--c);
--c:red;
}
#container:hover {
--c:blue;
}
#container > div {
width: 30px;
height: 100%;
background-color: var(--c);
}
<div id="container">
<div>
</div>
</div>
Why this can be better than using specific selector combined with hover?
I can provide at least 2 reasons that make this method a good one to consider:
If we have many nested elements that share the same styles, this will avoid us complex selector to target all of them on hover. Using Custom properties, we simply change the value when hovering on the parent element.
A custom property can be used to replace a value of any property and also a partial value of it. For example we can define a custom property for a color and we use it within a border, linear-gradient, background-color, box-shadow etc. This will avoid us reseting all these properties on hover.
Here is a more complex example:
.container {
--c:red;
width:400px;
display:flex;
border:1px solid var(--c);
justify-content:space-between;
padding:5px;
background:linear-gradient(var(--c),var(--c)) 0 50%/100% 3px no-repeat;
}
.box {
width:30%;
background:var(--c);
box-shadow:0px 0px 5px var(--c);
position:relative;
}
.box:before {
content:"A";
display:block;
width:15px;
margin:0 auto;
height:100%;
color:var(--c);
background:#fff;
}
/*Hover*/
.container:hover {
--c:blue;
}
<div class="container">
<div class="box"></div>
<div class="box"></div>
</div>
As we can see above, we only need one CSS declaration in order to change many properties of different elements.
Big thanks to Mike and Robertc for their helpful posts!
If you have two elements in your HTML and you want to :hover over one and target a style change in the other the two elements must be directly related--parents, children or siblings. This means that the two elements either must be one inside the other or must both be contained within the same larger element.
I wanted to display definitions in a box on the right side of the browser as my users read through my site and :hover over highlighted terms; therefore, I did not want the 'definition' element to be displayed inside the 'text' element.
I almost gave up and just added javascript to my page, but this is the future dang it! We should not have to put up with back sass from CSS and HTML telling us where we have to place our elements to achieve the effects we want! In the end we compromised.
While the actual HTML elements in the file must be either nested or contained in a single element to be valid :hover targets to each other, the css position attribute can be used to display any element where ever you want. I used position:fixed to place the target of my :hover action where I wanted it on the user's screen regardless to its location in the HTML document.
The html:
<div id="explainBox" class="explainBox"> /*Common parent*/
<a class="defP" id="light" href="http://en.wikipedia.or/wiki/Light">Light /*highlighted term in text*/
</a> is as ubiquitous as it is mysterious. /*plain text*/
<div id="definitions"> /*Container for :hover-displayed definitions*/
<p class="def" id="light"> /*example definition entry*/ Light:
<br/>Short Answer: The type of energy you see
</p>
</div>
</div>
The css:
/*read: "when user hovers over #light somewhere inside #explainBox
set display to inline-block for #light directly inside of #definitions.*/
#explainBox #light:hover~#definitions>#light {
display: inline-block;
}
.def {
display: none;
}
#definitions {
background-color: black;
position: fixed;
/*position attribute*/
top: 5em;
/*position attribute*/
right: 2em;
/*position attribute*/
width: 20em;
height: 30em;
border: 1px solid orange;
border-radius: 12px;
padding: 10px;
}
In this example the target of a :hover command from an element within #explainBox must either be #explainBox or also within #explainBox. The position attributes assigned to #definitions force it to appear in the desired location (outside #explainBox) even though it is technically located in an unwanted position within the HTML document.
I understand it is considered bad form to use the same #id for more than one HTML element; however, in this case the instances of #light can be described independently due to their respective positions in uniquely #id'd elements. Is there any reason not to repeat the id #light in this case?
WARNING: If you are able to use CSS: Use CSS without Javascript!
If you want to select an element with an unknown / or not reachable position you could use JavaScript.
const cube = document.getElementById('cube')
const container = document.getElementById('container')
cube.addEventListener('mouseover', () => container.style.backgroundColor = "blue")
cube.addEventListener('mouseleave', () => container.style.backgroundColor = "white")
div {
outline: 1px solid red;
width: 200px;
height: 30px;
}
#cube {
background-color: red;
}
<main>
<div id="container">
</div>
</main>
<div id="cube">
</div>
In addition to the common selectors already provided in the other answers, you can now rely on :has() in the situations where we don't have a parent/child or sibling relation.
Here is an example:
.box {
width: 200px;
aspect-ratio: 1;
display: inline-block;
border: 1px solid red;
}
.box > div {
margin: 5px;
height: 100%;
border: 2px solid green;
cursor: pointer;
}
/* when div1 is hovered make div2 blue */
body:has(#div1:hover) #div2 {
background: blue;
}
/* when div2 is hovered make div1 purple */
body:has(#div2:hover) #div1 {
background: purple;
}
<div class="box">
<div id="div1">
</div>
</div>
<div class="box">
<div id="div2">
</div>
</div>
As you can see, we can affect div1 by hovering div2 and vice versa.
#imageDiv:hover #detailDiv
{
z-index: -999 !important;
}
here
#imageDiv is first div that to be hover and
#detailDiv is second div on that css will apply on hover
so if i hover on first div then zindex will assign to second div
Works for me

Can Parent Tag styling taking precedence on similar styling at child tag on hover

I am facing some styling problems. How to apply the parent tag styling on the child tag (which as its own) on hover?
Taking a simple example to focus on the problem only.
.outer:hover {
background: yellow;
z-index: 1;
position: relative;
}
.inner {
background: red;
position: relative;
}
<div class="outer">
<div class="inner">Something</div>
</div>
Expected output;
Normally the background is red (from the inner class) but on hover I need to override it with the styling of the parent tag (outer class, backgroud: yellow).
The outer and inner class have to be in those tags only.
http://jsfiddle.net/P7c9q/2209/
Use CSS variable:
.outer:hover {
background: yellow;
--c:transparent;
}
.inner {
background: var(--c,red);
}
<div class="outer">
<div class="inner">Something</div>
</div>
Or use inherit:
.outer:hover {
background: yellow;
}
.outer:hover .inner {
background:inherit;
}
.inner {
background: red;
}
<div class="outer">
<div class="inner">Something</div>
</div>
Use :not selector to override with parent style when :hover .inner:not(:hover)
When it's not hovered over it will use it's own child style, when hoverd over - parent

Show popup when hover over a cell - without javascript

I want to show the popup when I hover over a cell
.cell:hover + .popup {
display: block;
}
But this is not working. It is only working when I hover over the hole table. Can you explain me why? This is strange for me because both are classes. I do not want to use javascript.
.table{
margin:auto;
display:table;
width:800px;
background-color:lightblue;
border-collapse:collapse;
}
.row
{
display:table-row;
}
.cell
{
display:table-cell;
height: 20px;
padding: 4px;
border:1px solid black;
}
.cell:hover {
background-color: #ff0000;
}
.popup {
display: none;
}
.table:hover + .popup {
display: block;
}
<div class="table">
<div class="row">
<div class="cell">1</div>
<div class="cell">2</div>
<div class="cell">3</div>
</div>
</div>
<div class="popup">Stuff shown on hover</div>
You're using the adjacent sibling selector, +. Since .popup is not a sibling of any .cell, nothing will happen.
Now, your other rule, .table:hover + .popup works because the popup element is a sibling of your table. If you want the cell hover to work without changing your HTML, you'll have to use JavaScript - there are no parent selectors in CSS.
#chazsolo is right about the sibling selector. However, you can still do it without JavaScript by moving the .popup class div inside the .row class div so that it IS a sibling of the .cell elements, then use CSS to position the popup wherever you want it on the screen. For example, something like:
.popup {
display: none;
position: fixed;
top: 30%;
}
As #chazsolo suggested, you would also need to switch from using the + sibling selector to using the ~ sibling selector, so the popup can be activated by hovering over any of the .cell elements.

Move one element before another with css/html

Is it possible to "move" one full width element that is below another one so that it appears to be above by only using CSS/HTML? (and not changing the markup order)
<div id="first">first</div>
<div id="second">second</div>
#first {…}
#second {…}
Desirable result:
second
first
You can use CSS Flexible Boxes for this. Specifically the order property.
In this example I've added width and background-color to help visualize. Please note that browser support for CSS flexible boxes is limited to modern browsers such as IE 10+, Chrome 21+, Firefox 20+ and may not work well in mobile browsers (especially older ones).
.container {
display: flex;
flex-direction: column;
}
#first {
order: 2;
width: 10em;
margin-top: 1em;
background-color: orange;
}
#second {
order: 1;
width: 10em;
background-color: yellow;
}
<div class='container'>
<div id=first>
first
</div>
<div id=second>
second
</div>
</div>
Only if you know exact height of second element:
#first {
margin-top: 20px;
}
#second {
top: 0;
position: absolute;
}
http://jsfiddle.net/9zmcnedy/
Js Fiddle
something like this
#first { position:relative; top:18px}
#second {position:relative; bottom:18px}
There is a work-around by using position: absolute but this has a drawback that the parent element will not stretch the height as the child container's (first element) height increase. (To fix that you should use javascript)
CSS
.container {
border: 1px solid green;
display: inline-block;
position: relative;
}
#first {
position: absolute;
top: 100%;
}
Working Fiddle