Can a nested div ignore the hover of a parent - html

If a div is nested inside of another, can the nested div ignore the hover of the parent. Here's an example
.Box {
width: 50px;
height: 50px;
background: red;
}
.Circle {
width: 20px;
height: 20px;
background: blue;
border-radius: 20px;
}
.Box:hover {
animation: expand .5s normal forwards;
}
#keyframes expand {
0% {
transform: scale(1);
}
100% {
transform: scale(1.6);
}
}
<div class="Box">
<div class="Circle"></div>
</div>
In this example would there be a way to make the Box expand but not the Circle

Technically the parent hover event doesn't get applied to the child.
But in your case the child is still effected, because you're scaling the parent. And thus everything inside of the parent is being scaled too.
In order to counter the scaling of the nested div, you can apply a reverse scaling effect when the parent div is hovered.
.Box{
width: 50px;
height: 50px;
background: red;
}
.Circle{
width: 20px;
height: 20px;
background: blue;
border-radius: 20px;
}
.Box:hover{
animation: expand .5s normal forwards;
}
.Box:hover .Circle {
animation: contract .5s normal forwards;
}
#keyframes expand {
0% {
transform: scale(1);
}
100% {
transform: scale(1.6);
}
}
#keyframes contract {
0% {
transform: scale(1);
}
100% {
transform: scale(0.625); /* 1 / 1.6 */
}
}
<div class="Box">
<div class="Circle"></div>
</div>

Because you are scaling the parent, everything inside it will be impacted. An alternative solution is to have a different sibling to the circle and apply the animation on that.
CSS:
.Box {
width: 50px;
height: 50px;
background: red;
}
.Circle {
width: 20px;
height: 20px;
background: blue;
border-radius: 20px;
position: absolute;
top: 10px;
left: 10px;
}
.Container {
position: relative;
}
.Box:hover {
animation: expand .5s normal forwards;
}
#keyframes expand {
0% {
transform: scale(1);
}
100% {
transform: scale(1.6);
}
}
HTML:
<div class="Container">
<div class="Box">
</div>
<div class="Circle"></div>
</div>
Demo: http://jsfiddle.net/lotusgodkk/GCu2D/2157/
Here, the circle is positioned so that it's position is not affected by the box

Related

change the height of a vertical line according to the bounce height of a circle

I want to:
Align the circle (containing exclamation mark) with the dashed vertical line.
Make the circle bounce along the vertical line while changing the height of the dashed vertical line accordingly.
Can you please tell me how can I achieve that in CSS? thank in advance.
.pin{
display:inline-block;
align-contents: center;
}
.circle {
color: #ffffff;
background: #ff5500;
width: 30px;
height: 30px;
border-radius: 50%;
text-align: center;
font-size: 25px;
font-weight: bold;
display: inline-block;
animation: blinkingBackground 1s infinite;
}
#keyframes blinkingBackground {
0% {
opacity: 0;
transform: translateY(-10px);
}
25% {
opacity: 0.025;
transform: translateY(10px);
}
50% {
opacity: 0.05;
transform: translateY(-10px);
}
75% {
opacity: 0.075;
transform: translateY(10px);
}
100% {
opacity: 1;
}
}
.vline{
border-left: 1px dashed orangered;
height: 50px;
position: relative;
}
<div class="pin">
<div class="circle">
!
</div>
<div class="vline"></div>
</div>
#1 Align circle with line
For your .vline class add those two properties. Width in order to have the one pixel width from your border. And margin: 0 auto will center your div inside the parent div.
width: 1px;
margin: 0 auto;
#2 Reduce height while bouncing
Just add another animation to your .vline class.
In the example below I also changed the height from 50px to 0, that's keeping the .vline at zero pixels after animation is done. And instead I'm setting at keyframe 0% the height to 50px.
Depending on how many pixels you want to reduce it, you will need more keyframes. In the example I've reduced the height by 10px per second, so I have 5 keyframes with 10px steps.
#keyframes reduceHeight {
0% {
height: 50px;
}
20% {
height: 40px;
}
40% {
height: 30px;
}
60% {
height: 20px;
}
80% {
height: 10px;
}
100% {
height: 0px;
}
}
And here the working example
.pin{
display:inline-block;
align-contents: center;
}
.circle {
color: #ffffff;
background: #ff5500;
width: 30px;
height: 30px;
border-radius: 50%;
text-align: center;
font-size: 25px;
font-weight: bold;
display: inline-block;
animation: blinkingBackground 1s infinite;
}
.vline{
width: 1px;
margin: 0 auto;
border-left: 1px dashed orangered;
height: 0;
position: relative;
animation: reduceHeight 5s;
}
#keyframes blinkingBackground {
0% {
opacity: 0;
transform: translateY(-10px);
}
25% {
opacity: 0.025;
transform: translateY(10px);
}
50% {
opacity: 0.05;
transform: translateY(-10px);
}
75% {
opacity: 0.075;
transform: translateY(10px);
}
100% {
opacity: 1;
}
}
#keyframes reduceHeight {
0% {
height: 50px;
}
20% {
height: 40px;
}
40% {
height: 30px;
}
60% {
height: 20px;
}
80% {
height: 10px;
}
100% {
height: 0px;
}
}
<div class="pin">
<div class="circle">
!
</div>
<div class="vline"></div>
</div>
It's not perfect yet and you'll have to play around with positionings (maybe even have to add them to the animations), depending on what exactly you wanna acchieve. But it should give you a general idea and ONE possibility on how to do it. There might be different methods to do the same.

