Display the input field once the label is clicked

I am encountering an issue with the following code:

.input {
  display: flex;
  flex-direction: column;
  border: 1px solid #000;
  height: 50px;
  justify-content: flex-end;
}

.input > * {
  padding: 5px;
}

#login {
  visibility: hidden;
  height: 0px;
  transition: .2s
}

#login:active {
  visibility: visible;
  height: 10px;
}
<div class="input">
  <label for="login" id="login1">Login</label>
  <input type="text" id="login">  
</div>

My aim is to have the input display after clicking on the label without using JavaScript. Currently, it only works when I hold the mouse button down. Is there a way to achieve this behavior upon a simple click?

Answer №1

You can apply the focus pseudo-class to enhance input styling
Adding the required attribute and :valid to the input element will maintain its height even when not empty

.input {
  display: flex;
  flex-direction: column;
  border: 1px solid #000;
  height: 50px;
  justify-content: flex-end;
}

.input > * {
  padding: 5px;
}

#login {
  visibility: hidden;
  height: 0px;
  transition: .2s
}

#login:active, #login:focus, #login:valid {
  visibility: visible;
  height: 10px;
}
<div class="input">
  <label for="login" id="login1">Login</label>
  <input type="text" id="login" required>  
</div>

Answer №2

You can incorporate the checked pseudo-class to toggle elements by checking a checkbox or radio button, similar to how the jQuery click function operates.

.input {
display: flex;
flex-direction: column;
border: 1px solid #000;
height: 50px;
justify-content: flex-end;
}
.input > * {
padding: 5px;
}
#chkId {
display: none;
}
#login {
visibility: hidden;
height: 0px;
transition: .2s
}
input[type="checkbox"]:checked + input#login {
visibility: visible;
height: 40px;
}
<div class="input">
<label for="chkId" id="login1">Login
</label>
<input name="login" type="checkbox" id="chkId">
<input type="text" id="login" autofocus="true">  
</div>

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

What is the best way to create transparency within an icon's background without affecting the surrounding box?

Is there a way to set the background color of the icon inside the circle without affecting the transparent box around it? Currently, when I use background:white, it changes both the circle and the surrounding box. https://i.sstatic.net/mlV1I.png ...

Sending a file to an HTML5 <audio> element using HTTP headers

Is it possible to use the new <audio> tag in HTML5 to play a wav file, especially since it is currently only supported in Firefox? https://developer.mozilla.org/En/HTML/Element/Audio I am attempting to retrieve a wav file from the hard drive using ...

Displaying static files in a base template using Django is a breeze

Is there a way to link base template with static files from outside the app in Django? I have been trying to figure out how to make static files used by the base template accessible to other templates that inherit from it. I have checked the Django docume ...

Using jQuery to perform multiple actions with a single button

Recently, I've been developing a form for my website that involves implementing some slide-down functions. In this setup, there are 3 divs and jQuery code to dynamically add or remove these divs as needed. Upon clicking a button for the first time, ...

Display the keyboard on IOS when an input field is selected

I'm facing an issue that seems to have no solution. When using input.focus() on IOS, the keyboard doesn't appear searchMobileToggle.addEventListener('click', function() { setTimeout(function(){ searchField.focus(); ...

Is there a way to obtain a user's screen size without relying on a physical device or measuring the diagonal?

I'm looking for a way to display something at its actual size on a browser. For instance, if something appears to be 20cm on the screen, I want it to truly measure up to 20cm. Some methods involve considering the diagonal resolution of the screen, li ...

Having trouble displaying the cover image properly on mobile devices using Bootstrap/MDBootstrap, facing issues with the live server as well

My project includes a cover image in .jpg format that displays perfectly on desktop, but is invisible on mobile devices. When viewed through Live Server on Vscode, the image appears at all sizes including mobile, but when hosted on Netlify and accessed fro ...

Learn how to prevent two-finger swipe forward/backward on a trackpad using JavaScript or HTML

My app includes a functionality where the URL changes when transitioning from one state to another, for example from home/#state to home/#state2. However, I noticed that when I perform a two-finger swipe using the trackpad from home/#state2, the page navig ...

Excessive HTML and CSS overflow stretching beyond the boundaries of the page on the right

It seems like the issue lies with div.ü and div.yayın, as they are causing overflow on the right side of the page. When these elements are removed, the overflow issue goes away. Are there different positioning codes that can be used to prevent this? d ...

Using Node.js and the Jade templating engine, display the value of a passed variable

Asking such a basic question makes me feel guilty. app.get('/skumanagement/:id', function (req, res){ var options = req.params.id; // req.params.id = itemidx database.skuGetDetail(options, function (error, data){ winston.log('inf ...

Troubleshooting an unresponsive webview pop-up

I am attempting to display a pop-up dialog with an image inside from a webview upon clicking. However, I am encountering an issue where the pop-up dialog is not appearing as intended; instead, it opens in fullscreen within the webview or in a browser. Is i ...

What is the best method for customizing the color of angularjs spinners?

As I delve into developing an AngularJS application, my focus has been on seamlessly integrating an AngularJS spinner. Taking it a step further, I managed to tailor some of the spin-kit CSS properties to customize the appearance and behavior of the spinn ...

Populate div with straight lines running vertically or horizontally

I'm wondering how to create vertical or horizontal lines inside a div. I tried this in the past but lost the code, and searching for "fill div with lines" isn't giving me any relevant results. Here's an image to illustrate what I'm try ...

Avoiding conflicts between banners, text, and images for HTML/CSS design

I'm having an issue with the banner I created for my project. It seems to be overlapping with text and images, hiding behind them. How can I fix this? Unfortunately, I can't post the link to my project here due to other files present. The specif ...

How can I align an image and text alongside another image using HTML and CSS?

Having some trouble positioning an Image and Text next to another Image. Check out an example here: I attempted using floats, but it doesn't seem to be working. Here is the code I have: .left {float: left;} .right{float: right;} <div class="left ...

What is the best way to retrieve all the listed TV and film ratings in descending order? Using Django

Our Goal I aim to organize movies based on their star ratings and filter out those with ratings lower than a specified threshold. 2. Retrieve the IDs of Movies and TV shows mentioned in the view, fetch the data and score through URLs one by one. 3. Presen ...

Automotive Dealership API by Edmunds

I am having some trouble with the Edmunds API data. I can't seem to get the JSON to display when using the example code provided. It just shows up as a blank page. I am quite new to working with API's and would really appreciate some guidance on ...

Two objects precisely located in the same spot yet not visible simultaneously

There are two elements here. One is a transparent button, and the other is an image positioned behind it. The background property didn't work for me, so I attempted different variations without success: .top-container > button { background-ima ...

Looking for some assistance with an auto-calculation form, anyone able to help out

Is there anyone skilled in JavaScript here? I am looking for a form that includes the following fields which will automatically calculate and display the total amount as I input numbers into the fields. <input type="text" name="actualprice" value=""&g ...

Utilize Java to extract content from an HTML document by implementing xpath

Seeking guidance on extracting content from HTML using xpaths within a Java environment. Nokogiri in Ruby allows for this functionality, as demonstrated below: xpath = '/html/body/div/div[2]/div[2]/div/div[2]/div[3]/p' doc = Nokogiri::HTML(ope ...