Tips for choosing an element based on its sequential order in CSS ---Looking to

Within a div that has the class of product, there are multiple hierarchical elements present. Among these elements, two images can be found. The initial image serves as the product thumbnail, while the second image represents the product's rating. Both images lack any assigned classes, and altering the existing HTML code is not an option. Occasionally, these images may be enclosed within an <a></a> tag.

I am tasked with isolating the image product exclusively. This refers to the first occurrence of the img element within each div.product.

<html>
    <style>
    img {
    border: solid 2px black 
     }
    </style>
    </head>
    <body>
      <div class="product">
       <a href="#"><img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"></a><br>
       <img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/>     
      </div>
      <div class="product">
       <a href="#"><img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"></a><br>
       <a href="#"><img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/></a>     
      </div>
      <div class="product">
      <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"><br>
       <a hre="#"><img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/></a> 
      </div>
      <div class="product">
       <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"><br>
       <img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"></img>
      </div>
    </body>
    </html>

Attempts using first-of-type and first-child proved ineffective, as they disregard child and subchild elements, leading to the wrapped image being overlooked. How can I accomplish this task without modifying the existing HTML code?

Answer №1

What do you think about this?

div.product img[data-pin-nopin="true"]:nth-child(1){
    border-color:red;
  }
<html>
  <head>
<style>
  img {
    border: solid 2px black
  }
</style>
</head>

<body>
  <div class="product">
    <a href="#">
      <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true">
    </a>
    <br>
    <img src="http://www.vistaview360.com/menuimages/stars-5.jpg" />
  </div>
  <div class="product">
    <a href="#">
      <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true">
    </a>
    <br>
    <a href="#">
      <img src="http://www.vistaview360.com/menuimages/stars-5.jpg" />
    </a>
  </div>
  <div class="product">
    <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true">
    <br>
    <a href="#">
      <img src="http://www.vistaview360.com/menuimages/stars-5.jpg" />
    </a>
  </div>
  <div class="product">
    <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true" />
    <br>
    <img src="http://www.vistaview360.com/menuimages/stars-5.jpg" />
  </div>
</body>

</html>

Answer №2

To enhance the appearance of the initial image with a specific property called border, you can apply styling through the following code:

.product img[border] {
  border: 5px solid green;
}

Answer №3

You have the option to utilize both methods:

div.product > img:last-child {
    border-color: red;  
}
div.product > a:last-child img {
    border-color: red;  
}

The first rule will only affect the last image if it is a direct child of the div.product element.
The second rule will only affect the image within the last anchor tag, also as a direct child of the div.product element.

An example that demonstrates this concept:

img {
  border: solid 2px black 
}
div.product > img:last-child {
  border-color: red;  
}
div.product > a:last-child img {
  border-color: red;  
}
<div class="product">
  <a href="#"><img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"></a><br>
  <img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/>     
</div>
<div class="product">
  <a href="#"><img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"></a><br>
  <a href="#"><img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/></a>     
</div>
<div class="product">
  <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"><br>
  <a hre="#"><img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/></a> 
</div>
<div class="product">
  <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"><br>
  <img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/>
</div>

Answer №4

When following @poi, don't forget to incorporate :not() for customizing the style of rating images.

/* Styling for Product Images */
.product img[border] {
  border: 5px solid green;
}

/* Styling for Rating Images */
.product img:not([border]) {
  border: 5px solid red;
}
<div class="product">
  <a href="#">
    <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true">
  </a>
  <br>
  <img src="http://www.vistaview360.com/menuimages/stars-5.jpg" />
</div>
<div class="product">
  <a href="#">
    <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true">
  </a>
  <br>
  <a href="#">
    <img src="http://www.vistaview360.com/menuimages/stars-5.jpg" />
  </a>
</div>
<div class="product">
  <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true">
  <br>
  <a hre="#">
    <img src="http://www.vistaview360.com/menuimages/stars-5.jpg" />
  </a>
</div>
<div class="product">
  <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true">
  <br>
  <img src="http://www.vistaview360.com/menuimages/stars-5.jpg"></img>
</div>

Answer №5

Here is a recommended selector for your code:

.product > img:first-child, .product > a:first-child > img 

This selector targets the first direct image children of the .product class and all images inside the first direct anchor children of the .product class.

img {
    border: solid 2px black;
     }
.product > img:first-child, .product > a:first-child > img {
  border: solid 2px red;
  }
      <div class="product">
       <a href="#"><img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"></a><br>
       <img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/>     
      </div>
      <div class="product">
       <a href="#"><img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"></a><br>
       <a href="#"><img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/></a>     
      </div>
      <div class="product">
      <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"><br>
       <a hre="#"><img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"/></a> 
      </div>
      <div class="product">
       <img border="0" src="http://www.vistaview360.com/cameras/images/nikon_images/nikon-d5500-s.jpg" alt="Nikon D5500" width="140" height="140" data-pin-nopin="true"><br>
       <img src ="http://www.vistaview360.com/menuimages/stars-5.jpg"></img>
      </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

Is there a way to pass an HTMLDivElement as a child in React components?

