Position image/icon within navigation bar

I am currently working on a school project to create a webpage, but I am struggling to align an image/icon with the other buttons in the navbar. Despite my efforts to find a solution through Google searches, I have not been able to resolve this issue. Furthermore, I am restricted to using only HTML and CSS without tools like Bootstrap.

Below is the code for my page:

HTML code:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Zero Hunger</title>
    <link rel="stylesheet" href="css/master.css">
  </head>
  <body>
    <nav>
      <ul>
        <li><img class="nav-img" src="images/2-zero-hunger.png"></li>
        <li class="nav-button"><a href="index.html">Home</a></li>
        <li class="nav-button"><a href="contact.html">Contact</a></li>
        <li class="nav-button"><a href="game.html">Game</a></li>
      </ul>
    </nav>
    <div class="">
      <p>Hello World</p>
    </div>
  </body>
</html>

CSS code:

body{
  margin: 0;
  font-family: sans-serif;

}

/*Navbar*/
nav ul{
  margin: 0;
  padding: 0;
  text-align: center;
  background-color: #D09E37;
}
nav ul li{
  list-style: none;
  display:inline-block;

}
nav ul li.nav-button{
  padding: 0 15px;
}
nav ul li.nav-button:hover{
  background-color: #bf8000;
}
nav ul li a{
  text-decoration: none;
  font-size: 1.5em;
  color: white;
  line-height: 54px;
}
.nav-img{
  height: 58px;
  display: inline-block;
}

Answer №1

I made changes to the CSS code. I recommend utilizing the flex properties.

body{
  margin: 0;
  font-family: sans-serif;

}

/*Navbar*/
nav ul{
  margin: 0;
  padding: 0;
  text-align: center;
  background-color: #D09E37;
  
  display: flex; /*add*/
  justify-content: center; /*add*/
  align-items: center; /*add*/
}
nav ul li{
  list-style: none;
  display:inline-block;

}
nav ul li.nav-button{
  padding: 0 15px;
}
nav ul li.nav-button:hover{
  background-color: #bf8000;
}
nav ul li a{
  text-decoration: none;
  font-size: 1.5em;
  color: white;
  line-height: 54px;
}
.nav-img{
  height: 58px;
  /*display: inline-block;*/ /*remove*/
}
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8>
    <title>Zero hunger</title>
    <link rel="stylesheet" href="css/master.css">
  </head>
  <body>
    <nav>
      <ul>
        <li><img class="nav-img" src="images/2-zero-hunger.png"></li>
        <li class="nav-button"><a href="index.html">Home</a></li>
        <li class="nav-button"><a href="contact.html">Contact</a></li>
        <li class="nav-button"><a href="game.html">Game</a></li>
      </ul>
    </nav>
    <div class="">
      <p>Hello World</p>
    </div>
  </body>
</html>

Answer №2

Have you thought about utilizing the power of display: flex for the ul? By setting the align-items: center, you can ensure that the ul spans 100% width within its parent element, nav. To add a bit more flair, consider applying a background color to the nav and adding some spacing around each li element for a polished look.

Here is an example:

nav {
  background-color: coral;
  width: 100vw;
}
ul {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1.5rem;
  width: 100%;
}

Hopefully, this suggestion sparks some inspiration for addressing your design challenge!

Answer №3

If you want to position the image absolutely, you can use the following code:

.nav-img{ position: absolute; 
          top: 0;
          left: 34rem; }

Make sure to set the ul element to position:relative;

nav ul { 
         position: relative;
       }

Feel free to customize the values of top and left according to your requirements. Remember to leave the existing code unchanged.

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

Encountering an issue... invariant.js:42 Error: A `string` value was received instead of a function for the `onClick` listener

Currently, I am working on creating a form that allows users to build quizzes without any restrictions on the number of questions they must include. To achieve this, I plan to add an "add question" button at the bottom of the quiz form, allowing users to d ...

Guide to setting up a Datetime picker in Bootstrap 5 user interface

I've been struggling to implement a datetime picker for Bootstrap 5 (my front end is using Bootstrap 5.1.0). While I came across a solution for Bootstrap 3, unfortunately it doesn't seem to work with Bootstrap 5. ...

