Determining the Desired CSS Style with Target Label

Seeking advice on how to target the label for an input using css3. I attempted the following method:

input[type="text"]:focus label:target {
    color:orange !important;
}

Unfortunately, it doesn't appear to be working. Is this something that can only be achieved with jQuery?

Thank you in advance!

Answer №1

When using the :target pseudo-class, it's important to note that it may not behave as expected. Additionally, it is impossible for a label to exist as a descendant of an input element in HTML, so any attempts to do so will not work as intended.

If you are looking to reference a label associated with an input through parentage or the for attribute, CSS3 does not provide a direct way to accomplish this. Instead, you can only target the label if it is a sibling that comes after the input element. You can achieve this using one of the following selectors:

input[type="text"]:focus + label
input[type="text"]:focus ~ label

If the label is located elsewhere in the DOM structure, such as an ancestor or preceding sibling, jQuery can be used to navigate the DOM and find the desired label.

Answer №2

Utilizing CSS4, you have the ability to achieve the following:

!label /for/ input[type="text"]:focus,
!label input[type="text"]:focus {
    ...
}

The first method allows for using <label for="inputid">, while the second option permits

<label><input type="text"/></label>

Despite the fact that support for CSS4 is currently very limited, there's no downside in including it.

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 Photoswipe pswp class Not Getting Properly Cleared Upon Closing Image

My website has a Photoswipe image gallery from . The issue I'm facing is that the CSS class does not reset or clear after closing the gallery for the second time. For example, when a user opens item 1, the images are loaded into the picture div via A ...

What is preventing me from toggling classes?

Managing a compact to-do list where you have the ability to add new tasks and when clicking on an item, it should toggle between two classes while removing one. <ul id="myUL" class="todoList"> <li class='todoList card border-primary&apos ...

Efficiently Fill JQUERY Mobile Multi-Pages with Dynamic Content

A Question for JQUERY Mobile Beginners: In my basic JQUERY Mobile application, I have a simple setup with two pages. When the user navigates to PAGE2, I need to make an AJAX call to retrieve a JSON object that contains a list of people along with their DB ...

Ensure that the div is set to start on a new line without causing any sudden jumps to the

I have 3 divs that I want to display side by side. If the text inside each div gets too long, I would like it to break to a new line and have the next div stay beside it. <div style="float: left;">This is some text</div> <div style="flo ...

Position a div at the center of the page while aligning it inline with another div

Is there a way to center an element on the screen while having another element positioned to its left? I have tried using float left and adjusting the padding of the center element to match the width of the left element. However, this method may not work ...

Issue in Angular with IE11: Failure to trigger method when using ng-repeat on options within a Multiple Select element and ng-mouseover

Background Story: I have a requirement to show additional information about an option element within a select box when hovered over. My initial plan was to display this extra info below the hovering option, and it seems to work perfectly in all browsers ex ...

Unclear when notifying div content

After creating a PHP page, I encountered an issue while trying to select the inner text of a div and alert it with jQuery. Instead of getting the expected result, it keeps alerting "undefined." What could possibly be causing this? Here is the code snippet ...

CSS: Achieving Layered Effect with Child Image Over Parent Background Image

I have set a background image on the .section class in my CSS stylesheet. The background also has an opacity effect applied to it. However, I am facing an issue where the child img (which is a transparent png) is displaying behind the background image of t ...

Ways to create a horizontal navigation menu using jQuery UI

Hey there, I'm really enjoying all the jQuery UI features! I have a question about the navigation menu - for some reason, I can't seem to get it to display horizontally. I've been playing around with the CSS but I must be missing something ...

How can I apply an underline effect when hovering over navigation links in a responsive menu?

I've successfully added an underline effect on hover for the navigation menu. However, I encountered an issue with the responsiveness of the menu. When viewed on screens smaller than 600px, the underline effect covers the entire width of the block ins ...

What is the best way to show an image on the screen once a submit button is clicked?

I have a hidden loader-bar gif that I want to display when the user submits a form. Here is the code: <p class="loadingImg" style="display:none;"><img src="/design/styles/_shared/classic-loader.gif" alt="" /></p> Is there a way to ...

Can jQuery be utilized on a freshly opened window?

Solving this challenge has become quite the task, as I've been at it for hours now. Maybe one of you can provide some assistance. My current code appears like this: $('.clickable').on('click', function() { var id = $(this).at ...

Display the div based on the choices made in both select elements

Image of code is currently not accessible on this device Apologies for the lack of visual aid, I have coded on a different device. I am looking for a way to make the div appear based on certain options selected from both dropdown menus. Any assistance wit ...

Element width shrinks inside container query

Can you explain why using container-type: inline-size renders the span normally but collapses the button? <span style="container-type: inline-size; outline: 1px solid blue;"> This is a span </span> <button style="container-type: inli ...

Using Javascript to automate the organization of links based on predefined criteria is an

Picture This: A digital library full of categorized links, totaling 1000 in number. Diverse themes are covered by these links, making it a treasure trove of information. Displayed prominently at the top are buttons labeled ALL, MOBILE, CARS, BOOKS, and TE ...

Creating a function within document.ready using jQuery is a common practice for ensuring that

I am currently working on creating a straightforward function that will replace an attribute when called using onclick="changeYoutube('XXXXXX');" within a link tag. However, I am encountering an issue where it says calling "changeYoutube" is resu ...

Utilizing Phonegap to implement a QR scanner specifically designed for iPhones

Currently, I am in the process of developing an iPhone application through Phonegap. My goal is to incorporate a basic QR scanner into the app, and I'm searching for the most effective solution. Despite attempting XZing, I encountered numerous bugs al ...

What is the best way to align the navbar items at the center in Bootstrap when mx-auto is not working?

<nav class="navbar navbar-expand-sm navbar-light"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls ...

Centralize and confine the menu division

I have been working on creating a hover menu using CSS and Bootstrap, specifically for flex grid layouts. However, I have run into an issue where my menu is only as wide as its parent div. Furthermore, I am struggling to center the hover menu relative to ...

What is the best way to sum up array elements until they equal a target number, and then create objects using these summed values?

Suppose I have an array containing 5 different values for width, with a maximum width of 6 that needs to be reached. How can I iterate through the array and construct an object with those values each time it hits the maximum value without exceeding it? Le ...