css label not behaving properly when clicked

Is it possible to reduce the label size when focused for text input and restore it when no text is entered or clicking elsewhere? Here is the code snippet I have been experimenting with:

div {
  height: 50px;
  width: 200px;
  border-bottom: 1px solid black;
  -webkit-transition: width 2s;
  transition: width 2s;
}
div:focus {
  background-color: transparent;
  width: 300px;
  tansition: ease;
  outline: 1px solid black;
}
label {
  font-size: 20px;
}
<div tabindex="0">
  <label>Three-colored border!</label>
</div>

Is there a way to achieve this effect using only CSS, or would JavaScript or AngularJS be required?

Answer №1

To adjust the font size within this div when selected, you can include

div:focus label { font-size: 14px; }
in your CSS code. This will cause the font size to decrease when the div is in focus.

div {
  height: 50px;
  width: 200px;
  border-bottom: 1px solid black;
  -webkit-transition: width 2s;
  transition: width 2s;
}
div:focus {
  background-color: transparent;
  width: 300px;
  transition: ease;
  outline: 1px solid black;
}
div:focus label {
  font-size: 14px;
  transition: font 1s ease
}
label {
  font-size: 20px;
  transition: font 1s ease
}
<div tabindex="0">
  <label>Three-colored border!</label>
</div>

Update: The font reduction now occurs as part of the transition effect with the div.

Answer №2

Styling elements using CSS is a common practice, but when it comes to detecting empty input fields, JavaScript is required as CSS cannot analyze the content of elements. To achieve this, you can use the following code snippet:

var size = $("input").text().length;

Include an onfocusout function in your HTML to check the size like so:
<label onfocusout="fn()">Three-colored border!</label>

Here's how to bring it all together:

function fn() {
    var size = $("label").text().length;
    if (size>0) {
        $("div").addClass("goodClass");
    } else {
        $("div").addClass("badClass");
    }
}

Keep in mind that the styles for "goodClass" and "badClass" should be defined in your CSS.

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

Use checkboxes to switch specific divs on and off based on their two assigned classes

Hey, I'm trying to implement some code with a specific structure: Initially, a main div is opened and checkboxes are created using a foreach loop. A checkbox is generated for each 'model' in an array. <div id='coltext'> & ...

Navigating the itemlist HTML to extract and manipulate data in a Flask application

In my current project, I am attempting to retrieve a value from an array in Flask based on the user's selection. Additionally, I need to perform calculations on this value within Flask as well. Below is the code snippet: HTML Code: <div class="f ...

Leveraging Axios for form data submission

Is it possible to serialize data from an HTML form element and then post the data using a Post request with Axios? Below is the event code triggered when a button click occurs to submit the post: function form_submission(e) { var data = document.getEleme ...

Adjusting the size of Bootstrap cards to ensure they are uniform in appearance

Currently, I am facing an issue where the card-img-top in my bootstrap cards are resizing unevenly. I am looking for a CSS solution that can help me make them consistent while ensuring that the quality remains the same or similar. https://i.sstatic.net/ja ...

Error encountered: MongoDB failed to execute Javascript as it was unable to save a DBQuery object in the specified location within the collection.js file

When attempting to modify an existing document in a MongoDb collection, an exception is generated with the following message: javascript execution failed : can't save a DBQuery object at src/mongo/shell/collection.js The following actions are perform ...

Having issues with jQuery not functioning on this specific web page across all browsers. The reason behind this puzzling dilemma remains elusive to me

I am experiencing issues with the functionality of my website. The buttons with + signs are meant to be drop-down toggles, and the mini-tabs below them should work as tabs. Additionally, the sample images at the bottom are not loading properly when clicked ...

Tips for presenting JSON date in JavaScript using Google Chart

I am in urgent need of assistance with this issue. I am trying to display the date from PHP JSON data retrieved from my database in a Google Chart using JavaScript. Below is the PHP code snippet: $data_points = array(); while($row = mysqli_fetch_array($r ...

What is the method for notifying the latitude within this Google Maps javascript code?

There's a specific line that triggers an alert showing "undefined". However, when I just alert results[0].geometry.location, it displays (41.321, 41.556). My goal is to alert this value and then convert it (as an integer) to my id_lat and id_longs... ...

Submitting with enter key on typeahead suggestions

Seeking guidance on Bootstrap typeahead: how can I configure it to submit the entered text upon pressing the enter key? Specifically, if I type "hello" in the typeahead input and hit enter, how can I submit "hello"? I've successfully disabled the aut ...

Attempting to replace an AngularJS object with another service results in it being reinitialized upon using ctrl + f5 in the application

In my AngularJS application, I have two services. One service is used to declare objects with default values, while the other service is responsible for assigning new values to those objects. Everything works fine in the controllers after rewriting, but wh ...

Unable to pass input data when ngIf is being used

I have been working on an application that allows users to add categories and change the order of them. The issue I am facing is related to sending form data when there is a conditional statement (*ngIf) within the input field. Even after trying different ...

PHP variables that are constantly changing

I recently developed a website that showcases random entries from a database to viewers. Here is the current code snippet I am using: $records = "SELECT * FROM xxx"; $records_query = mysql_query($records); $num_records = mysql_num_rows($records_query); $i ...

What is the best way to implement a delay before calling the owlCarousel function?

Is there a way to delay calling the owlCarousel function for 5 seconds? I attempted the following: $(document).ready(function(){ setInterval(function(){ $(".demo-slide").owlCarousel(); },5000); }); However, I encountered ...

Popstate functionality not functioning correctly, requiring multiple consecutive clicks

I am currently delving into the world of History Web API, but I've encountered my first challenge. While I have successfully implemented history.pushState, I am facing difficulties with popstate. It works perfectly the first time after an AJAX page l ...

Access the inner element with Selenium WebDriver in Java

Looking for a way to automate clicking the 'Download' button on a pdf-viewer page using Selenium? Check out this html code snippet from the page: https://i.sstatic.net/5qUtT.png To access the element enclosed within the red frame, you may need t ...

CSS not working when attempting to override Angular (13) Material file

In my Angular (13) application with Material, I have developed a CSS file specifically for overriding certain Material styles. This CSS file is imported into the styles.scss file as the last line. However, despite importing the external file, the CSS def ...

Once you have reviewed the initial reply, proceed to send a follow-up message

Once I have received a response from the server for one string, my goal is to send the next JSON object. I need to verify the first object and then proceed to send the second one based on that verification. How should I handle the server's response? ...

Displaying an iframe in full screen to adjust to different screen resolutions and portrait mode on tablets

My latest coding project involved creating a responsive fullscreen iframe, and for the most part, it's been working really well. However, I encountered an issue with screen resolutions like 1680x1050 (check out the screenshot here). In cases like thes ...

Ajax function implemented successfully with stylish design

Hi there! :) I'm encountering an issue with my code. I am trying to create a dropdown filter using ajax, php, and json. The first dropdown menu (for selecting the build year of a car) is populated through php. The second dropdown menu allows users to ...

What is the most efficient way to calculate the total sum of all product amounts without using Jquery?

I am working with a dynamic table where data is inserted and the total of each product is calculated by multiplying the price by the quantity. What I need now is to get the sum of all the totals for each product. You can see how the table looks here: htt ...