I am trying to get a child element to change when the parent is hovered. I also want an attribute of that parent to change as well. I am trying to get the background color of #action to change and the color of the a or h1 to change when action is hovered over. Is this possible?
here is the html
<section id="action" class="general">
<div class="container">
<h1>This text</h1>
</div>
</section>
and here is the css. CSS is built using SASS that is why it is structured like that.
#action {
background-color: $bgLight;
border-top: 1px solid #252525;
-webkit-transition: all 0.2s ease-out;
-moz-transition: all 0.2s ease-out;
-ms-transition: all 0.2s ease-out;
-o-transition: all 0.2s ease-out;
transition: all 0.2s ease-out;
a {
text-decoration: none;
h1 {
margin: 0;
color: $colorLight;
font-weight: 300;
text-align: center;
}
}
}
#action:hover a {
background-color: #76A7D1;
color: $colorDark;
}
Try this:
#action {
background-color: $bgLight;
border-top: 1px solid #252525;
-webkit-transition: all 0.2s ease-out;
-moz-transition: all 0.2s ease-out;
-ms-transition: all 0.2s ease-out;
-o-transition: all 0.2s ease-out;
transition: all 0.2s ease-out;
a {
text-decoration: none;
h1 {
margin: 0;
color: $colorLight;
font-weight: 300;
text-align: center;
}
}
}
#action:hover{
background-color: #76A7D1;
a{
h1{
color: $colorDark;
}
}
}
You can do the same as #Alessandro Minoccheri suggested but in a less verbose way which I like particularly:
#action {
background-color: $bgLight;
border-top: 1px solid #252525;
-webkit-transition: all 0.2s ease-out;
-moz-transition: all 0.2s ease-out;
-ms-transition: all 0.2s ease-out;
-o-transition: all 0.2s ease-out;
transition: all 0.2s ease-out;
a {
text-decoration: none;
h1 {
margin: 0;
color: $colorLight;
font-weight: 300;
text-align: center;
}
}
&:hover{
background-color: #76A7D1;
a{
h1{
color: $colorDark;
}
}
}
}
The & within #action refers to the parent element, in other words to #action itself.
I like this approach because everything gets self contained within one style declaration and is less repetitive.
It is like saying: "... and when this element is hovered apply these styles to it, and these styles to a and h1".
One small comment regarding your markup #zachstames: a (anchor element) is an inline content element, while h1 (header of level 1) is a block element. According to the W3C specifications an inline element should not contain block elements but only data.
Hope it helps.
Cheers!
This is what you want? DEMO
You are able to make this:
#action:hover {
background: yellow;
}
#action:hover a {
background-color: #76A7D1;
color: white;
}
As you can see I repeat the use of the pseudo-class #action:hover. I'm saying:
"When action is hover, change it's backgroud AND when action is hover, change the background and the font color of the a element".
Hope I've helped.
Be good,
Leonardo
Related
So, I'm trying to make this so when I hover over the image, the div appears. Unfortunately, I am not able to get it to do so, and I'm at a loss as to why it's not working.
Here is my code:
.summary:hover {
opacity: .6;
-moz-transition: all .1s ease-in-out;
-o-transition: all .1s ease-in-out;
-webkit-transition: all .1s ease-in-out;
transition: all .1s ease-in-out;
}
.summaryOverlay {
position: absolute;
margin-left: 5%;
margin-top: 3%;
height: 200px;
width: 280px;
z-index: 2;
background-color: #E7DFDD;
color: #0E0B16;
font-family: Verdana;
font-size: 15px;
opacity: .9;
-moz-transition: all .1s ease-in-out;
-o-transition: all .1s ease-in-out;
-webkit-transition: all .1s ease-in-out;
transition: all .1s ease-in-out;
text-align: center;
display: none;
}
.summary:hover .summaryOverlay {
display: block;
}
<div class="leftAlign" id="lastWish">
<img class="summary" alt="the last wish" src="lastWish.jpg" style="width: inherit; height: inherit;" />
</div>
<div class="summaryOverlay">
Geralt is a witcher.
<br><br> Yet he is no ordinary killer-for-hire.
<br><br> His sole purpose: to destroy the monsters that plague the world.
<br><br> But not everything monstrous-looking is evil and not everything fair is good. . . and in every fairy tale there is a grain of truth.
</div>
Because .summaryOverlay isn't child of .summary, as you stated here:
.summary:hover .summaryOverlay {
display: block;
}
So you need to hover in .leftAlign (.summary's parent) which is the .summaryOverlay sibling, you need for that to use the adjacent sibling selector +
Note: remove the inline styles, they are a bad practice to use them
.summary:hover {
opacity: .6;
-moz-transition: all .1s ease-in-out;
-o-transition: all .1s ease-in-out;
-webkit-transition: all .1s ease-in-out;
transition: all .1s ease-in-out;
}
.summaryOverlay {
position: absolute;
margin-left: 5%;
margin-top: 3%;
height: 200px;
width: 280px;
z-index: 2;
background-color: #E7DFDD;
color: #0E0B16;
font-family: Verdana;
font-size: 15px;
opacity: .9;
-moz-transition: all .1s ease-in-out;
-o-transition: all .1s ease-in-out;
-webkit-transition: all .1s ease-in-out;
transition: all .1s ease-in-out;
text-align: center;
display: none;
}
.leftAlign:hover + .summaryOverlay {
display: block;
}
<div class="leftAlign" id="lastWish">
<img class="summary" alt="the last wish" src="//placehold.it/100x100" />
</div>
<div class="summaryOverlay">
Geralt is a witcher.
<br><br> Yet he is no ordinary killer-for-hire.
<br><br> His sole purpose: to destroy the monsters that plague the world.
<br><br> But not everything monstrous-looking is evil and not everything fair is good. . . and in every fairy tale there is a grain of truth.
</div>
Your CSS is not asking the browser to display the div on hovering over the image as you intended. Rather, it is asking the browser to display a child of .summary which has a class summaryOverlay. img can't ever have a child element, can it? Even if it could, .summaryOverlay is not its child. So, it won't work as you intended. Try this:
#lastWish:hover + .summaryOverlay{
display:block;
}
Barring any CSS specificity issues, it should give you what want. Let me know how it goes.
I want the class to appear a different color after selected. Why does it just go back to the non active color? what can I do so that it stays focused on my class?
I tried many things on this forum but nothing seems to work. I need it for school really quickly.
.Auswahl {
outline: none;
background-color: #F6F6F6;
border: none;
margin-right: -4px;
float: left;
-webkit-transition: background-color 0.2s linear;
-moz-transition: background-color 0.2s linear;
-o-transition: background-color 0.2s linear;
transition: background-color 0.2s linear;
}
.Auswahl:hover {
background-color: #FF0004;
}
.Auswahl:active {
background-color: #00FF2B;
}
.Auswahl:focus {
background-color: #7100FF;
}
<div class="Auswahl">
<h1>Sparkasse</h1>
<br>Vorteile
<br>Nachteile
</div>
The :focus pseudo-class applies while an element has the focus
(accepts keyboard events or other forms of text input).
More info here
Change your HTML like this:
.Auswahl {
outline: none;
background-color: #F6F6F6;
border: none;
margin-right: -4px;
float: left;
-webkit-transition: background-color 0.2s linear;
-moz-transition: background-color 0.2s linear;
-o-transition: background-color 0.2s linear;
transition: background-color 0.2s linear;
}
.Auswahl:hover {
background-color: #FF0004;
}
.Auswahl:focus {
background-color: #7100FF;
}
.Auswahl:active {
background-color: #00FF2B;
}
<button class="Auswahl">
<h1>Sparkasse</h1>
<br>Vorteile
<br>Nachteile
</button>
Try this. It's got a very small amount of jQuery to add a new class on click.
HTML
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<div class="Auswahl">
<h1>Sparkasse</h1>
<br>Vorteile
<br>Nachteile
</div>
CSS
.Auswahl {
outline: none;
background-color: #F6F6F6;
border: none;
margin-right: -4px;
float: left;
-webkit-transition: background-color 0.2s linear;
-moz-transition: background-color 0.2s linear;
-o-transition: background-color 0.2s linear;
transition: background-color 0.2s linear;
}
.Auswahl:hover {
background-color: #FF0004;
}
.Auswahl:active {
background-color: #00FF2B;
}
.Auswahl:focus {
background-color: #7100FF;
}
.newClass {
background-color: #7100FF;
}
JQUERY
$(".Auswahl").click(function(){
$(this).addClass('newClass');
});
Here's a Codepen example too: EXAMPLE HERE
This question already has answers here:
How to affect other elements when one element is hovered
(9 answers)
Closed 8 years ago.
I am trying to make it so that so that a hover effect will apply for both a listed item tag and an anchor tag that it is nested in. Ideally I want it so that all the CSS is on one tag instead of split into two. I want the hover effect of the anchor tag to animate when the listed element tag is triggered. I'm assuming the solution would be to merge the styles into one but I don't know how to do it.
HTML:
<ul class="nav">
<li>
CONTACT
</li>
<li>
ABOUT
</li>
<li>
PORTFOLIO
</li>
</ul>
CSS:
body{
background: #000;
}
ul{
list-style-type:none;
display: inline-block;
}
.nav{
float:right;
list-style-type:none;
overflow: hidden;
}
.nav li{
float:right;
overflow: hidden;
color: #00bff3;
border: 1px solid #00bff3;
padding: 8px;
margin-left: 10px;
text-align: center;
-webkit-transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in;
-o-transition: all 0.3s ease-in;
}
.nav li:hover{
background:#00bff3;
color:#000000;
}
.blue{
color: #00bff3;
text-decoration: none;
-webkit-transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in;
-o-transition: all 0.3s ease-in;
}
.blue:hover{
color:#000000;
}
http://jsfiddle.net/2gbu5yrz/
It's easy enough to move the relevant styles to the links themselves (really where they should be anyhow):
http://codepen.io/pageaffairs/pen/PwNeEO
.blue{
color: #00bff3;
text-decoration: none;
-webkit-transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in;
-o-transition: all 0.3s ease-in;
display: block;
text-align: center;
padding: 8px;
}
.blue:hover{
color:#000000;
background:#00bff3;
}
Maybe this is what you are looking for: Replace .blue:hover with .nav li:hover .blue.
http://jsfiddle.net/p0ahhp5c/
The solution is to make your a use block display style:
.blue{
display: block;
color: #00bff3;
text-decoration: none;
-webkit-transition: all 0.3s ease-in;
-moz-transition: all 0.3s ease-in;
-o-transition: all 0.3s ease-in;
}
Try this
.nav:hover .blue:hover {
/*your code here*/
}
DEMO
As you can see in my jsbin the background overlays the text in my three link objects (move your cursor above it to see it).
I have tried around with the z-index (as suggested by a friend), however that doesn't seem to have any effect.
How would you go about fixing it?
Here's the relevant CSS:
a {
color: #CCCCCC;
}
a:hover {
background-color: #CCCCCC;
}
As you can see, The font color and background color are the same on hover. The z-index has nothing to do with it. Change the color on :hover and you will see the text, as demonstrated on this fiddle: http://jsfiddle.net/yVdvx/
Change this CSS code.
From:
a {
z-index: 10000;
text-decoration: none;
border-bottom: 1px dotted #666666;
color: #CCCCCC;
-webkit-transition: 0.25s ease;
transition: 0.25s ease;
}
a:hover {
background-color: #CCCCCC;
opacity: .9;
-webkit-transition: 0.25s ease;
transition: 0.25s ease;
}
To (my example makes a:hover color blue):
a {
z-index: 10000;
text-decoration: none;
border-bottom: 1px dotted #666666;
color: #CCCCCC;
-webkit-transition: 0.25s ease;
transition: 0.25s ease;
}
a:hover {
background-color: blue;
color: #393434;
opacity: .9;
-webkit-transition: 0.25s ease;
transition: 0.25s ease;
}
Besides, it may be good to include CSS code in a separate file and load it in the HTML file.
Does anybody know why this is happening?
On all the anchors, I have:
-webkit-transition: all .2s ease-in-out;
-moz-transition: all .2s ease-in-out;
-o-transition: all .2s ease-in-out;
transition: all .2s ease-in-out;
Why is that 'flash' occurring?
Update
By flash, I mean where the the H1 goes from purple to white.
Update 2
It seems to happen almost all anchor links.
For the example which is the logo, the HTML is:
<h1 class="logo">Test</h1>
In the CSS I have:
a {
text-decoration: none;
color: white;
-webkit-transition: all .2s ease-in-out;
-moz-transition: all .2s ease-in-out;
-o-transition: all .2s ease-in-out;
transition: all .2s ease-in-out;
}
The h1 is in header and for the header, I have certain CSS for the header links:
.header a {
color: white;
text-shadow: 0 -1px rgba(0,0,0,0.2);
}
And for the h1, I have:
h1 {
margin: 0;
float: left;
font-family: "proxima-nova";
font-size: 26px;
font-weight: 800;
text-transform: uppercase;
letter-spacing: 2px;
height: 30px;
line-height: 30px;
}