CSS Hover affecting other cards

Asked

Viewed 39 times

1

Hello, I am beginner in the area, I am practicing HTML/CSS and would like your help with this problem.

When hovering the mouse inside a card (Hover) I want the same to make an animation, but the same is affecting other cards on the page, how to solve? follows the problem link in Jsfiddle:

https://jsfiddle.net/6adbt81k/

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    background-color: #e8ebf0;
    /* border: 1px solid red; */
    font-family: monospace;
}

header {
    text-align: center;
    color: red;
    letter-spacing: 0.5em;
    margin-bottom: 5%;
}

main {
    display: flex;
    justify-content: center;
    margin: 15px;
    /* border: 1px solid red; */
}

/* CARD */

.card {
    /* border: 1px solid #808080; */
    width: 250px;
    background-color: #ffffff;
    border-radius: 10px;
    margin: 10px;
}

.card:hover {
    /* transform: scale(1.1); */
    transition: 0.5s;
    box-shadow: 0 0 10px 4px #a2a8b6;
}

.card img {
    width: 250px;
    height: 200px;
    border-radius: 10px;
}

.cardfooter p {
    cursor: default;
    opacity: 0;
}

.card:hover .cardfooter p {
    opacity: 1;
    transition: 1.5s;
}

.cardfooter {
    letter-spacing: 0.2em;
    text-align: center;
    font-size: 1.2em;
    margin-top: -60px;
}

.card:hover .cardfooter {
    transition: 0.5s;
    animation: anim 0.5s forwards;
}

@keyframes anim {
    0% {
        margin-top: -60px;
    }

    100% {
        margin-top: 0;
    }
}
<header><h1>CLÃ UCHIHA</h1></header>
    
    <main>
        <div class="card">
            <div class="cardHeader">
                <img src="https://img.quizur.com/f/img5f56fd05af96d6.11314610.jpg?lastEdited=1599536396">
            </div>
            <div class="cardfooter">
               <p>Itachi Uchiha</p>
            </div>
        </div>

        <div class="card">
            <div class="cardHeader">
                <img src="https://static2.cbrimages.com/wordpress/wp-content/uploads/2020/01/Sasuke-Uchiha-in-Naruto-Shippuden-featured-image-Cropped.jpg">
            </div>
            <div class="cardfooter">
                <p>Sasuke Uchiha</p>
            </div>
        </div>
    </main>

1 answer

1


Face it happens because you didn’t use an explicit vertical alignment class for the flex container .main

inserir a descrição da imagem aqui

In the class container .main you have to declare align-items: flex-start;, so you overwrite the default showing that is stratch for flex-start and avoids this problem.

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    background-color: #e8ebf0;
    /* border: 1px solid red; */
    font-family: monospace;
}

header {
    text-align: center;
    color: red;
    letter-spacing: 0.5em;
    margin-bottom: 5%;
}

main {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    margin: 15px;
    /* border: 1px solid red; */
}

/* CARD */

.card {
    /* border: 1px solid #808080; */
    width: 250px;
    background-color: #ffffff;
    border-radius: 10px;
    margin: 10px;
}

.card:hover {
    /* transform: scale(1.1); */
    transition: 0.5s;
    box-shadow: 0 0 10px 4px #a2a8b6;
}

.card img {
    width: 250px;
    height: 200px;
    border-radius: 10px;
}

.cardfooter p {
    cursor: default;
    opacity: 0;
}

.card:hover .cardfooter p {
    opacity: 1;
    transition: 1.5s;
}

.cardfooter {
    letter-spacing: 0.2em;
    text-align: center;
    font-size: 1.2em;
    margin-top: -60px;
}

.card:hover .cardfooter {
    transition: 0.5s;
    animation: anim 0.5s forwards;
}

@keyframes anim {
    0% {
        margin-top: -60px;
    }

    100% {
        margin-top: 0;
    }
}
<header><h1>CLÃ UCHIHA</h1></header>
    
    <main>
        <div class="card">
            <div class="cardHeader">
                <img src="https://img.quizur.com/f/img5f56fd05af96d6.11314610.jpg?lastEdited=1599536396">
            </div>
            <div class="cardfooter">
               <p>Itachi Uchiha</p>
            </div>
        </div>

        <div class="card">
            <div class="cardHeader">
                <img src="https://static2.cbrimages.com/wordpress/wp-content/uploads/2020/01/Sasuke-Uchiha-in-Naruto-Shippuden-featured-image-Cropped.jpg">
            </div>
            <div class="cardfooter">
                <p>Sasuke Uchiha</p>
            </div>
        </div>
    </main>

  • Thanks hugocsl! I found that the align-items had as standard the flex-start, but I made a mistake. Thank you so much for getting me this question.

  • @Igorbezerra quiet my dear, both vertical and horizontal is stratch, so was giving this problem ai. But cool that solved []s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.