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

What is the best way to choose the initial element in every group using jQuery?

If we have the following HTML input: <p>A</p> <p>B</p> <p>C</p> <div>D</div> <p>E</p> <p>F</p> <p>G</p> We can select B, C, F and G using the following code: $('p + ...

Checking for empty inputs using Html, JavaScript, and CSS

I need help with a form that has 6 input fields. I want to ensure all fields are filled out, and if not, display an alert message. Any suggestions on how to achieve this? Additionally, I'm experiencing difficulties implementing different font styles. ...

Every time I attempt to load the table, I encounter an error

Encountering errors when attempting to load the table: 'Uncaught ReferenceError: usersArray is not defined at loadUsers (trgames.js:20:17) at trgames.js:22:1' and 'Uncaught TypeError: Cannot set properties of null (setting ...

Ways to eliminate unused classes from JQuery UI

We have successfully implemented JQuery UI library for enhancing our interface. However, since we no longer need to support outdated browsers like IE6, classes such as .ui-corner-all, .ui-corner-top, .ui-corner-left, .ui-corner-tl are unnecessary and clu ...

Desktop media queries are functioning properly, yet experiencing issues on mobile devices

I've come across this same question multiple times, but the solutions I've found so far haven't been helpful. My media queries are working perfectly on desktop, but they don't seem to take effect on three different android devices. It&a ...

Prevent selection of specific weekdays in Kendo grid calendar

When editing a record in a Kendo grid with a date field, is there a way to only allow the selection of Monday and disable all other days of the week? ...

How do I incorporate pagination into a PL-SQL dynamic content table in Oracle Apex?

I've successfully created a PL-SQL dynamic content report in Oracle Apex, but I'm struggling with implementing pagination. With too many rows to display at once, adding pagination will greatly enhance the user experience. Here is a snippet of my ...

Verify if a personalized angular directive is able to display or conceal an element

I have a custom directive that toggles the visibility of an element based on the value of another service. I attempted to write a test to verify if the directive functions as intended. Initially, I used the ":visible" selector in my test, but it consistent ...

Stylish CSS round link positioned on a half-circle

As a beginner in web design, I'm struggling with CSS. However, I have been given the task of creating a button like the one shown below. I am unsure of how to create a circular link like this. Any assistance would be greatly appreciated. ...

jQuery: Function is not running as expected

I'm facing a challenge in executing a function twice, and I'm having trouble identifying the issue. Check out my JSFiddle at the following link: http://jsfiddle.net/g6PLu/3/ Javascript function truncate() { $(this).addClass('closed&apo ...

Manipulating certain attributes of a CSS class for a specific division element

I'm working with a div that has a CSS class applied to it. The class declares the height as x pixels, but I actually want my div to be y pixels high. Is there a way to override the height parameter of the CSS without making changes to the CSS file? ...

Fresh sheet with a view through the window

I need to print a table and two popup windows when an action is triggered, but I am facing issues with the page-break in CSS. None of the solutions I have attempted so far seem to work. Currently, the two pop-up windows overlap with the table data, and I w ...

Ways to halt a CSS animation when it reaches the screen boundary

I put together this demo: By clicking, a red box falls down. The issue arises when trying to determine the screen size using only CSS. In my demo, I set the box to fall for 1000px regardless of the actual screen height. Here is the keyframe code snippet ...

Creating a banner image that scrolls while maintaining a fixed size

I'm looking to add a banner image with a scrolling effect as users navigate down the page, but I'm not sure what this technique is called. An example of what I'm trying to achieve can be seen on this site. As the user scrolls down, the res ...

Is placing JavaScript on the lowest layer the best approach?

I'm facing a unique situation that I haven't encountered before and am unsure of how to address it. My website has a fixed top header and footer. On the left side, there is a Google Adsense ad in JavaScript. When scrolling down, the top header s ...

Tips for avoiding flickering in a background image when it is being changed

Utilizing JavaScript, I am setting a repeated background image from a canvas to a div in the following way: var img_canvas = document.createElement('canvas'); img_canvas.width = 16; img_canvas.height = 16; img_canvas.getContext('2d' ...

Is there a way to use a Google Chrome command line switch to simulate the dimensions of a

Seeking information on the available Google Chrome command line switches for emulating device sizes. Specifically, I need to test a component that utilizes CSS @media queries for min-device-width/min-device-height. Previous attempts with --window-size an ...

What is the best way to choose the first parent element that includes a specific class within it?

I am searching for a method to identify the initial parent element that includes another element with a specified class within it. For instance: <div class="wrapper"> <div class="item"> <div class="inner-eleme ...

The term 'undefined' does not refer to an object

My div contains the following code: <em id="ProductPrice" class="ProductPrice VariationProductPrice">$75.00</em> I want to change the text color if the value changes. This is what I tried: <script> $(document).ajaxSuccess(function(){ ...

To ensure proper formatting, I must include a comma operator for numbers while typing and limit the decimal places to two

Could someone assist me in formatting a number to include commas and restrict the decimal places to two using regex? I have attempted the following, but need help making it work properly. Currently, when I enter a number it shows up as 1231244, however I ...