When the browser is resized, text shifts and adjusts accordingly

Is there a way to prevent the text in my header from moving when I resize the browser? I want it to stay in place. Any help is greatly appreciated!

Check out the code on JSFiddle.

Here's how it looks Normal:

And here's how it looks Resized:

HTML:

<div id="NewNavBar"><ul class="nav">
            <li><a href="me" style="margin-left:10px;"><b>Home</b></a></li>
            <li><a href="community">Community</a></li>
            <li><a href="staff">Staff</a></li>
            <li><a href="shop">Shop</a></li>
        </ul></div>
        </div>

        <div id="NewNavB"><ul class="navb">
        <li><a href="#}><b>{username}</b></a></li>
            <li><a href="#">My Page</a></li>
            <li><a href="account">Account Settings</a></li>
            </ul>   
        </div>

CSS:

#NewNavBar {
text-align: center;
position:relative;
width:100%;
min-height:40px;
border-top:2px solid #3f82a3;
border-bottom:2px solid #3977af;
background-color:#529ce2;
line-height: 40px;
}

li {
    vertical-align: middle;
    display: inline-block;
}

#NewNavBar li + li::before {
    content: " | ";
}

#NewNavB li + li::before {
    content: " | ";
}

li a{
padding:8px;
    }

    #NewNavBar ul a:hover{
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
    border: 1px solid #3f82a3;
    padding: 7px;
    text-decoration: none;
    background-color: #4683bc;
}

ul {

    list-style-type: none;
    margin: 0;
    padding: 0;
}

.nav {
    float: left;
    color: white;
    margin-left:202px;
}

.navb {
    float: left;
    color: #53729c;
    margin-left:202px;
}

#NewNavB {
margin: 0 auto;
position:relative;
margin-bottom:15px;
width: 100%;
min-height: 30px;
border-bottom:2px solid #c1c1c1;
background-color:#f9f9f9;
line-height: 32px;
}

Answer №1

To achieve the desired layout, you can set the margin-left of both .nav and .navb using percentages or viewport width units (vw) instead of pixels (px). This will ensure that your layout scales better when the user zooms in. Check out the example on this jsFiddle page for a visual representation.

.nav {
    float: left;
    color: white;
    margin-left: 25vw;
}
.navb {
    float: left;
    color: #53729c;
    margin-left: 25vw;
}

If the user zooms in significantly, it may disrupt your layout. To address this issue, you can use media queries based on em units (max-width: 30em, for instance) to enhance the layout when the user zooms and provide better responsiveness on mobile devices. Take a look at the code snippet below and the demonstration on jsFiddle:

@media (max-width: 30em) {
    .nav {
        margin-left: 0;
    }
    .navb {
        margin-left: 0;
    }
}

Answer №2

To properly set the width of #NewNavBar, use pixels instead of percentage.

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

Diverse outcomes generated by a single dropdown selection - Harnessing the power of JavaScript and HTML

I am seeking a way to dynamically show different sections of my form based on the selection made from a drop-down menu. Currently, I am facing two issues: it only hides the first area I want hidden, and I am struggling with the syntax to make multiple opt ...

Manage numerous canvas animations

Is there a way to interact with an already drawn Canvas animation? I am attempting to create 3 distinct tracks that can be controlled by a "Start" and "Stop" button. However, when I click the Start button on the first canvas, it triggers the last canvas in ...

Automatic page updates triggered by code modifications

I am currently working on a .NET ASPX page where I usually write my classes separately in the app_code folder. However, for this specific page, I needed a temporary class that is only required by this page. So instead of creating a separate file, I decid ...

Use CSS to create boxes of a specific size within a table cell that spans the entire height

