Scss :last-child selector problem with attribute selector - css

Here is the html:
<div class="card">
<!-- First child -->
<div class="card__side">
Front
</div>
<!-- Last child -->
<div class="card__side--back-1">
Back
</div>
</div>
and here is the scss code:
.card {
perspective: 150rem;
position: relative;
height: 50rem;
&:hover &__side {
&:first-child {
transform: rotateY(180deg);
}
&:last-child {
transform: rotateY(0deg);
}
}
[class*="card__side"] {
font-size: 2rem;
height: 50rem;
transition: all .8s ease;
position: absolute;
width: 100%;
backface-visibility: hidden;
&:first-child {
background-color: $color-white;
}
&:last-child {
background-color: $color-white;
transform: rotateY(180deg);
}
}
}
when I use the class name card__side--back-1, the selector [class*="card__side"] is working but the selector :last-child is not working,
however when I change the class name to card__side for both of the divs, it works.
I know I can give different classes for the task but asking to learn the main reason behind this. This is my first post so I hope I am making it right. Thanks in advance.
when I change the class name to card__side for both of the divs, it works.

I've solved it, the selectors work fine now.
the problem was at the &:hover &__sideselector at line 5.
I've changed the 5th line of the scss code to &:hover [class*="card__side"] now it selects.
.card {
perspective: 150rem;
position: relative;
height: 50rem;
&:hover [class*="card__side"] {
&:first-child {
transform: rotateY(180deg);
}
&:last-child {
transform: rotateY(0deg);
}
}
[class*="card__side"] {
font-size: 2rem;
height: 50rem;
transition: all .8s ease;
position: absolute;
width: 100%;
backface-visibility: hidden;
&:first-child {
background-color: $color-white;
}
&:last-child {
background-color: $color-white;
transform: rotateY(180deg);
}
}
}

Related

css hover an element without affecting :after

How do I stop an pseudo element from being affected by parents hover?
.list-item {
position: absolute;
background: black;
color: white;
padding: 10px 20px;
transition: all 250ms ease-in-out;
}
.list-item:hover {
transform: scale(1.5);
}
.list-item:after {
content: '';
position: absolute;
width: 20px;
height: 20px;
background: orange;
z-index: 10;
border-radius: 100%;
}
<div class="list-item">
test
</div>
When hovering it applies all hover effects to :after pseudo element
One method can be to apply an opposing transition to the ::after pseudo element:
.list-item:hover {
transform: scale(1.5);
}
.list-item:hover::after {
/* transform: scale(calc(1 - (your main scale amount - 1) / 2)); */
transform: scale(0.75);
}

I'm trying to implement effect of stacking cards with css, but pseudo elements keep appearing on the front

I tried different things described here: Is it possible to set the stacking order of pseudo-elements below their parent element?
And even though the parent element appeared in the front, when i hover over - it goes in the background.
It seems that when i use trasfrom on parent element it creates a new stacking context and everything goes brrrrrrrr.
I already thought just to create 3 separate divs with different z-indexes and translate them, when i hover over, but is there nice a way to do the this effect with pseudo elements?
Here is a basic example illustrating the issue:
.card_container {
width: 33.333%;
padding: 1rem;
}
.card {
display: block;
height: 100%;
text-align: center;
background-color: #f18e9b;
position: relative;
will-change: transform;
}
.card::before {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: -1;
background-color: #e84a5f;
}
.card::after {
content: "";
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
z-index: -2;
background-color: #cc1a32;
}
.card:hover {
-webkit-transform: translate(-5px, 0);
transform: translate(-5px, 0);
}
.card:hover:before {
-webkit-transform: translate(5px, 0);
transform: translate(5px, 0);
}
.card:hover:after {
-webkit-transform: translate(10px, 0);
transform: translate(10px, 0);
}
.content {
padding: 4rem 1rem;
}
.title {
font-size: 1.25rem;
background-color: gray;
border-radius: 3px;
padding: 0.5rem;
}
<div class="card_container">
<div class="card">
<div class="content">
<div class="title">Title</div>
<div class="subtitle">Subitle</div>
<div class="text">Random random random text</div>
</div>
</div>
</div>
If you want to see the version with sass go here

Animation with Sass and hover doesn't work?

