What is the best way to add style to a class or id with a dynamic index value?

This is my current style:

#detailTable4 tbody tr td:last-child {
  background-color: #7E0303;
}

I want to apply this style to all elements with #detailTable (regardless of the index at the end). Can I achieve this using just CSS?

Answer №1

To target elements with an ID that starts with a specific value, you can use the id attribute selector as shown in the example below. The [id^="detailTable"] selector will select any element whose id begins with detailTable.

[id^="detailTable"] tbody tr td:last-child {
  background-color: #7E0303;
}
<table id='detailTable1'>
  <tbody>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
  </tbody>
</table>

<table id='detailTable2'>
  <tbody>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
  </tbody>
</table>

<table id='detailTable4'>
  <tbody>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
  </tbody>
</table>

<table id='detailTable-abcd'>
  <tbody>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
    <tr>
      <td>One</td>
      <td>Two</td>
      <td>Three</td>
    </tr>
  </tbody>
</table>

Answer №2

Implementing CSS using id starting with selector:

[id^="infoContainer"] article section div:last-child {
  color: #2A9D8F;
}

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

Issue with positioning in Excanvas VML

After struggling through the implementation of excanvas on ie8, I managed to sort out the dynamic element issue. The once hidden elements are now rendering properly throughout most of the app, but just when it seemed like all was going well, another proble ...

The collapsed Navbar button is noticeably wider than expected

My Bootstrap 4 Navbar has a "Logout" button on the left side that appears too wide when the navbar is collapsed. How can I adjust the size of the "Logout" button when the navbar is collapsed? <!doctype html> <html lang="de"> <head> ...

Disabling animations in Reactjs with CSSTransition and Group Transition

Currently, I am experimenting with REACTJS to build a basic app featuring Transitions. In my project file, I have imported CSSTransitions and Group Transition. However, when attempting to implement CSSTransition for specific news items, the animations are ...

Slide up a row in the table using jQuery

Currently, I am in the process of creating a unique jQuery plugin that enables users to instantly delete records within a table. One key feature I would like to implement is changing the background-color of the deleted row to red and then smoothly sliding ...

Styling the ::selection inline with CSS allows for custom

I have a div element that I would like to customize the text selection style for. Currently, setting the style using CSS works perfectly: <div>Text text here</div> <style> div::selection { background: #FFF; color: #0 ...

bootstrap 4 - create a responsive two-column layout where the second column displays additional details specifically designed for mobile devices

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" crossorigin="anonymous"> <body> <div class="container"> <div class="row"> <div class="col-md-6 col-xs-12"> ...

Bootstrap grid columns cannot properly handle overflowing content

Utilizing bootstrap version 5.2.3 I'm facing a challenge not with bootstrap itself, but rather my limited comprehension of the flex system and overflow. Displayed below is a scenario where the first div has a fixed height of 100vh. My goal is to kee ...

Ensure that a link housed within a div does not possess dual hover effects

I'm struggling to achieve the desired hover state because there are two states triggered due to the link being inside the div button! Intended Unhovered State - Unexpected Hovered State - Desired Hovered State - HTML - <li> <div id= ...

Wordpress has issues with scrolling on touch devices

I stumbled upon a parallax website that incorporates multiple plugins (along with Wordpress - unfortunately) and functions perfectly on desktops. However, when viewed on a mobile device or in Device Mode using Chrome Inspector, scrolling gets disabled. Eve ...

Make sure the text remains separate while on the same line as the div element

I am currently using CSS to make part of my text bold, but I only want a specific portion of the line to be bold. How can I achieve this effect without making the entire line bold? Check out my code below: HTML <div> <div id="titleLabels"> ...

When viewing a page in IE 9 within a frame, not all CSS styles are properly rendered, causing it to enter Quirks mode

When a web page is loaded within a frame, not all of its styles are applied. The specific stylesheet used for the div elements is as follows: div.super{ position:absolute; font-family: Helvetica; font-size: 0.75em; padding:4px; overflo ...

Retrieving text using selenium automation software

I'm attempting to extract the text 'My name is' from this code snippet. Since the actual text may vary, I am unable to rely on it in the element locator. <span class="tabComboBoxName" id="tab-ui-id-1607647" aria-hidden ...

What is the best way to resize the entire width of my website?

I am currently working on a project where I want to limit the width of my website to only part of the screen. Here is a basic idea of what I am trying to achieve: http://prntscr.com/nq83xo I have attempted using flexbox and adjusting the body width, but s ...

"Utilizing a flexible grid system in Bootstrap to create 2 or

I am looking to create a layout with a variable number of columns using row span. Specifically, I want 3 columns on XS, 2 columns on SM (with the first 2 columns stacked on top of each other), and 3 columns on LG. I am unsure how to make the row span respo ...

Eliminated the right margin for desktop display

I'm looking to have a menu on the right side of my page. It looks good on mobile view with the menu aligned to the left and white space on the right. However, on desktop view, I want the menu to be aligned to the right with white space on the left. Ho ...

Interactive World Map with Fluid Motion Animation built using HTML, CSS, and JavaScript

I am in need of an uncomplicated way to visually represent events taking place around the globe. This involves creating a 2D image of a world map, along with a method to show visual alerts when events occur at specific geographical coordinates [lat, lng]. ...

Bullet points may not appear when printing in IE8/9 unless you select the option to print background colors and images

I created a jsfiddle on http://jsfiddle.net/hxsy6/ The problem I'm facing is that when printing the page in IE7, the bullet points are printed with the actual bullet image. However, in IE8/9, they do not print. If I go to Print Preview -> click th ...

Thumbnails being centered

I am facing an issue where I am attempting to center a group of fluid thumbnails, but they are always aligning left instead of center. Can someone help me with this problem? To better illustrate the problem, here's the JSFiddle link: http://jsfiddle. ...

The grid columns are not wrapping properly as anticipated

I currently have a bootstrap-4 column setup that looks like this: <div class="container"> <div class="row"> <div class="col-xs-12 col-sm-4">A</div> <div class="col-xs-6 col-sm-4"& ...

Troubleshooting a Border Problem in Bootstrap 5 Accordions

Having trouble with a Bootstrap 5 accordion header that has an icon drop-up menu. The bottom border disappears under the icon and I can't figure out why? HTML <div class="w-50 mt-5 mx-auto"> <div class="accordion favorites-f ...