Preserve 3d of child element, as its parent spins

I have an element that rotates, and inside of it I have .pop-out-item. This has translateZ(500px) on it.
When the rotating element (.rotator--child) spins, the .pop-out-item stays "attached" to the rotating div (see code snippet)
#keyframes spin {
0% {
transform: rotateY(0deg);
}
100% {
transform: rotateY(360deg);
}
}
.rotator {
position: relative;
perspective: 500px;
}
.rotator--element {
display: block;
width: 300px;
background: red;
aspect-ratio: 1/ 1;
animation: spin 10s infinite;
}
.pop-out-item{
width: 50px;
aspect-ratio: 1 / 1;
background: orange;
transform: translateZ(500px);
transform-style: preserve-3d;
}
body{
padding: 60px;
}
<div class="rotator">
<div class="rotator--element">
<div class="pop-out-item"></div>
</div>
</div>
I expect it to look different, something like this:
Sorry for terrible drawing.
What am I doing wrong? Thanks.
I assume what you want is for the orange square to move with the red one, but translated. I moved the transform-style: preserve-3d to the red rotator--element and made the translateZ value 50px as it's easier to see that way. As the transform-style MDN documentation states:
The transform-style CSS property sets whether children of an element are positioned in the 3D space or are flattened in the plane of the element.
#keyframes spin {
0% {
transform: rotateY(0deg);
}
100% {
transform: rotateY(360deg);
}
}
.rotator {
position: relative;
perspective: 500px;
}
.rotator--element {
display: block;
width: 300px;
background: red;
aspect-ratio: 1/ 1;
animation: spin 10s infinite;
transform-style: preserve-3d;
}
.pop-out-item{
width: 50px;
aspect-ratio: 1 / 1;
background: orange;
transform: translateZ(50px);
}
body{
padding: 60px;
}
<div class="rotator">
<div class="rotator--element">
<div class="pop-out-item"></div>
</div>
</div>

Two divs orbiting around a center CSS animation

I am attempting to "orbit" two separate divs in circular motion around a center, however I am having trouble getting the two divs to follow the same circular path in my CSS animation.
.container {
width: 500px;
height: 500px;
display: flex;
align-items: center;
justify-content: center;
}
.box {
background-color: pink;
width: 100px;
height: 100px;
animation: battle 6s linear infinite;
position: absolute;
margin: 10px;
}
#keyframes battle {
from {
transform: rotate(0deg) translateX(150px) rotate(0deg);
}
to {
transform: rotate(360deg) translateX(150px) rotate(-360deg);
}
}
<div class="container">
<div class="box">
</div>
<div class="box">
</div>
</div>
Jsfiddle
Let your parent element be the guide;
When the goal is to rotate in a consistent spacing around a center (as opposed to say an "elliptical orbit" that is more of an oval pattern) than the easiest technique is to provide a parent to set a consistent boundary and attach children within it to use its position as their animation path. The goal is to just supply an illusion of individual elements orbiting in sync when in reality just one is rotating with its default transform-origin of center acting as the guide for the children "orbiting" within it.
In our case we took a parent whose equal circumference is roughly the size of the "orbit desired" and we gave it a border-radius of 50% to create a circle. This makes no point on the element less than or greater distance from the other. We make it a position: relative element so that we can apply position: absolute to any children of it. In this example we use pseudo elements but they could just as easily be additional DOM node elements like divs.
By fixing our children to specific points on the parent we create the equal distance from the X/Y of the parent's transform-origin center we desire and apply the rotate transform to spin the parent. However if we only did that then the children would also follow that path and not keep vertical (as it is assumed is desired) so we simply re-use the same animation applied to the parent but in reverse to offset its rotation. The result is a parent element spinning one direction, and the children in the other to create the effect seen in the example. Hope this helps!
#rotator {
position: relative;
width: 7rem;
height: 7rem;
animation: rotations 6s linear infinite;
border: 1px orange dashed;
border-radius: 50%;
margin: 3rem;
}
#rotator:before, #rotator:after {
position: absolute;
content: '';
display: block;
height: 3rem;
width: 3rem;
animation: inherit;
animation-direction: reverse;
}
#rotator:before {
background-color: red;
top: -.25rem;
left: -.25rem;
}
#rotator:after {
background-color: green;
bottom: -.25rem;
right: -.25rem;
}
#keyframes rotations {
to { transform: rotate(360deg) }
}
<div id="rotator"></div>
Something I did many years ago might be close to what you are looking for:
// Base
body {
background: #252525;
}
// Keyframes
#keyframes rotateClockwise {
100% {
transform: rotate(360deg);
}
}
#keyframes rotateCounterClockwise {
100% {
transform: rotate(-360deg);
}
}
// Ring
.ring {
position: relative;
left: 50%;
top: 50px;
margin-left: -100px;
height: 200px;
width: 200px;
border: 10px solid #666;
border-radius: 50%;
}
// Dots
.dot {
position: absolute;
height: 250px;
width: 40px;
top: -25px;
left: 50%;
margin-left: -20px;
&:before {
display: block;
content: '';
height: 40px;
width: 40px;
border-radius: 50%;
box-shadow: 0 2px 3px rgba(0,0,0,.1);
}
}
.dot--one {
animation: rotateClockwise 4s linear infinite;
&:before {
background: #e6a933;
}
}
.dot--two {
animation: rotateCounterClockwise 2s linear infinite;
&:before {
background: #e63348;
}
}
.dot--three {
animation: rotateClockwise 7s linear infinite;
&:before {
background: #70b942;
}
}
.dot--four {
animation: rotateCounterClockwise 12s linear infinite;
&:before {
background: #009ee3;
}
}
<div class="ring">
<div class="dot dot--one"></div>
<div class="dot dot--two"></div>
<div class="dot dot--three"></div>
<div class="dot dot--four"></div>
</div>
https://codepen.io/seanstopnik/pen/93f9cbcbcf9b38684bfc75f38c9c4db3

