user11714727
user11714727

Reputation:

Unable to apply transition onto this drop down

I can't seem to apply a transition to this dropdown I made. The reason it doesn't look good is because it's just for testing purposes, hence why I've come here for help. If you can help me that would be much appreciated :)

It might just be something obvious that im doing wrong or the way that I've made the drop-down so that transition doesn't affect anything.

Heres my HTML code:

* {
    margin: 0;
    padding: 0;
    font-family: Arial, Helvetica, sans-serif;
    outline: 0;
}

body {
    display: grid;
    place-items: center;
    min-height: 90vh;
}

.container {
    width: 200px;
    height: 400px;
    border: 1px solid black;
    text-align: center;
    text-transform: capitalize;
    list-style: none;
    font-size: 20px;
}

li {
    padding: 10px;
    border-bottom: 1px solid black;
    cursor: pointer;
}

li, .box {
    transition: .25s;
}

li:hover, .box:hover {
    background-color: #f1f1f1;
}

.list-item-1:hover + .dropdown {
    display: grid;
}

.dropdown {
    display: none;
    padding: 10px 50px;
    border-bottom: 1px solid black;
}

.dropdown:hover {
    display: grid;
}

.box {
    padding: 5px 0;
    border-bottom: 1px solid black;
    cursor: pointer;
}

.box:last-child {
    border-bottom: 0;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <ul class="container">
        <li class="list-item-1">something</li>
        <div class="dropdown">
            <div class="box">link 1</div>
            <div class="box">link 1</div>
            <div class="box">link 1</div>
            <div class="box">link 1</div>
        </div>
        <li class="list-item-1">something</li>
        <div class="dropdown">
            <div class="box">link 1</div>
            <div class="box">link 1</div>
            <div class="box">link 1</div>
            <div class="box">link 1</div>
        </div>
    </ul>
</body>
</html>

Upvotes: 0

Views: 41

Answers (3)

Gildas.Tambo
Gildas.Tambo

Reputation: 22643

you can use max-height to deal with that kind of things

Here is the animation with your current structure

 * {
        margin: 0;
        padding: 0;
        font-family: Arial, Helvetica, sans-serif;
        outline: 0;
    }

    body {
        display: grid;
        place-items: center;
        min-height: 90vh;
    }

    .container {
        width: 200px;
        height: 400px;
        border: 1px solid black;
        text-align: center;
        text-transform: capitalize;
        list-style: none;
        font-size: 20px;
    }

    li {
        padding: 10px;
        border-bottom: 1px solid black;
        cursor: pointer;
    } 

    li:hover, .box:hover {
        background-color: #f1f1f1;
    }

    .list-item-1:hover + .dropdown {
        display: grid;
        max-height:999px;
        padding: 10px 50px;
        border-bottom: 1px solid black;
    }

    .dropdown {
        overflow:hidden;
        max-height:0em;
        transition: max-height .25s ease;
    }

    .dropdown:hover {
        display: grid;
    }

    .box {
        padding: 5px 0;  
        border-bottom: 1px solid black;
        cursor: pointer;
    }
    .box:first-child {  
        margin-top: 10px; 
    }

    .box:last-child {
        border-bottom: 0;
    }
<ul class="container">
    <li class="list-item-1">something</li>
    <div class="dropdown">
        <div class="box">link 1</div>
        <div class="box">link 1</div>
        <div class="box">link 1</div>
        <div class="box">link 1</div>
    </div>
    <li class="list-item-1">something</li>
    <div class="dropdown">
        <div class="box">link 1</div>
        <div class="box">link 1</div>
        <div class="box">link 1</div>
        <div class="box">link 1</div>
    </div>
</ul>

but i think you may want to change it to something like this

* {
    margin: 0;
    padding: 0;
    font-family: Arial, Helvetica, sans-serif;
    outline: 0;
}

body {
    display: grid;
    place-items: center;
    min-height: 90vh;
}

.container {
    width: 200px;
    height: 400px;
    border: 1px solid black;
    text-align: center;
    text-transform: capitalize;
    list-style: none;
    font-size: 20px;
}
[class^="list-item"],
.dropdown li {
    padding: 10px;
    border-bottom: 1px solid black;
    cursor: pointer;
} 

.box:hover {
    background-color: #f1f1f1;
}

.list-item-1:hover .dropdown {
    display: grid;
    max-height:999px;
}

.dropdown {
    overflow:hidden;
    max-height:0em;
    transition: max-height .25s ease;
}

.dropdown:hover {
    display: grid;
}

.box {
    padding: 5px 0;  
    border-bottom: 1px solid black;
    cursor: pointer;
}
.box:first-child {  
    margin-top: 10px; 
}

.box:last-child {
    border-bottom: 0;
}
<ul class="container">
    <li class="list-item-1">something
        <ul class="dropdown">
            <li class="box">link 1</li>
            <li class="box">link 1</li>
            <li class="box">link 1</li>
            <li class="box">link 1</li>
        </ul>
    </li>
    <li class="list-item-1">something
        <ul class="dropdown">
            <li class="box">link 1</li>
            <li class="box">link 1</li>
            <li class="box">link 1</li>
            <li class="box">link 1</li>
        </ul>
    </li>
</ul>

Upvotes: 0

Errold Tumaque
Errold Tumaque

Reputation: 11

You have use height instead of display if you want a slide down animation. It will transition from 0px to a set or variable height. Using display will just make it appear and disappear.

Upvotes: 1

BugsArePeopleToo
BugsArePeopleToo

Reputation: 2996

You have not declared a property to transition. From MDN Docs:

div {
  transition: <property> <duration> <timing-function> <delay>; 
}

Keep in mind not all properties can be transitioned.

Resource: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions

Upvotes: 1

Related Questions