Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Nav Bar niggles- 2 columns on page

Edrol97

Bronze Coder
Hi all, I have problems with a nav bar on a word game I've made. Currently the page is in two columns- one larger than the other. How do I ensure the nav bar is at the top of the page? Full code below and a screenshot of how it looks in practice.

HTML:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <title>Politicordle</title>
    <link rel="stylesheet" href="words.css" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  </head>
  <body>
    <div class="sunnavigationbar">
      <a href="Hi.html">Hi</a>
      <a href="about_me.html">Bio</a>
      <a href="Art.html">Show-Off</a>
      <a href="cv.html">CV</a>
      <a href="Inspa.html">Inspa</a>
      <a href="Games.html">Play</a>
    </div>
    <br />
    <br />
    <div class="wrapper">
      <h1>Guess the Word</h1>
      <div class="content">
        <input type="text" class="typing-input" maxlength="1" />
        <div class="inputs"></div>
        <div class="details">
          <p class="hint">Hint: <span></span></p>
          <p class="guess-left">Remaining guesses: <span></span></p>
          <p class="wrong-letter">Wrong letters: <span></span></p>
        </div>
        <button class="reset-btn">Reset Game</button>
      </div>
    </div>

    <script src="words.js"></script>
    <script src="wordsl.js"></script>
  </body>
</html>

CSS:
@font-face {
  font-family: Eliot Lord;
  src: url(Fonts/Eliothand2-Regular.otf);
}
body {
  display: flex;
  padding: 0 10px;
  min-height: 100vh;
  align-items: center;
  justify-content: center;
  font-family: Eliot Lord;
  cursor: url(img/background/cursors/Mouse%20cursor.png),
    url(img/background/cursors/pointer1.png), pointer;
}
.sunnavigationbar {
  background-color: #ec1801;
  padding: 20px 20px 20px;
  font-family: Helvetica;
  font-weight: 100;
  text-decoration: none;
}
.sunnavigationbar a {
  font-family: Helvetica;
  font-size: 2.6dvw;
  color: white;
  padding: 10px;
  text-decoration: none;
  display: inline-block;
}
header .sunnavigationbar a:hover {
  font-family: Helvetica;
  color: white;
  padding: 10px;
  font-weight: 900;
  font-size: 2.6dvw;
  float: none;
  text-decoration: none;
  display: inline-block;
  background-image: url(img/background/Underline%20White.png);
  background-repeat: no-repeat;
  background-size: 100% 20%;
  background-position: 0px 85%;
}
:hover {
  cursor: url(img/background/cursors/pointer1.png);
}
p {
  font-size: 80px;
}
.wrapper {
  width: 100%;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0);
}
.wrapper h1 {
  font-size: 25px;
  font-weight: 500;
  padding: 20px 25px;
}
.wrapper .content {
  margin: 25px 25px 35px;
}
.content .inputs {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}
.inputs input {
  font-family: Eliot Lord;
  height: 57px;
  width: 56px;
  margin: 4px;
  font-size: 24px;
  font-weight: 500;
  color: white;
  text-align: center;
  outline: none;
  background: url(img/background/Politicordle%20right.png) no-repeat left top;
  pointer-events: none;
  border: white;
  background-size: 100% 100%;
  text-transform: uppercase;
}
.typing-input {
  opacity: 0;
  z-index: -999;
  position: absolute;
  pointer-events: none;
}
.inputs input:first-child {
  margin-left: 0px;
}
.content .details {
  margin: 20px 0 25px;
}
.details p {
  font-size: 30px;
  margin-bottom: 10px;
}
.content .reset-btn {
  font-family: Eliot Lord;
  width: 100%;
  border: none;
  cursor: pointer;
  color: #fff;
  outline: none;
  padding: 15px 0;
  font-size: 17px;
  border-radius: 5px;
  background: black;
  transition: all 0.3s ease;
}
.content .reset-btn:hover {
  background: #18a589;
}
@media screen and (max-width: 460px) {
  .wrapper {
    width: 100%;
  }
  .wrapper h1 {
    font-size: 22px;
    padding: 16px 20px;
  }
  .wrapper .content {
    margin: 25px 20px 35px;
  }
  .inputs input {
    height: 51px;
    width: 50px;
    margin: 3px;
    font-size: 22px;
  }
  .details p {
    font-size: 17px;
  }
  .content .reset-btn {
    padding: 14px 0;
    font-size: 16px;
  }
}
 

Attachments

  • Screenshot 2024-09-01 at 23.44.07.png
    Screenshot 2024-09-01 at 23.44.07.png
    160.9 KB · Views: 4
Last edited:
Solution
The reason your nav bar is centered is because you have align-items: center; set on your body tag. If you remove that, you should see your nav go to the top of the page.

I would recommend making a separate <div> container and applying your flex rules to that, rather than on the <body> tag. Also, you shouldn't use <br/> elements for spacing, that should be done with CSS.

If you want the navbar to always be pinned to the top of the screen, you can give it a position of fixed and set the top, left and right to 0 like so:

Code:
.sunnavigationbar {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
}

You can also give it a position of sticky instead of fixed if you find it's...
The reason your nav bar is centered is because you have align-items: center; set on your body tag. If you remove that, you should see your nav go to the top of the page.

I would recommend making a separate <div> container and applying your flex rules to that, rather than on the <body> tag. Also, you shouldn't use <br/> elements for spacing, that should be done with CSS.

If you want the navbar to always be pinned to the top of the screen, you can give it a position of fixed and set the top, left and right to 0 like so:

Code:
.sunnavigationbar {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
}

You can also give it a position of sticky instead of fixed if you find it's overlapping too much with your site's content. There's advantages and disadvantages to fixed vs sticky so one may work better for you than the other. I find sticky can sometimes be buggy but recommend you look into both fixed and sticky positioning if you always want the nav at the top of your page.
 
Solution
Position sticky is tricky. The element is 'stuck' in relation to it's parent container, so the sticky item should be a direct child and there needs to be enough content to allow it to actually stick while scrolling. May need position:relative on parent.
 

New Threads

Buy us a coffee!

Back
Top Bottom