List items are not appearing correctly on display

When attempting to design a basic navbar, I encountered an issue where the list items were stacking on top of each other instead of displaying horizontally as intended. The parent container has a position of relative, while the child is set to absolute.

body {
margin: 0;
padding: 0;
background-color: #eee;
}


.navbarMenu {
width: 100%;
background-color:#fff;
position: relative;
}
.navbarMenu li {
list-style: none;
text-align: center;
position: absolute;
display: inline-block;
right: 5px;
}
<ul class="navbarMenu">
  <li><a href="#">Display</a></li>
  <li><a href="#">Float</a></li>
  <li><a href="#">flexbox</a></li>
  <li><a href="#">Home</a></li>
</ul>

Answer №1

I updated the CSS by removing the absolute positioning and using margin-left instead of right. This change adds margins to the elements relative to each other, giving you the desired outcome:

Check out the updated code here!

body {
margin: 0;
padding: 0;
background-color: #eee;
}

.navbarMenu {
width: 100%;
background-color:#fff;
position: relative;
}
.navbarMenu li {
list-style: none;
text-align: center;
display: inline-block;
margin-left: 5px;
}
<ul class="navbarMenu">
  <li><a href="#">Display</a></li>
  <li><a href="#">Float</a></li>
  <li><a href="#">flexbox</a></li>
  <li><a href="#">Home</a></li>
</ul>

It's important to understand the distinction between relative and absolute positioning in CSS, as mentioned by others.

Answer №2

I want to arrange them closer together, similar to the layout of Twitter's navbar.

Is there a simple way to achieve this?

body {
margin: 0;
padding: 0;
background-color: #eee;
}

.navbarMenu {
width: 100%;
background-color:#fff;
position: relative;
padding: 10px;
}

.navbarMenu li  {
list-style: none;
text-align: center;
display: inline-block;
margin-left: 160px;
padding: 2px;

}
a:hover {
color: firebrick;
}
<ul class="navbarMenu">
  <li><a href="#">Display</a></li>
  <li><a href="#">Float</a></li>
  <li><a href="#">flexbox</a></li>
  <li><a href="#">Home</a></li>
</ul>

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

Trouble with DevExpress ASPxButton: Some Properties Fail to Update

It's been a while and I'm facing a tough challenge. I need to change the image source of an ASPxButton from the code behind, but it seems like the height, width, and border style are getting lost in the process (resulting in an image with its own ...

Unable to establish the origin for an element using JavaScript

Currently, I am developing a simple game in JavaScript that includes a grid and various cells. Here is the current look of the game which functions perfectly. However, I am facing an issue with setting the margin to 0 and wanting to center the canvas inste ...

How can you center a div at the top of another div?

I am working with a nested div structure and trying to center the inner content of one div within another. Is it possible to achieve this using CSS? <div id="outer"> bla, bla....... <div id="inner"> <p>Test</p> </div> ...

Teaching jQuery selectors to detect recently-added HTML elements

Unable to find a solution in the jQuery documentation, I am seeking help here for my specific issue. Embracing the DRY principle, I aim to utilize JavaScript to include a character countdown helper to any textarea element with maxlength and aria-described ...

Apply the cursor property to the audio element and set it as a pointer

I have a question: how can I apply a cursor style to my <audio> controls? When I try to add them using CSS, the cursor only appears around the controls and not directly on the controls themselves. Below is the code snippet: <audio class="_audio" ...

Adjust the parent's height to match the height of its content (CSS)

Could anyone assist me in figuring out how to adjust the height (red border) to fit the content (green border)? https://i.sstatic.net/l1p9q.png I'm struggling to determine which property to use, I'm aiming for this outcome but haven't had ...

"Using hashtags in your Pinterest sharing description is a clever way to

Hi there, I've been working on setting up social sharing with Pinterest on my website. Everything seems to be functioning correctly, except for one issue - the hashtags aren't showing up as they should. To illustrate the problem, I created a JSF ...

I have created a textbox with a button, but I am unsure of how to delete it

var emailFields = document.getElementById('emails'), addLinkButton = document.createElement('a'), fieldTemplate = emailFields.getElementsByTagName('div'), currentFieldCount = fieldTemplate.length, maxFields ...

The clearfix method seems to be ineffective when dealing with elements positioned absolutely

I have a div with position absolute and float left and right. I am trying to add an image below this div. The issue I am facing is that the image is overlapping with the first div. I want the image to be displayed below the first div without any overlap. ...

Steps for adding a border to kendo grid row hover

One of my tasks involved creating a grid using Kendo, and I wanted to display borders on a grid row when the cursor hovers over it. I attempted the following code snippet: .k-grid > table > tbody > tr:hover, .k-grid-content > table > tbody ...

Is there a way for me to display a gif similar to 9GAG on my

I'm looking to implement a feature on my website that allows me to pause and play a gif, similar to the functionality on 9gag. Can anyone provide guidance on how I can achieve this? I understand that I need to use both .jpg and .gif files, but my at ...

A seamless border encircling an inline span accompanied by consistent line spacing

I am trying to achieve a single contiguous outline around a <span> element nested within a <p> and <div>. I came across a solution here: CSS/Javascript: How to draw minimal border around an inline element? which works well except when the ...

Modify the active link color in a Bootstrap menu by adjusting the CSS

Hi, I'm experiencing an issue with my CSS Bootstrap menu. I am unable to change the active link color, meaning that when I click on a link and move my mouse pointer out of the area, the link background turns white. I would like it to remain transparen ...

Footer not positioned at the bottom of the page

I have been experimenting with different ways to keep my footer at the bottom of the page. Initially, I tried using position: static, but the footer ended up above the bottom of the page and the background was visible (similar to the image below). Then, I ...

Tips for preventing the sidebar from extending beyond the footer

I am facing an issue with my sidebar that follows as I scroll. How can I make it stop following when it reaches the footer? Here's what I have attempted so far: $(function() { var floatPosition = parseInt($("#left_menu").css('top')); ...

Animation fails to initiate when the object enters the viewport

I attempted to inject some enchantment into my project by implementing code from a tutorial found on this CodePen. However, I encountered an issue where the code only functions properly within that specific CodePen environment. Even after copying the same ...

Display the country code according to the option chosen by the user in the dropdown menu

To enhance user experience, I am implementing a feature where the user can select their country from a drop-down list and then have the corresponding country code displayed in a text field for easy entry of their phone number. Below is the list of countri ...

How can I delete a CSS class from an element?

Is there a way to remove the "ui-widget-content" class from the code below? It appears in multiple places throughout my code. Here is an example snippet: <pre> <form id="clientForm"> <div id="clientData"> <div id="gbox_grid_1" class=" ...

Is it possible to alter CSS based on the width of the webpage using javascript

We are currently developing a web application that will be deployed on various devices such as mobile phones, iPads, iPhones, and Android. Instead of using user agents to show different views, I prefer having CSS that adjusts based on the screen width. We ...

Enhancing BarGraph and Bars Size in Oracle ADF

We have created a web application using Oracle ADF. The application includes a page with a graph displayed below. Currently, we are looking to enlarge the bar graph by increasing its height, width, and size of the bars themselves. Difficulty 1: While we ...