I'm frustrated - let's place two divs side by side within a centered container that remains fixed at the

I'm really struggling with this issue. I have a container with 2 divs inside that I want to stick to the top of the screen. One div contains a menu, while the other has some links. The container needs to be centered above the inner page and have a background width of only 960px. Div1 and Div2 should be inline within the container, with one aligned left and the other right. The challenge is making the whole thing stay sticky at the top.

I've tried several approaches but can't seem to get it right. My current setup is messy and Div2 doesn't stay within its bounds in the container. It moves around and even goes outside the background when the username changes or the window width adjusts.

Below is a simplified version of my code:

<div class="container">

<div id="sticky-top-head">
    <div id="block-superfish-1">
        <ul id="superfish-1" class="sf-menu">
            <li class=""><a href="#">Contribute</a>
                <ul class="sf-hidden"><!--li items -->
                </ul>
            </li>
            <li class=""><a href="#">My Lists</a>
                <ul class="sf-hidden"><!--li items -->
                </ul>
            </li>
        </ul>
    </div>
</div>

<div class="user-logout">
    <a id="username" class="sf-right" href="/user">Mattc</a>
    <a id="signout" class="sf-right" href="user/logout">Sign Out</a>            
</div></div>

CSS:

    div#sticky-top-head {
        position: fixed;
        top: 0;
        width: 100%;
        z-index: 999;
    }
    div#sticky-top-head #block-superfish-1 {
        width: 100%;
        background: silver;
        height: 21px;
        color: #eee;
        font-size: 12px;
        box-shadow: 1px 1px 2px 1px #1e1e1e;
    }
    .sf-menu li {
        background: silver;
    }
    .user-logout {
        position: fixed;
        right: 23%;
        top: 0;
        z-index: 999;
        margin-top: 1px;
    }
    .user-logout a.sf-right {
        color: white;
        margin-right: 14px;
    }
    .user-logout a#signout {
        position: absolute;
        left: 105px;
        top: 0;
        width: 75px;
    }

Wire frame Example

Answer №1

If you're looking for a sleek and simple alternative, consider this approach. It maintains a clean aesthetic while effectively achieving your desired outcome. For demonstration purposes, I've included borders and limited the width, but feel free to customize these elements as needed.

.wrapper {
  width: 100%;
  position: fixed;
  border: 1px solid green;
  }

.wrapper .inner-content {
  width: 100%;
  max-width: 500px; /* ADJUST TO FIT YOUR CONTENT */
  margin: 0 auto;
  border: 1px solid yellow;
  }

.wrapper .inner-content .menu {
  float: left;
 }

.wrapper .inner-content .signin {
  float: right;
  }

.clearfix {
  clear: both;
  }
<div class="wrapper">
   <div class="inner-content">
       <div class="menu">Main Menu</div>
       <div class="signin">Sign In</div>
       <div class="clearfix"></div>
   </div>
</div>

Answer №2

include display = inline-block; within

.sf-menu li {
        background: silver;
        display = inline-block;
    }

However, I recommend utilizing bootstrap's navbar for a simpler solution. It is straightforward to implement using the navbar component.

    div#sticky-top-head {
        position: fixed;
        top: 0;
        width: 100%;
        z-index: 999;
    }
    div#sticky-top-head #block-superfish-1 {
        width: 100%;
        background: silver;        
        height: 21px;
        color: #eee;
        font-size: 12px;
        box-shadow: 1px 1px 2px 1px #1e1e1e;
    }
    .sf-menu li {
        background: silver;
        display:inline-block;
    }
    .user-logout {
        position: fixed;
        right: 23%;
        top: 0;
        z-index: 999;
        margin-top: 1px;
    }
    .user-logout a.sf-right {
        color: white;
        margin-right: 14px;
    }
    .user-logout a#signout {
        position: absolute;
        left: 105px;
        top: 0;
        width: 75px;
    }
<head>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<div class="container">

