I struggle with generating a transition effect in the input box through label transformation

Why is the input box not using the specified CSS styles for the input and label tags? The transform property doesn't seem to be working as expected. I've highlighted the areas where I'm facing issues with bold text, and I've included both my CSS and HTML code below.

Here's the code I tried:

.main {
    display: flex;
    flex-direction: column;
    height: 400px;
    width: 200px;
    margin: auto;
    border: 2px solid black;
    align-items: center;
    justify-content: center;
}

.searchdiv {
    position: relative;
    width: 100%;
    /* border: 2px solid red; */
}

.search {
    width: 100%;
    border: 0px;
    border-bottom: rgba(22, 22, 22, 0.349);
    background: rgba(211, 210, 210, 0.705);
}

label {
    position: absolute;
    top: 0%;
    left: 0%;
    transition: 0.3s ease;
}

.search:focus,
.search:valid {
    outline: none;
    background-color: rgba(85, 206, 247, 0.699);
}

#searchbar:focus~label,
#searchbar:valid~label {
    transform: translatey(-10px);
}
<div class="main">
    <div class="searchdiv">
        <input type="text" id="searchbar" class="search"><label for="searchbar">Search</label>
    </div>
    <div><button class="btn srchbtn">SEARCH</button></div>
</div>

Answer №1

I believe I have grasped your intentions correctly

Approach 1 - CSS

The adjustments are made in .search:focus and #searchbar:focus+label. However, once the input field loses focus, the text "Search" will revert to its original position.

If you prefer the change to occur only once, check out Approach 2 below

.main {
    display: flex;
    flex-direction: column;
    height: 400px;
    width: 200px;
    margin: auto;
    border: 2px solid black;
    align-items: center;
    justify-content: center;
}

.searchdiv {
    position: relative;
    width: 100%;
}

.search {
    width: 100%;
    border: 0px;
    border-bottom: rgba(22, 22, 22, 0.349);
    background: rgba(211, 210, 210, 0.705);
}

label {
    position: absolute;
    top: 0%;
    left: 0%;
    transition: 0.3s ease;
}

.search:focus {
    outline: none;
    background-color: rgba(85, 206, 247, 0.699);
}

#searchbar:focus + label {
    transform: translatey(-20px);
}
<div class="main">
    <div class="searchdiv">
        <input type="text" id="searchbar" class="search">
        <label for="searchbar">Search</label>
    </div>
    <div><button class="btn srchbtn">SEARCH</button></div>
</div>

Approach 2 - JS and CSS

If you desire the transition to occur just once upon clicking within the input element, JavaScript is required. This script will apply a style to the label element with the ID of searchbarLabel when the input element with the ID of searchbar is clicked.

document.getElementById("searchbar").addEventListener("click", function () {
    var y = document.getElementById("searchbarLabel");
    y.setAttribute('style', 'transform: translatey(-20px);')
});
.main {
    display: flex;
    flex-direction: column;
    height: 400px;
    width: 200px;
    margin: auto;
    border: 2px solid black;
    align-items: center;
    justify-content: center;
}

.searchdiv {
    position: relative;
    width: 100%;
}

.search {
    width: 100%;
    border: 0px;
    border-bottom: rgba(22, 22, 22, 0.349);
    background: rgba(211, 210, 210, 0.705);
}

label {
    position: absolute;
    top: 0%;
    left: 0%;
    transition: 0.3s ease;
}

.search:focus {
    outline: none;
    background-color: rgba(85, 206, 247, 0.699);
} 
<div class="main">
    <div class="searchdiv">
        <input type="text" id="searchbar" class="search">
        <label for="searchbar" id="searchbarLabel">Search</label>
    </div>
    <div><button class="btn srchbtn">SEARCH</button></div>
</div>

Answer №2

Enhancing the code practice involves avoiding placing labels inside input fields and ensuring CSS specificity, such as:

.main .search-div input /* for clarification */

If you refine your code structure and quality, we may be able to offer more assistance in resolving issues.

Answer №3

From my perspective, it appears that the transformation is functioning correctly. If you suspect that the issue may be related to your browser settings, you can try incorporating a reset.css file similar to the one provided below:

/* http://meyerweb.com/eric/tools/css/reset/ 
   v2.0 | 20110126
   License: none (public domain)
*/

html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure, 
footer, header, hgroup, menu, nav, section {
    display: block;
}
body {
    line-height: 1;
}
ol, ul {
    list-style: none;
}
blockquote, q {
    quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
    content: '';
    content: none;
}
table {
    border-collapse: collapse;
    border-spacing: 0;
}

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

Having trouble retrieving req.session variables in Express/NodeJS?

I have come across various versions of this particular question, however, none of them seem to address my issue directly. My current objective is to establish a Node.js server using Express. Below is my existing server configuration: var express = require ...

quickest method for accessing the selected radio button using jQuery

