CSS Tip: Enhance your divs by adding a vertical pipe to each one

I am looking to insert a pipe character between links, with the exception of after the last one. The HTML is being rendered in such a way that it has the same class applied.

<!DOCTYPE html>
<html>
<head>
<style> 
p:last-of-type {
    background: #ff0000;
}
</style>
</head>
<body>

<h1>This is a heading</h1>
<div class="sample" style="display:inline-block">
    <div >
        <p class="para">link1</p>
    </div>
</div>
<div class="sample" style="display:inline-block">
    <div >
        <p class="para">link1</p>
    </div>
</div>
<div class="sample" style="display:inline-block">
    <div >
        <p class="para">link1</p>
    </div>
</div>


</body>
</html>

I want to display a Pipe character after each link, but not after the final one. How can I achieve this within this scenario?

Fiddle Link

P.S. : Although it's easy when dealing with different classes for each link, my case involves dynamic links without displaying a pipe after the very last one.

Answer №1

For achieving this effect, you can utilize the following selector: .sample:not(:last-of-type), which will specifically target all elements except for the last one.

It's important to keep in mind that when using a combination of a class name with last-of-type, it will apply to any element type that is not the last one with the specified class (in cases where there are multiple elements).

Check out the updated fiddle (included a pipe to the div to prevent it from inheriting color from the p)

.sample:not(:last-of-type) > div::after {
  content: ' | ';
}

An alternative approach is utilizing the last-child selector, like so: .sample:not(:last-child), which will also target all elements except for the final one.

With the use of last-child, the styling applies to the very last element regardless of its class or element type. Therefore, if there's another element after the last one with the sample class, it will be considered as the last one instead. You can see an example demonstrating this scenario in this fiddle sample.

View the updated fiddle (added a pipe to the div to avoid inheriting color from the p)

.sample:not(:last-child) > div::after {
  content: ' | ';
}

A third option involves utilizing the immediate sibling selector +, targeting all sibling elements with the provided class except for the first one.

p:last-of-type {
    background: #ff0000;
    display: inline-block;
}

.sample + .sample > div::before {
  content: ' | ';
}
<h1>This is a heading</h1>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
    </div>
</div>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
    </div>
</div>
<div class="sample" style="display:inline-block">
<div >
<p class="para">link1</p>
    </div>
</div>

Answer №2

Here's an alternate approach:

'Insert a vertical line before each link if it follows another link'

.sample + .sample .para:before {
  content: "|";
  margin-right: 10px;
  color: red;
}

.sample + .sample .para:before {
  content: "|";
  margin-right: 10px;
  color: red;
}
<h1>This is a title</h1>
<div class="sample" style="display:inline-block">
    <div >
        <p class="para">link1</p>
    </div>
</div>
<div class="sample" style="display:inline-block">
    <div >
        <p class="para">link1</p>
    </div>
</div>
<div class="sample" style="display:inline-block">
    <div >
        <p class="para">link1</p>
    </div>
</div>

Answer №3

It appears that this is what you are looking for.

.example:last-child div {
   border: none;
}
.example div {
  border-right: 2px solid #333;
  margin-right: 15px;
  padding-right: 12px;
}
<h2>Here is a title</h2>
<div>
<div class="example" style="display:inline-block">
    <div>
        <p class="text">click here</p>
    </div>
</div>
<div class="example" style="display:inline-block">
    <div>
        <p class="text">click here</p>
    </div>
</div>
<div class="example" style="display:inline-block">
    <div>
        <p class="text">click here</p>
    </div>
</div>
</div>

Answer №4

If you're looking to achieve this effect, one method is to incorporate the following CSS snippet:

.example:not(:last-of-type) {
    border-right: 1px solid black;
}

For a live demonstration, check out this JSFiddle link.

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

Struggles with embedding a table within a form while working with jQuery Mobile

Currently facing a problem trying to embed an HTML table inside a form in a jQuery Mobile environment. An error message pops up: Uncaught TypeError: Cannot read property 'not' of undefined The Chrome debugger points to this line in jquery.mobil ...

Instructions for transferring an object from a list box with Selenium WebDriver

My webpage has 2 list boxes, one on the left and one on the right, with transfer arrows. To move an item from the left list box to the right side, I need to select it from the left list and click on the arrow. I attempted to write the code below but it is ...

Tips on fetching the ID of the selected option using JavaScript without relying on jQuery

Looking to print the selected option ID using pure Javascript (not JQuery) for multiple select tags. If we have more than one select tag, how do we achieve this? <select onchange="showOptions(this)" id="my_select1"> <option value="a1" id="ida ...

