The float right attribute doesn't stay contained within the box; instead, it drifts outside the box

I've been struggling to keep this box fixed at the top of the browser, but I'm facing an issue with the positioning on the right side. I can't seem to figure out how to solve it, so I'm reaching out for some help.

#StickyBar #RightSideOfStickyBar
{
    float: right;
}

The intention is for this element to float all the way to the far-right side within the #BoxAlpha container. However, it's currently extending all the way to the edge of the browser instead.

Additionally, I am using a CSS reset from HTML5 doctor.

Below is the HTML5 code:

<div id="BoxAlpha">

        <section id="StickyBar">

            <section id="LeftSideOfStickyBar">

                <ul class="Home">
                    <li class="HomeList">
                    <a href="#">Home</a>
                    </li>
                </ul>

            </section>

            <section id="RightSideOfStickyBar">

                <ul class="SignUpAndSignIn">
                    <li class="SignUpAndSignInList">
                    <a href="#">Sign Up</a>
                    </li>

                    <li class="SignUpAndSignInList">
                    <a href="#">Sign In</a>
                    </li>
                </ul>

            </section>

        </section>

    </div>

And here is the associated CSS:

#BoxAlpha
{
    width: 1000px; height: auto; margin-left: auto; margin-right: auto;
}

    #StickyBar
    {
        background-color: #EBEBEB; font-family: "Comic Sans MS", cursive, sans-serif; font-size: 1em; width: 100%; height: auto;
        line-height: 50px; background: linear-gradient(#00FFFF, #4D70DB); border-style: solid; border-color: #999999;
        position: fixed; top: 0; z-index: 1;
    }

        #StickyBar #LeftSideOfStickyBar
        {
            float: left;
        }

            #StickyBar #LeftSideOfStickyBar .Home
            {
                list-style: none;
            }

            #StickyBar #LeftSideOfStickyBar .Home .HomeList
            {
                float: left; width: 7em; text-align: center;
            }

            #StickyBar #LeftSideOfStickyBar .Home .HomeList > a
            {
                font-weight: 550; text-decoration: none; color: #000000; cursor: default;
            }

        #StickyBar #RightSideOfStickyBar
        {
            float: right;
        }

            #StickyBar #RightSideOfStickyBar .SignUpAndSignIn
            {
                list-style: none;
            }

            #StickyBar #RightSideOfStickyBar .SignUpAndSignIn .SignUpAndSignInList
            {
                float: left; width: 7em; text-align: center;
            }

            #StickyBar #RightSideOfStickyBar .SignUpAndSignIn .SignUpAndSignInList > a
            {
                font-weight: 550; text-decoration: none; color: #000000; cursor: default;
            }

Answer №1

Alright, let's talk about simplifying your code a bit. The header has a position: fixed, which means it's not bound by its container. It will act like the only element on your page.

According to CSS absolute and fixed positioning on w3.org:

Fixed positioning is essentially a specialized form of absolute positioning; elements with fixed positioning are fixed in relation to the viewport/browser window rather than the containing element. Even if the page is scrolled, they remain in the exact position within the browser window.

If you want to maintain position: fixed, you can give it a max-width:

CSS

#StickyBar {
  max-width: 1000px;
}

Here's an example.

Alternatively, if you prefer not to use position: fixed, remove it and add overflow:hidden like this:

CSS

#StickyBar {
    overflow: hidden;
}

Now, it will adhere to the width of its parent element.

Here's an example without using position:fixed


If you're interested, I've also designed a simple layout for you to try out.

Check out the new layout example!

HTML

<header>
  <nav class="pages">
    <a href="#">Home</a>
  </nav>
  <nav class="signin">
    <a href="#">Signup</a>
    <a href="#">Signin</a>
  </nav>
</header>

CSS

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html,body {
  height: 100%;
  font: 100% Helvetica;
}

