Child Hover not returning to unhovered state - css

I am trying to construct a chunk of code that is an image and a text caption, which is a single anchor. the image is an image tag and the text is in a DIV tag.
When the anchor is hovered, the image+text box has a border appear, and the text div transitions between text to then show the background image (using opacity 1 to 0)
USING CSS ONLY
My issue is that I can't seem to find the best CSS to write this code, what I have is:
HTML:
<div class="outerCCBox">
<a href="*url*" >
<img src="images/logo/clarityTeeth.png" alt="">
<div class="clarityUnderBox">
<div class="clarityBox">
Clarity Makeup
</div>
</div>
</a>
</div>
The "clarityUnderBox is a presized box containing the background image that appears when the covering text fades out on hovering over the anchor tag.
CSS:
.clarityUnderBox {
width:256px !important;
height:86px !important;
background:url('../../images/logo/Clarity-C-320.png') no-repeat;
background-size:contain;
}
.clarityBox {
width:100% !important;
height:100% !important;
background-color: #000;
opacity:1;
color:#f0f0f0;
transition: color 0.4s linear,opacity 0.6s;
}
All CSS is simplified for this question (fonts, transition -types- etc removed).
The issue I am having appears to be with the next piece of code, the "hover" element:
.outerCCBox a:hover > .clarityUnderBox .clarityBox {
opacity:0;
color:transparent;
}
EDITED CSS:
originally
.outerCCBox a:hover .clarityUnderBox .clarityBox {
opacity:0;
color:transparent;
}
which behaves in the same way, as with the ">" selector.
The issue is that the hover works fine when hovering over the anchor element but when moving away, the .clarityBox class doesn't return to it's pre-hover state.
1) How do I make this return to it's pre hover state?
1b) Do I need to make a separate ~ a:not-on-hover CSS declaration?
2) How can I tidy up and make the "hover" CSS line more specific? - the way I've done it above works, but I'm sure there's a better syntax to it. I have tried things like using "*" and ">" selectors with limited success (combined with some rearrangement of class orders in the HTML)
Thanks for your guidance.
EDIT:
As requested, a fuller fiddle is here:
http://jsfiddle.net/gwrrezys/9/
But this fiddle doesn't show the image above the text, but it does replicate the general issue with the hover not updating / or not returning to its original state.
cheers
SOLUTION:
As suggested in comments by Martin, making the anchor a block element fixed this issue, I have retained the issue in the jsFiddle for reference and can be found by repeatedly hovering and then hovering off the anchor area.

Your actual problem is with the hovered parent (your anchor element) not having a width set.
If you make the anchor a block element it will fix the "leaking" content issue. by either
making the anchor display: block with set width and height
or making the parent fit the content by making it display: inline-block
DEMO
General to displaying children on hovered parents:
As soon as you extend a child of a :hover element over the whole screen (100% width and height) the parent will stay in the hovered state as long as you are hovering over the child.
To get around that you need to break the child out of its parents flow ... for example by making it's position: fixed (or position: absolute if the parent has no position: relative).
For example by using something like this on the child - and the z-index: -1; here makes sure it moves behind the parent:
position: fixed;
width: 100%;
height: 100%;
top:0;
left: 0;
z-index: -1;
DEMO
Or (depending on what area exactly you wan to cover with the child) you can alternatively extend the child only over a particular hover area (e.g. its parent) ... here you would then use position:absolute on the child and position: relative on the parent (to make sure you keep the child in the parents flow).
DEMO
A quick read on positioning elements: http://css-tricks.com/absolute-relative-fixed-positioining-how-do-they-differ/

Related

Force a parent's height with hidden child element in CSS