Is the CSS and jQuery plugin combination blocking the AJAX call or event listener from functioning properly?

After finally getting superfish to work, I encountered a problem where an ajax call would not trigger due to the event listener being prevented. By commenting out the plugin initialization, the event fired but with incorrect data. Any insights on why this ...

Error: The function onSelect is not defined and cannot be executed by clicking on the HTML element with the id of "onclick" at line

I keep encountering this error message in my console: Uncaught ReferenceError: onSelect is not defined at HTMLAnchorElement.onclick (VM998 :14) This is the list data in my .html file: <ul class="nav navbar-nav"> <li& ...

Tips for highlighting HTML syntax within JavaScript strings in Sublime Text

Is there a Sublime package available for syntax highlighting HTML within JavaScript strings specifically? (Please note that the inquiry pertains to highlighting HTML within JS strings only, not general syntax highlighting.) Currently, I am developing Ang ...

What is the best way to format MarkDown content within a nextJs environment?

Markdown-it is successfully converting markdown to html in my Nextjs project. However, I am facing a styling issue with specific elements such as h2, h3, ul, and li. Here's the code snippet: <h1 className="text-3xl mb-3 leading-snug d ...

Having trouble getting the align-items-end property in Bootstrap 4 to function

After dedicating almost two full weeks to this project, I'm still struggling to achieve the desired outcome. I suspect that the issue may lie in the depth of the rows and columns I'm working with, but I'm open to any external perspectives or ...

Implement a Selection Feature in Angular

Currently, I am working on an application where users can add new rows with the same fields. One of the requirements is to allow users to add an option to a select element. While I have successfully implemented this in jQuery, I am facing challenges integr ...

Is there a way to position a heart-shaped animation element on the top right corner of a Bootstrap card image?

I am looking to incorporate an animation in the upper right corner of the bootstrap image, but I'm unsure about how to go about it. Here is the concept I have in mind: https://i.sstatic.net/rIvky.jpg Specifically, I'd like to include a heart an ...

What is the best way to utilize justify-content or align-self to perfectly center this?

Despite my efforts to center it, I'm struggling to do so. I've experimented with justify-content and align-items for each row and col class, but unfortunately, nothing seems to be effective. In the breakpoints (md, lg), I really need the 4 lorem ...

Is it possible to align images vertically with text in a multi-column Bootstrap 4 container?

Inquiring on how to achieve vertical center alignment of text and images for a website section similar to the one shown in this image: https://i.sstatic.net/s8gNh.jpg. The intention is to align the logos and the corresponding text of company names situated ...

Design a dynamic responsive navbar with dropdown functionality

I need assistance creating a responsive navbar dropdown that expands to full screen when viewed on mobile devices. I attempted to position it to the right using "right: 0", but it only goes to the right of the toggle button. How can I make it go to the rig ...

Confirming file type input is correct

Is there a way to check if the user has chosen an image when clicking the upload button using jQuery? Can I also set restrictions on the resolution of the uploaded image, for example allowing only images with a resolution above 900 x 400? Below is the form ...

Streamlined Infinite Scrolling Data Visualization Using SVG Implemented in HTML and CSS

I have developed a dynamic graph that continuously updates with new data points being plotted. Right now, I am utilizing requestAnimationFrame() to render the element positions 30 times per second, but performance can be sluggish with numerous SVG element ...

Enhance the slider's worth

Hey there, just wanted to mention that I didn't write this code myself. Another person did, and I'm a bit lost trying to figure out how to assign values to the three sliders. I don't want a default value; I want each slider to take its value ...

Is there a way to narrow down the font choices using HTMLPurifier?

- Currently in my project, I have incorporated an HTML-WYSIWYG Editor that allows users to utilize various Webfonts for styling their texts. The editor I am utilizing can be found at NiceEdit. It generates code snippets such as: <font face="c ...

Switching background image when hovering over a link purely through CSS

I'm attempting to achieve a similar effect on my website, like this example: (scroll down to "we specialize in") I am fairly inexperienced with HTML and CSS, and I'm working within WordPress which adds an extra layer of complexity. I started my ...

Create an endless loop of two distinct animations with animate.css

Lately, I've encountered an issue with animate.css while working on my latest project. What I'm aiming to achieve is to update a paragraph of text on my website every five to ten seconds using jQuery. However, I don't want the text to simpl ...

Width of flex container overflowing from relative position is at its full capacity

I'm attempting to create a horizontal line across an overflowing flex container by using an absolutely positioned child element with both the left and right set to 0. This requires the container to have a relative position to contain the absolutely po ...