Ways to properly align a navigation bar at the center of the webpage while maintaining the

Whenever I zoom in or out on my page, the homepage button loses its color and fails to expand to the right side. So, I have two main goals:

1) To center the selected area (highlighted by a red border in the image).

2) To extend the green color of the homepage all the way to the end of the page.

Apologies for any language barriers. Feel free to ask if anything is unclear in my question.

View My Objective

My Current Situation

The Frustration I'm Facing

HTML Code

<!DOCTYPE html>
<html>
  <head>
    <title>Infusion</title>
    <link rel="stylesheet" type="text/css" href="infusion.css">
    <meta charset="utf-8"/>
    <link href="https://fonts.googleapis.com/css?family=Hind" rel="stylesheet">
  </head>
  <body>
    <div class="navbar">
      <ul class="unlist">
        <a href="#"><li class="homepage"><p class="parhome">infusion</p></li></a>
        <a href="#"><li class="nav-elements">design folio</li></a>
        <a href="#"><li class="nav-elements">services</li></a>
        <a href="#"><li class="nav-elements">our business</li></a>
        <a href="#"><li class="nav-elements">how we help</li></a>
        <a href="#"><li class="nav-elements">take the tour</li></a>
        <a href="#"><li class="nav-elements">contact</li></a>
      </ul>
    </div>
  </body>
</html>

CSS Code

body, html {
  margin: 0;
  padding: 0;
}
.navbar {
  width: 2000px;
  margin: 0 auto;
}
.unlist {
  display: flex;
  margin: 0;
  padding: 0;
}
.homepage {
  background-color: #63C6AE;
  display: inline-block;
  list-style: none;
  width: 360px;
  height: 70px;
  color: white;
  font-size: 25px;
  text-align: right;
  vertical-align: middle;
  line-height: 75px;
}
.parhome {
  height: 50px;
  padding: 0;
  margin-right: 30px;
  margin-bottom: 0;
  margin-top: 0;
}
.parhome:hover {
  color: #586165;
}
.unlist.a:first-child {
  width: 148px;
}
.nav-elements {
  display: inline-block;
  list-style: none;
  height: 70px;
  padding-right: 25px;
  padding-left: 25px;
  text-align: center;
  vertical-align: middle;
  line-height: 70px;

}
.unlist a {
  text-decoration: none;
  color: #63C6AE;
  text-transform: uppercase;
  font-family: 'Hind', sans-serif;
  font-weight: bold;
}
.inlist, a:hover {
  color: #586165;
}

Answer №1

Putting a p tag inside an li element is not valid HTML practice. Instead, consider using a span.

In addition, take note of the CSS rule that restricts the width of your navbar to .navbar { width: 2000px; [...] }.

As you zoom out, the 2000px set for the navbar may appear smaller than expected due to the zoom level, causing the left border of the green area to align with the start of the zoomed 2000px.

To address this issue, adjust the navbar width setting to 100%.

Answer №2

There are various methods you can utilize to achieve your desired outcome. However, most of these methods require a bit of tweaking or "hacking." In other words, the solution I am about to propose is not the most elegant way to handle things.

The main challenge here is that your goal involves two specific tasks:

  1. Centering the navbar
  2. Expanding the homepage container all the way to the left with a green background

Attempting to tackle both of these tasks simultaneously can lead to conflicts that are tough to resolve, especially if you want your page to remain responsive.

Prior to making any changes, as pointed out by @Johannes, it's worth noting that there are elements in your code that are invalid. Therefore, I have made some adjustments to your code to the best of my ability.

Here is the approach I recommend:

body, html {
  margin: 0;
  padding: 0;
}
.navbar {
    width: 100%;
    padding: 0;
}
.cont {
    width: 1280px;
    display: flex;
    margin: 0 auto;
    height: 70px;
}
.unlist {
    display: flex;
    margin: 0;
    padding: 0;
}
.homepage {
    background-color: #63C6AE;
    display: inline-block;
    list-style: none;
    width: 360px;
    height: 70px;
    color: white;
    font-size: 25px;
    text-align: right;
    vertical-align: middle;
    width: 1200px;
    margin: 0;
    margin-left: -950px;
}
ul.homepage a {
    color: #fff;
}
.parhome {
  height: 50px;
  padding: 0;
  margin-right: 30px;
  margin-bottom: 0;
  margin-top: 0;
}
.parhome:hover {
  color: #586165;
}
.unlist.a:first-child {
  width: 148px;
}
.nav-elements {
    display: inline-block;
    list-style: none;
    height: 70px;
    padding-right: 20px;
    padding-left: 20px;
    text-align: center;
    vertical-align: middle;
    line-height: 70px;
}
.unlist a {
  text-decoration: none;
  color: #63C6AE;
  text-transform: uppercase;
  font-family: 'Hind', sans-serif;
  font-weight: bold;
}
.inlist, a:hover {
  color: #586165;
}
<link href="https://fonts.googleapis.com/css?family=Hind" rel="stylesheet">
<div class="navbar">
<div class="cont">
<ul class="homepage">
<li class="nav-elements parhome"><a href="#">infusion</a></li>
</ul>
<ul class="unlist">
<li class="nav-elements"><a href="#">design folio</a></li>
<li class="nav-elements"><a href="#">services</a></li>
<li class="nav-elements"><a href="#">our business</a></li>
<li class="nav-elements"><a href="#">how we help</a></li>
<li class="nav-elements"><a href="#">take the tour</a></li>
<li class="nav-elements"><a href="#">contact</a></li>
</ul>
</div>
</div>

