Why is my <li> not centered and my navbar appears 'transparent' when I scroll through in HTML CSS?

https://i.sstatic.net/Qa2iS.png I am looking to achieve a solid navbar with centralized workouts and diets.

https://i.sstatic.net/4bUlX.png I attempted to create a navbar, but when I added a search bar, it caused my "Workouts" and "Diets" to be off-center. I'm not sure why this is happening. Removing the search bar fixes the issue, but adding it back causes the list to shift to the left. Additionally, my navbar appears transparent, and when scrolling through the website, the words seem to overlap with the navbar. Can anyone advise me on why this is happening? Apologies if this question seems basic, I'm new to HTML and CSS.

#navbar {
  position: fixed;
  top: 0;
  width: 100%;
  overflow: auto
}

.container1 {
  background-color: white;
  border: 1px solid black;
  max-height: 30px;
}

.container2 {
  background-color: white;
  border: 1px solid black;
  max-height: 40px;
}

a {
  text-decoration: none;
  color: black;
}

a:visited {
  text-decoration: none;
  color: black;
}

#brand {
  margin: 0;
  padding: 0;
  top: 29px;
  left: 20px;
  font-size: 30px;
  float: left;
  position: fixed;
  font-family: Roboto, sans-serif;
  font-weight: 300;
  font-style: italic;
}

#brand:hover {
  color: white;
  background-color: black;
}

ul {
  list-style-type: none;
  padding: 0;
  margin: 2px;
  text-align: center;
}

li {
  color: black;
  display: inline;
}

li a {
  font-family: Roboto, sans-serif;
  font-style: bold;
  padding: 2px;
  font-size: 20px;
}

li a:hover {
  background-color: #111;
  color: white;
}

.search {
  margin: 0;
  padding: 0;
  float: right;
}

#avatar {
  text-align: center;
  margin-top: -3px;
  padding-top: 5px;
}

#avatar1 {
  max-height: 25px;
  max-width: 25px;
}
<head>
<meta charset="UTF-8">
<title>Welcome to Smart Sports!</title>
<link type="text/css" rel="stylesheet" href="Sportshome.css">
<link href="https://fonts.googleapis.com/css?family=Roboto:700" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Roboto:300i" rel="stylesheet">
<link href='navbar.css' rel='stylesheet' type='text/css'>
<div id="navbar">
    <div class="container1">
        <p id="avatar">
        <img src="avatar1.png" id="avatar1">
        </p>
    </div>
    <div class="container2">
        <a href="Sportshome.html" id="brand">Sports. Sweat. Smart.</a>
        <p><input type="text" class="search"></p>
        <ul>
            <li><a href="workout.html">Workouts</a></li>
            <li><a href="diet.html">Diets</a></li>
        </ul>
    </div>
</div>
</head>

Answer №1

You can easily accomplish this task by utilizing the power of flexbox. Below is the updated CSS and HTML code:

.navbar {
  position: fixed;
  top: 0;
  width: 100%;
  overflow: auto
}

.nav-container1 {
  background-color: white;
  border: 1px solid black;
  max-height: 30px;
}

.nav-container2 {
  background-color: white;
  border: 1px solid black;
  max-height: 40px;
  display: flex;
  align-items: center;
}

.nav-container2 div {
  width: 100%;
}

.link {
  text-decoration: none;
  color: black;
}

.link:visited {
  text-decoration: none;
  color: black;
}

.brand {
  margin: 0;
  padding: 0;
  font-size: 20px;
  font-family: Roboto, sans-serif;
  font-weight: 300;
  font-style: italic;
}

.brand:hover {
  color: white;
  background-color: black;
}

.list {
  list-style-type: none;
  padding: 0;
  margin: 2px;
  text-align: center;
}

.list-item {
  color: black;
  display: inline;
}

.list-item a {
  font-family: Roboto, sans-serif;
  font-style: bold;
  padding: 2px;
  font-size: 20px;
}

.list-item a:hover {
  background-color: #111;
  color: white;
}

.search {
  margin: 0;
  padding: 0;
  float: right;
}

.avatar {
  text-align: center;
  margin-top: -3px;
  padding-top: 5px;
}

.avatar-img {
  max-height: 25px;
  max-width: 25px;
}
<html>


<head>
  <meta charset="UTF-8">
  <title>Welcome to Smart Sports!</title>
  <link type="text/css" rel="stylesheet" href="Sportshome.css">
  <link href="https://fonts.googleapis.com/css?family=Roboto:700" rel="stylesheet">
  <link href="https://fonts.googleapis.com/css?family=Roboto:300i" rel="stylesheet">
  <link href='navbar.css' rel='stylesheet' type='text/css'>
  <div class="navbar">
    <div class="nav-container1">
      <p class="avatar">
        <img src="avatar1.png" class="avatar-img">
      </p>
    </div>
    <div class="nav-container2">
      <div>
        <a href="Sportshome.html" class="brand">Sports. Sweat. Smart.</a>
      </div>

      <div>
        <ul>
          <li><a href="workout.html">Workouts</a></li>
          <li><a href="diet.html">Diets</a></li>
        </ul>
      </div>
      <div>
        <input type="text" class="search">
      </div>
    </div>
  </div>
</head>

Note: It's recommended to use classes instead of Id's for better control and maintainability.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Adjust the language of the submit and reset button text based on a variable

