Unique CSS specifically designed for consecutive elements listed in succession

I have a list of notes in a document, but occasionally I may include some document updates (such as "Document closed", "Revision made"...).

Here is the basic HTML structure of a document and its notes:

<p class="document-note">My note</p>
<p class="document-note">Another note</p>
<p class="document-note">Third note</p>

If a note includes any updates, the HTML can appear like this:

<p class="document-note">My note</p>
<p class="document-update">Update</p>
<p class="document-update">Another update</p>
<p class="document-note">Another note</p>
<p class="document-update">Additional update here</p>
<p class="document-note">Third note</p>

If a document-update follows another document-update, I want to apply special styling to it (like reducing the margin on the bottom of the first item).

For example:

<p class="document-note">My note</p>
<p class="document-update">Update</p> <== A document-update comes after this item, apply special CSS only to this item
<p class="document-update">Another update</p>
<p class="document-note">Another note</p>
<p class="document-update">Additional update here</p> <== This document-update stands alone (no other document-update after this item), so no special CSS is needed here
<p class="document-note">Third note</p>

Is there a way to achieve this?

I've attempted using .document-update + .document-update, but it hasn't given me the desired outcome.

Update I've included the CSS I currently have and what I tried to accomplish:

.document-note, .document-update {
    padding: 15px;
    margin-bottom: 15px;
}

/* This styling should be applied only to elements followed by another .document-update as shown in my example above */
.document-update + .document-update {
    margin-bottom: 3px;
}

/* Custom classes such as changing color on .document-update */
/* ... */

Update #2 This is the effect I am aiming for (demonstrated with inline styles in the DOM): https://i.sstatic.net/OTWrV.png

Answer №1

It appears that this code closely resembles your image:

.ticket-message, .ticket-activity {
    padding: 15px;
    margin-bottom: 15px;
    background: #f2f2f2;
}

.ticket-activity {
  margin-top: 0;
  margin-bottom: 3px;
}
<p class="ticket-message">My message</p>
<p class="ticket-activity">Activity</p>
<p class="ticket-activity">Another activity</p>
<p class="ticket-message">Another message</p>
<p class="ticket-activity">Some activity here</p>
<p class="ticket-message">My message</p>
<p class="ticket-activity">Activity</p>
<p class="ticket-activity">Another activity</p>
<p class="ticket-activity">Another activity</p>
<p class="ticket-message">Another message</p>
<p class="ticket-activity">Some activity here</p>

Answer №2

Choosing the second item depending on the first is possible, however not vice versa.

Opt for using top margins in place of bottom margins.

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

Retrieve CSS height using Jquery, based on the declared value rather than the computed value

In a previous version of jQuery, the default behavior was different. When I use .css("height") and .height(), it gives me the computed height instead of what I actually want. I only need the height value if it is explicitly declared in the CSS for that ele ...

Toggle sidebar: expand/collapse

Looking for some assistance with my website project. I currently have two arrows to open and close the sidebar, but I would like one button to handle both actions instead. Can someone help me edit my code snippet to achieve this? Keep in mind that I am new ...

Enhance the appearance of your Vuejs application with conditional style formatting

I want to enhance a basic table by applying conditional CSS styles to the cells based on their values. For example: If area1 == 'one', then assign the td-one CSS style. <tr v-for="version in versions" :key="version.id"> < ...

Floating point expansion caused by the addition of padding

Currently working on a basic 2 column layout design for a website, but ran into a hiccup. Whenever I try adding padding to the left-floated column, it ends up expanding beyond the width that I have set. Unfortunately, I haven't been able to find a sol ...

Bootstrap carousel powered by AngularJS

Struggling to incorporate bootstrap, angular, and a carousel to showcase images from local folders on my drive. Unsure how to customize the angular file properly. The example provided in the link below fetches images from the Internet, but I need to modify ...

What steps are involved in creating a table using Bootstrap framework?

Hey there! I need your expertise on how to create a specific layout using bootstrap: codepen.io/anon/pen/bqJdQJ <div class="container-fluid"> <div class="row"> <div class="col-md-12"> <div cl ...

Utilizing Robot Framework to select CSS attributes

The Selenium documentation provides an example of how to use Get Element Attribute: ${id}= Get Element Attribute css:h1 id However, I encountered a problem with this selector: ${VISIBILITY}= Get Element Attribute css:visibility mySidebar I ...

Prevent the link from stretching to cover the entire width of the page

I'm looking for a solution that can consistently restrict the clickable link area to just the text within my h2 element. The problem arises when the space to the right of the text is mistakenly included in the clickable region. Here's an example ...

CSS :empty selector failing to target elements

I have a situation where I utilized the :empty selector within the .error class. However, I'm encountering an issue where even if there is no content inside the div with the error class, the error class does not get removed entirely. Upon examination ...

The footer seems to be malfunctioning

Let's start with the code: CSS: *{ margin:0; padding:0; } body,html{ height:100%; } body{ background:url('../images/bg.png'); background-position:center; background-repeat:no-repeat; background-attachment:fixed; height:100%; ...

Footer positioned correctly with relative DIV

I find myself in a state of complete confusion. Coding is not exactly my forte, so I suspect I have made a significant error somewhere that is causing the following issue: My goal is to create a sticky footer. The footer does indeed stick to the bottom of ...

The Video Element Is Not Expanding to Fill the Entire Parent Div

My goal is to design a responsive layout where a video element spans the full width of its parent div with the class .hero. However, despite my best efforts, the video doesn't seem to be expanding as expected. Here's the relevant CSS code I' ...

Is it possible to run quirks mode in one frame while running standards mode in another?

Back in the IE6 days, I developed an old application that relies on frames instead of iframes and runs in quirks mode. Now, I'm wondering if it's possible to have one frame remain in quirks mode while another operates in standards mode when using ...

Tips for placing text on top of an image within a <p> element

I am grappling with a situation where I have a div containing text within <p> tags, and also an accompanying PNG image. My goal is to position the image to the left of the div so that it overlaps with the text. Despite my attempts at utilizing z-inde ...

Having several horizontal scrollbars at once on a single webpage

I stumbled upon a great example showcasing how to scroll a div on my page: Check out this Codepen - https://codepen.io/zheisey/pen/xxGbEOx Although, I am looking to extend this functionality to allow multiple divs to scroll together. Any suggestions? I ...

Styling your Kendo grid in ASP.NET MVC with custom CSS styling

I am facing a challenge with displaying two kendo grids side by side on a single view page. To modify the CSS style of a kendo grid using ASP.NET, I typically use: .HtmlAttributes(new { style="width:50%"}) However, when attempting to change more than one ...

Screen resolution for the background image on the banner

I'm currently working on a website that can be found at this temporary link: The main banner of the site features a background image with fading text, which looks great on standard desktop screens. However, when viewed on wider screens like 1600x1200 ...

Conceal / Modify / Incorporate a picture

My ultimate goal is to have a hidden box and image that will be revealed based on the result of other functions. The box should change its background color, display an image, and switch images depending on the function called. I am facing two issues with ...

How can I ensure that my navbar-right remains fixed to the right side of the screen and is always visible at the top of the page as I

When the page first loads: After scrolling down: I am attempting to create a fixed navigation bar at the top of the screen, so that it remains visible as the user scrolls down the page. However, I am facing an issue where my navbar brand is positioned to ...

Displaying the :before attribute while concealing the element solely with CSS

I have encountered a situation where I am unable to edit the HTML of a document, specifically a payment page on Shopify, for security reasons. Unfortunately, editing JavaScript is also restricted in this scenario. However, there is a workaround available ...