Display:none Removing Pseudo-elements - css

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');

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).

Chrome and Firefox overflow:hidden jumping elements

I stumbled over a strange behaviour that occurs in Chrome and Firefox when you have got an element with "position:relative;" and "overflow:hidden;" and an anchor in it with "position:absolute;".
Whenever the anchor gets focus the element above it magically jumps to the top, even though its styles and markup tell a different story.
Example: http://codepen.io/mediadivisiongmbh/pen/pJWmxp
All you need is a setup similar to this:
HTML
<div class="container">
<h1>I can fly</h1>
<a class="focus-me" href="#">Evil Link</a>
</div>
CSS
.container {
position:relative;
overflow:hidden;
/* Optional */
border:1px solid gray;
}
.focus-me {
position:absolute;
}
Thanks for your answers so far. In order to clarify the issue please take a look at this example:
http://codepen.io/mediadivisiongmbh/pen/bdRjKy
When you hover over the container, the absolute positioned element containing the anchor will transition in view. When you click on it a lightbox (in this case Magnific Popup) is opened. After you close this lightbox the anchor gets focus again and jumps into view, which looks pretty odd.
Setting the anchor to display:none by default and display:block when hovering over the container worked for solving this issue.
After more research I figured out that the problem is caused by an accessibility feature in chrome.
In my case I just needed to make sure the anchor tag is only accessible while hovering over the container element.
Therefore the solution was altering the CSS like this:
.container {
position:relative;
overflow:hidden;
/* Optional */
border:1px solid gray;
}
.focus-me {
position:absolute;
display:none;
}
.container:hover .focus-me {
display:inline-block;
}
I'm not pretty clear about what you're looking for since you have different story. But if you mean you want to prevent jump because of focus on your link, you can use tabindex="-1" on your link to makes it not accepting tab stop. Check the Updated Pen
EDIT
Well when seeing your pen, I think you need to set display: none to your evil link and set it to display: inline-block when hovering to your container. Check Updated Pen.

Child Hover not returning to unhovered state

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/

hover div to change an other div

How can I prevent a child div :hover event to be overridden by its parent :hover event?
This is how I create the div element:
HTML
<script type="text/javascript">
var counterd = document.createElement("div");
counterd.id='counterdiv';
counterd.innerHTML = "(<?php echo $woocommerce->cart->cart_contents_count ?>)";
document.getElementById('menu-item-130').appendChild(counterd);
document.getElementById("counterdiv").setAttribute('onclick', 'location.href = "/varukorg"');
</script>
The CSS to change style is:
#menu-item-130:hover #counterdiv, #menu-item-130.current_page_item #counterdiv {
color: black;
}
I also have some CSS rules to style #menu-item-130 on :hover. The problem occurs when the mouse goes over #counterdiv, it changes the #counterdiv text to black. I don't want the #counterdiv element to trigger the parent's event when it is hovered. The hover CSS for the #menu-item is the following:
#menu-item-130 a:hover, #menu-item-130.current_page_item a {
background: url(../images/cart-59-24black.png) gray 21px 18px no-repeat!important;
}
and as both div elements have the property position:relative;, I have tried with z-index but it does not seems to work. I know that its impossible to make a parent div to change style when hover a child.
Edit: Hi guys i find a solution like a hardcode but its work ;) as the counter div was on top i made the #counterdiv on hover at the same size as the #menu-item-130 and puted the same backround style to it so now its work. Thanks for the help anyway..
I am not sure if i understood your question, but here is an example how it could work for you:
DEMO
Explanation
The :hover even you already fine works fine. But there is something called propagation, which will make the browser detect in which parent element your cursor is, and trigger the events/pseudo-elements/pseudo-classes related to this element. In order to stop this propagation, (in CSS), you need to counter it with the child element's properties.
Parent's background property: gray
Child's background property: transparent !important
To understand what !important clearly does, I suggest you to read more about CSS Specificity (MDN)
Solution
Apply a transparent background to the element itself with the !important notion:
HTML
<div id="item-130">Item 130
<div id="counterdiv">CounterDiv</div>
</div>
CSS
#item-130:hover > #counterdiv {
background-color:gray;
}
#counterdiv:hover {
background-color:transparent !important;
}
Live Demo

border-radius doesn't affect the background of an inner div (Chrome, Opera)

Is it possible to use border-radius on a div (in Chrome and Opera) so that the inner div background would also be affected? If not, how else can this effect be achieved?
Example: http://jsfiddle.net/fE58b/1/
It works well in IE9 & FF7.
Thank you.
It works if you just move the background image from #content to .box:
.box {
background:url(...so_70s_background.jpg) top left repeat;
}
Also if you really need to have the borders in a separate div, you can always add new divs outside the box:
<div class="box-outer">
<div class="box">
<div id="content"></div>
</div>
</div>
Just make sure to apply:
.box-outer {
width:500px;
height:500px;
position:absolute;
}
So, as Cristi stated above, some browsers seem to have a little trouble with clipping overflowing children along the curved border of their parents. It appears that, in order (for at least Webkit) to render child overflow affected by parent border-radius, both parent and child must be statically positioned.
Furthermore, even with this static positioning, it appears that setting an opacity value for the child element will cause it to break the overflow boundaries of its parent (if I had to guess, I'd imagine this has something to do with the opacity value triggering hardware accelerated graphics, in which case a 3D-transform will produce the same glitch).
Luckily, you can use an absolutely-positioned grandparent control the dimensions and position of its static children, and rgba on background and text to simulate element opacity. I just so happen to have a jsfiddle snippet demonstrating some of these workarounds which you might find helpful.
user following to solve border radous issue
border-radius: 20%; /* FOR ALL NEW BROWSER OF HIGHER VERSION*/
-webkit-border-radius: 20%; *FOR ALL OHTER OLD BROWSER*/
-moz-border-radius: 20%; /* FOR MOZILLA FF*/

Resources