I'm facing a simple question but I'm stuck right now... and I was hoping someone could assist me. My issue is as follows: I need to switch the values of two buttons, reset and submit, from Greek to English and vice versa based on a variable retri ...

Creating a "briefing" iframe at the top of a webpage

I'm looking for a way to allow users to embed my iframe at a specific size, like 100x100, and then have it resize dynamically to fit the page size when they click on it. I want this functionality to work regardless of how the HTML embedding the iframe ...

Creating a CSS Box Along the Border of Another Box: A Step-by-Step Guide

Is there a way to render a box on another box's border, similar to the example image here: Expected Output I attempted using flexbox to achieve this, but unfortunately wasn't successful in finding a solution. If you have any advice or suggestio ...

Controlling opacity with jQuery animate() function using a click event

I have a specific requirement for an animation. When the button is clicked, I need the element to transition from 0 opacity to full 1 opacity within 300 milliseconds. The issue I am facing is that when the button is clicked, the animation does not work a ...

Prevent web browsers from interpreting media queries

Creating a responsive website has been challenging, especially when it comes to accommodating IE7. I'm considering turning off ALL media queries for just IE7 while maintaining the styling and layout in desktop mode. Is this possible? ...

The feature to select a cell on a Bootstrap table is not functioning as expected

Is there a way to retrieve the Cell value from a Bootstrap table when clicked? I have a method set up, but for some reason, after clicking on the table heading, the tdOnClick() function stops working. It seems to only be functional before clicking on the t ...

Having trouble with Vue.js not returning HTML elements from methods properly?

I have been attempting to retrieve html elements from a method, and I thought of using v-html for this purpose (not sure if there is a better approach). However, I seem to have encountered an issue with backtick templates and string interpolation. An error ...

The div is repositioned without the use of padding or margin properties

I am facing an issue with my Next.JS application where a div is not aligning properly at the top of the page. Despite setting padding and margin to 0 for body, HTML, and the div itself, it seems to be slightly off. I have checked all components thoroughly ...

Why does the order of CSS styling impact my design outcome?

Within my CSS file, I have defined the following styles: .myDiv{ float:left; width:100px; height:100px; } .yellow{ background:#faf8c7; } .lightGrey{ background:#f8f8f8; } In my HTML code: <div class="myDiv lightGrey yellow">& ...

Ways to center Bootstrap panel in the middle of a webpage

Is there a way to center this entire panel on the web page? I could use some guidance with this. Does anyone have recommendations for a good book to learn Bootstrap design? <div class="panel panel-default" style="max-width:500px;margin-left:auto;margi ...

What is the method for inserting the document URL into a text input?

Can someone provide guidance on grabbing the top URL and inserting it into a text input field? I've tried the following method, but it's not working. Any suggestions or ideas? Additionally, is there a way to make this text input field uneditable? ...

Hovering over a link causes malfunction in CSS drop-down menu

I have been struggling for hours to make this drop-down menu work, but it just won't cooperate. I want the list menu .dropdown to appear when you hover over .droptoggle. I'm including the entire page because I can't figure out what's wr ...

Having trouble getting items to align properly in Bootstrap

As a student, I am struggling with my development skills while attempting to create a personal website. I am encountering difficulty aligning items on my rows in bootstrap. Here is what I am trying to accomplish: 1. However, I am currently stuck at this po ...

The fixed position setting does not anchor the elements to the bottom of a container

When applying the following styles: #fullpage-menu > .gradient { position: fixed; bottom: 0; left: 0; width: 100%; height: 0.3rem; } To the element with ID #fullpage-menu, which is styled as follows: #fullpage-menu { height: 100 ...

Can both Bootstrap 5 scroll-spy methods be utilized simultaneously on a single webpage?

I have a requirement for my website to have 2 navigation bars and also 2 sets of navigation links. I would like to implement scroll-spy functionality on both navigation bars, even though only one will be visible at a time. The issue I am facing is that w ...

Tools for generating Xpath or CSS selectors on Firefox and Chrome browsers

Struggling with finding helpful plugins for my Selenium webdriver development. Firebug for FF and Selenium IDE are not working for me. Despite trying multiple plugins for both FF & Chrome, I can't find anything to generate xpath or CSS strings. Lookin ...

Retrieving a pair of data points from a Vue <select> event when it changes

I am facing an issue with a dropdown menu that is using an array with key:value pairs. I want the dropdown to only show values, but when a selection is made, I need to pass both the value and the key using the @change event. <select @change=" ...

Move a div by dragging and dropping it without creating duplicates

I'm in need of some assistance. It seems like a simple task, but I'm having trouble finding a solution. I've managed to create draggable elements that work smoothly without any issues. The drag and drop functionality is working perfectly. ...

Having trouble invoking PHP functions from AngularJS

Hey there, I'm a complete beginner in web development. I have an HTML file with AngularJS code that calls a .php file, both located in my local folder. I've tried looking online, but: Installing a server is not possible as this will be used ...

The CSS3 rotation transform will rotate by an angle of n degrees

I'm currently working on a cube-like element made up of 4 divs that I am rotating using CSS3 transforms (limited to -webkit for a Chrome extension). But there's an issue, if you visit http://jsfiddle.net/CxYTg/ and click through "one", "two", "t ...