Conflict between the top menu and body on the HTML page is causing issues

Encountered an issue with my HTML code:

CSS:

.fixedmenu {
    overflow: hidden;
    background-color:rgb(153,0,51);
    position: fixed; /* Ensures the navbar stays in place */
    top: 0; /* Positions it at the top of the page */
    width: 100%; /* Occupies full width */
    font-size:2em;
}

#bodybox {           
    border:0px;
    width:80%;
    padding:0px;
    margin-left: auto; 
    margin-right: auto;
    background:red;
}

This is the HTML code within the body:

<div class="fixedmenu">
    <div style="float: left;color:white;padding:0.5% 0% 0.5% 3%;">YggDrasil ||</div>
    <div style="float: right;color:white;padding:0.5% 3% 0.5% 0%;"> || Login</div>
    <div style="margin:0 auto; width:300px;color:white;padding:0.5% 0% 0.5% 0%;"> Welcome to YggDrasil. </div>
</div>

Everything was fine until this addition:

<div id="bodybox"> 
    hi  
</div> 

After adding this, the margin of my top menu shifts to the right. How can I fix this?

Answer №1

To ensure everything fits nicely on your window and the new div appears after the menu, you can apply margin: 0; to both body and .fixedmenu, as well as set position:relative; for both elements. Additionally, adjust the width of the menu by using width: 100vw;.

<body>
    <div class="fixedmenu">
        <div style="float: left;color:white;padding:0.5% 0% 0.5% 3%;">YggDrasil ||</div>
        <div style="float: right;color:white;padding:0.5% 3% 0.5% 0%;"> || Login</div>
        <div style="margin:0 auto; width:300px;color:white;padding:0.5% 0% 0.5% 0%;"> Welcome to YggDrasil. </div>

    </div>
    <div id="bodybox"> hi</div> 

</body>

Styles applied:

body{
    margin: 0;
}
.fixedmenu {
    overflow: hidden;
    background-color: rgb(153, 0, 51);
    position: fixed;
    top: 0;
    width: 100vw;
    font-size: 2em;
    margin: 0;
    position:relative;
}

#bodybox {
    border: 0px;
    width: 80%;
    padding: 0px;
    margin-left: auto;
    margin-right: auto;
    background: red;
    position:relative;
}

Final result:

https://i.stack.imgur.com/NY5Yb.png

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

Utilizing an external SCSS or CSS file with Material UI in React: A step-by-step guide

Encountering issues with external CSS files in my React project that uses Material UI components. Despite creating a CSS file for each component in the react components folder, they fail to load upon hard reloading the site. I prefer using external CSS ov ...

forming a boundary that stands alone, unattached to any other boundaries

I am trying to design a navigation bar that resembles the one depicted in the image provided. The concept involves creating a navigation bar where each border is separated from the others, potentially achieved using hr tags. However, I am struggling to fi ...

What is the process for incorporating the !important declaration into a CSS-in-JS (JSS) class attribute?

I'm currently exploring the use of CSS-in-JS classes from this specific response in conjunction with a Material UI component within my React project. In order to override the CSS set by Bootstrap, I've decided to utilize the !important modifier. ...

What is the best way to ensure an image is responsive in CSS and aligns perfectly in height with its neighboring text?

There is an image within the <img> tag and some text inside the <p> tag, both enclosed in a <div>. Although the image is responsive and resizes proportionally when the browser window changes size, there is an issue where as the text wrap ...

Is there a way to prioritize the table row background color over the cell input elements background color?

After a user selects a table row, the row is assigned the class below: tr.selected { background-color:#FFA76C; } However, if there are input fields in the cells with background colors, they end up overriding the row's background color. (For instance ...

jquery counter is malfunctioning

I noticed a strange issue with the counters on my website. They count up as expected when the page loads, but for some reason, when the screen width shrinks below 800px, they don't start automatically. However, if I quickly scroll to where the counter ...

Use a PHP form to send an email with HTML formatting, rather than plain text

I am facing an issue with sending emails from a web form. The received email needs to be displayed as HTML and not text. function createMailBodyLine ( $title, $value ) { $value = nl2br(htmlspecialchars($value)); return "<tr> ...

What is the best way to assign a unique ID to every element in this situation?

Here is the given code: var words = ['ac', 'bd', 'bf', 'uy', 'ca']; document.getElementById("wordTable").innerHTML = arr2tbl(2); function arr2tbl(ncols) { return words.reduce((a, c, i) => { ...

unable to reset contact form functionality

I need to implement a Contact Us form on my website. The form should clear the text fields after submission and display a Thank You message. Below is the HTML code: <div class="form"> <div id="sendmessage">Your message has been sent. Thank yo ...

Creating a custom design for ng-bootstrap accordion using CSS styling

I've encountered an issue with my Angular 2 component that utilizes an accordion from ng-bootstrap. While the functionality works perfectly, I'm facing a problem with applying custom styles using the .card, .card-header, and .card-block classes o ...

The external HTML page fails to load within a div in Firefox browser

My jquery script successfully loads an external HTML page into a div when tested on Chrome, but it's not functioning properly on Firefox. Here is the demo link. Could someone please explain why this code snippet is not working on Firefox? Code: &l ...

Getting the dimensions of an image when clicking on a link

Trying to retrieve width and height of an image from this link. <a id="CloudThumb_id_1" class="cloud-zoom-gallery" rel="useZoom: 'zoom1', smallImage: 'http://www.example.com/598441_l2.jpg'" onclick="return theFunction();" href="http ...

Guide on transporting PNG files over the boundary

I am working with two specific css commands. code h1 { background: #000000; border-radius: 6px; color: #fff; display: block; font: 40px/60px "Sans", Inconsolata, "Lucida Console", Terminal, "Courier New", Courier; padding: 40px 40px; text-align: center; ...

I am having trouble with Popover and Tooltip not functioning properly in my browser

Today, I spent countless hours trying to implement popovers in my table to display additional information for each cell. However, I eventually discovered that the popovers do not seem to be functioning at all in my code. What could be causing this issue? D ...

"Adding a large amount of HTML using .append() is causing slow performance in Internet Explorer 10

Lately, I've been encountering some challenges with jQuery code in my workplace. Unfortunately, I don't have the exact code on hand at the moment, but I'll do my best to provide pseudo-code. Being relatively new to JavaScript, there may be e ...

div is consistently correct across all web browsers

After creating the following CSS code for a div to always be positioned on the right: #button > .right { background-position: -268px 1415px; height: 180px; position: fixed; right: -90px; top: 40%; width: 263px; -webkit-transform ...

Tips for seamlessly incorporating advertisements into a mixed application

I am having trouble adding banner ads to my hybrid application developed with Telerik. Despite my extensive research, I have been unable to find a suitable solution. Is there any html5 or javascript banner advertising service/API that is compatible with ...

Unable to apply the flex-grow property to a column flex box that has a specified minimum height

I am currently working on designing a website layout that harnesses the power of flexbox to fill the entire page with content, even when the content does not fully occupy the length of the page. My approach involves creating a large flexbox container with ...

The functionality of min-height in CSS seems to be malfunctioning

One of my HTML elements has a parent with the ID #out, and a child with the ID #in. The parent is positioned absolutely, with a minimum height of 100%. It seems to be functioning correctly in most browsers, except when I also set the child's min-heigh ...

"How can I adjust the positioning of a Materialize modal to be at the top

I am encountering an issue where the modal is being displayed automatically without any trigger when I try to set the top property in the #singlePost. Despite attempting $('#singlePost').css("top", "1%");, it does not seem to work. I have also ex ...