IMPORTANT: Feel free to experiment with values like `margin-left` and `width` for precise adjustments.

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

The sidebar.querySelector method is not working as expected

Attempting to assign an 'active' class to the clicked 'nav-link' element in order for it to stay active on the next page the user navigates to. Encountering an issue with sidebar.getElementsByClassName is not a function showing up in t ...

Switch the selected option in JQuery UI dropdown using a clickable button

I have a code snippet that is almost working. My goal is to change the selection of a JQuery dropdown select combobox using a separate button named "next". What I want is for the JQuery dropdown to automatically switch to the next selection every time I c ...

Creating HTML code from a template using different programs

Currently, I am in the process of developing a website using only HTML, CSS, and JS without the need for a backend. It is a straightforward site designed for presenting information. Each page follows a standard template consisting of a header, content area ...

Struggling to connect CSS to HTML on Github Pages

I'm new to using Github, and I noticed that when I open the website, my HTML code is displaying without the associated CSS. Here is a snippet of my HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

I am encountering a horizontal scroll bar despite setting the width to 100%

I am just starting out as a beginner in web development. I decided to create a webpage to practice my HTML and CSS skills. However, when I set the width of all elements to 100%, I noticed that I am getting a horizontal scroll bar. I have tried troubleshoot ...

Why using the display:none property does not successfully conceal Struts 2 tags such as <s:textfield>

I am curious as to why the div tag is unable to hide the Struts2 tags. I have set up a div that should be hidden on load, and using onChange, I am triggering jQuery to toggle the visibility of the div tag... <%@ taglib prefix="s" uri="/ ...

Can we stop a specific container from resizing on mobile devices?

My goal is to have the entire page scale down, except for the navigation container. I'm adjusting some CSS rules to improve accessibility on smartphones, but the issue arises with the multiple images it uses. I need these images to display in their or ...

Tips for displaying diverse content in a DIV container when users click on various sections of an image

Apologies for the unclear title, but I'm struggling to explain my technical goal using jargon. The basic concept is this: there will be an image on the webpage, and when a user clicks on a specific area of the image, relevant information will appear ...

I am having trouble getting Google Maps to load

Why does the map on my website only load when the browser window is resized? Below is the JavaScript code being used: <div class="map-cont"> <div id="map" class="location-container map-padding" style="width:100%;height:400px;background:y ...

Eliminate any blank spaces in the SELECT Angular application for IE-CSS

One issue I encountered is removing the default selected "SELECT" option from a select drop down on my webpage. Currently, I am using to remove it successfully in Chrome and Firefox browsers, but unfortunately IE does not respond to this method. I ha ...

Retrieve container for storing documents in JavaServer Pages

Previously, I created JSP and HTML code to upload a file from the hard disk into a database using <input type="file" name="upfile"> However, a dialog box with an "Open" button is displayed. What I am looking for is a "Save" button that will allow u ...

What is the best way to delete HTML classes that were generated by a function?

Currently, I'm immersed in the Etch A Sketch project as part of my journey through The Odin Project. Using DOM manipulation, I successfully created a grid and displayed it on the screen. Now, my aim is to allow users to resize the grid by removing the ...

tainted ajax outcome

Check out this HTML page I am using: <html> <head> <title>AJAX Example</title> <meta http-equiv="Content-Type" content="text/html"; charset="iso-8859-1"> </head> <script language="JavaScript" src="ajaxlib.js"> ...

"Android Webview's evaluateJavascript function is not returning the expected value

I am attempting to retrieve the username from a webview, but it is returning a null object. webView.settings.javaScriptEnabled = true webView.evaluateJavascript( "(function() { return document.getElementsByClassName('lgn-loginname') })() ...

What is the best way to showcase AJAX data within a complex HTML structure?

I need assistance with displaying the JSON output in my HTML code. My current HTML code is quite complex, so I am unsure how to include this data and repeat the HTML section for every JSON element. Can you provide guidance on how to achieve this? HTML COD ...

The input field cannot accommodate the lengthy value in the Mat Select option

When a user selects a value in my mat select, it doesn't display well in the selection box. The text wraps when the selection is opened, but once a choice is made, it gets cut off without proper spacing between the ellipses and the dropdown arrow. Th ...

Tips for emphasizing a chosen hyperlink within a jQuery Mobile panel

I am currently working on a jquery mobile page that includes a navigation menu in a sliding panel. While the functionality is good, I am looking to enhance the user experience by highlighting the selected link in the navigation. Typically, I would utilize ...

Unable to alter the background color of the text box

I am currently struggling with my code where I am trying to overlay a text box on an image. Even after setting the background color to white, it still shows up as transparent. I have successfully done this in the past, but for some reason, it is not work ...

Displaying a pop-up window containing information retrieved from the console output

Upon user input in the form on my web application, an scp and ftp process is initiated that takes around 2-3 minutes to complete. The result page consequently experiences a similar delay in loading. To enhance user experience and provide real-time updates ...

CSS Challenge: How to crop an image without using its parent container directly

I'm currently facing a complex CSS challenge that I can't seem to solve. I want to create an image controller (two-by-two layout on two lines) that can display: The top-left image in full size, The top-right with horizontal scrolling, The botto ...