Guide on populating List Items <li> using CSS

My goal is to dynamically generate <li> elements from a CSS file, as opposed to manually defining them in the HTML.

Purpose/Goal: I have multiple HTML files, each containing a menu structured using LIST items (<li>).

Instead of adding each item individually to every HTML file, I would like to populate the list items using my CSS file. This way, if I need to add a new <li> in the future, I can do so without having to edit each HTML file separately. By simply adding one list item in the CSS file, it will automatically appear in all lists across my HTML files.

---CURRENT HTML CODE:

 <div id="menu">
    <ul>
      <li><a href="../index.html">Home</a> </li>
      <li><a href="../about-us.html">About us</a> </li>
    </ul>
 </div>

I am aiming to have <li> and <a> elements generated from CSS rather than hardcoded into the HTML file.

Answer №1

This task can easily be accomplished using JavaScript

<html>

<script>

   function generateList() {
      // set up necessary variables
      var list = document.getElementById("list");
      var item01 = document.createElement("li");
      var item02 = document.createElement("li");

      // create list items
      item01.appendChild(document.createTextNode("Item 01"));
      item02.appendChild(document.createTextNode("Item 02"));

      // append list items to the list
      list.appendChild(item01);
      list.appendChild(item02);
   }

</script>

<body>

<ul id="list"></ul>

<script>
   // calling the function
   generateList();
</script>

</body>
</html>

It is recommended to keep the script in a separate JavaScript file and simply call it whenever you need to create your list

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

The dimensions of the webpage determined by the size of the screen, with a scrollable

Let's set the stage: I've got an asp.net webpage that showcases dynamic data in a gridview. The challenge I'm facing is integrating this gridview within a div in the contentplaceholder of a master page, where the header and footer are dis ...

What is the best way to place an icon in the lower right corner of a design

I have encountered an issue while building the testimonial section below. The problem arises when the amount of text in each block is not the same, causing the quote icon to be displayed improperly in the bottom right corner. Sometimes, it even extends out ...

Updating an already opened HTML page from a Java application

I am working with an application that generates an HTML page using Freemarker. Once the webpage is generated, I use Desktop to open it from the application as follows: public void openPage() { if (Desktop.isDesktopSupported()) { try { ...

Navigate through the items in my subnavbar by scrolling the content

HTML Code <div id="subnavigation"> <?php $verbindung = mysql_connect("host", "user" , "pw") or die("Verbindung zur Datenbank konnte nicht hergestellt werden"); mysql_select_db("db") or die ("Datenbank konnte nicht ausgewählt w ...

Scrolling effect made with CSS for displaying a carousel of logos

Seeking assistance in creating a LOGO slider that seamlessly transitions at the end. I have resorted to duplicating a section to ensure continuous movement. I opted for using the translateX value for the animation effect. .box { width: 600px; //ove ...

Styling Tip: Aligning text to the baseline of a height-adjusted input field using CSS

As per the information from the Mozilla documentation, it states that I can align the text in an input field to the baseline if the element's display property is set to inline-block. I have made adjustments to the height of several input elements, bu ...

Utilizing JSON for HTML conversion in Codeigniter

public function getCrew(){ $search = $this->input->post('name'); if($this->input->post('ajax') && (!empty($search))){ $result = $this->model->getNames($search); foreach($result as $r){ ...

What are some methods for embedding HTML content onto a specific section of a webpage in a Node.js + Express application, without displaying it as the

Objective My goal is to insert a portion of HTML into a web page. Issue The page is not displaying properly with the desired styling and layout. I have a specific page that must be written in plain HTML rather than Jade, although I will still be usin ...

Enhance the appearance of your image by adding a stylish frame or border without altering its

Is there a way to add a frame to an image without causing it to shrink due to borders? Below is the code I have tried using: .img-border { height: 100%; width: 100%; position: absolute; z-index: 1; } <div class="border border-black i ...

Guide on using a single submit button for two separate HTML forms

Is it possible to use the same submit button for two different HTML forms on one page? I want both forms to have the same action and be handled by the same servlet. <form action = "add"> Enter first number: <input type = " ...

Sorting Columns in JQuery Datatables

Encountering an issue with JQuery DataTables (datatables.net) where it takes 2 clicks to sort the columns when there are only 2 rows in the table. The first column sorts on the first click, but subsequent columns require multiple clicks. Despite testing th ...

Incorporating a hyperlink and an image into a table sourced from a database

Currently facing a minor dilemma as I work on my project. Successfully connected to my database and managed to display the desired data on my website. However, I am now attempting to organize this data in a table. Initially, I had this code snippet: echo ...

How to prevent text from wrapping around images in WordPress

Hmm, I'm facing an issue at the moment: https://i.sstatic.net/MCesD.jpg I really need to ensure that the text never goes beneath the image. Since my website runs on Wordpress and every post is unique, I'm looking for a solution that can ...

Are the site-wide navigation links working on one page but not the other?

Currently, I am in the process of constructing a website on rails and have successfully integrated a site-wide navigation bar to display on all pages. However, an issue has arisen where the navbar is visible on both pages, yet the links and hover effects a ...

Unable to resolve issue with Display:flex in my CSS, despite numerous attempts

Here is the structure of my CSS and HTML: #TopBar{ display: flex; justify-content: space-between; z-index: 1; position: fixed; top: 0px; left: 0px; background-color: rgb(25,25,25); height:115px; width: 2000px; } #Logo{ top: 0px; height: 110px ...

Adjusting the alignment of a facial image on Canvas by selecting specific click-points

I have a unique idea for an app that allows users to correct a tilted face with just 2 clicks The concept is simple - users click on the middle of the nose and the middle of the eyebrows within the image to generate two points: eyebrowMiddle(x1,y1) and no ...

Ways to display or conceal a textbox depending on the choice made from a dropdown list

I'm a beginner in using jquery. I'm trying to create a dropdown menu with different options. When "Others" is selected from the dropdown, I want a text box to be displayed. Can someone provide guidance on how to achieve this? Here is the code sni ...

Issue with Bootstrap 3: Element refuses to remain within the defined width of the div container

While utilizing bootstrap 3 within a small div, I noticed that when I enlarge the window by dragging it horizontally, the fields (username and password input fields) that should remain inside the div end up shifting towards the center of the window. Can an ...

What is the reason for min-content not being compatible with auto-fill or auto-fit?

My confusion lies in the fact that this code snippet works: .grid { display: grid; grid-template-columns: repeat(4, min-content); } Whereas this one does not: .grid { display: grid; grid-template-columns: repeat(auto-fill, min-content); } I am ...

I've noticed that my alert is not appearing when I click submit. Can someone please help me figure out what I could

I am having trouble getting my alert to display when the form is not validating. It should show an alert message if the form is valid but it's not working as expected. I've spent hours trying to fix this issue with no luck. I appreciate any help ...