Generating a vertical bar adjacent to a bullet point in a list

Is there a way to add a vertical line next to each list item (except the last) without adding a new div? Adding a border line added an extra one that I didn't want.

Sample HTML:

<ul class="list">
            <li><span id = "home">Home</span></li>
            <li><span id = "about">About</span></li>
            <li><span id = "portfolio">Portfolio</span></li>
            <li><span id = "contact">Contact</span></li>
        </ul>

CSS:

.list li {
display: inline;
margin: 0px 25px 0px 0px;
white-space: nowrap;
font-family: Oswald;
color: white;
}

.list {
    text-align: right;
    padding-right: 2%;
    }

#home {
height: 50px;
background-color: black;

}

Answer №1

To create a right border on all the list items (li), simply add the style to each one and then remove it from the last item using the :last-child pseudo-class. This technique is perfect for handling dynamic content.

View Example Here

.list li {
    display: inline-block;
    border-right: 2px solid;
    padding: 10px;
}
.list li:last-child {
    border-right: none;
}

Alternatively, you can use the :not() pseudo-class to apply the border to all but the last item initially.

View Example Here

.list li:not(:last-child) {
    border-right: 2px solid;
}

Check browser support for these methods here.


If you're worried about browser compatibility, an alternative approach is to add a left border and then remove it from the first child element. Using :first-child may have better support than :last-child or :not, especially for older versions of Internet Explorer.

View Example Here

.list li {
    display: inline-block;
    border-left: 2px solid;
    padding: 10px;
}
.list li:first-child {
    border-left: none;
}

Answer №2

To create a unique effect, consider adding a border to each <li> element and then removing the border from the final element by utilizing the css last child selector: li:last-child targets the last li element in its parent.

You can enhance this technique by incorporating the :not selector within a single css block.

Check out the JSFiddle Demo for a visual example.

Answer №3

To enhance the list, one option could be to add a border-right to the entire list and then utilize jQuery to eliminate it from the last item.

If you are unsure of the ID for the last item in advance, it becomes slightly more complex, but you can still achieve the desired result by targeting the item "Contact" with the following code:

$("#contact").attr('style','border-right:none');

Answer №4

Here is a way to enhance your list items:

li:before {
    content: " - ";
}
li:first-child:before {
    content: none;
}

Alternatively:

li:not(:first-child):before {
    content: " - ";
}

Answer №5

Check out this straightforward and versatile solution that is compatible with older browsers: http://jsfiddle.net/d4n7pRy2/.

HTML:

<ul>
    <li>Home</li>
    <li>About</li>
    <li>Portfolio</li>
    <li>Contact</li>
</ul>

CSS:

ul > li {
    display:inline-block;
    padding: 5px 10px;
}

ul > li + li {
    border-left: 1px solid;
}

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

Stopping the sound when its' Div is hovered over

I've managed to successfully trigger the audio to play on hover, but I'm having trouble getting it to pause when my mouse leaves the area. Check out the code in action here: https://jsfiddle.net/jzhang172/nLm9bxnw/1/ $(document).ready(functio ...

Utilizing the Material Design card div element, however the elements inside the card are not properly aligned in the center

I'm attempting to include the align="center" attribute to this Material Design Lite card: <main class="mdl-layout__content" align="center"> <div class="mdl-cell mdl-card mdl-shadow--4dp portfolio-card"> <div class="mdl-card__title"&g ...

Looking for assistance with extracting only numerical values from a webpage using Selenium in Python

Website Elements <td style="font-size:20px;font-family:LucidaGrande,tahoma,verdana,arial,sans-serif;padding:10px;background-color:#f2f2f2;border-left:1px solid #ccc;border-right:1px solid #ccc;border-top:1px solid #ccc;border-bottom:1px solid #ccc; ...

Maximize background width with a gradient that is compatible with web-based email clients such as Outlook and Gmail

Recently, I was given the task of creating a newsletter to support cancer fundraising. Excitedly, I accepted the assignment but encountered a frustrating issue with web-based email clients (such as outlook.com and gmail.com) not accepting some of my stylin ...

What is the method to run a script within a particular div?

