Text remains unaltered on input focus

When I click on the input field, I expect the label to transform but it doesn't. However, using ".user-input:focus, .user-input:valid" changes the input, indicating that it should work with the label as well.

.user-input:focus+.user-label,
.user-input:valid+.user-label {
  color: yellow;
  transform: translate(10px, -14px) scale(.8);
}
<div class="container">
  <div class="input-group">
    <label for="user" class="user-label">Username</label>
    <input type="text" name="user" class="user-input" required>
  </div>
</div>

Answer №1

  • When working with CSS, we are limited to selecting the next sibling element and cannot target a sibling that precedes the selector's sibling in the DOM tree.
  • As a workaround, I have positioned the label element after the input element.
  • To ensure the label appears before the input visually, you can utilize the following CSS code snippet:
.input-group {
  display: flex;
  flex-direction: row-reverse;
  justify-content: start;
}

.input-group {
  display: flex;
  flex-direction: row-reverse;
  justify-content: start;
}

.user-label {
  transition: 0.5s;
}

.user-input:active+.user-label,
.user-input:focus+.user-label,
.user-input:valid+.user-label {
  color: yellow;
  transform: translate(10px, -14px) scale(.8);
}
<div class="container">
  <div class="input-group">
    <input type="text" name="user" class="user-input" required>
    <label for="user" class="user-label">Username</label>
  </div>
</div>

Answer №2

To achieve the desired effect, simply adjust your HTML structure by swapping your input and label tags. Keep in mind that you cannot apply CSS styles to elements within the same div that come before the target element.

HTML

 <div class="container">
      <div class="input-group">
        <input type="text" name="user" class="user-input" required>
        <label for="user" class="user-label">Username</label>
      </div>
 </div>

CSS

.user-input:focus + .user-label {
  color: yellow;
  transform: translate(10px, -14px) scale(.8);
}

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

Having trouble with your GitHub Pages site displaying properly on desktop but working fine on mobile?

I am facing an issue with my GitHub pages site on a custom domain. It works perfectly fine on mobile, but when I try to access the same URL from desktop, I encounter a DNS_PROBE_FINISHED_NXDOMAIN error. Any suggestions on why this might be happening? You c ...

Utilize CSS to create a column layout

I have some HTML output that I would like to style using CSS, but unfortunately I have no control over how the HTML is generated and cannot make any changes to it. Right now, I have a two-column layout set up here: http://jsfiddle.net/uvg6f3tr/ I'm ...

When attempting to select a HTML element within a <ng-template> tag using d3.select(), it will return null

I have a situation where I have an element < svg > inside an < ng-template > <div *ngIf="data == undefined; else elseBlock"> Sorry no data! </div> <ng-template #elseBlock> <svg id="areachart" width="100%" height="210 ...

Modal Pop-ups Failing to Open on Subsequent Attempts

My table consists of buttons on the right-hand side for a menu. The first button in the menu is supposed to open a modal box upon clicking. However, the first buttons in the subsequent rows do not function as expected and fail to open the modal. Refer to t ...

Tips for preventing the appearance of two horizontal scroll bars on Firefox

Greetings, I am having an issue with double horizontal scroll bars appearing in Firefox but not in Internet Explorer. When the content exceeds a certain limit, these scroll bars show up. Can someone please advise me on how to resolve this problem? Is ther ...

CSS for a Dropdown Menu with Multiple Layers

I've successfully implemented a dropdown menu, but now I'm facing the challenge of adding another level to it. Specifically, I want a second level to drop down when hovering over "Test3". What do I need to add or modify in the code to achieve thi ...

HTML makes column text wrapping automatic

Is it feasible to implement text wrapping into two columns or more in HTML using solely CSS? I have a series of continuous text enclosed within p tags only, structured as follows: <div id="needtowrap"> <p>Lorem ipsum dolor sit amet, ...&l ...

Adaptable Design - Concealing Facebook Page Plugin at Specific Screen Widths

Can someone help me figure out how to hide the Facebook page plugin on my website? I tried using CSS but it doesn't seem to be working. Here is the code snippet: @media only screen and (max-width:800px){ .fb-page{ visibility: hidden; } This is t ...

Creating a split button can be enhanced by incorporating a disabled button with a tooltip feature

I am attempting to create a split button using Material UI components such as ButtonGroup and Button. You can find more information about split buttons here. The issue I am facing is that the first button may need to be disabled and display a tooltip when ...

When submitting a form in HTML, ensure that the input checkbox returns 'On' instead of 'True'

My MVC3 app is using Project Awesome from http://awesome.codeplex.com/, but I'm encountering a strange issue with checkboxes. Inside a Modal popup, I have the following simple Html code: <input type="checkbox" class="check-box" name="IsDeleted"> ...

issues with conditional statement

I'm encountering an issue with the if statement not functioning as expected, and I can't seem to figure out why. The if condition always gets displayed regardless of the input. To troubleshoot, I initially used a temporary code snippet for the co ...

Is it necessary to include the Roboto font when using MUI?

After reviewing the Material UI documentation, it appears that users are responsible for loading the Roboto font: Material UI is designed to use the Roboto font by default. You may add it to your project with npm or yarn via Fontsource, or with the Googl ...

JavaScript generated form fails to submit

I am currently facing an issue with submitting form data to a PHP file when it is generated using JavaScript. I am unsure of how to resolve this problem. The form submission works fine on a test .html file by itself, but not when it is generated by JavaScr ...

Repositioning a div element within a different parent div

Is there a way to transfer the contents of one div to another using Javascript? I am looking to move the contents of .sidebar from .post to .carousel. Can someone provide guidance on how to achieve this using Javascript? I aim to transform the structure ...

IE11 blocking .click() function with access denied message

When attempting to trigger an auto click on the URL by invoking a .click() on an anchor tag, everything works as expected in most browsers except for Internet Explorer v11. Any assistance would be greatly appreciated. var strContent = "a,b,c\n1,2,3& ...

What is the best way to incorporate a unique box shadow onto my svg image?

While I've seen a similar question here: How to add box-shadow to a svg path circle shape, I am struggling to grasp how the transition from box shadow characteristics to SVG drop shadow characteristics was achieved. (Especially given that SVG Drop sha ...

The alignment of flexNav.js submenus is not consistent

I'm looking to implement the Flex Navigation plugin for a responsive menu. Although the plugin functions properly, I'm encountering an issue with the alignment of submenus under their respective parent items. You can view the problematic behavi ...

Having trouble with jQuery hover and AJAX loaded content not functioning properly?

When I receive content through AJAX, it looks like this: <div class="message" id="1"> blah <div class="details" id="1" style="float: left; display: none;">hidden information</div> <div class="spacer" style="clear: both;"&g ...

Jquery failing to function properly when scrolling

I have been attempting to implement a feature where an element changes its position to fixed after scrolling past a certain point. However, no change occurs when I scroll. In the <head> section of my code, I have included <script src="stiler/jquer ...

Sending data to an Express server in Node.js using JavaScript

My current project involves the use of javascript, html, nodejs, express, and mysql to fetch values from a database and send them back to the html interface. In this setup, the user inputs a domain in the 'btnSearch' textbox and clicks on the Lo ...