I'm having trouble using CSS to create two boxes (one red and one green) inside a table cell. I can't seem to make them fill the entire height of the cell. Here is what I have tried so far: td { background-color: lightblue; padding: 5px; ...

Can the recaptcha icon be customized with a new hue?

I have been attempting to alter the captcha icon/logo in recaptcha v2. Initially, I tried to modify it using regular CSS without success. Then, I experimented with jQuery to determine if the iframe had loaded completely before proceeding with the change. ...

How to Take Out Table Borders in Outlook 2007 HTML Email

I'm currently working on removing table borders. It looks good in the GMAIL Webapp, but Outlook keeps displaying default black borders. How can I eliminate them? <TABLE style="BORDER-TOP: 0px; BORDER-RIGHT: 0px; BORDER-COLLAPSE: collapse; BORDER-B ...

Having trouble with the Handlebars {{#if}} and {{elseif}} block helpers not functioning properly?

When working with handlebars.js, I am faced with the task of parsing complex JSON and displaying names in different styles based on certain conditions. The JSON structure is as follows: "TradeLine":{ "TradeLine":{ "Mor ...

tcpdf - bug causing incorrect indentation following a line break

Are you familiar with how to adjust text alignment after a line break using tcpdf? With tcpdf 6.2.13 and the writeHTML method, I am attempting to achieve perfectly left-aligned text. However, whenever the line is too long, the following line gets shifted ...

Using HTML and CSS, we can achieve a fixed position using the `position: sticky`

On my website, I have elements that utilize transformations. One issue I've encountered is that these transformations end up resizing the viewport. Normally, I would address this by setting overflow-x: hidden for both html and body, but doing so disru ...

Justifying content is failing to perform its duty, while aligning items is successfully fulfilling its role

Can someone explain why the align-items property in this code is centering child elements on the x-axis instead of the y-axis? Also, the justify-content: center doesn't seem to have any effect. Even when I remove justify-content, the items are still a ...

What could be the reason why 'Col' is not functioning properly within React bootstrap?

I am currently implementing react bootstrap with Container, Row, and Col components. However, my code isn't functioning as expected - instead of displaying columns, they are appearing in a row-like fashion. Here is the code snippet that I am referrin ...

Can you provide tips on using the FileReader API to upload an image file from a URL?

When working with an HTML form, we often have an image field for uploading files. I recently followed a tutorial on using the HTML5 FileReader API and successfully uploaded an image file from my local disk. However, I encountered a scenario where I needed ...

Enhance your navbar on user scroll with Bootstrap 4 by moving or adding elements dynamically

I recently built a Wordpress site using Bootstrap 4.5 for the front-end, marking my first experience with Bootstrap. The site currently features the standard sticky Bootstrap 4 navbar as shown in the code snippet below: <header> <nav c ...

Unable to eliminate added jQuery Element

Note: This question may seem similar to a previous one I posted, but it actually addresses a different issue. I am currently utilizing Jquery.dotdotdot to handle truncation with a "Read More" feature. What is the problem? After clicking on "Read More" t ...

Divide HTML elements every two words

Can you use CSS to break up HTML content after every 2 words, ensuring it works for any word combination? Example: // Original HTML content The cat is sleeping // Desired result: The cat is sleeping ...

Using HTML in jQuery or JavaScript: A Step-by-Step Guide

Here's the deal - I've got a bunch of buttons. https://i.sstatic.net/839MH.png What I want is, when the 4th button is clicked, to trigger a select menu option like this: The outcome I'm aiming for after clicking the button is https://i.ss ...

Adjusting nearby parts when hovering in CSS

I'm curious as to why the size of the second div does not change when hovering over the third div, unlike when hovering over the first div. #block { width: 50%; white-space: nowrap; } .div { display: inline-block; text-align: center; } di ...

What is the best way to align a jQuery Cycle 2 Slider in the middle of the

Having some trouble centering a jQuery Cycle 2 Slider on my page. You can see the slider here. I've attempted multiple methods to center it, but none have been successful. Check out the HTML code: <div class="slider"> <div id=outside> ...

Issues with Internet Explorer displaying webpages accurately due to a PHP class conflict

I'm facing a strange issue with my website. It works perfectly on all browsers except for Internet Explorer, and I'm trying to figure out the root cause. After some investigation, I've identified a PHP file that seems to be causing the prob ...

What could be the reason for the hr tag's CSS not appearing in React code?

In my React code, I am trying to create a border using the following code: <hr className="borderLine" /> However, when I attempt to style it with CSS, I utilize this snippet of code: .divider { margin: 2rem; width: 100%; color: ...