<div id="sticky-top-head">
    <div id="block-superfish-1">
        <ul id="superfish-1" class="sf-menu">
            <li class=""><a href="#">Contribute</a>
                <ul class="sf-hidden"><!--li items -->
                </ul>
            </li>
            <li class=""><a href="#">My Lists</a>
                <ul class="sf-hidden"><!--li items -->
                </ul>
            </li>
        </ul>
    </div>
</div>

<div class="user-logout">
    <a id="username" class="sf-right" href="/user">Mattc</a>
    <a id="signout" class="sf-right" href="user/logout">Sign Out</a>            
</div></div>

Answer №3

Maybe consider using float:left and float:right instead? Since your container is fixed, the contents should stay in place without needing to specify it.

Answer №4

I didn't quite grasp it, but are you in search of something along these lines?

    div#sticky-top-head {
        position: fixed;
        top: 0;
        width: 100%;
        z-index: 999;
    }
    div#sticky-top-head #block-superfish-1 {
        width: 100%;
        background: silver;
        height: 21px;
        color: #eee;
        font-size: 12px;
        box-shadow: 1px 1px 2px 1px #1e1e1e;
    }
    .sf-menu li {
        background: silver;
        display:inline-block;
    }
    .user-logout {
        position:absolute;
        right: 23%;
        z-index: 999;
        margin-top: 1px; 
        display:inline-block;
    }
    .user-logout a.sf-right {
        color: white;
        margin-right: 14px;
    } 
    .user-logout a#signout {
        position: absolute;
        left: 105px;
        top: 0;
        width: 75px;
    }
<div class="container">

<div id="sticky-top-head">
    <div id="block-superfish-1">
        <ul id="superfish-1" class="sf-menu">
            <li class=""><a href="#">Provide Input</a>
                <ul class="sf-hidden"><!--list items-->
                </ul>
            </li>
            <li class=""><a href="#">My Listings</a>
                <ul class="sf-hidden"><!--list items-->
                </ul>
            </li>
        </ul>
    </div>
  </div>  

  <div class="user-logout">           
      <a id="username" class="sf-right" href="/user">Mattc</a>
      <a id="signout" class="sf-right" href="user/logout">Sign Out</a>            
    </div></div>

Answer №5

Here's a simple demo using flexbox

.sticky {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 30px;
    background: #ddd;
}

.container {
    margin: 0 auto;
    width: 100%;
    max-width: 960px;
    height: 30px;
    color: white;
    background: red;
    display: flex;
    align-items: center;
}

.nav {
    flex: 1 1 auto;
    width: 100%;
}

.user {
    flex: 0 1 auto;
}
<div class="sticky">
    <div class="container">
        <nav class="nav">Navigation</nav>
        <div class="user">User</div>
    </div>
</div>

You can also view it on jsfiddle

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

Create a dynamic JavaScript animation with frames

Is there a simpler method to animate a sprite with just 4 frames in HTML Canvas? Having a background in AS3 and C#, I found the code below to be overly complicated. It seems like I'll be spending hours trying to decipher it. Is there a more modern or ...

Is there a way to retrieve all the items from the array?

