Home > Back-end >  Navbar will not move into top right corner
Navbar will not move into top right corner

Time:04-17

So I am trying to have my navbar line up horizontally with some space between each link, but for some reason the about me hyperlink would stack on itself whenever i added padding to give each link some space. If possible, can someone tell me what I am doing wrong or missing because i am very confused at the moment. Please and thank you.

/*css reset*/body, header, nav, main, footer{
    margin: 0;
    padding: 0;
    border: 0;
}
*{
    box-sizing: border-box;
}

body{}
/*styles for the header area*/
header{
    height:180px;
    background: linear-gradient(to bottom, #d4c7d2, #fff)
}

header img{
    width: 13%;
    float: left;
}

header p{
    font-family: "Cinzel", sans-serif;
    font-size: 1.75em;
}


/*styles for nav area*/
nav{
    
    position: absolute;
    top: 0;
    right: 10%;
}

nav ul{
    display: block;
}

nav ul li{
    padding-right: 5%;
    display: inline-block;
    list-style: none;
}

nav ul li:last-child{
    padding-right: 40%;
}

nav ul a {
    color: black;
    font-size: 1.75em;
    text-decoration: none;
}


/*styles for footer area*/
footer{
    text-align: center;
    bottom: 0;
    position: absolute;
    width: 100%;
}
<!DOCTYPE html>

<html lang='en'>

<head>
    <title>Jay Photography</title>
    <link rel="stylesheet" type="text/css" href="../css/styles.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Cinzel&display=swap" rel="stylesheet">
</head>

<body>
    
    <!--content for the header area-->
    <header>
        <img src="../images/logo.png" alt="Jay photography logo"/><br />
        
        
    </header>
    
    <!--content for the nav area-->
    <nav  id="nav">
        <ul>
            <li><a href="index.html" target="_blank">Home</a></li>
            <li><a href="about.html" target="_blank">About Me</a></li>
            <li><a href="contact.html" target="_blank">Contact</a></li>
        </ul>
            
    </nav>
    
    <!--content for main area-->
    <main>
        
        
    <main>
        
    <!--content for the footer area-->
    <footer>
        <p>&copy;Copyrights 2022. All Rights Reserved.</p>
        <p><a href="mailto:[email protected]">[email protected]</a></p>
    </footer>
        
</body>

</html>

CodePudding user response:

Use flex for ul. also set a min-width for lis:

/*css reset*/body, header, nav, main, footer{
    margin: 0;
    padding: 0;
    border: 0;
}
*{
    box-sizing: border-box;
}

body{}
/*styles for the header area*/
header{
    height:180px;
    background: linear-gradient(to bottom, #d4c7d2, #fff)
}

header img{
    width: 13%;
    float: left;
}

header p{
    font-family: "Cinzel", sans-serif;
    font-size: 1.75em;
}


/*styles for nav area*/

nav{
    
    position: absolute;
    top: 0;
    right: 10%;
}
nav ul{
    display: flex;
}

nav ul li{
    padding-right: 5%;
    list-style: none;
    min-width: max-content;
}

nav ul li:last-child{
    padding-right: 40%;
}

nav ul a {
    color: black;
    font-size: 1.75em;
    text-decoration: none;
}


/*styles for footer area*/
footer{
    text-align: center;
    bottom: 0;
    position: absolute;
    width: 100%;
}
<!DOCTYPE html>

<html lang='en'>

<head>
    <title>Jay Photography</title>
    <link rel="stylesheet" type="text/css" href="../css/styles.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://fonts.googleapis.com/css2?family=Cinzel&display=swap" rel="stylesheet">
</head>

<body>
    
    <!--content for the header area-->
    <header>
        <img src="../images/logo.png" alt="Jay photography logo"/><br />
        
        
    </header>
    
    <!--content for the nav area-->
    <nav  id="nav">
        <ul>
            <li><a href="index.html" target="_blank">Home</a></li>
            <li><a href="about.html" target="_blank">About Me</a></li>
            <li><a href="contact.html" target="_blank">Contact</a></li>
        </ul>
            
    </nav>
    
    <!--content for main area-->
    <main>
        
        
    <main>
        
    <!--content for the footer area-->
    <footer>
        <p>&copy;Copyrights 2022. All Rights Reserved.</p>
        <p><a href="mailto:[email protected]">[email protected]</a></p>
    </footer>
        
</body>

</html>

CodePudding user response:

First of all you shouldn't make layouts with position: absolute, you need to learn flexbox. But to fix your nav you need to remove this:

nav ul li:last-child {
    padding-right: 40%;
}

Instead, to add some space on the right you can increase right property on nav. To "unstack" links you need to add some width to nav:

nav {
    width: 400px; /* new */
    position: absolute;
    top: 0;
    right: 10%;
}
  • Related