I have some cards in a div and want them to change width on hover, but have two issues whith it:
I want to use transition , but the cards that are not hovered do transition after hovered card ends one. I want do it simultaneously.
How to change origin for 3rd and 4th card, so they change their width from right to left?
here is my code
body {
background-color: #333;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.cardBox {
width: 800px;
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 20px;
background-color: rgb(200, 201, 202);
padding: 20px;
overflow: hidden;
}
.card {
height: 450px;
background-color: #fff;
width: 100%
}
.card:hover {
width: 450px;
transition: all 1s ease;
}
.card:hover~ :not(:hover) {
width: 100%;
transition: all 1s ease;
}
.card img {
width: 100%;
height: 90%;
}
.card span {
text-transform: uppercase;
color: rgb(41, 40, 40);
font-weight: 600;
letter-spacing: 1.2px;
font-size: 20px;
display: block;
text-align: center;
line-height: 30px;
}
<div >
<div >
<!-- <img src="/1.jpg" alt=""> -->
<span>CSS</span>
</div>
<div >
<!-- <img src="/3.jpg" alt=""> -->
<span>image</span>
</div>
<div >
<!-- <img src="/2.jpg" alt=""> -->
<span>hover</span>
</div>
<div >
<!-- <img src="/4.jpg" alt=""> -->
<span>effect</span>
</div>
</div>
CodePudding user response:
body {
background-color: #333;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.cardBox {
width: 800px;
display: flex;
gap: 20px;
background-color: rgb(200, 201, 202);
padding: 20px;
overflow: hidden;
}
.card {
height: 450px;
background-color: #fff;
width: 100%;
flex: 1;
}
.card:hover {
width: 450px;
transition: all 1s ease;
flex: 4;
}
.card:hover~ :not(:hover) {
width: 100%;
transition: all 1s ease;
}
.card img {
width: 100%;
height: 90%;
}
.card span {
text-transform: uppercase;
color: rgb(41, 40, 40);
font-weight: 600;
letter-spacing: 1.2px;
font-size: 20px;
display: block;
text-align: center;
line-height: 30px;
}
<div >
<div >
<!-- <img src="/1.jpg" alt=""> -->
<span>CSS</span>
</div>
<div >
<!-- <img src="/3.jpg" alt=""> -->
<span>image</span>
</div>
<div >
<!-- <img src="/2.jpg" alt=""> -->
<span>hover</span>
</div>
<div >
<!-- <img src="/4.jpg" alt=""> -->
<span>effect</span>
</div>
</div>
CodePudding user response:
The problem was grid display and wrong css selector (.card:hover~ :not(:hover)
). Also, use transition in the selector not in :hover selector.
body {
background-color: #333;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.cardBox {
width: 800px;
/* display: grid;
grid-template-columns: repeat(4, 1fr); */
display: flex;
gap: 20px;
background-color: rgb(200, 201, 202);
padding: 20px;
overflow: hidden;
}
.card {
height: 450px;
background-color: #fff;
/* width: 100% */
width: 25%;
transition: all 1s ease;
}
.card:hover {
width: 450px;
/* transition: all 1s ease; */
}
/* .card:hover~ :not(:hover) {
width: 100%;
transition: all 1s ease;
} */
.card img {
width: 100%;
height: 90%;
}
.card span {
text-transform: uppercase;
color: rgb(41, 40, 40);
font-weight: 600;
letter-spacing: 1.2px;
font-size: 20px;
display: block;
text-align: center;
line-height: 30px;
}
<div >
<div >
<!-- <img src="/1.jpg" alt=""> -->
<span>CSS</span>
</div>
<div >
<!-- <img src="/3.jpg" alt=""> -->
<span>image</span>
</div>
<div >
<!-- <img src="/2.jpg" alt=""> -->
<span>hover</span>
</div>
<div >
<!-- <img src="/4.jpg" alt=""> -->
<span>effect</span>
</div>
</div>