I'm trying to use jQuery to retrieve the selected radio button and display the text associated with that radio button. <div id="Poll"> <div id="PollQuestion">Your favorite color?</div> <ul> <li class="liAnsw ...

Search a database for a specific set of ObjectID using Mongoose

I'm currently developing an API using node.js, express, and mongoose. As I am still new to mongosse, I have been exploring different approaches to achieve what I need. In my database, I have two collections: users and expenses. Here's an exampl ...

the absence of any content being shown when using v-else

I'm currently delving into Vue.js and stumbled upon an unusual behavior that I couldn't find any information about on the Internet. For some reason, the DIV with v-else isn't rendering any content that I place inside it. I tried to underst ...

Incorporating ContentPlaceHolder in ASP.NET MasterPage

For my In-Application CMS project, I am considering allowing users to upload an HTML and a CMS file as the Masterpage. One solution I have in mind is to have users include Pre-Defined Tags within their HTML files, which I can then replace with ContentPlac ...

When implementing multer in an express application, I encountered an issue where req.files appeared empty

Currently, I am facing some issues while attempting to upload various file types to the server using multer in an express application. Whenever I make the request, the server responds with a TypeError: req.files is not iterable. Upon investigation, I notic ...

Guide to refreshing the modal component with updated properties

In my application, I have created a modal component for managing recipes. This modal allows users to save recipes to a list. let modal = <Modal saveRecipe={this.saveRecipe} closeModal={this.toggleModal}/> However, I also want to utilize the same m ...

Displaying content conditionally - reveal a div based on user selection

I have a dropdown menu and need to determine whether to display a specific div using the value selected via v-if. <select class="custom-select custom-select-sm" id="lang"> <option value="1">English</option> <option value="2">Sv ...

Is it possible to extract tooltip text from a website using Python and Selenium, specifically when the text is generated by JavaScript?

Can anyone help me retrieve the tooltip text that appears when I hover over the time indicating how long ago a game was played? You can find my summoner profile here. I have noticed that the tooltip text is not visible in the HTML code and suspect it may ...

Is there a way for me to ensure that a response is only returned once a method call has been completed in

Seeking assistance with a Node.js application using Express. I am trying to create a REST endpoint that returns the response of an HTTP call. However, no matter what I attempt, it always returns before the HTTP request has completed. Can anyone provide g ...

What is the best way to arrange an array of words expressing numerical values?

Is there a way to alphabetize numbers written as words (one, two, three) in Javascript using Angularjs? I need to organize my array of numeric words $scope.Numbers = ["three", "one", "five", "two", ...... "hundred", "four"]; The desired output should be ...

Is your PHP random advertisement feature malfunctioning?

<div id="bannerAd"> <?php //THIS SCRIPT RANDOMLY SELECTS AN AD. $randomAd = rand(1,3); switch($randomAd) { case "1": $adLink = "/sale.php"; $imageFile = "/images/saleAd.png"; ...

It is not possible to import node_modules within an electron worker process

Question I'm currently experimenting with using web workers within an Electron application. I have been successful in creating the worker process from the renderer process, but I am encountering a crash when attempting to use require('some_modul ...

Revise: Anticipated output missing at conclusion of arrow function

Here is the code snippet that I am having trouble with: <DetailsBox title={t('catalogPage.componentDetails.specs.used')}> {component?.projects.map(project => { projectList?.map(name => { if (project.id === name.id) { ...

Unusual Behavior in AngularJS: Using ng-include Triggers Full Page Reloading

An issue arises where a simple ng-include command causes the entire website to be printed recursively in a specific area of the page, ultimately crashing the browser. Even changing the path doesn't resolve the problem, suggesting that it's not ev ...

an online platform design

I am currently working on building a webpage, but I am facing some issues with the layout. Specifically, I am unable to make the div automatically adjust its size (particularly the height). Here is the code that demonstrates the problem: <!DOCTYPE html ...

To extract three records from storage and store them in the dbResult using a promise join technique

How can I efficiently retrieve and store 3 records in dbResult using promise join? Currently, I have code that retrieves a single record as shown below: req.oracleMobile.storage.getById(registry.getIncidentPhotoStorageName(), incident_id + '_01&apos ...

Ensure that the navigation menu is consistently displayed properly, without any of its contents extending beyond the

I am experiencing an issue with the navigation menu on my website. Everything looks great in normal view, but when I resize the browser window to a smaller width, the menu ends up overflowing below the main content. I want the navigation menu to always dis ...

Assigning the "action" attribute of a form to direct to the homepage

Working on a login form, I've encountered an issue with the action attribute when set to "index.cfm". This seems to work fine for all other pages except for the index page. Looking for some insights if anyone else has faced this problem before. I&apos ...

I am unable to view the GridView features that have been set using jQuery in the JavaScript code on the webpage

I'm facing an issue with the properties of a GridView. On the aspx page, I have a container using a <div>. <div id="DivRecords"> Within this container, I am dynamically adding a GridView using jQuery. In the js file, I am creating the Gr ...