Scraping data from a website using Python by targeting the `<td

Just starting out with Python and Web Scraping... I'm trying to extract the numbers 1.16, 7.50, and 14.67 from the highlighted portion of code using td, class, table-matches__odds pageSoup.find_all, but so far haven't had any luck. Anyone have an ...

Implementing Multiple Identification using JavaScript and PHP

I need to complete a simple task. Here is the code snippet: echo' <div class="col-sm-12" id="recensioni_titolo"> <form role="form" id="review-form" method="post" action="php\insert_comment.php"> ...

Scroll sideways with AJAX content replacement

I've successfully discovered various methods for loading and replacing content with ajax, as well as ways to hide/show with effects. However, I'm now seeking a technique that allows the new content to slide into a div while the old content slide ...

The Angular tutorial for the "Tour of Heroes" is experiencing issues with aligning the heroes' list properly

I am currently working on the Angular tour of heroes tutorial. However, I am facing an issue when trying to display the list of heroes as it appears like this: It is strange because even though the CSS/HTML/TS code from the tutorial seems correct, the lis ...

Update the less mixin

I attempted to eliminate the border radius from all Bootstrap elements by creating a custom-mixins.less file with the following lines in it. My intention was for these lines to overwrite the original .border-radius mixin, but unfortunately, it did not work ...

It can be challenging to manage numerous if-else conditions in JQuery

I am having trouble with my JQuery code that checks multiple conditions. No matter what I enter, it always goes to the else condition. $(function() { $('#installment').on("keydown keyup", check); function check() { var inst = Number($( ...

Choose a particular element within an element class using a variable as the selector in jQuery

Is there a way to dynamically add a class to a specific element in an array based on a variable in a for loop, rather than random selection? I need to target elements with the variable provided and possibly apply the class to more than one element if neces ...

Fontawesome is unable to update the class due to the presence of invalid characters in the string

const toggleDarkOrLight = document.getElementsByTagName('i')[0]; var toggled = false; const toggleFunction = () => { if (toggled === false) { toggleDarkOrLight.classList.remove('fa fa-toggle-off'); toggleDarkOrLight.classLi ...

Tips for applying a jQuery class when the page is both scrolled and clicked

As I work on building a HTML website, I encountered an interesting challenge. I want to create a dynamic feature where, as users scroll through the page, certain sections are highlighted in the navigation menu based on their view. While I have managed to a ...

Error: AsyncPipe received an invalid argument of type '[object Object]'. This has caused an error due to an invalid pipe argument

I'm currently working on developing a contact management system that includes CRUD operations using Angular 8 and Spring Boot. Every feature is functioning correctly except for the search functionality. My goal is to search for data based on a specifi ...

Tips for achieving consistent outcomes on both IE and Chrome when utilizing the `min` attribute in HTML input tags

The display of input fields is inconsistent between IE (version 11) and Chrome (version 46.0.2490.71) when using the min option in the HTML input tag. In Chrome, the input fields appear extra long, while in IE they display correctly (same as when min is no ...

Issues with HTML5 tag <animate> in Internet Explorer 11

After getting the hang of some HTML basics, I decided to get creative and make an animation. Surprisingly, it works perfectly on my Firefox browser. However, when I tested it on IE 11, it failed to play the animation as expected. Everything is displayed bu ...

What is the best way to apply focus to a list element using Javascript?

I recently created code to display a list of elements on my webpage. Additionally, I implemented JavaScript functionality to slice the elements. Initially, my page displays 5 elements and each time a user clicks on the "show more" link, an additional 5 ele ...

Step-by-step guide on achieving 100% height for the <main> element in a Material UI drawer demo

I have a question regarding the Material UI drawer. In my project, I am trying to change the background color of the main tag. However, the main tag's height is restricted to the content inside. Check out this link for reference Setting the height t ...

Vue JS nested component does not appear in the document object model

I developed two separate VueJS components - one displaying a modal and the other featuring HTML input fields. When I attempt to nest these two components, the inner component fails to appear in the DOM. What could be causing this issue? Here's a snip ...

Arrange fixed-position elements so that they adhere to the boundaries of their adjacent siblings

Is there a way to keep two fixed elements aligned with their sibling element on window resize? <div class="left-img"> IMAGE HERE </div> <!-- fixed positioned --> <div class="container"> Lorem ipsum... </div> <div class=" ...

Initiate a click event on an anchor element with the reference of "a href"

I'm currently facing an issue with clicking a href element on my HTML website. The click event doesn't seem to be triggered. CODE: HTML CODE: <div class="button" id="info" ></div> <ul id="game-options"> <li><a ...

Paginator for Angular Material Cards

This section contains the TypeScript file for a component import { MatTableDataSource, MatPaginator } from "@angular/material"; import { FoodService } from "./food.service"; import { Food } from "./food"; @Component({ selec ...