I aim to change the label color when its corresponding input has :focus
I am struggling to understand why one works and one does not. Can someone explain what is going on and how to make the non-working example work?
Not working
<form>
<div class="form-group">
<label>Why does this not work</label>
<input name="name" type="text" class="form-control" required>
</div>
</form>
<style>
form input:focus + label {
background: rgba(0, 0, 0, 0.5);
color: red;
z-index: 2;
text-transform: uppercase;
}
<style>
Working
<form>
<div class="form-group">
<input name="name" type="text" class="form-control" required>
<label>Why does this not work</label>
</div>
</form>
<style>
form input:focus + label {
background: rgba(0, 0, 0, 0.5);
color: red;
z-index: 2;
text-transform: uppercase;
}
<style>
JSFiddle: https://jsfiddle.net/rjaqff2c/