Skeptar
Skeptar

Reputation: 189

moving div with transition height

Last question for today :/ If someone hover over a link, the height should change. But the problem is now, the complete div moves. Here is the page: Page

Here is the code:

*{
    font-family: "Open Sans";
    margin: 0px;
    padding: 0px;
}

body{
    background: url("images/bg.png") repeat-x scroll left top #9EB5D6;
}

nav{
    text-align: right;
}

nav > ul{
    margin: 0px;
    padding: 0px;
}

nav ul > li{
    display: inline-block;
    list-style-type: none;
    margin-left: 10px;
}

nav ul li > a{
    display: block;
    text-decoration: none;
    color: black;
    padding: 10px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    background:linear-gradient( #FCFCFC, #C8CACB);
    border: 2px solid black;
    border-bottom: 0px;
    transition: padding-top 0.5s;
}

nav ul li > a:hover{
    background:linear-gradient( #C8CACB, #FCFCFC);
    padding-top: 15px;
}

.wrapper{
    margin-top: 100px;
    width: 70%;
    margin-right: auto;
    margin-left: auto;
}

.content{
    background-color: #E7E5BE;
    padding: 10px;
    border: 2px solid black;
}
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="index.css" >
        <!-- Open Sans -->
        <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    </head>
    <body>

        <div class="wrapper">
            <nav>
                <ul>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                </ul>
            </nav>
            <div class="content">
                <p>Content</p>
            </div>
        </div>
    </body>
</html>

I hope thats enough info.

Upvotes: 2

Views: 54

Answers (1)

Alexander O&#39;Mara
Alexander O&#39;Mara

Reputation: 60527

One option would be to use negative margins to negate the added padding.

Excerpt:

nav ul li > a{
    display: block;
    text-decoration: none;
    color: black;
    padding: 10px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    background:linear-gradient( #FCFCFC, #C8CACB);
    border: 2px solid black;
    border-bottom: 0px;
    transition: padding-top 0.5s, margin-top 0.5s;/* <-- transition margin */
}

nav ul li > a:hover{
    background:linear-gradient( #C8CACB, #FCFCFC);
    padding-top: 15px;
    margin-top: -15px;/* <-- negative margin */
}

Working Example:

*{
    font-family: "Open Sans";
    margin: 0px;
    padding: 0px;
}

body{
    background: url("images/bg.png") repeat-x scroll left top #9EB5D6;
}

nav{
    text-align: right;
}

nav > ul{
    margin: 0px;
    padding: 0px;
}

nav ul > li{
    display: inline-block;
    list-style-type: none;
    margin-left: 10px;
}

nav ul li > a{
    display: block;
    text-decoration: none;
    color: black;
    padding: 10px;
    border-top-left-radius: 10px;
    border-top-right-radius: 10px;
    background:linear-gradient( #FCFCFC, #C8CACB);
    border: 2px solid black;
    border-bottom: 0px;
    transition: padding-top 0.5s, margin-top 0.5s;
}

nav ul li > a:hover{
    background:linear-gradient( #C8CACB, #FCFCFC);
    padding-top: 15px;
    margin-top: -15px;
}

.wrapper{
    margin-top: 100px;
    width: 70%;
    margin-right: auto;
    margin-left: auto;
}

.content{
    background-color: #E7E5BE;
    padding: 10px;
    border: 2px solid black;
}
<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="index.css" >
        <!-- Open Sans -->
        <link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    </head>
    <body>

        <div class="wrapper">
            <nav>
                <ul>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                    <li><a href="#">Link 1</a></li>
                </ul>
            </nav>
            <div class="content">
                <p>Content</p>
            </div>
        </div>
    </body>
</html>

Upvotes: 1

Related Questions