CSS: Targeting subsequent elements with a certain class

I'm looking for a way to target only the span elements with the class of selection, but only those that are located after a select element with an id starting with "cf-":

<select id="cf-1234567891322418287202826"></select>
<span class="select2 select2-container">
    <span class="selection">
        <span class="select2-selection"></span> // Apply CSS HERE
    </span>
</span>

<select id="cf-8298298876787346863834334"></select>
<span class="select2 select2-container">
    <span class="selection">
        <span class="select2-selection"></span> // Apply CSS HERE
    </span>
</span>

<select id="tu-656555"></select>
<span class="select2 select2-container">
    <span class="selection">
        <span class="select2-selection"></span> // DO NOT Apply CSS HERE
    </span>
</span>

I've tried the code below, but it doesn't seem to work. Can you spot my mistake?

select[id^="cf-"] + span.select2-selection {
    color: blue;
}

Answer №1

The element span.select2-selection is not directly next to select[id^="cf-"], so the selector needs to be adjusted as follows:

select[id^="cf-"] + span span.select2-selection {
   ...
}

This change is necessary because you are targeting a span.select2-selection that is nested inside another span element, which in turn is a sibling of select[id^="cf-"].

Answer №2

Try this out:

[id*="cf-"] .selection{
 //insert your CSS code here
}

Here's some extra information:

[id*="sometext"]

This code will target any element with an ID containing "sometext"

By adding a space after that, you can specify an element within this targeted element

Using .selection will specifically target the element with the class "selection" inside the element with the matching ID

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

Unable to interpret the remainder: '('static', filename='main.png')' within the context of 'url_for('static', filename='main.png')'

I am a beginner with django and I'm attempting to display an html page. I have a test HTML page and a URL for testing purposes. When accessing the URL, I encounter the following error: Could not parse the remainder: '('static', filena ...

Eliminate the table background effect on hover using HTML and CSS

If you need assistance with a contact form, please visit this link. There seems to be an issue with the hover effect on the fields causing them to turn dark grey. This may not align with your desired plain white background. You can view a screenshot https ...

PHP Administrator Access

When a user logs in as an admin on my website, I want certain options to be available. My concern is ensuring the security of this admin account. Currently, after the login process is authenticated, I set $_SESSION['status'] = 'authorized&ap ...

Issue with fullPage.js - Unable to scroll to footer on the last section of the page

I'm currently working with the fullPage.js plugin () and encountering some issues. While sliding through each section, everything functions as expected. However, when I reach the last section and try to scroll down to the footer below it, I encounter ...

The content momentarily flashes on the page during loading even though it is not visible, and unfortunately, the ng-cloak directive does not seem to function properly in Firefox

<div ng-show="IsExists" ng-cloak> <span>The value is present</span> </div> After that, I included the following lines in my app.css file However, the initial flickering of the ng-show block persists [ng\:cloak], [ng-cloak], ...

Create a CSS popup alert that appears when a button is clicked, rather than using

Is there a way to customize CSS so that the popup alert focuses on a button instead of just appearing like this? https://i.sstatic.net/r25fd.jpg I have implemented this type of validation for a text box: <div class="form-group"> <label class="co ...

Choose a unique text color

Having trouble with my HTML select form and modifying text color for specific options. In the provided example, I changed the color for the Tuesday option, but it only shows up when scrolling through the options. How can I make the color change visible for ...

Is there a way to automatically adjust the size of an input field after dynamic spans have been

I am looking to replicate the tagging functionality found on StackOverflow when posting a new question. In this case, spans are dynamically added using an input field located next to them. Here is an example: <div> <div id="multiple-span"&g ...

Assistance required for HTML, CSS, and jQuery

I have an image that I want to incorporate into my website in a unique way. Instead of simply uploading the whole image, I want to use HTML and CSS to add additional images and text onto the main background image. Despite reading multiple tutorials, I am s ...

The chat window is tall, stretching to its full height and remains stationary, not moving with the

I seem to be facing a CSS issue with the chat window not taking full height. It doesn't follow along when I scroll to the bottom of the screen. I'm stuck and unsure where to apply min-height: 100%; or if that's even the root cause of the p ...

Tips for dividing a JPEG image into separate sections using HTML

Looking for guidance on slicing a JPEG image into HTML. Unsure about the div positioning, can anyone provide advice? The JPEG is attached with this question. Thanks, Umair ...

Lisp and HTML Scraping: A Powerful Combination

Looking for some guidance related to web scraping in Common Lisp, specifically in extracting data from a webpage. I am utilizing drakma for sending the http request and chtml for scraping the data. The webpage I am targeting is . Here's a snippet of t ...

Conceal the navigation bar when scrolling to the top of the

Hey there! I'm looking for a way to hide my navigation bar when not scrolling, and then display it again once I start scrolling. Currently, I have two menus on this website: one with a white background and the other with a blue background. You can fi ...

When the user clicks, reveal the form with a seamless transition

Check out my jsfiddle here: http://jsfiddle.net/tz52u/9/ I'm curious about how to create a smooth transition for the 'on click' event, so that it gradually appears on the page instead of appearing instantly? Any assistance would be greatl ...

Having trouble getting background image transitions to work properly in CSS?

I'm encountering an issue with setting the transition CSS property for a hover effect, as it doesn't seem to be working correctly. Here is the relevant HTML: <div class = "menuItemBox" id = "Mughlai"> <p class = ...

Unable to accept the link ID in the modal

Presently, I am facing an issue with a modal that links a product with a customer and involves product discount calculations. The customer is automatically selected using the modal id, and the product is chosen through select options with a while loop. T ...

The file path selected in Internet Explorer's open file dialog box is not retained

Currently, my Uploadify setup allows users to upload files by selecting them through the open file dialog. However, I've noticed that Internet Explorer does not store the path once a file is selected and uploaded. This means that every time a user wan ...

Unable to set width for td element in media query is not functioning as expected

Is there a way to adjust the width of td elements for smaller screens within an email template? I have tried setting the style as important, but it doesn't seem to be working. CSS .alignmentColumn { width: 25% !important; //for desktop @med ...

Is there a way to get rid of those pesky red lines that appear when declaring variables in a .scss file in VS

Encountering red lines when defining variables in a .scss file using VS Code. The error message reads as follows: I have attempted various settings configurations in VS Code, but none seem to resolve the issue. Any suggestions for fixing this problem? ...

The Angular integration with Semantic UI dropdown does not display the selected value clearly

I have put together a small example to demonstrate the issue. http://plnkr.co/edit/py9T0g2aGhTXFnjvlCLF Essentially, the HTML structure is as follows: <div data-ng-app="app" data-ng-controller="main"> <select class="ui dropdown" id="ddlStat ...