-1

How to find the previous sibling of HTML tag using CSS only:

<div class="form-group">
  <label for="userID">User ID</label>
  <input id="userID" type="text" class="form-control" placeholder="Enter User ID" tabindex="1"/>
</div>
seven-phases-max
  • 11,765
  • 1
  • 45
  • 57

1 Answers1

0

I don't think you can, you can however find the next sibling. You didn't even write from which element you want to search and what is the target but I assume you want to go from the <label> to <input>..?

Then just reverse the order they come in html and you can put back the order using css.

html:

<div class="form-group">
  <input id="userID" type="text" class="form-control" placeholder="Enter User ID" tabindex="1"/>
  <label for="userID">User ID</label>  
</div>

css

.form-group {
    display: flex;
    flex-direction: column; // if they should be one one line each
}

[for="userID"] {
    order: -1;
}
OZZIE
  • 6,609
  • 7
  • 55
  • 59