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

focusing on two different sections with a single hover effect

Is it possible to modify the styles of two different divs using CSS when hovering over one div? #down-icn { position: absolute; bottom: 0; right: 25px; color: #fff; -webkit-transition: color 0.25s ease; border-color 0.5s ease; -moz-transition: colo ...

The Role of Filling in a Process

I am looking to create a rectangle that fills up gradually every day, increasing by 1% each time. This is the basic concept. My main struggle is figuring out how to fill it up. I need the rectangle to increase by 1% of its width each day. So essentially, ...

The two <p> elements are being pushed to the right column, which is not their intended display

A snippet of less.css code is available at this link: #content { overflow: hidden; width: 100%; p { float: left; width: 465px; padding: 0px 25px 0px 35px; margin: 0px; br { line-height: 2. ...

Is it possible to choose only half of the elements using the :nth-child selector in

Consider this code snippet: <ul> <li>Hello Universe</li> <li>Hello Universe</li> <li>Hello Universe</li> <li>Hello Universe</li> </ul> Can we select exactly half of the total elements by ...

The toggle feature in jQuery doesn't hide content in the same manner as it was originally shown

Just diving into the world of jquery and I'm experimenting with the toggle function to display and hide a div, along with adding or removing an active class. http://jsfiddle.net/MAkyw/ I've encountered 2 issues: 1) The 'active' class ...

distinguish different designs for individual components in Angular 5

My project is divided into two main parts: one for public web pages and the other for an admin control panel. Each part has its own set of CSS and javascript files for custom templates. If I include all CSS and js files in index.html, they all load at the ...

Is there a way to make the header reach the full width of the page?

Is there a way to make my header extend across the entire page? I attempted using margin-left and right, but it didn't yield the desired outcome. Header.css .header{ background: green; height: 70px; width: 100%; display: flex; ju ...

A step-by-step guide on extracting the image source from a targeted element

Here is a snippet of my HTML code containing multiple items: <ul class="catalog"> <li class="catalog_item catalog_item--default-view"> <div class="catalog_item_image"> <img src="/img/01.png" alt="model01" class="catalog_it ...

Positioning of Ajax modal popups on smaller screens

In my Asp.net/C# web application, I have a modal popup that contains multiple placeholders. However, I am facing an issue when trying to access the modal popup on smaller screens like iPads or iPhones. The page only displays half of the modal popup, maki ...

What is the method to adjust the opacity of a background in CSS without affecting the entire container?

Although it seems simple, I am trying to design a website background with a container that has semi-transparent properties. When I include opacity: 0.5; within the #content code, the entire container, along with its content and text, becomes transparent. ...

Tips for convincing the React/MUI Theme Engine to apply namespaces to all MUI CSS rules?

Encountering an issue with React Apps imported as microfrontends. Our Landingpage (built in React/MUI) imports multiple Apps (created in React/MUI v4 or v5, designed to run independently with their own themes). The problem arises when each App creates its ...

Align the ion content (or text or label) to the center vertically

I am striving to achieve a similar look as shown in the following images: However, I am currently at this stage: Please note that the first image showcases a bootstrap form, while the second image displays my design using ionic. Although I prefer not to ...

Vertical alignment to the top is not possible for Inline-Block divs

When attempting to create a responsive 'grid' with two (div) panels that appear side by side on wide screens and stacked on top of each other on smaller screens, I came across an issue where the divs align at the bottom when displayed as 'bl ...

How to make a Material UI Dialog Box automatically expand to fit the Dialog Content Area

In my project, I am working on a Dialog component which has a maximum width and a minimum height. Inside the DialogContent, I want to have a Box element that stretches to fill out the remaining space of the DialogContent. The goal is to have a background i ...

Tips for detecting the height of a row with 2 columns in CSS

I am attempting to create two text/image boxes side by side, where one box dictates the height of both on desktop. In this scenario, the leftcol box should expand its background color to match the size of rightcol and center the content. On mobile devices, ...

What is the best way to prevent certain bootstrap classes from being applied across all elements?

After testing my HTML and CSS code on my local computer, it looks perfect. However, when I upload it to a server, the appearance changes. You can see it in action here: . The issue seems to be related to some bootstrap classes being activated differently ...

Trouble encountered with card flip style login form in Vue.js, as the card is not maintaining its position properly during transition animations

Need help styling a login/signup component in Vue.js where flipping between the two cards isn't working smoothly. The transition is causing one card to move down and right while the other comes in from the bottom right. It's hard to explain the i ...

I am interested in altering the color of table rows using either JQuery or CSS

Looking to update row colors based on grouping. For example: Color the TR accordingly for every 5 rows: First 5 rows in green (0-4 Rows), Next five rows in red (5-9 Rows), Following five rows in yellow (10-14 Rows) and repeat the pattern... ...

Different methods to disable scrolling when the mobile menu pops up

I have implemented a mobile menu option that appears when you click on the triple line logo, but unfortunately, users can still scroll while the menu is open. I've tried using position:fixed; but haven't been successful in preventing scrolling be ...

Guide to setting a background image on a div when hovering using jQuery

I'm having trouble adding a background image to this specific div element: <div class="logo-main"></div> Here is the script I've been using, but it doesn't seem to be working as expected: <script type='text/javascript& ...