I'm stuck on :hover doesn't work. I suspect the absolute and float position but don't have any clear idea. I'm a beginner user of Sass and the css animation. Any help ?
.menus {
cursor: pointer;
&:hover >.box {
transform: scale(0) translateX(300px);
.check {
transform: rotate(0deg);
}
}
}
.box {
float: right;
position: absolute;
top: 0;
right: 0;
width: 20%;
height: 100%;
background-color: yellow;
border-radius: 4px;
color: green;
transform: scale(1) translateX(0);
transition: transform 330ms ease-in-out;
.check {
display: block;
float: right;
position: absolute;
right: 30%;
top: 35%;
transform: rotate(-360deg);
transition: transform 500ms ease-in-out;
}
}
<div class="restaurants menus">
<div class="box"><img class="check" src="img/check24.png" alt="#"></div>
<h3>Citrus Squid Carpaccio</h3>
<p>with orange zest</p>
<img class="heart " src="img/heart-30.png" alt="#">
</div>
The problem lies here :
.menus {
cursor: pointer;
&:hover + .box {
Your selector &:hover + .box selects the .box elements that are siblings of .menus, and not children. You can use &:hover .box if you want to select all boxes children and grand children of .menus, or &:hover>.box to select direct .box children.
Here's a link with all the css selector that might be useful in the future: https://www.w3schools.com/cssref/css_selectors.asp

CSS TranslateX doesn't move to the right of div

I'm trying to get the toggle to move it 100% to the right. As I'm trying to make it responsive, I can't set it to move an xx amount of pixels.
Can you please help?
input:checked + .slider:before {
-webkit-transform: translateX(100%);
-ms-transform: translateX(100%);
transform: translateX(100%);
}
https://jsfiddle.net/Lc1tdhgb/1/
Thanks
setTimeout(function() {
document.getElementById('togBtn').checked = true;
}, 1000)
#toggle {
width: 100%;
height: 100%;
position: relative;
}
.switch {
position: absolute;
display: inline-block;
width: 100%;
/*min-height: 32px;*/
height: auto;
top: 0;
}
.switch input {
display: none;
}
.slider {
cursor: pointer;
background-color: #ca2222;
-webkit-transition: .5s;
transition: .5s;
border-radius: 32px;
padding: 12px 0;
}
.slider:before {
position: absolute;
content: "";
height: 1.1em;
width: 1.1em;
left: 3px;
bottom: 3px;
background-color: white;
-webkit-transition: .5s;
transition: .5s;
border-radius: 50%;
}
input:checked+.slider {
background-color: #3eab37;
}
input:focus+.slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked+.slider:before {
left: calc(100% - 20px);
/*-webkit-transform: translateX(100%);
-ms-transform: translateX(100%);
transform: translateX(100%);*/
}
/*------ ADDED CSS ---------*/
.slider:after {
content: 'OFF';
color: white;
display: block;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
font-size: 0.7em;
font-family: Roboto, sans-serif;
}
input:checked+.slider:after {
content: 'ON';
}
/*--------- END --------*/
<div id="toggle">
<label class="switch"><input type="checkbox" id="togBtn"><div class="slider round"></div></label>
</div>
Well, just make sure that the container of the elements follow a position: relative;, so the wrapper have the restrains for the absolute elements inside of it. Then, right is actually how far from right you want the element to be, in this case, you could've used either right: 0%; or left: 100%; although you've encountered the error in the fact that you'd be ignoring margins from the parent's style. That's why I added left: calc(100% - 20px); (20px was on trial and error, until I got it aligned with the outter border of the switch!), then now it works as wanted. Glad to help :)

Flip a 3D card with CSS

I'm trying to make a 3D card flipping effect with CSS like this.
The difference is that I want to use only CSS to implement it.
Here is the code I tried:
/*** LESS: ***/
.card-container {
position: relative;
height: 12rem;
width: 9rem;
perspective: 30rem;
.card {
position: absolute;
width: 100%;
height: 100%;
div {
position: absolute;
height: 100%;
width: 100%;
}
.front {
background-color: #66ccff;
}
.back {
background-color: #dd8800;
backface-visibility: hidden;
transition: transform 1s;
&:hover {
transform: rotateY(180deg);
}
}
}
}
HTML:
<div class="card-container">
<div class="card">
<div class="front"><span>Front</span></div>
<div class="back"><span>Back</span></div>
</div>
</div>
The issue is that the card doesn't flip, it snaps from back to front like this:
Is it possible to implement this 3d card flip on hover effect using only CSS?
I simplified the code to make it shorter and make the 3d card flip on hover. The card flips on the Y axis from the front face to the back face this is what it looks like:
Here is an example of a simple CSS only flipping card the flip animation is launched on hover :
.card {
position: relative;
width: 50vh;
height: 80vh;
perspective: 500px;
margin: 10vh auto 50vh;
}
.front,
.back {
position: absolute;
width: 100%;
height: 100%;
transition: transform 1s;
backface-visibility: hidden;
transform-style: preserve-3d;
}
.front {
background-color: #66ccff;
}
.back {
background-color: #dd8800;
transform: rotateY(180deg);
}
.card:hover .front {
transform: rotateY(180deg);
}
.card:hover .back {
transform: rotateY(360deg);
}
<div class="card">
<div class="front"><span>Front</span></div>
<div class="back"><span>Back</span></div>
</div>
Note that you will need to add vendor prefixes depending on the browsers you want to support. See canIuse for 3d transforms and transitions.
This is what I changed in your code for the flip effect:
the front face wasn't rotated on th Y axis on hover
the hover effect was launched when the .back div was hovered. This can create flickering as that div is rotating and "disapears" at mid rotation. It's better to launch the animation when the static parent is hovered.
the first parent isn't really usefull so I removed it
yes it can be done using CSS only and you can do by using CSS3 animation property. Here is example of flipping card animation.
<div class="container text-center">
<div class="card-container">
<div class="card">
<div class="front">
<span class="fa fa-user"></span>
</div>
<div class="back">User</div>
</div>
</div>
The CSS
.card-container {
display: inline-block;
margin: 0 auto;
padding: 0 12px;
perspective: 900px;
text-align: center;
}
.card {
position: relative;
width: 100px;
height: 100px;
transition: all 0.6s ease;
transform-style: preserve-3d;
}
.front, .back {
position: absolute;
background: #FEC606;
top: 0;
left: 0;
width: 100px;
height: 100px;
border-radius: 5px;
color: white;
box-shadow: 0 27px 55px 0 rgba(0, 0, 0, 0.3), 0 17px 17px 0 rgba(0, 0, 0, 0.15);
backface-visibility: hidden;
}
.front {
display: flex;
align-items: center;
justify-content: center;
font-size: 30px;
}
.back {
display: flex;
align-items: center;
justify-content: center;
font-size: 18px;
}
.card-container:hover .card {
transform: rotateY(180deg);
}
.back {
transform: rotateY(180deg);
}
You can also read this article about CSS Flip Animation on Hover
You can also find demo and download source from the article.

Resources