Delay specific transform property CSS

I'm trying to make it so that the rotate(-45deg) property gets delayed a shortly after the first property translateY(6px) with the help of a delay. But how do I do that?
Code:
transform: translateY(6px) rotate(-45deg);
I first thought it was something like:
transform: translateY(6px) rotate(-45deg, 2s);
There is no trivial way to do this but in your particular case you can split the transformation using two different properties. You keep the rotation within transform and you use top/bottom to add the translation.
.box {
margin: 20px;
position: relative;
width: 200px;
height: 200px;
background: red;
top: 0;
transition: transform 0.5s, top 0.5s 0.5s;
}
.box:hover {
transform: rotate(-45deg);
top: -50px;
}
<div class="box">
</div>
Or you can consider animation:
.box {
margin: 20px;
position: relative;
width: 200px;
height: 200px;
background: red;
top: 0;
}
.box:hover {
animation:change 1s linear forwards
}
#keyframes change {
50% {
transform: rotate(-45deg);
}
100% {
transform: translateY(-50px) rotate(-45deg);
}
}
<div class="box">
</div>

CSS transform-origin + stacked divs

I am attempting to rotate/spin-in-place some stacked divs, but the 'transform-origin' property seems to be ignored when using absolute divs.
Attached is an example, the divs are stacked using stack class. Would using SVG be a better solution?
.circle {
height: 250px;
width: 250px;
border-radius: 50%;
border: 50px solid white;
margin: auto;
}
body {
background: black;
overflow: hidden;
}
.circle_one {
animation: rotateY 3s infinite linear;
}
.circle_two {
animation: rotateX 2s infinite linear;
}
.spinMe {
animation: spinMe 2s infinite linear;
transform-origin: 50% 50%;
}
.stack {
position: absolute;
top: 0;
left: 0;
}
#-webkit-keyframes rotateY {
to {
transform: rotateY(360deg);
}
}
#-webkit-keyframes rotateX {
to {
transform: rotateX(360deg);
}
}
#-webkit-keyframes spinMe {
to {
transform: rotate(360deg);
}
}
<div class="spinMe">
<div class="circle circle_one stack"></div>
<div class="circle circle_two stack"></div>
</div>
The problem is that the spinMe element has 100% width and zero height due to the absolutely positioned children. If you give spinMe a defined width and height equal to .circle it works correctly.
.circle {
height: 250px;
width: 250px;
border-radius: 50%;
border: 50px solid white;
margin: auto;
}
body {
background: black;
overflow: hidden;
}
.circle_one {
animation: rotateY 3s infinite linear;
}
.circle_two {
animation: rotateX 2s infinite linear;
}
.spinMe {
animation: spinMe 2s infinite linear;
transform-origin: 50% 50%;
width: 350px;
height: 350px;
}
.stack {
position: absolute;
top: 0;
left: 0;
}
#-webkit-keyframes rotateY {
to {
transform: rotateY(360deg);
}
}
#-webkit-keyframes rotateX {
to {
transform: rotateX(360deg);
}
}
#-webkit-keyframes spinMe {
to {
transform: rotate(360deg);
}
}
<div class="spinMe">
<div class="circle circle_one stack"></div>
<div class="circle circle_two stack"></div>
</div>