If I have a hidden child element (with either display: none or visibility: hidden), how can I have the parent div keeps it's height so that when the child is made visible, the parent height does not change?
Do I need to set an absolute height on the parent or can I still have it calculate its height from the child?
display:none removes the element from the flow, so no way to make the parent keept the height (other than hard-coding a fixed value). It should also hide it from screen readers and crawlers.
visiblity:hidden keeps the element in the flow, and therefore, keeps the space reserved for it, so the parent will keep the height just as if the element was visible.
opacity:0 will also act just like visibility:hidden, while allowing the reveal of the element to be transitioned / animated to opacity:1.
So you should use either visibility:hidden or opacity:0, depending on if you want to show the element in a jumpy reveal or transition.
Edit:
It should also be noted that visibility:hidden will not fire events (such as a click, hover, etc) while opacity:0 will. So there are even some rare cases on which you could use both together. For instance, if you want the element to start hidden, then show up with a transition, and have another event linked to it that should fire only when the element is visible
In the following example, there's a click event linked to the div element that will fire only when visible (so couldn't use just the opacity), but also have a transition when revealing (so couldn't use just visibility)
$('button').click(function() {
$('.opacity').toggleClass("visible");
});
$('.opacity').click(function() {
alert("clicked");
});
div {
width: 100vw;
height: 100vh;
transition: opacity 1s ease;
background: chartreuse;
}
.visibility{
visibility:hidden;
}
.opacity{
visibility:hidden;
opacity:0;
}
.visible{
opacity:1;
visibility: visible;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button>
toggle
</button>
<div class="opacity"> opacity:0 </div>
<hr>
If you use visibility: hidden; to hide the child, space will still be reserved for it. You can show it again, by setting visibility: visible;.
To cite the MDN docs for visibility:
The visibility CSS property can show or hide an element without affecting the layout of a document (i.e., space is created for elements regardless of whether they are visible or not).

Display:none Removing Pseudo-elements

Hi I was answer this question and I notice an strange behavior
The Context
I have an HTML structure like this:
<div class="btn">
Click me
</div>
<div class="element">
Div Box With Pseudo Element
</div>
And CSS Just the relevant
.element {
display:none;
}
.element:after {
content:" ";
display:block;
width:0;
background:black;
transition:6s ease;
}
.element.clicked:after {
width:100%;
}
Where the element needs to be display:none and need to be show/hide when click the btn element. That works fine with Jquery and fadeToggle.
Also I add a class to animate a pseudo-element with transition and width. Need to animate at the same time of the fade on the parent.
The problem
If you see this FIDDLE, you can notice at first click the expected behavior is the pseudo-element grows form 0 to 100% but instead is 100% without grow.
If you click again then it's fine changing from 100% to 0
Question
I notice whit the inspector that setting display:none to the element makes the pseudo-element disappear.
This causes the element can't be from 0 to 100% since doesn't exist.
Anyone Knows How to stop that behavior or how to avoid the non-render of the element. I was wonder about the form pseudo-elements were rendered and If they need a Visible parent
This issue doesn't happen with visibiliy or opacity just with display
I believe this issue lies in the fact of how CSS transition works. As you well know a css transiton is applied when an element has a property changed from one value to another.
But in your scenario, the property isn't in fact changing. A pseudo element does not exists while its parent is in the display: none property. So when you call the fadeToggle(), the element becomes display: block and then pseudo is created.
But immediately it is already affected by the .clicked class of the parent, which gives the pseudo a width: 100% property.
So, essencially, the width property is never changed. It goes from "non existent" to "100%", therefore, no transition is applied.
EDIT
So, what you really need is to revert the order of the apply of .clicked class to after the fade started:
Updated Fiddle
$('.element').stop().fadeToggle(3000).toggleClass('clicked');

CSS perspective on a long, scrollable div

I've got a click style that involves tilting the element via a 3D rotate when clicked on. Here's what it looks like:
And here is a GIF of the effect working properly with a short list!
I have perspective on the parent div set at about 1500px, and the list div is set to inherit perspective. When this scrollable div gets really long, though, you start to see effects like this when clicking elements toward the bottom of the list:
I'm assuming this is because the perspective is applied to the entire height of the div, rather than just the visible height. I tried to resolve this by having the perspective of the list div inherit from the parent div (who's height is only the visible area), but this had no effect.
Any ideas on how to solve this?
Thank you!
EDIT: Relevant HTML:
<div class="container">
<ul class="artists">
<li>38 Special</li>
<li>A Flock Of Seagulls</li>
<!-- etc. -->
</ul>
</div>
Relevant CSS:
div.container {
position: relative;
height: calc(100% - 120px);
overflow: hidden;
perspective: 1500px;
}
ul.artists {
position: absolute;
top:0;
bottom:0;
left:0;
right:0;
/* Animation stuff, list styles, etc */
perspective: inherit;
overflow-y: auto;
overflow-x: hidden;
}
When pressed, a style like this is applied to the list item:
{
transform: rotate3d(4.05, 6.1635, 0, 15deg);
}
EDIT: Repro here:
http://codepen.io/anon/pen/LqAhK
Notice how as you scroll down the list, the perspective is applied as though the viewport is as high as the list itself, rather than the container - resulting in a different effect depending on your position in the list! How can I get perspective to be applied on the list items using the dimensions of the container only?
My best workaround would be to freeze the list and set a static height on the click event, but that has some side effects and is a somewhat hacky solution!
I'm not sure if this is what you're looking for, but...
ul.list li {
perspective: inherit;
}
...changing it so that the <li> elements themselves are what reset the perspective, rather than the container <ul>, seems to make all the elements appear the same.
http://codepen.io/anon/pen/zGpKk

how to make text change when you hover over it without it twitching?

As I understand it, CSS can be used to change the content of an element on :hover in a basic way. I am using this code
HTML:
<button><span>3 replies</span></button>
CSS:
button {width:6em}
button:hover span {display:none}
button:hover:before {content:"Reply!"}
but when I hover over the button, it twitches very badly
I want mine to be smooth like the music player at this link
When you hover over one of the buttons under lease, premium, or trackout price, they switch over to the +add text
here is part of my player http://djwckd.com/test
The important thing is to make sure that your layout does not change on hover. The easiest way to achieve this would be to allocate some space in your layout for all of the parts even when not hovering. I'm not sure what sort of layout you are trying to achieve but here is an example:
button { width: 6em }
button:hover span {display:none}
button:before { width: 100px; content: ""; }
button:hover:before {content: "Reply!"}
By giving the :before pseudo-element a size even when it's not hovered the layout shouldn't change when the content changes. You may need to adjust this for the specific layout you want but the general principle is to make sure all of the size-related properties are specified without :hover and then only adjust non-layout properties (that is, properties that don't affect any box sizes) in the :hover state.
As you provided the link is hovering the background images but in your test link you have given background images before to <a> elements, if you want exactly same as link use background-image: url('image1'); to a and background-image: url('image2'); to a:hover.
You can still use positioning the background-images, for this you should have like this.
+--------------+
| | position this background to a
+--------------+
| + Add | position this background to a:hover
+--------------+
Ok! for this make your background 64px width and 32 px height.
then position your background to
a{background-image: url('image') left top no-repeat; background-position: 0% 100%;}
now position your background to
a:hover{background-position: 0% 0%;}
I think I have a solution. The trick was one main thing: setting the width of the text's container. I also used onmouseenter instead of onmouseover for faster text change (my theory that onmouseenter is faster then onmouseover). Here is an example:
var videoplayer = {
text: "<b>Hello World</b>",
author: "(you)"
}
<div onmouseenter="this.innerHTML = videoplayer.text;" onmouseleave="this.innerHTML = '<b>(hover over me)</b>';" style="background-color: red; padding: 10px; width: 110px; text-align: center; color: white;">(hover over me)</div>
Just make sure you set the div's width to the width you need. (If you don't want a background, just change the part that says background-color: red to background-color: transparent). One more thing: you have to use a div or other container with display:block set as its default. I suggest using div. Hope this helps!

CSS parent position

I have a background on an div element with position top. Now this background spans across multiple li floats (progress indicator).
div.myListingProgressWrapper{
width: 807px;
height: 39px;
background: url(background.png) top no-repeat;
}
ul.fiveStepProgress li a{
float: left;
}
Now I would like to apply an active state to the a class and move the background of the parent container to bottom as the background contains a different colour and a progress arrow.
My question is, can I move the parent container background to bottom by applying the active class on the child a link?
Cheers
You cannot select a parent from within CSS. There have been several suggestions to the W3C on how to do this over the past few years, but the performance concerns have shot everything down. Plenty of reading on this, here's a link: http://css-tricks.com/7701-parent-selectors-in-css/
You're best off setting the a class on the parent, which affects the child... since you can't do it the other way around without javascript anyway.

Resources