Adjust the positioning of the navbar to the right using CSS styling

Here is my progress so far. I encountered an issue where my <li> elements were stacking in the left corner. How can I align them inline to the right corner?

HTML

<div id="page">
    <div class="header Fixed"> <a href="#menu"></a>Demo
        <ul id="account">
            <li><a href="#">Username</a>

            </li>
            <li><a href="#">Logout</a>

            </li>
        </ul>
    </div>
    <div class="content" id="content">
        <div id="first">
            <p><strong>This is the first section.</strong>

                <br />Notice how the fixed header and footer slide out along with the page.</p>
            <p><a href="#menu">Open the menu.</a>

            </p>
        </div>
    </div>
    <div class="footer Fixed">Copyright @ 2015 , All rights reserved.</div>
    <nav id="menu" th:include="${filename}"></nav>
</div>

CSS

html, body {
    padding: 0;
    margin: 0;
}
body {
     background-color:#fff;
     font-family:'Lucida Grande', Tahoma, Verdana, sans-serif;
     font-size:14px;
     line-height:22px;
     color:#666;
     position:relative;
    -webkit-text-size-adjust:none;
}
body * {
     text-shadow:none;
}
h1, h2, h3, h4, h5, h6 {
     line-height:1;
     font-weight:bold;
     margin:20px 0 10px 0;
}
h1, h2, h3 {
    font-size:18px;
}
h4, h5, h6 {
     font-size:16px;
}
p {
    margin:0 0 10px 0;
}
a, a:link, a:active, a:visited, a:hover {
    color:inherit;
    text-decoration:underline;
}
nav:not(.mm-menu) {
     display:none;
}
.header, .content, .footer {
     text-align:center;
}
.header, .footer {
     background:#0ca3d2;
     color:#fff;
     line-height:40px;
     -moz-box-sizing:border-box;
     box-sizing:border-box;
     width:100%;
    height:40px;
    padding:0 50px;
}
.header.fixed {
    position:fixed;
    top:0;
    left:0;
}
.footer.fixed {
    position:fixed;
    bottom:0;
    left:0;
}
.header a {
    background:center center no-repeat transparent;
    background-image:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADhJREFUeNpi/P//PwOtARMDHQBdLGFBYtMq3BiHT3DRPU4YR4NrNAmPJuHRJDyahEeT8Ii3BCDAAF0WBj5Er5idAAAAAElFTkSuQmCC);
    font-size:16px;
    font-weight:bold;
    display:block;
    width:40px;
    height:40px;
    position:absolute;
    top:0;
    left:10px;
}
.footer a {
    font-size:12px;
    font-weight:bold;
    display:block;
    bottom:0;
}
.content {
    padding:150px 50px 50px 50px;
}
#account li {
    list-style-type:none;
    top:0;
    display:inline;
    padding:0px 5px;
    float:right;
}
#account li a {
    text-align:right; 
    text-decoration:none;
}
#account li a:hover {
    color:#f0ad4e;
}
#intro, #first {
    height:400px;
}
#intro {
    padding-top:0;
}
#first {
    border-top:1px solid #ccc;
    padding-top:150px;
}

Answer №1

position: absolute ---The element is positioned relative to its first positioned (not static) ancestor element

The issue arises from the following style being applied:

.header a{
    position: absolute;
    top: 0;
    left: 10px;
}

As a result, all your <a> elements are being positioned according to the values of top and left because there isn't any positioned (non-static) ancestor element.

To resolve this, add position: relative to ul#account li and it will function as intended.

Here is a simplified example demonstrating the desired outcome (referencing the image link in comments)

.header {
    background: #0ca3d2;
    color: #fff;
    line-height: 40px;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
    height: 40px;
    padding: 0 50px;
}

.fixed{
    position: fixed;
    top: 0;
    left: 0;
}

#account{
    margin: 0;
    float: right;
    text-align: center;
}

#account li{
    float: left;   
    list-style: none;    
}

#account li a{
    display: inline-block;
    line-height: 40px;
    color: #fff;
    padding: 0 15px;
    text-decoration: none;
}
#account li a:hover{
    background: #216A9D;
}
<div class="header fixed"> <a href="#menu"></a>Brand Name
    <ul id="account">
        <li><a href="#">Username</a>

        </li>
        <li><a href="#">Logout</a>

        </li>
    </ul>
</div>

Answer №2

Experiment with using float:right; The display will improve when the code is displayed...

Answer №3

Instead of:

.header a

try using:

.header > a

along with some additional style modifications:

#account {
    float: right;
}
#account li {
    list-style-type: none;
    padding: 0px 5px;
    display: inline;
}

There were errors in your code, particularly in the float property you specified. Make sure to review: https://developer.mozilla.org/en-US/docs/Web/CSS/float

Additionally, avoid using position:absolute for .header a as it will affect all anchor elements within the header. This was likely the main issue.

I hope this explanation helps.

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 process for dynamically creating a new textbox inside a div when a kendo button is clicked?

