Select the initial element from a group of elements with a specific attribute using CSS

Imagine we are faced with the following code snippet:

<node>
  <element bla="1"/>
  <element bla="2"/>
  <element bla="3"/>
  <element bla="3"/>
  <element bla="3"/>
  <element bla="4"/>
</node>

How can we select the first child element that has an attribute equal to 3? Initially, I tried using this selector:

element[bla="3"]:first-child

...however, this approach did not yield the desired result.

Answer №1

The :first-child pseudo-class will only target the very first child node. If that first child does not match the criteria of being an element[bla="3"], then nothing will be selected.

There is no equivalent filter pseudo-class specifically for attributes. One workaround is to select all elements and then exclude those that come after the first one (as explained in more detail here and here):

element[bla="3"] {
}

element[bla="3"] ~ element[bla="3"] {
    /* Revert the rule above */
}

This method works well for applying styles, but if you need to identify that specific element for reasons other than styling (especially if your markup is arbitrary XML rather than HTML), you may have to resort to using functions like document.querySelector():

var firstChildWithAttr = document.querySelector('element[bla="3"]');

Alternatively, you can use an XPath expression:

//element[@bla='3'][1]

Answer №2

:not([abc="5"]) + [abc="5"] {
  background-color: blue;
}
<div>
  <p abc="1">Element 1</p>
  <p abc="2">Element 2</p>
  <p abc="3">Element 3</p>
  <p abc="3">Element 3</p>
  <p abc="3">Element 3</p>
  <p abc="4">Element 4</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

Setting up the nth-of-type() function to dynamically target elements in sequence

I need to apply specific CSS styles to elements in a certain order within an image carousel. The carousel may contain any number of images, but only three images are displayed at a time. I want to style the first and third images differently from the secon ...

Switching out an element within a constrained array causes a momentary disappearance of the item

My playlist features artwork images that can be clicked. Upon clicking an image, the current track is replaced by the new selection in the array. An issue arises when Ember re-renders the items, causing a brief moment where the replaced element disappears ...

Increasing the size of a card beyond the container using the CSS PRE tag

I am facing an issue with a card on my webpage. It contains a pre tag inside it, which is causing the size of the card to extend beyond its container, resulting in a horizontal scroll bar appearing. How can I make the content within the pre tag scroll hori ...

Utilizing JQuery click function to dynamically modify CSS styles

Snippet: <div id="r1242"> <img class="trans" src="http://sakshamcomputer.com/images/printerLogo.jpg"/> <div class="labels" id="p1242"> <strong>Available Printers:</strong><br><br> ...

Cutting diagonally through a text along the trajectory of an SVG line

Is it feasible to have diagonal text cutting effect when an svg line overlaps with the text? Below is the code snippet and a visual representation of the desired outcome. <!DOCTYPE html> <html> <head><title></title> <sty ...

Displaying videos in full screen using HTML5

I am attempting to create a fullscreen webpage with a video using the following code: <video id="backgroundvideo" autoplay controls> <source src="{% path video, 'reference' %}" type="video/mp4"> </video> ...

The buttons in the Bootstrap modal are unresponsive

I'm attempting to create a modal navigation bar for mobile view but encountering an issue. When I click on the icon bar or menu bar in mobile view, none of the buttons inside the modal are responding. I'm quite stuck on this and would appreciate ...

Ways to display an icon in Angular 10 only when it is hovered over

Just getting started with Angular and still learning the concepts. Trying to figure out how to show an icon only when it's hovered over. Can anyone assist me? Sorry, can't share the code because of company rules. The icons are for sorting and ...

creating a multi-page form using HTML and JavaScript

I need help creating a multi-page form with a unique tab display. The first page should not have a tab-pill, while the following pages should display tabs without including the first page tab. Users can navigate to the first page using only the previous b ...

Tips on personalizing chosen date formats within the Mantine DatePicker?

I've integrated the Mantine DatePicker into my web application and now I want to customize the default style for when a user selects a date. Here's the current style: https://i.sstatic.net/hjQOi.png This is what I'm aiming for: https://i ...

No matter how tall I make it, the scroll bar just won't stop showing

Check out this fiddle for more information: http://jsfiddle.net/LAueQ/ Even after increasing the height of the campaignDiv div, the scrollbar keeps showing up. If possible, I would like to avoid using overflow: hidden. ...

css avoiding code duplication with nested classes

I created a custom CSS class named button.blue: button.blue { background-color: blue; ... } button.blue:active { background-color: darkblue; ... } button.blue:hover { background-color: lightblue; ... } To implement this class, I use the following code: ...

Centering content vertically in CSS

I am facing an issue with aligning buttons vertically inside a div. The number of buttons may vary, but I want them to be aligned vertically in a single column. Using "vertical-align: middle" does not seem to work. How can I achieve this? The height of th ...

One cannot focus on HTML if it's disabled

I am currently exploring options to prevent an input from receiving focus without disabling it entirely. I need the backend to still retrieve data, but I want to restrict user editing of the input fields at certain times. Disabling the input is not an opti ...

Rendering High-quality Images in Internet Explorer Browser

Currently, I am working on optimizing my website for high resolution monitors, particularly the new iPad. The site is already formatted to my liking, with images having increased resolutions while still fitting into specific DIVs. For instance, an image wi ...

Manipulate the css pseudo-element :after using jQuery's .siblings() method

How can I use jQuery to edit the :after element created by CSS? <div class="box"> <h3 class="social">Social</h3> <ul> <li><a href="https://www.youtube.com/" onmou ...

Tips on adjusting the size of a Materialize CSS datepicker to perfectly fit within a card

Currently, I am developing a login page in html utilizing the materialize css framework. In my design, I created a card where Login and Register are displayed as tabs within the card. However, one of the forms includes a datepicker from materialize which ...

What could be the reason for Firefox failing to display the border of a table when the tbody is

If Firefox isn't displaying table cell borders correctly in the presence of an empty tbody, a simple solution is to use the pseudo selector tbody:empty {display:none;} to hide the empty tbody element. This will ensure that everything is rendered as ex ...

How can I retrieve the inner html content of an element in HandsomeSoup?

Let's say the html I'm currently parsing includes an anchor tag: <a href="/here">this is what I want</a> Following the example provided in the package documentation, I can extract the href string "/here" using: links <- runX $ d ...

Looking for consistent vertical and horizontal scrolling behavior in a React project

I am in need of assistance as I lack the necessary expertise in ReactJs to transform this design. Currently, it is written in Jquery but I would like guidance on how to recreate it in ReactJs. Specifically, I want the left div with the topic for each row t ...