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

Building an Angular 4 app featuring a customized default landing page and URL parameters functionality

I am in the process of developing a web portal that will be embedded within an iFrame. Below is the code snippet I am using to set up the portal: Routes Configuration const routes: Routes = [ { path: '', redirectTo: '/dash ...

Navigating from a Card to a new View in Angular

I am currently developing a project using Angular (latest version). Within my application, I have the functionality to dynamically generate bootstrap cards from an Order Array and display them in my "Order-Item-Component through its respective template. ...

What is the best way to adjust the height of my website to properly display on all screen sizes

Is there a way to make my web page fit perfectly on the screen in terms of both width and height? I'm familiar with using Media Queries for adjusting widths, but how can I also control the height? Any suggestions on setting the height? I would great ...

Issue with DIV height in Internet Explorer occurs only when application is accessed using server name

Encountering a unique issue specific to IE (confirmed in v8 and v9). When utilizing jquery to determine the height of a div: $('#sys_MainPage').height() In Chrome, Firefox, and when accessing using the IP address, this code returns around 26 ...

Bring to life a dashed slanted line

After incorporating the code from this post on Stack Overflow about animating diagonal lines, I was pleasantly surprised with how well it worked. However, my next goal is to style the line in such a way that it appears dotted or dashed, as opposed to one ...

Understanding Node.js document object

Hey, I'm currently trying to execute a JavaScript function on the server side using node.js, but I've encountered an issue. The function relies on accessing hidden values within the returned HTML using the document DOM, however, the document obje ...

What is the best way to center align and add an icon to the header in Ionic?

How can I center align an "ion-ios-arrow-up" icon in the header of my Ionic app, similar to the concept shown below? This is my HTML template: <ion-view cache-view="false" view-title="Historical HPP"> <ion-nav-bar class="nav-title-slide-ios7 ...

CSS 3 - Apply transition to the 'display' property

I have a question about using the transition effect in conjunction with the display property: Currently, I am testing on Safari. input.input_field { display:none; transition-property: display; transition-duration: 2s; -webkit-transition-p ...

Setting the margin to 0 auto to create a CSS Grid layout featuring an Image taking up the entire right side

I’ve been grappling with finding a consistent method to ensure uniform white space for all containers like this: https://i.stack.imgur.com/wtk0G.png Important: The grey rectangle signifies an image. My goal is for it to extend to the viewport's en ...

What is the best way to trigger a function when a button is enabled in Angular 8?

Here is a portion of my sign-in.component.html file. I have created a function in the corresponding sign-in.component.ts file that I want to trigger when the button below becomes enabled. Here is an example of what I am trying to achieve: <div class= ...

I'm currently in the process of incorporating horizontal scrolling into various sections of an image gallery. The images will stack vertically only when the window width

I am currently developing an image gallery with multiple sections that contain various images. My goal is to arrange each section of images in a single row, allowing horizontal scrolling similar to Netflix's layout. However, I'm facing challenges ...

Determine if a paragraph contains a specified value using jQuery

I need to only select the checkboxes that do not have a value in the paragraph. Some mistake seems to be causing all the checkboxes to be marked when I click the button (Value > 0). Can you spot where I went wrong? $("input[id*='btnValue']").c ...

Styling a div element in CSS with absolute positioning and the ability to

I am looking to set specific positions and dimensions for the div elements within my HTML document. Refer to this image for reference: https://i.stack.imgur.com/ANBVm.png For each of these div elements, I want a vertical scrollbar to appear if the content ...

Encountering a problem when attempting to send an HTML email through PHP

Trying to use PHP to send email within an HTML code snippet. Here's the code I've written: <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { if ( mail ('<a href="/cdn-cgi/l/email-protection" class="_ ...

placing a div inside another div

As I work on my website, I have created several panels with a repeating texture. To enhance the visual appeal of the site, I decided to add colored divs and adjust opacity for a tint effect instead of using separate images. The issue I'm facing is th ...

CSS shimmer effect does not smoothly transition between borders

Currently, I'm diving into a tutorial on YouTube that teaches how to craft a CSS Glowing Border Animation After putting in the effort to replicate the animation, I noticed a glitch that's been bothering me. It seems like there's an abrupt s ...

Using the CSS property `transform:scale(2,2)` causes an image to suddenly appear in the

Currently utilizing Joomla 3.3, I am seeking to enlarge an image on mouseover. The html code for the image is as follows: <img class="enlarge" style="float: right; margin: 10px; border: 5px solid black;" title="Chapter 1: Physical Differences" src="im ...

The elements on the webpage are spilling over with content

I encountered an issue while creating a dashboard with a sidebar on the left side. When adding content to the page, some of it ended up hidden behind the sidebar. I tried using overflow-x:auto and this was the result: https://i.stack.imgur.com/5qHJY.jpg Be ...

Having trouble with syntax highlighting on Node.js Vash view files in Visual Studio 2015 Update 3?

While working on a Node.js application using Vash as the view engine in Visual Studio 2015 Update 3, I encountered an issue with syntax highlighting. When writing HTML in the vash files, the code appears as plain text without any syntax highlighting. For e ...

Hyperlink -> Boundary

Our homepage features various elements as part of the menu. Each element consists of a picture, title, and small description. The issue is that these menu items always display a border when the mouse hovers over them, despite attempts to hide it. You can ...