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

Make sure PTable maintains a horizontal layout on any device with PrimeNG

When I view my Ptable on desktop or iPad, it looks like this: https://i.stack.imgur.com/ONqZV.png However, when I switch to a device like an iPhone X, it changes to this layout: https://i.stack.imgur.com/H2q7j.png I want the horizontal layout to displa ...

Revamping the hyperlinks in my collapsible menu extension

Is there a way to modify the links in this accordion drop menu so that they lead to external HTML pages instead of specific areas on the same page? I've tried various methods but can't seem to achieve it without affecting the styles. Currently, i ...

The v-select menu in Vuetify conceals the text-field input

How can I prevent the menu from covering the input box in Vuetify version 2.3.18? I came across a potential solution here, but it didn't work for me: https://codepen.io/jrast/pen/NwMaZE?editors=1010 I also found an issue on the Vuetify github page t ...

Is there a way to create a navigation menu that highlights as we scroll down the page?

Check out this example of what I am looking for. https://i.stack.imgur.com/fdzvZ.png If you scroll, you will notice that the left menu highlights. Sub-menus will extend when the corresponding menu is highlighted and collapse when other menus are highlig ...

Enable CDATA support in ActionView::Base.full_sanitizer

One method I found helpful was using the ActionView::Base.full_sanitizer.sanitize(value) to remove HTML content. It generally works well, but there is an issue when the value passed into the method is wrapped in because it returns a blank ...

Guide to incorporating vertical scrolling for a grid of items in Material UI

Is there a way to implement vertical scrolling in a grid container so that when the height of components exceeds a certain maximum, they can be scrolled through vertically? ...

Using Paypal API in PHP to create a payment form

<?php $action=$_REQUEST['action']; if ($action=="") { ?> <center> <form name="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post"> <input type="hidden" name="cmd" value="_xclick"> <input type=" ...

Retrieve information from the table and display it in a reverse order

Below is a link to view the table in my database: click here I want to retrieve and display the names of users whose user_group is "member". I plan to put their ratings in an array and then print their names in descending order based on their rating. For ...

Error: Invalid Argument - The argument 'PanelController' is expected to be a function, but it is undefined

Here is a snippet of my HTML code: <body ng-controller="StoreController as store"> ........... <section ng-controller="PanelController as panel"> <ul class="nav nav-pills""> <li ng-class="{active:panel.isSe ...

What is the best way to remove the hover effect from a specific element within a div?

I am looking to achieve a specific hover effect where the white part does not darken when hovering over a certain element within its child elements. Here is the HTML code I have: <div className= {css.searchBarDiv}> <div className={css.searchBar ...

Aptana Studio 3 fails to detect PHP code embedded within an .html file

When writing code, such as: <!DOCTYPE html> <html> <body> <?php echo "My first PHP script!"; ?> </body> </html> After <?php, I am encountering a red X icon error indicating that a ">" is missing at the end of the ...

Issue with Material Design Lite input floating label not functioning properly post page navigation

I am currently developing a mobile hybrid application using Material Design Lite, but I have run into a small issue. When I add input field elements to my pages, the floating text and placeholder do not function properly. In my application, I am utilizing ...

Verifying the timestamp of file submission in a form

My goal is to create an HTML form that allows users to send a file to my server, while also recording the exact time they initiated the file transfer. However, I'm facing an issue where only the time the file is received is being logged, rather than w ...

Choose the content in the second column

Can anyone help me with CSS selectors? I've been trying to target the second .col_content element in my code to apply a specific background image, but I haven't had any luck so far. Adding a second class would be an easy fix, but I'm curious ...

Having issues with contenteditable functionality not functioning properly on elements that are dynamically generated

Creating an unordered list dynamically and adding items to it on a button click. Appending it to a section with contenteditable set to true, but encountering issues. The code snippet below demonstrates the process: // Create text input var categoryInput = ...

When the browser window is resized, the footer starts to cover the content

To prevent the divs from overlapping when the browser window is resized vertically, take a look at the code snippet below: <html> <body> <style> #box { display: flex; flex-direction ...

What could be the reason behind the malfunctioning of my media query in the

I am trying to connect an external stylesheet to my React component in order to apply different styles based on screen width. Specifically, when the screen width is less than 300px, I want the logo to have a height of 100vh. However, it seems that the medi ...

Creating a Mondrian-inspired design using a combination of red, blue, and yellow within an HTML table

Is there anyone who can assist me in recreating this painting using HTML <table> tag and CSS within a browser? The image to replicate: https://i.stack.imgur.com/84y4I.jpg I attempted to complete this task, but my instructor is dissatisfied with th ...

Guidelines for retrieving a class name using jQuery when hovering over a div

I need assistance in fetching the class name by hovering over a div. Both divs have the same id but slightly different class names. Here is an example: <div id="1-someid" class="1-example-class border cz"> ...more elements go here.... </div> ...

What is the best way to use CSS to connect the tips of shapes for perfect alignment?

In an attempt to utilize CSS3 for creating shapes and aligning them to resemble a certain image, I have successfully crafted a pink square along with 4 gray rectangles. Initially, my approach involved creating separate div elements for each shape and adjus ...