Scenario Currently, I am in the process of developing a React application (rails-react) where the main component is called GameTracker. Within this parent component, there are two child components: EquipmentPanel and PinnedPanels. To achieve a specific fu ...

Vuetify's v-badge showcasing an exceptionally large number in style

Encountering an issue with using v-badge and v-tab when dealing with large numbers in a v-badge. Managed to find a CSS workaround by setting width: auto; for adjusting the size of v-badge to accommodate huge numbers, but now facing an overlap with my v-ta ...

Is there a way to adjust the height of an image to be responsive in tailwind css?

<!-- this is the section with the services description and an image of an egg --> <!-- this is the first section with the description --> <div class="flex flex-1 flex-col items-center lg:items-start p-4"> &l ...

Import files from local directory to iframe in Electron application

I am currently working on an application using Electron that allows users to preview HTML5 banner ads stored locally on their computer. At this point, I have enabled the ability to choose a folder containing all the necessary files. Once a directory is s ...

Is it possible to update the content of a page using only HTML and CSS, without the need for

Is it possible to update a page's content using only HTML and CSS without JavaScript? I came across this tutorial, but I'm not sure if updating the page on click without JavaScript is achievable. Check out this CodePen @keyframes hide1 { fro ...

What are the best ways to utilize props that are not filled?

Hey there!, how can I utilize these empty props (attributes) in a VUE component? This is the HTML snippet: app-page represents the component name <app-page table>one item</app-page> <app-page flex>two item</app-page> <app-page ...

Guide on breaking up a string into separate lines

Can someone help me with separating the strings in this line "Add Problem Report \n Add Maintenance Report \n Add Expenses Report \n Add Contract", into new lines? I have tried using br, pre, and \n but it does not seem to work. HTML ...

Start your slideshow automatically (with an option to toggle)

http://jsfiddle.net/r6uf38v4/ After the page loads, my goal is to have the slider begin automatically. However, I also want to provide the option for users to pause it using a checkbox and then resume the slider. $('#checkbox').change(function( ...

How can I create spacing between squares in an HTML div element?

Currently, I am working on my project using Laravel 5. I retrieve some integer values from a database and use them to create square divs in HTML. Below is the current output of my work. https://i.stack.imgur.com/sy2ru.png You may notice that there is spa ...

Exploring High-Density Mobile Devices with Bootstrap 3 Landscape Mode

After completing my first Bootstrap site using version 3, I noticed a display issue when viewing it on an iPhone 5 or LG G2 in landscape mode due to the pixel density. This was not a problem with the Responsive Grid system I previously used. Although I sp ...

Customizing text appearance with innerHTML in JavaScript: A guide to styling

Below is the code I have for a header: <div id="title-text"> The Cuttlefisher Paradise </div> <div id="choices"> <ul> <li id="home"><a href="#">Home</a></li> <li id="contact">&l ...

Adjusting panel height dynamically based on the size of the screen

Looking for a way to adjust panel height based on screen size? <div class="container-fluid"> <!--Heading--> <div class="row " align="center"> <h1 style="font-family: 'Roboto', sans-serif; font-size: 45px;"> Test Aut ...

CSS: Inaccurate positioning and border-radius causing gap between div and border-bottom

My goal was to create a card game with multicolored, double-sided borders, but I encountered issues when trying to implement this feature. Initially, I attempted to use an onclick event to change the border color, but it didn't work as expected. I fi ...

Animate linear-gradient using jQuery script

I have been searching for a circular progress bar plugin but haven't found one that suits my needs, so I decided to build my own using pure CSS: http://jsfiddle.net/9RFkw/ While it looks great, there are two issues I am facing: 1.) When at 25% in F ...

Generating Code Snippets in HTML5 Projects using Netbeans Integrated Development Environment

Is there a method to generate reusable HTML5 code snippets in Netbeans for projects? I know in Dreamweaver it's referred to as a Library Item. Essentially, I have chunks of code (header, footer, nav, etc.) that I want to utilize throughout the entire ...

Navigate to a Website Directly in Browser without Pop-up Menu

My website features two dropdown menus and a submit button that redirects visitors to specific URLs based on their choices from the dropdown menus. The problem: Upon clicking the "submit" button, a popup menu appears in the browser as shown in the screens ...

Several treeviews for selecting data

I need some help with a specific assignment. The back end code is all set, but I'm struggling with the UI component. The task requires two tree views, one on the left and one on the right. The left tree view will contain states with multiple children, ...

Highlighter for HTML - Prism

I have been using Prism for styling CSS and it's working well: <pre><code class="language-css">p { color: red }</code></pre> However, I am facing issues with highlighting HTML code: <pre><code class="language-html"& ...

Modify CSS progress circle to update the percentage of completion

I stumbled upon this code snippet, and although it works flawlessly, I encountered a limitation - I can only set the percentage to 25, 50, or 75. If I try to increase it to 85%, the circle fills up completely. Does anyone have any suggestions on how to res ...

Position the image on top of the details

I have an image with a spinner rotating around it through animation, accompanied by two boxes containing information as depicted in the uploaded image below. However, I would like to position the image above the boxes rather than beside them. .about-img ...