HTML and CSS code: Navigation bar dynamically adjusts its size to fit and is capable of handling various HTML content

Is there a way to make li elements in a dynamically generated ul have equal width and fit on one line using CSS, without knowing the number of lis beforehand?

W3Schools provides an example of a fixed-width navigation bar here, but adding another li disrupts the layout instead of scaling to accommodate it.

Here is the example they provide:

CSS:

ul
{
  list-style-type:none;
  margin:0;
  padding:0;
  overflow:hidden;
}
li
{
  float:left;
}
a:link,a:visited
{
  display:block;
  width:120px;
  font-weight:bold;
  color:#FFFFFF;
  background-color:#98bf21;
  text-align:center;
  padding:4px;
  text-decoration:none;
  text-transform:uppercase;
}
a:hover,a:active
{
  background-color:#7A991A;
}

HTML:

<ul>
<li><a href="#home">Home</a></li>
<li><a href="#news">News</a></li>
<li><a href="#contact">Contact</a></li>
<li><a href="#about">About</a></li>
</ul>

Ideally, I would like to be able to add another li and have the menu adjust gracefully.

Answer №1

Although I strongly dislike this solution, if you absolutely need it to function using only HTML/CSS and automatically adjust width, a table could solve the problem.

Alternatively, I would suggest implementing some basic JavaScript to handle the width updates for you. If you are introducing additional options through JavaScript, integrating this feature should be straightforward.

UPDATE: Depending on the browsers you are targeting, you have the option of utilizing the display: table technique, although it is limited to IE8 and later versions.

DEMO

Answer №2

It seems like the solution may involve utilizing javascript...

By leveraging javascript, you have the ability to gather information on the window width and quantity of list items. With this data in hand, you can then dynamically set a fixed width using javascript.

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

Generate vibrant hues using Emmet shortcuts

I am currently using Vim version 8.2 with vim-emmet version 0.86. I am struggling to write the abbreviation for CSS as shown below: border: 1px solid #dcd2ba; Upon referencing the manual, I came across the instructions for css abbreviation which can be ...

Ways to retrieve the bounding rectangle of an element PRIOR to applying a transformation?

Is there a way to obtain the dimensions and position of an element using Element.getBoundingClientRect(), but without taking into consideration any transformations applied through the CSS property transform? I am looking for a method that provides the rect ...

Steps for retrieving a JSON file from a web browser containing data in the JSON format

I need to download a text/json file using a string that contains the data in JSON format. In my controller, I am adding an ArrayList containing objects of the Service Class to the model. Here is the code snippet: @RequestMapping("/Application.html") p ...

Is it possible to fix the rightmost column in a scrollable HTML/CSS table with Angular when using overflow-x?

Is there a way to lock the right-most column in an HTML CSS Angular table that is scrollable using overflow-x? I have attempted using the sticky property and also creating two tables side by side, but neither method has successfully achieved the desired ...

Steps for developing a user settings dropdown menu:

Seeking assistance in developing a user setting drop-down menu resembling the design of Facebook and Google on the right side. Please refer to the image attached for reference. Any guidance or support would be greatly appreciated. ...

Assigning a php variable within an html document by utilizing the file_get_contents() function

My email system is automated and sends an html file as an email. I use PHPMailer to bring that file into my email, like so: $mail->msgHTML(file_get_contents('mailContent.html'), dirname(__FILE__)); Before I add the mailContent.html in the PH ...

ERROR : The value was modified after it had already been checked for changes

Encountering an issue with [height] on the main component and seeking a solution Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '753'. Current value: '731'. I have th ...

The content spills over the edges, yet a scrollbar emerges to guide the way

I have created an Image Slideshow using HTML5 and CSS. The slideshow consists of a div with the property overflow:hidden. The images are animated to move from right to left within the div. While this functionality works smoothly in Chrome, I am facing an ...

Arranging divs in a horizontal line while keeping the content organized in a vertical manner within the footer

I've been searching for a while now, but I haven't found any solutions that actually work. This might be repetitive, so my apologies in advance. The issue at hand is aligning three divs horizontally to create a footer, while maintaining a vertic ...

Fixing the PHP AJAX issue with loading additional content

Hello, I've created an app where users can click a "Load More" button to display data from a database. However, the problem arises when all the data has been fetched and the button continues to appear. If the user clicks the button again, the same dat ...

Icons disappear from the navbar toggle when clicked on a small screen

Using Django and Bootstrap 4, I've created a navbar with a toggle feature. However, when viewed on a small screen, the toggle button does not display the icons as expected. Instead, it shows empty lines. Strangely, when the phone is rotated, the toggl ...

Transform every individual word in the paragraphs of the website into a clickable button with corresponding text on it

I've been developing a Google Chrome extension that extracts all the words from paragraphs within a web page and organizes them into buttons. This feature is just one part of a larger project I'm working on. You can check out a demo of this funct ...

What is the best way to create a tooltip that appears when a user hovers over text within an <ins> tag?

Alright, I've defined a custom <ins> tag with the following CSS: ins{ color:blue; font-weight:500; text-decoration:none; } ins:hover{ cursor: pointer; cursor: hand; text-decoration:underline; } I'd like to add a ...

Tips for incorporating seamless animation effects to an element with jQuery

As I work on coding the navbar for my HTML website, I'm incorporating Bootstrap for styling and using a jQuery function to change the color of the navbar dynamically. However, I want to ensure that the color transitions smoothly when it changes. How c ...

What could be the reason behind the appearance of borders in my modal window?

I can't seem to find the source of the 2 silver borders in my modal. I've checked the CSS code, tried using developer tools, and looked through the entire code but still can't figure it out. Here is the JSX code I'm working with: impor ...

Encountering a console error during the migration of a Vue App with VueRouter

I am currently in the process of integrating a standalone Vue application into a larger Java Spring web project. The Vue app relies on Vue router as a dependency. However, when attempting to load the main page, I encountered an error in the browser console ...

Troubleshooting issue: BS3 carousel not functioning smoothly in Firefox despite animate.min.css integration

I am currently using Firefox version 33.0 on Ubuntu operating system version 14.04. I have a website running locally (localhost) which includes a bootstrap 3 carousel. I have added the "animated pulse" class from animate.min.css to the images inside the "i ...

Press the delete button to remove both the box and text dynamically with JavaScript

My task involves managing a shopping list with delete buttons next to each item. I am trying to use JavaScript to remove both the button and the corresponding text when the button is clicked. So far, my attempts have been unsuccessful. I used document.getE ...

Transferring post view ID to a different controller in order to retrieve table information in Laravel

On my posts page, each post has a unique ID that corresponds to the posts table. I can access all relevant information about a post through the PostsController using variables like $post->id and $post->name. However, I also have another table called ...

What is the functionality of this pseudo element?

I am interested in understanding the purpose of the pseudo element and how it affects the layout without altering the existing markup. Check out the code demo div{ border: 1px solid #000; height: 300px; } div:before{ content: ""; ...