Home > Back-end >  How can I write a Heading without influencing my navigationbar on the side of the page?
How can I write a Heading without influencing my navigationbar on the side of the page?

Time:10-23

my problem is that the heading "greeting" influences the position of my navigation bar.
I want it to stay at the top right corner of the screen but the heading moves it down a bit, which is inconvenient.
Could someone please give me some advice?
I thank you in advance
here the html code:

    <!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" href="style.css">  
    </head>
    <h1 id = "greeting"> Hello </h1>
    <body>
        <ul >
            <li><a  href="about.html"> About me </a></li>
        </ul>
    </body>
</html>

and the css code:

@font-face {
    font-family: Terminal;
    src: url(Fonts/Terminal.ttf);
}

    body {
    background-color: #333333;
}

#greeting {
    text-align:center;
    font-family:Terminal;
}

.navBar {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 200px;
    background-color: black;
    height: 5000px;
    z-index: 0;
  }
  
.navLinks {
    display: block;
    color: lime;
    padding: 8px 16px;
    text-decoration: none;
    font-family: Terminal;
}
  
  
.navLinks:hover {
    background-color: lime;
    color: black;
}

* {
    padding: 0px;
    margin: 0px;
}

CodePudding user response:

you can simply give your navigation bar an "absolute" position and set its top:0

@font-face {
    font-family: Terminal;
    src: url(Fonts/Terminal.ttf);
}

    body {
    background-color: #333333;
}

#greeting {
    text-align:center;
    font-family:Terminal;
}

.navBar {
    list-style-type: none;
    top: 0;
    position: absolute;
    margin: 0;
    padding: 0;
    width: 200px;
    background-color: black;
    height: 5000px;
    z-index: 0;
  }
  
.navLinks {
    display: block;
    color: lime;
    padding: 8px 16px;
    text-decoration: none;
    font-family: Terminal;
}
  
  
.navLinks:hover {
    background-color: lime;
    color: black;
}

* {
    padding: 0px;
    margin: 0px;
}

CodePudding user response:

You need to wrap the navbar and the content (contains H1) with a flex parent:

<!DOCTYPE html>
<html>
<head>
  <link rel="stylesheet" href="style.css" />
</head>

<body>
  <div >
    <ul >
      <li><a  href="about.html"> About me </a></li>
    </ul>
    <div >
      <h1 id="greeting">Hello</h1>
    </div>
  </div>
</body>
</html>

And then apply this CSS to them:

.flex-grid {
  display: flex;
}
.main-content {
  flex-grow: 1;
}
.main-content h1 {
  text-align: center;
}
  • Related