What could be causing my image alignment to be disrupted by my navigation bar?

When the navigation bar is absent, my image aligns perfectly centered on the page.

However, once I include the nav bar (created with an unordered horizontal list), the image shifts more towards the left of the page.

This layout is very basic in nature.

<div>
<img ...>
<ul>....</ul>
</div>

To view the complete code (which cannot be fully displayed here), please visit: https://jsfiddle.net/ps84wbx0/

I am unable to provide the image, but this issue likely affects any image placement.

The following is the design I am attempting to achieve:

https://i.sstatic.net/m5SLR.jpg

Snippet below:

/* Styling for Logo */
div.homepage {
    position: fixed;
    left: 50%;
}

img.homepage {
    position: relative;
    left: -50%;
}


/*Styling for Nav Bar*/
ul {
    list-style-type: none;
    margin-left: -50%;
}

li.button {
    display: inline-block;
    margin: 0 1em 1em 0;
    padding: 0 4em;
    font: 300 1.5em/3em 'Open Sans', sans-serif;
    letter-spacing: .08em;
    color: #fff;
    background: #0090C0;
    border-radius: 2px; 
}

li.button:hover{
    background: #007DA7;
    box-shadow: 0 0 3px rgba(black, .5) inset;
}
 
a:link, a:hover, a:active, a:visited {
    text-decoration: none;
    color: inherit;
}
<div class="homepage">
    <img src="images/homepage.png" class="homepage">
    <ul>
        <li class="button"><a href="index.html" data-text="Home">Home</a></li>
        <li class="button"><a href="about.html" data-text="About">About</a></li>
        <li class="button"><a href="services.html" data-text="Services">Services</a></li>
        <li class="button"><a href="contact.html" data-text="Contact">Contact</a></li>
    </ul>
</div>

Answer №1

There are numerous methods to tackle this issue, but I find this approach to be one of the simplest. I made modifications to the CSS of div.homepage, img.homepage, and ul. Below is the updated code:

/* Custom Styling for Logo */
div.homepage {
    position: fixed;
    text-align: center;
    margin: 0 auto;
    left: 0;
    right: 0;
}

img.homepage {
    position: relative;
}


/* Navigation Bar Styling */
ul {
    list-style-type: none;
}

li.button {
    display: inline-block;
    margin: 0 1em 1em 0;
    padding: 0 4em;
    font: 300 1.5em/3em 'Open Sans', sans-serif;
    letter-spacing: .08em;
    color: #fff;
    background: #0090C0;
    border-radius: 2px; 
}

li.button:hover{
    background: #007DA7;
    box-shadow: 0 0 3px rgba(black, .5) inset;
}
 
a:link, a:hover, a:active, a:visited {
    text-decoration: none;
    color: inherit;
}
<div class="homepage">
    <img src="images/homepage.png" class="homepage">
    <ul>
        <li class="button"><a href="index.html" data-text="Home">Home</a></li>
        <li class="button"><a href="about.html" data-text="About">About</a></li>
        <li class="button"><a href="services.html" data-text="Services">Services</a></li>
        <li class="button"><a href="contact.html" data-text="Contact">Contact</a></li>
    </ul>
</div>

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 technique for incorporating FontAwesome icons onto an HTML 5 canvas?

