Separate each menu item by using the pipe symbol within the list tags

Can you help me style the menu list in CSS by adding a separator | between each element?

I attempted using content: "|", but it didn't produce the desired outcome.

The current look of the menu is as follows:

<ul>
  <li><a href="...">link</a></li>
  <li><a href="...">link</a></li>
  <li><a href="...">link</a></li>
  <li><a href="...">link</a></li>
  <li><a href="...">link</a></li>
</ul>

The current output is:

link link link link

But I am looking for:

link | link | link | link

I have a workaround in JavaScript, but leveraging CSS would be preferred. The menu is dynamically generated and I want to avoid altering its structure.

Answer №1

To achieve this effect, utilize a pseudo-element such as ::before or ::after:

li + li::before {
    content: "| ";
}

View a demonstration here.

If you are aiming for a line instead, consider using border-(left|right) along with appropriate padding or margin, as other users have pointed out.

Answer №2

To achieve this effect, I recommend using borders in the following way:

li a { border-right: 1px solid #333; display: block; }

If you wish to exclude the first item from having a border, apply the following style:

ul li:first-child a { border:none; }

Answer №3

Sharing a fiddle of your current progress would be beneficial, but the following code snippet should do the trick:

li:not(:last-child):after {
  content:"|";
}

View a live demonstration on JSFiddle.

Answer №4

Try out the following CSS code:

li {border-left: 1px solid black;}
li:first-child {border: none;}

Answer №5

Check out this JavaScript example.

li
{
    display:inline-block;
    list-style-type:none;
}
li:after
{
    content:"|";
    margin:0px 3px;
}

UPDATE

After realizing my mistake, I noticed another user had already provided the correct solution before me. Instead of removing my original code, I have made the necessary adjustments here: updated version.

li
{
    display:inline-block;
    list-style-type:none;
}
li+li:before
{
    content:"|";
    margin:0px 3px;
}

Answer №6

Here is an example

http://example.com/code123

ul li a {
    display: block;
    padding: 0 15px;
    border-left: 1px solid #000;
}

ul li:first-child a {
    border-left: none;
}

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

Issue with horizontal scrolling in ng-scrollbars occurs when scrolling from right to left

We are currently developing a single page application that supports two languages, one being right to left and the other left to right. For scrolling functionality, we have implemented ng-scrollbars, an Angularjs wrapper for the malihu-custom-scrollbar-pl ...

Having trouble retrieving the value from the input text in a dynamically generated table

How can I retrieve values from a dynamically generated table after clicking a button? Below is the code I have implemented to create a table with input fields: function createTable(rows, cols){ var body = document.body, table = document.createEl ...

Embracing JQuery for Frontend Development with Node.js

I'm currently enhancing my node.js skills by utilizing express, and I've encountered a situation that is causing me some confusion. Essentially, when a user requests a webpage and the backend sends them the HTML page, how can I incorporate a Java ...

Expand <a> to occupy the <li> element within the menu

Encountering a bit of a challenge... Is there a way to expand my <a> elements to completely fill the <li> containers they are placed in? I'm aiming to enhance the accessibility of the links for easier clicking. Check out my jsFiddle: htt ...

Using HTML to showcase various prompt messages based on the screen resolution

For the button click event, when clicked on desktop screens it will show a prompt message saying 'Hi'. On the other hand, when clicked on mobile screens, the prompt message displayed will be "Hello". ...

An alternative method to confirm the checkbox selection without physically clicking on it

Currently, I'm in the process of creating a basic to-do list and have been attempting to connect the task with its corresponding checkbox. My goal is for the checkbox to automatically be checked when the task is clicked. Instead of using HTML to add ...

The overflowing pop-up container

When I trigger a popup that dynamically generates a fixed background with a centered dialog, it works well. However, on smaller monitors like laptops, tablets, and phones, the content can overflow beyond the visible area due to its fixed container, making ...

Utilizing AJAX to upload a file and managing it through a Java servlet on the server side

I've been struggling with this particular issue for the past few days and haven't found any helpful resources elsewhere. I'm attempting to upload a file through an HTML form that includes other input fields: var formData = new FormData() ...

Creating a dynamic sidebar menu with clickable submenus through jQuery and CSS for a user-friendly experience

<div id="sidebar" class="sidebar"> <div class="slimScrollDiv" style="position: relative; overflow: hidden; width: auto; height: 100%;"> <div data-scrollbar="true" data-height="100%" data-init="true" style="overflow: hidden; width: a ...

Easily move elements using a jQuery click animation

Having trouble animating a div's top position by -130px to move it off screen? Can't figure out why it's not working? I'm fairly new to jQuery/Javascript. I have a button/hyperlink with the ID #NavShrink. When clicked, I want the div # ...

Setting minimum date on Bootstrap datepicker in Asp.net core: A quick guide

This form uses the native bootstrap datepicker input. I'm trying to establish a minimum date that the user cannot select by clicking, but my current method is ineffective. What is the correct approach to make this function properly? Here is the cod ...

Updating the text of a Mat-Label dynamically without the need to reload the page

In my application, there is a mat-label that shows the Customer End Date. The end date is fetched initially through a GET request to an API. Let's say the end date is 16-05-2099, which is displayed as it is. There is also a delete button implemented f ...

Tips for generating a JSON string with nested structure

Can someone assist me in generating a JSON based on the HTML elements provided below? { "AppName": "ERP", "ModuleDesc": [ { "Name": "Payroll", "AcCESSRIGHTS": [ { "Create": "Y", "Retrive": "Y", "U ...

Guide to redirecting traffic from one domain to another while preserving the path component

I am looking to create a redirection from one subdomain to another while keeping the same path intact. I am using a blogging platform and do not have access to a server, but I can modify the global head section of my blog. Currently, I am able to redirec ...

Why is the option value not visible in the dropdown selection list?

On my website, I am trying to use a date range to aggregate data from MySQL and display it based on the selected time frame. However, I am facing an issue where the values for each option in the drop-down menus are not showing up. Below is the PHP code I a ...

External website's Sharepoint modal dialog

Recently, I've been working with a Microsoft Sharepoint 2010 installation and noticed that when I search for items using the integrated search feature, the results contain a javascript call like this: <a id="SRB_g_b60c4a68_5348_49ec_846c_b9d069d67 ...

Is there a way to identify if you are at the bottom row of a column defined by CSS styling?

I have implemented the new CSS-style column to divide a single unordered list into multiple columns. Now, I am trying to figure out how to target the last element in each column using JavaScript or CSS. Here is my HTML: <ul> <li /> <li ...

Utilizing jQuery to pinpoint elements with personalized data attributes

I am struggling with a few elements that look like this <p data-lang-bg="Bulgarian" data-lang-en="English" >Text</p> <p data-lang-bg="Other bulgarian text" data-lang-en="Other english text" >Text 2</p> How can I write a jQuery sel ...

What is the best way to incorporate this PHP script into an HTML document?

Recently, I made the decision to create a booking calendar and integrate it into my website. However, I encountered an issue when trying to include it in the html file. I attempted using the <?php ?> tag, but unfortunately, it did not display as expe ...

Embracing right-to-left (RTL) languages

I am looking to create a website that can support both left-to-right (LTR) and right-to-left (RTL) languages seamlessly. My goal is to have the text direction switch automatically to RTL if the content is in an RTL language. Additionally, I want the input ...