Here is an example of the DOM structure: <div> <button>Click me</button> <img src=#> </div> <div> <button>Click me</button> <img src=#> </div> <div> <button>Clic ...

Incorporating text onto an HTML webpage

Context: My program reads a file and displays it on an HTML page. The code below utilizes regex to extract errors from the file. Instead of using console.log for debugging, is there a way to display the results on the HTML page? When attempting: document ...

Determine whether the color is a string ('white' === color? // true, 'bright white gold' === color? // false)

I am facing an issue with multiple color strings retrieved from the database. Each color string needs to be converted to lowercase and then passed as inline styles: const colorPickerItem = color => ( <View style={{backgroundColor: color.toLowerC ...

Learn how to showcase a text file uploaded to a webpage with NODE js and HTML

<!DOCTYPE html> <html> <body> <form action = "/submit" method = "post"> Select a file: <input type="file" name="file"> <input type="submit"> </form> </bod ...

Developing an interactive contact page using bootstrap technology

I am looking for a way to structure my contact page using Bootstrap. https://i.sstatic.net/e3TCu.png If you're interested, the code for this layout can be accessed here: https://codepen.io/MatteCrystal/pen/xxXpLmK <div class="container" ...

Adding the "input-invalid" class to the input doesn't take effect until I click outside of the input field

When the zipcode field is updated, an ajax call is made. If there are no zipcodes available, I want to mark it as invalid by adding the "input-invalid" class. However, the red border validation only appears when clicking outside of the input field. Is ther ...

HTML: Issue with H1 alignment in class

I'm a beginner with HTML and I've been attempting to center the H1 using a CSS class, but it doesn't seem to be working for me. Here's my code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8> ...

Using Jquery to dynamically adjust the size of a div based on the width and height of the browser window

How can I dynamically change the height of a .test div based on the browser width and height? I want the value to update whenever the browser is resized. $(document).ready( function() { window.onresize = function(event) { resizeDiv(); ...

I am looking to eliminate the right padding from a group of div elements

Is there a way to remove the right padding from the class "no-border"? I attempted some CSS adjustments, but so far nothing has worked. I tried using "padding-right:none" without success. #menu-bar-container-2 { border: 1px solid gray; } .menu-bar-2 a ...

Tips for organizing dynamic table data following an append operation

Hey there! I'm currently working on a project involving sorting students after applying filters. Once the students have been filtered, I need to append classes and text to buttons as shown in the image below: https://i.stack.imgur.com/c9Mtm.png The HT ...

Tips for eliminating the default arrow on the HTML select tag?

I am currently utilizing vuejs along with tailwindcss and I am trying to figure out how to get rid of the default arrow from an HTML select element. Despite my attempts to remove it by using CSS properties like -moz-appearance, -webkit-appearance, -ms-appe ...

What could be causing this issue where the input button displays perfectly centered in Chrome, but not in IE and Firefox?

Have you noticed that this input button appears perfectly centered in Chrome, but in IE or Firefox it seems to be off to the right? You can see it as the second button on the right side of this page: Site : HTML : <div style="float:center;text-align: ...

Exploring JqueryUI tab navigation with the help of the <a> tag

I have come across several articles mentioning the possibility of navigating JqueryUI tabs using a button or anchor tag. Here is the method I am currently using: $("#vtabs").tabs(); $("#tabsinner").tabs(); $(".changeTab").click(function() { alert("as ...

Problem aligning ul within div element

I'm having trouble aligning my ul element in the center of a div. I tried using the margin: 0 auto method, but it's not working as expected. I've always wondered if setting the width of the ul within a div is necessary for using margin: 0 au ...

What is the method to process a JSON path containing a special character like "@"?

I'm attempting to retrieve JSON data from an API with the following structure. My current approach involves using JavaScript. The code snippet I have utilized is displayed below. <p>"stations": [</p> <p id="data"></p> ...

Using a Node.js module to shrink HTML files

Is there a way to minify HTML before rendering it? I'm aware of console minifiers like: html-minifier However, I am looking for a solution that can be implemented in the code itself. Something along the lines of: var minifier = require('some- ...