I've been on the hunt for this information but unfortunately, I haven't found a satisfactory answer yet. Currently, I am utilizing Json encode to transfer my array to JavaScript: In PHP while($row = mysql_fetch_assoc($select)) { $event_day = $ ...

PHP is struggling to retrieve the value of "img src"

CSS <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <form action="./test2.php" method="POST"> ...

Tips for aligning the menu in the center

I've been dealing with an issue while working on a webshop - I can't seem to center the top menu properly. Here's the code snippet in question: <div id="webshop-topmenu" class="row logoRow ProductMenu_TD"> <div align="center ...

The floating property doesn't appear to function properly in Internet Explorer versions 6 through 8 when

I've noticed that my website in Dutch looks great on Firefox and Safari (both Mac and PC), as well as Chrome (Mac tested, PC untested), but for some reason it's not displaying correctly on IE 6-8 (which is unavailable on Mac). I suspect there may ...

Using JQuery to dynamically set dropdown option values from a JSON object

I have an HTML code snippet: $.ajax({ type: "POST", url: "hanca/hanca_crud.php", dataType: 'json', data: { id_hanca: id_hanca, type: "detail_hanca" }, //detail_hanca success: function(data) { var teks = ""; $.each( ...

Say goodbye to my HTML5 creations

I am currently working on an HTML5 grid project that involves implementing a rectangle select tool for use within the grid. Everything is going smoothly, except for the fact that when I attempt to use the rectangular select tool, the grid disappears from t ...

issue encountered when attempting to execute the function during the on-change event

While attempting to develop a todo app, I encountered an issue where calling the function markComplete on each checked or unchecked checkbox resulted in an error message stating cannot read property 'completed' of undefined. What could be the so ...

Ensure there is a visible separation between standard div elements and fixed div elements

I have two divs - one is fixed as the header for all pages, and the other is for content. Here is my code: <style> #print-head { display: none; } @media print { #print-head { display: block; position: fixed; top: 0pt ...

Two-column list using XSLT

I am seeking assistance with xsl transformation. The xml generated by SharePoint Server control is as follows: <rows> <row>A</row> <row>B</row> <row>C</row> <row>D</row> <row>E ...

Using the .each() method in jQuery to dynamically assign an attribute to a parent element in JavaScript

I'm new to JavaScript and jQuery and I need help transitioning my code from jQuery to pure JS. Within my HTML, there are multiple parent divs each containing a child div. My goal is to assign a class to both the child and parent elements, with the p ...

Utilizing jQuery Methods within Node.js

Recently delved into the world of node.js and created multiple pages (such as login, signup, blog, etc). If I desire an effect in which: 1. Hovering over the "News" button triggers a slider to appear downwards, To make adjustments to an image within ...

What are some effective methods for organizing HTML in a clean and orderly manner?

I have limited experience with HTML, but I need to layout a form in a precise manner similar to WinForms. Here is my attempt using the little CSS and HTML knowledge I possess: The issue lies in everything inside the white area being ABSOLUTELY LAID OUT .. ...

Issue encountered while transferring data using Ajax

I am a novice in the realm of ajax and currently immersed in a project that demands passing data from an ajax function to a php page for the purpose of dynamically generating and displaying modals. Although I am successful in creating the modal, I suspect ...

What is the best approach in Ruby for managing an unordered list with split columns and counting each

I am currently in the process of strategizing the layout for a webpage utilizing Bootstrap 4 within Ruby on Rails. Specifically, I need to create a sidebar submenu that can expand and collapse, feature bullet points, and include a counter. The desired out ...

Querying a key press action with jQuery based on the class attribute

I'm currently working on implementing some view behavior using jQuery. I need to be able to input directly into a table td and trigger an event when doing so. However, the event doesn't seem to be recognized. What steps should I take next? Belo ...

Retrieve data from a function using a jQuery plugin

Highlighted Text: <p>Hello, World!</p> Source Code: $('p').click(function(){ $(this).text('Text has been clicked'); }); Custom Plugin: (function($){ $.fn.modifyText = function(action) { return this.e ...

Navigating between windows in Selenium with Webdriver - Handling unnamed child windows

Currently, I am conducting acceptance testing using webdriver and codeception. This is a relatively new area for me, so your patience is much appreciated. My current challenge involves switching to a child window that pops up after clicking a button: < ...

The masonry plugin overlays images on top of each other

I have gone through similar questions but haven't found anything that applies to my specific case. Following an ajax call, I am adding li elements to a ul $.ajax({ url: 'do_load_gallery.php?load=all' }).done(function(result) { ...

Exploring the Wilderness: Full-screen Safari Adventure

Currently, I've been working on a website called In the 'Have you seen us' section, there is a full-width block with two columns; the right column features an image while the left displays text content. When viewing on desktop, I set the i ...