I am just starting to explore kendo UI. Within my HTML, I have a div: <div id="newlabel"></div> Inside this div, I would like to dynamically add new labels with increasing numbers using the functionality of a kendo UI button. @(Html.Kendo(). ...

Merge web address when form is sent

I'm currently working on a search application using the Django framework and the Yelp API as my backend, which is functioning properly. However, I am facing some challenges with the frontend development, specifically integrating Leaflet.js. My search ...

What is preventing me from creating a table with a filter option?

I've been attempting to create a filterable table similar to this example. I followed the code from the w3schools website, but unfortunately, it does not seem to be working for me. When I type in text, nothing gets filtered. <div id="users-tab ...

Getting Your Content Centered Horizontally with Fixed Positioning in CSS

I would like the div to be centered horizontally with a fixed position, but it doesn't seem to work in my code. Can someone help me troubleshoot? Check out the demo here. HTML: <div id="fancybox-thumbs" class="bottom" style="width: 675px;"> ...

Activate on click using JavaScript

When a link with the class .active is clicked, I want to use a JavaScript function to deactivate any other active links. The structure of the code should be as follows: <ul class="product"> <li><a href="#myanmar" class="active">Mya ...

What is the reason the font size in an iframe is not updating?

Is there a way to increase the font size of an iframe to 150% using CSS? I have tried setting it up in my stylesheet, but the text remains small. What could be causing this issue? Here are my CSS rules: #frame{ font-size:150%; } .frame { z-index:2000; } ...

Creating an application for inputting data by utilizing angular material and javascript

I am looking to create an application using Angular Material Design, AngularJS (in HTML), and JavaScript. The application should take input such as name, place, phone number, and email, and once submitted, it should be stored in a table below. You can fin ...

Generating text that remains in a fixed position while the page is being scrolled through, until it reaches a designated

I am attempting to replicate the scrolling effect seen on this webpage: The text follows the scroll direction and then halts at a specific point as you continue down the page. Could someone provide guidance on how to achieve this same effect? Thank you ...

Tips for optimizing Slider Code to showcase an expanded selection of slides

I am currently troubleshooting a slider code on my ASP.NET website. After examining the HTML, JS, and CSS from the page theme, I noticed that only two slides are being displayed. However, when attempting to add a new slider HTML and adjusting the CSS for t ...

Addressing Browser Incompatibility in React.js Event Handling

I've recently embarked on my journey to learn React.js by delving into various tutorials and documentation. However, I'm encountering a peculiar issue specifically in Google Chrome: https://i.sstatic.net/qODiP.png Interestingly, in Firefox, it ...

Generating a list of items to buy using a JSON document

So here's the json file I'm working with: [ {"task":"buy bread","who":"Sarah","dueDate":"2023-10-18","done":false}, {"task":"clean car","who":"David","dueDate":"2023-08-30","done":true}, {"task":"write report","who":"Jenny","dueDate":"2023-09 ...

Is there a way to reverse the hover effect on div elements?

Let's start by examining the code I've written: HTML: <div class="button_container"> <div class="inner_button"> <a href="#" class="button_text">Button</a> </div> <div class="button_side"> ...

Custom Line-height Mixin for Styling Efficiency

I'm currently utilizing a certain mixin to create font sizes in rem with fallback pixel values, while also determining a line-height that is 1.5 times the font size. .font(@size: 16px, @line: @size) { @remfont: (@size / 10); @remline: (@size / 10) * ...

Looking for a way to track the number of visits your website receives using Splunk?

Examples I've attempted: "url" | stats count index="indexname" "url" |stats count Is it necessary to establish logging on my webpage before I can access the hit count? ...

Setting the value of an input box using jQuery

As someone who is new to jQuery, I am encountering an issue with setting a default value for an input text box. Despite trying both the val method and the .attr method, neither has seemed to work for me. Below you will find the input HTML along with the tw ...

Customize WPBakery Accordion Background Color

I'm currently using WPBakery's Accordion section, version 5.4.7, to showcase a Gravity Form. I am attempting to modify the background color of the active accordion section to black. After exploring various forum examples, I have attempted to add ...

What is the best way to bring the borders closer to the text on both sides?

Is there a way to adjust the padding on the left and right side of my Autumn outfits header so that it sits closer to the text? I've already tried setting padding-left and padding-right to 2px, but the element remains unchanged. Below is a snippet of ...

If additional content has been included in the `div`, be sure to scroll all the way down to view it

I have a div that needs to automatically scroll to the bottom when new content is added. This is a common feature in chat applications where the user wants to see the latest messages without having to manually scroll down each time. How can I achieve this ...

Hide bootstrap card on smaller screens such as sm and md

Utilizing the Bootstrap 4.1 card component, I showcase categories within the right sidebar - check out a sample image of the card here: Card example image; When viewing on smaller screens, it's preferable to collapse this large card as shown in this ...

The page's dimensions exceed the size of the device screen

I created this basic HTML content using React <!doctype html> <html><head><title data-react-helmet="true"></title><style type="text/css" data-styled-components="" data-styled-components-is-local="true"></style>< ...