The issue
I am facing a challenge where I need to change the display of HTML code without altering its output. While I know jQuery can help me achieve the desired results, I am wondering if it is possible to do so purely with CSS?
What needs to be done
I want to show the input element from the HTML below while getting rid of the unnecessary label. The text "Username" and line break tag should not be displayed.
It's clear that using
label[for="user_login"]{ display: none; }
won't work as it would hide everything within the selector.
The original HTML
<label for="user_login">
Username
<br>
<input id="user_login" class="input" type="text" size="20" value="" name="log">
</label>
The desired HTML
<label for="user_login">
<input id="user_login" class="input" type="text" size="20" value="" name="log">
</label>
Preferred CSS over jQuery
$(document).ready(function(){
var username_label = $('label[for="user_login"]'),
username_input = username_label.find('input');
username_label.html(username_input);
});
Reason for avoiding jQuery
The login form layout varies based on screen size. Labels need to be hidden on smaller screens to save space but visible on larger ones. Utilizing jQuery would remove labels across all screen sizes.
While I can detect screen width initially and use resize event if needed, sudden changes (e.g., tablet moving from portrait to landscape) may cause labels to disappear when they shouldn't.
Query
Is there a way to achieve similar results with pure CSS?