Employ CSS to target the initial and final items within sets of identical elements

I created the following CSS styles:

div {
  width: 300px;
  text-align: center;
}
p:not(.vrt) {
  text-align: left;
}
p.vrt {
  writing-mode: vertical-rl; 
  display: inline-block;
}
div :nth-child(1 of p.vrt) {
  margin-left: 0;
}
div :nth-last-child(1 of p.vrt) {
  margin-right: 0;
}

to be used with this HTML structure:

<div>
    <p>This is some sample text which is not meant to be read.</p>
    <p class="vrt">幾見本言葉</p>
    <p class="vrt">幾見本言葉</p>
    <p class="vrt">幾見本言葉</p>
    <p>This is some sample text which is not meant to be read.</p>
    <p class="vrt">幾見本言葉</p>
  </div>

The quantity of elements with classes p and p.vrt can vary. It could be none, one, or multiple groups containing five or more consecutive p.vrt elements.

My goal is to select only the first p.vrt element in a group and then select the last p.vrt element specifically. Currently, my CSS code selects both the initial and final elements within a group. In the provided example, the "first" CSS would target the 1st and 4th p.vrt elements, while the "last" CSS would focus on the 3rd and 4th p.vrt elements.

Answer №1

To target the first element in each "group," you can utilize

:not(p.vrt) + p.vrt

This will select every p.vrt element that does not have a previous sibling of p.vrt.

For the last element, consider using

p.vrt:not(:has(+ p.vrt))

This targets every p.vrt without a succeeding p.vrt sibling.

div {
  width: 300px;
  text-align: center;
}

p:not(.vrt) {
  text-align: left;
}

p.vrt {
  writing-mode: vertical-rl; 
  display: inline-block;
}

:not(p.vrt) + p.vrt {
  margin-left: 0;
  color: #F0A;
}
p.vrt:not(:has(+ p.vrt)) {
  margin-right: 0;
  color: #FA0;
}
<div>
    <p>This is some sample text which is not meant to be read.</p>
    <p class="vrt">幾見本言葉</p>
    <p class="vrt">幾見本言葉</p>
    <p class="vrt">幾見本言葉</p>
    <p>This is some sample text which is not meant to be read.</p>
    <p class="vrt">幾見本言葉</p>
    <p class="vrt">幾見本言葉</p>
    <p class="vrt">幾見本言葉</p>
  </div>

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

Using jQuery to temporarily disable a div element

I need to implement a timer in a div that will disable it for a specific duration, such as 3 seconds. Here is the HTML code snippet: <div class="answ-container" align="center"> <div class="c_answer" id="1316" name="1" data-rcat-no="1"> ...

"My attempts to link various buttons in separate Bootstrap modals to their specific content are not yielding successful results

Greetings fellow members of Stack! Please bear with me as I am a newcomer here. I am currently working on a Business website for a friend using Bootstrap3. On our team page, we have multiple members and I would like to display additional details for each ...

Navigating the complexities of applying CSS exclusively to child grids within Kendo Angular may seem challenging at first

This image illustrates the grid layout I have created an angular UI using kendo that features a nested grid. I am trying to apply CSS specifically to the child grid without affecting the parent grid. However, no matter what CSS I write, it ends up being a ...

Resolving CSS Fluid Image Problem

Even though my site background is responsive and functions well, I am encountering problems with the images. I want them to appear "fixed" in the same position as the background, regardless of resolution changes. For example, resizing the browser from 990p ...

What is the best choice for my CSS: creating a block or an element using BEM methodology

** According to the official BEM website ** Create a block: If a section of code can be reused and does not rely on other page components being implemented. Create an element: If a section of code cannot be used separately without the parent entity (the ...

Ways to avoid divs overlapping on a mobile device?

If you visit this page: The recent searches section displays correctly on a computer, but it overlaps with the footer when viewed on an iPhone 6. What steps can I take to avoid this issue? ...

Creating sibling classes with tailwind using the map function

Currently, I am combining tailwind, react, and nextjs to develop a website. My goal is to implement the check radio button technique to dynamically display different content on the website without relying on JavaScript. To streamline data management, I am ...

The component's width appears to be constrained by the use of display flex

I am currently working on a reactjs application that includes a header and another component. Initially, everything looks fine, but when I apply display: flex to their div, it seems to reduce the width of the header. Here are some images showing the issue: ...

Struggling to align list-items in a horizontal manner

I'm having trouble aligning the list-items in my horizontal navbar. Can anyone assist me in identifying the error? Below is the HTML code, which has a complex structure due to the use of the Wordpress thesis theme and Cufon for font replacement: < ...

Struggling to incorporate a basic drop-down into my design despite implementing transitions

Looking to optimize space on a webpage, I am interested in creating a hover effect for topics that reveals sub-topics with a smooth ease-out animation. Here is an example of the effect I am aiming for: https://jsfiddle.net/170z6pj1/ I have been strugglin ...

Exploring ways to customize the input color of Material UI TextField when it is disabled [Version: 5.0.8]

I am having trouble changing the border color and text color when an input is disabled. I have tried multiple variations, as seen below: const textFieldStyle = { '& label': { color: darkMode?'#1976d2':'', } ...

What is the best way to implement media queries for mobile phones and desktop computers?

I've come across similar questions before but still can't wrap my head around it. Here's my dilemma: I want the index page of my website to display in desktop layout on desktops and mobile jquery on mobile devices. Currently, I have set up m ...

The CSS properties intended for ion-button elements are not taking effect

I'm attempting to set a background color when a ion-button is clicked or maintain the ion-ripple effect after it has filled the button in Ionic 4. I experimented with applying custom CSS states in global.scss, but encountered issues where the active ...

Tips for incorporating animated features into a bar graph using jQuery

How can I create a dynamic animated bar graph using jQuery? I am looking to make the bar slide up from the bottom, incorporating images for both the bar and background. I have already set the positioning in CSS and now need to add animation to make the bar ...

Why does the div inside the tbody element get automatically shifted outside of the tbody during rendering?

Below is a snippet of my code: <body> <table> <tbody> <div>test</div> </tbody> </table> </body> Upon executing the code mentioned above, it transformed automatically in ...

Adjusting image sizes in WordPress using CSS and HTML

I am currently working on a blog using the Marlene theme on Wordpress. For my posts, the default image resolution is 835x577, which suits my needs perfectly (see example post). However, I also have a "News" page where I want to display a list of all news ...

Using Tailwind CSS to center a NexJS <Image /> component within a modal

In an effort to style my MapBoxGL popup content, I am working on aligning the text above the image to the left and centering the image below within the popup. As shown in the image below, this is currently proving to be a challenge as I transition from usi ...

What is the best way to create separation between the href attribute of an a tag and the

Currently, I am utilizing Material UI in React and encountering an issue with the spacing between an <a tag and some text both before and after it. The lack of space is causing them to merge together. <Typography style={{ fontSize: 14, textAlig ...

Attempting to output numerical values using Jquery, however instead of integer values, I am met with [Object object]

I am struggling to figure out how to display the value contained in my object after attempting to create a Calendar using Jquery. I attempted to use JSON.toString() on my table data, but it didn't solve the issue. Perhaps I am not placing the toString ...

The HTML background color is refusing to change

I've been struggling to tweak the background color of a specific page. I attempted setting the HTML as the background color, using !important, but unfortunately, it didn't yield the desired result. The same goes for trying to set the background c ...