header { 
  max-width: 1000px;
  height: 80px;
  background:linear-gradient(#0FF,#4D70DB);
  display: table;
  width: 100%;
  margin: 0 auto;
}

header nav { 
  display: table-cell;
  text-align: left;
  vertical-align: bottom;
}

header .signin {
  text-align: right;
}

a {
  color: #000;
  text-decoration: none;
  display: inline-block;
  padding: 10px;
}

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 implement jQuery on my website?

Instead of copying all the example code here, you can check out the jQuery demo page: Demo Page I would like to integrate the Latest News example with scrolling text in red from the demo page into my website. The demo page also provides links to the sour ...

Transmit information to a different JavaScript framework using Vue

Utilizing Vue, I create a JSON file using classic ASP from SQL Server and import the data. My objective is to showcase the received data on the page and in ApexCharts. The structure of the HTML page, getData.js, and the JSON from dataSql.asp is outlined b ...

Ways to conceal one message within another

I am currently working on incorporating Facebook's comments box into my website. My goal is to have the comments that I retrieve through GET requests and display in the template remain invisible to users, but visible to search engines. As part of an ...

Enhance User Experience with the Tooltip Feature and Customized

Here is the jQuery code I am using to style my tooltips: $(function() { // select all input fields within #tooltips and attach tooltips to them $("#tooltips :input").tooltip({ // place tooltip on the right edge position: "cen ...

Floating CSS3 animations within HTML elements

I'm struggling to understand why the animated arrow on my website refuses to go behind the other elements. The animation code I've used for the arrow in CSS3 is as follows: -webkit-animation-fill-mode:both; -moz-animation-fill-mode:both; -ms-an ...

The mouseenter event in jQuery is failing to trigger

I'm encountering an issue with the mouseenter event on a div section of my webpage. I am attempting to alter the background color of this div when the mouse enters, but it seems to be disregarded. This is the basic HTML code: <div id="services" c ...

What is the best way to use CSS in conjunction with a Master Page and a new content page, ensuring that the new CSS does not interfere with

Currently, I am working with an ASP.Net 4.5 website that incorporates a master page. The site utilizes a CSS file that is applied universally across all pages. Recently, I added a new page to the website which contains its own custom CSS specifically desig ...

Error: jQuery is unable to access the property 'xxx' because it is undefined

While attempting to make a post request from the site to the server with user input data, I encountered an error message saying TypeError: Cannot read property 'vehicle' of undefined as the response. Here is the HTML and script data: <!DOCTY ...

Using CSS files that are not properly imported into React components

Currently, I am utilizing multiple CSS files for a specific purpose. The issue I am facing is that when I apply styles to one component in a CSS file, it inadvertently affects other components as well, not just the intended one. I believe the root of this ...

Require assistance with displaying a menu on a website using JQuery

Is there a way to create a menu similar to the one shown in the image below?https://i.sstatic.net/QxNPL.png To learn more about this menu, you can visit their website by clicking on this Link. I have copied some code (HTML code and links to CSS and .js fi ...

What are some ways to eliminate the excess white space beneath the footer in HTML?

After creating a responsive webpage that works flawlessly on mobile devices and tablets, I noticed a problem with the footer when viewed on desktop. There is an empty white space at the end of the webpage, and upon inspecting the browser, it focuses on the ...

Is there a method to stop a mobile device from rotating my responsive website on mobile devices?

Currently, I am in the process of developing a mobile responsive website and I'm aiming to prevent any rotation on Safari specifically. Is there a way that I can achieve this without affecting other browsers like Android? ...

What is the correct way to place an item within a grid layout?

My current layout is structured with a single row class and two columns using the bootstrap grid system. However, I am facing an issue where I need to add another item within that grid system, but I am struggling to position it correctly. It keeps appeari ...

Mastering the art of incorporating background image transitions and creating a seamless slider for navigation

Is there a way to create a navigation menu like the one on this theme? On this theme, when you hover over a menu item, the background image smoothly moves left or right to the item you are hovering on. If anyone knows of plugins that can achieve this eff ...

What is the secret to keeping a hover effect active?

As I hover over the buttons in my stack, a text box appears on the right side. The intention is for this text box to act as a scroll box, but it disappears when I move my mouse to scroll because it needs to be continuously hovered upon to stay active. To ...

What is the best way to deactivate all 67 checkboxes once 4 of them have been chosen?

I am looking to limit the selection of checkboxes to only 4 out of a total of 67. However, I am currently struggling to figure out how to monitor the checkboxes and determine if exactly 4 have been selected. <table class="mdl-data-table mdl-js-data-t ...

Adjust the size of a panel in Extjs when its parent div is resized

Within my div, I have included an Extjs panel using the renderTo configuration option. Can someone please advise on how to adjust the panel size dynamically when the div is resized? Appreciate any help! Thank you. ...

JavaScript form button press tracker

Hello! I've been tackling a challenge involving a JavaScript function to count button clicks. The catch is, the button type is set to submit, causing the page to reload every time I click it. Below is the snippet of code that contains the problemati ...

Unusual problem arises with styling and element measurement (scrollWidth / scrollWidth) in Gatsby framework

Currently, I am working on a component that splits lines based on the parent container's width without overflowing. Despite successfully implementing this in a sandbox or pure react application (even with custom fonts), I encountered issues when using ...

Display the PHP variable's contents as a text string within an HTML document

Looking at a WordPress PHP script, I came across the following snippet: echo '<div class="slide-media">' . $slide_media . '</div><!--/.slide-media-->' . "\n"; } I am interested in viewing the ...