I am encountering an issue while trying to use FontAwesome icons within my HTML 5 canvas. Here is what I have attempted: ct.fillStyle = "black"; ct.font = "20px Font Awesome"; ct.textAlign = "center"; var h = 'F1E2'; ct.fillText(String.fromCha ...

Designing versatile buttons with HTML

When accessing the website on a phone, I have trouble seeing and tapping on these two buttons because they are too far apart. I would like to change it so that once a file is selected, the 'choose file' button will be replaced by the upload butto ...

Sophisticated filter - Conceal Ancestry

Check out this snippet of my HTML: <td> <a class="button" href="#"> <input id="download">...</input> </a> <a class="button" href="#"> <input id="downloadcsv">...</input> </a> </td> I am ...

Prevent vertical scrolling only on mobile devices while allowing horizontal scrolling

Currently, I am working on a web page that utilizes a horizontal layout. However, when viewing the page on mobile devices, I want to restrict scrolling to only the horizontal direction, preventing users from being able to scroll up or down. As it stands n ...

Eliminate resistance on HTML components

Have you ever noticed that when you click on an HTML element, such as images, you can drag a "shadow version" of them around on the screen? For example, take the Stack Overflow logo. If you click and drag it, you'll see a dark version of the logo mov ...

How to retrieve a value from an Angular form control in an HTML file

I have a button that toggles between map view and list view <ion-content> <ion-segment #viewController (ionChange)="changeViewState($event)"> <ion-segment-button value="map"> <ion-label>Map</ion-label> & ...

I'm having trouble getting my paragraph to center with both Bootstrap 4's margin:auto and text

I've been trying to center a paragraph in my project but it's not working out. I attempted using margin:0px auto; and the text-center features, but they didn't have the desired effect. <div class="row text-center"> <p> You c ...

The upper margin is ineffective

Apologies, I am new to CSS. Here is the HTML code I have: <div class="box-A" >Box A content goes here</div> <div class="box-B" >Box B content goes here</div> I attempted to apply the following CSS to it: .box-A{ background-c ...

Retrieving a value from an input field within a table cell using jQuery

Looking for some help with a complex HTML Table structure like this one. <table id="items"> <tr class="total_up"> <td colspan="2" class="blank"></td> <td colspan="2" class="total-line">Total</td> ...

CreateJS animations in HTML5 are not compatible with iPads

I've been working on an animation project using CreateJS and spritesheet, but I'm encountering issues with it not functioning properly on iPad devices. Strangely, it works perfectly on the Chrome browser. As far as I know, the CreateJS framework ...

What is the reasoning behind the presence of hidden elements on the Google Search Result Page?

While using the debugging feature, I stumbled upon some hidden elements on the page. Can anyone shed some light on why these elements are present with a display attribute of none? The first one on the left is an iframe, followed by multiple text areas. ...

Is it possible to install a Chrome extension specifically for YouTube on Google Chrome?

Hey, I'm trying to eliminate thumbnail images from YouTube. The code I am currently using is: while (true) { $("ytd-thumbnail").remove() } As of now, when I input this code in the console, it successfully removes all thumbnail images. However, I ...

Unfortunately, the YouTube iframe Embed feature does not have the ability to adjust audio settings on an

I've been experimenting with the YouTube iframe API and created a simple set of player controls for the YouTube player using Flash CC HTML5 canvas. The controls include a play/pause button, a draggable video bar with buffering visualization, and a vol ...

Flexbox's bootstrap columns display issue on smaller screens with no wrapping

I am currently working on a section of my website where I have applied some CSS to 2 divs and an anchor tag in order to center the content vertically. However, I am facing an issue with the flex style properties when the window size is less than 768px. In ...

Is the issue of Padding Overlap with Sidebar Item List getting in the way?

I am currently new to coding in HTML and CSS, so please bear with me if my code seems outdated or unconventional. I am working on creating a basic test website with a sidebar, but I'm encountering an issue where the items in my list are overlapping du ...

Scale transformation - I am aiming for it to exceed the limits, yet it remains contained within

Currently, I am working on enhancing my carousel by implementing a zoom effect when hovering over the images. However, I have encountered an issue where the image gets hidden within the div container and doesn't overflow as expected. I tried adjusting ...

Live Server in VS Code not refreshing automatically as expected

My problem is with VS Code and Live Server for HTML CSS. I expected the page to automatically reload after editing my HTML, but that's not happening. Even though I have Auto-Save enabled, Live Server isn't updating or refreshing my page automati ...

Adjusting the selected state of an HTML/CSS checkbox with JavaScript

After downloading a theme with a tailored switch component that replaces the standard checkbox functionality, I noticed that the 'checked' status of the underlying checkbox does not change upon click or touch events. Here is the HTML structure: ...

Bootstrap is unable to function properly without jQuery, throwing an error message that states: "Bootstrap's JavaScript

Attempting to create a Bootstrap interface for a program. Added jQuery 1.11.0 to the <head> tag, however upon launching the web page in a browser, jQuery throws an error: Uncaught Error: Bootstrap's JavaScript requires jQuery Various attempts ...

Adjust the Bootstrap row height to both occupy the full height while also being limited by the height of a designated container

Here's a puzzling question.. I've got a container with multiple rows, and I want these rows to completely fill the container's height without overflowing. I'm trying to avoid manually setting the row heights. When I add class="h-1 ...