Home > front end >  Issues I have with making a navbar in CSS
Issues I have with making a navbar in CSS

Time:02-14

I tried adding a navbar, and I use this.

.topnav {
  background-color: rgb(0, 0, 0);
  overflow: hidden;
  width: 1920;
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.topnav a {
  position: relative;
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 7px 16px;
  text-decoration: none;
  font-size: 17px;
  transition: .5s;
  border-radius: 30px;
}

.topnav a:hover {
  background-color: #b0fcbd;
  color: black;
  border-radius: 30px;
  padding: 7px 16px;
  opacity: 70%;
}

.topnav a.active {
  background-color: #21ff46;
  color: black;
  border-radius: 30px;
}
<div >
  <a  href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
</div>

Yet, the navbar (the blackest part - the background is a bit lighter) starts out a few pixels below and to the right of when the page starts. Also, the Links are in the complete middle. I just want them centered at the top. Hope someone can help.

The webpage.

CodePudding user response:

To fix your positioning issue, just set margin: 0; on body.

To align the navbar items at the top, you can set align-items: start; on .topnav.

body {
  margin: 0;
}

.topnav {
  background-color: rgb(0, 0, 0);
  overflow: hidden;
  width: 1920;
  height: 200px;
  display: flex;
  align-items: start;
  justify-content: center;
}

.topnav a {
  position: relative;
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 7px 16px;
  text-decoration: none;
  font-size: 17px;
  transition: .5s;
  border-radius: 30px;
}

.topnav a:hover {
  background-color: #b0fcbd;
  color: black;
  border-radius: 30px;
  padding: 7px 16px;
  opacity: 70%;
}

.topnav a.active {
  background-color: #21ff46;
  color: black;
  border-radius: 30px;
}
<div >
  <a  href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
</div>

CodePudding user response:

You need to add reset css to your page. Glad it help!

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

#app {
 min-height: 100vh;
 width: 100vw;
 background: #0f1114;
}

.topnav {
  background-color: rgb(0, 0, 0);
  overflow: hidden;
  width: 1920;
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.topnav a {
  position: relative;
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 7px 16px;
  text-decoration: none;
  font-size: 17px;
  transition: 0.5s;
  border-radius: 30px;
}

.topnav a:hover {
  background-color: #b0fcbd;
  color: black;
  border-radius: 30px;
  padding: 7px 16px;
  opacity: 70%;
}

.topnav a.active {
  background-color: #21ff46;
  color: black;
  border-radius: 30px;
}
<div id="app">
  <div >
    <a  href="#home">Home</a>
    <a href="#news">News</a>
    <a href="#contact">Contact</a>
    <a href="#about">About</a>
  </div>
</div>

CodePudding user response:

setting your width and height to 100% worked for me instead of manually setting them to a weird pixel state. try this:

.topnav {
    background-color: rgb(0, 0, 0);
    overflow: hidden;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  
  .topnav a {
    position: relative;
    float: left;
    color: #f2f2f2;
    text-align: center;
    padding: 7px 16px;
    text-decoration: none;
    font-size: 17px;
    transition: 0.5s;
    border-radius: 30px;
  }
  
  .topnav a:hover {
    background-color: #b0fcbd;
    color: black;
    border-radius: 30px;
    padding: 7px 16px;
    opacity: 70%;
  }
  
  .topnav a.active {
    background-color: #21ff46;
    color: black;
    border-radius: 30px;
  }
<html>
<head>
<body>
  <div >
        <a  href="#home">Home</a>
        <a href="#news">News</a>
        <a href="#contact">Contact</a>
        <a href="#about">About</a>
       </div>
</body>
</head>
</html>

CodePudding user response:

Is this what you were looking for?

The extra pixels at the top / around the page are part of the default css that different browsers set

You can remove that with margin:0 on body

.topnav {
  background-color: rgb(0, 0, 0);
  overflow: hidden;
  height: 200px;
  display: flex;
  align-items: start;
  justify-content: center;
}

body {
  margin:0;
}

.topnav a {
  position: relative;
  float: left;
  color: #f2f2f2;
  text-align: center;
  padding: 7px 16px;
  text-decoration: none;
  font-size: 17px;
  transition: .5s;
  border-radius: 30px;
}

.topnav a:hover {
  background-color: #b0fcbd;
  color: black;
  border-radius: 30px;
  padding: 7px 16px;
  opacity: 70%;
}

.topnav a.active {
  background-color: #21ff46;
  color: black;
  border-radius: 30px;
}
<div >
  <a  href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
  <a href="#about">About</a>
</div>

  • Related