Floating label hides the textbox text with a unique style

I'm trying to create a floating label effect for textboxes in my form that moves up on hover and focus. However, I'm running into an issue where the text gets hidden when the label moves up.

Below is the HTML and CSS code I've been working with:

.float-label {
    position: absolute;
    left: 0;
    top: 0;
    padding: 10px 6px;
    color: #767676;
    pointer-events: none;
    transition: 0.2s ease all;
}

.form-control:focus ~ .float-label, 
.form-control:not(:focus):valid ~ .float-label {
    top: -18px;
    margin-left:5px;
    font-size: 12px;
    opacity: 1;
    z-index: 1;
    background-color: white;
}
                            <div class="form-group border-lable-flt">
                                <input type="email" class="form-control" id="work_email" required>
                                <label class="float-label" for="workEmail">Work email</label>
                            </div>

Answer №1

If you prefer not to use the color: transparent property for the placeholder text, you can achieve the same effect by closely matching the styles of the label and placeholder using the :placeholder-shown pseudo-class.

.form-group {
  position: relative;
  padding: 20px;
}
.float-label {
    position: absolute;
    left: 25px;
    top: 21px;
    color: #767676;
    pointer-events: none;
    transition: 0.2s ease all;
}

input::placeholder {
  color: transparent;
}

.form-control:focus ~ .float-label, 
.form-control:not(:focus):valid ~ .float-label,
input:not(:placeholder-shown) ~ .float-label {
    top: 5px;
    font-size: 12px;
    opacity: 1;
    z-index: 1;
    background-color: transparent;
}
    <div class="form-group border-lable-flt">
        <input type="email" class="form-control" id="work_email" required placeholder="Work email">
        <label class="float-label" for="workEmail">Work email</label>
    </div>

Answer №2

Consider implementing the following modifications :

.input-group{
  margin-top:20px;
  position:relative;
}
.label-float {
  position: absolute;
  left: 0;
  top: 0;
  padding:0;
  color: #767676;
  pointer-events: none;
  transition: 0.2s ease all;
}

Answer №3

Here is the code snippet you can use to create a stylish email input field:

<div class="form-group border-lable-flt textinput">
  <input type="email" id="email" class="form-control"  />
  <label for="email">Email address</label>
</div>

And here is the corresponding CSS styling:

.textinput {
  height: 3em;
  margin: 1em 0;
  position: relative;
  width: 100%;
}

.textinput input,
.textinput label {
  cursor: text;
  font-size: 1.5em;
  padding: 0.6em 1% 0.15em 1%;
  position: absolute;
  transition: all 0.15s ease;
  width: 98%;
}

.textinput input {
  border: solid black 1px;
  border-radius: 0.15em;
}

.textinput label {
  color: #BCB9B8;
  padding: 0.5em 1%;
}

.textinput input.filled ~ label,
.textinput input:focus ~ label {
  font-size: 0.6em;
  font-weight: 600;
  position: absolute;
}

.textinput input.filled ~ label {
  color: #007932;
}

.textinput input[type="text"].filled,
.textinput input[type="email"].filled:valid {
  background: #B8DB9B;
}

.textinput input[type="text"].filled ~label:after,
.textinput input[type="email"].filled:valid ~label:after {
  color: #007932;
  content: "\f058";
  display: inline-block;
  font: normal normal normal 14px/1 FontAwesome;
  font-size: 3em;
  text-rendering: auto;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  position: absolute;
  top: 0.3em;
  right: 0.3em;
  transform: translate(0, 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

IE - Adjusting the Width of the Vertical Spry Menu Bar

As I delve into the world of web design using Dreamweaver, I find myself faced with some challenges as a beginner. One specific issue I'm encountering is related to a vertical Spry Menu Bar on my website that functions well in most browsers except for ...

Controlling CSS Styles in Angular using TypeScript

Currently, I am working on an Angular project that involves dynamically populating a calendar. In addition to this, I have a range of dates and the task at hand is to modify the background for specific days within this date range. To manage dates effective ...

Enhance the functionality of Woocommerce email notifications by incorporating a customized VAT field

I have exhausted all options and tried various email hooks without success. I inherited an outdated PHP code that was developed by someone else, which I updated for new woocommerce hooks (since the code is 4 years old). Everything is functioning smoothly e ...

Issue with Expo Google Places Autocomplete: ListView not clickable

I am currently encountering an issue with the GooglePlacesAutocomplete feature. When I input the address query, such as "São C," the 'listView' returns options like "São Caetano, São Paulo ...", however, when I attempt to select an option from ...

Enhanced auto-zoom feature with orientation change for iOS 7

My web page was functioning perfectly until the release of iOS 7 by Apple today, causing the layout to break when the screen orientation is changed. When focusing on a text area and rotating the screen to landscape view, the entire page zooms in. However, ...

Deactivating one div's class upon clicking on another div

Below is the HTML code snippet: <div class="container"> <ul class="navbar"> <li class="nb-link"><a>Home</a></li> <li class="dropdown"> <a>CBSE</a> <ul class="dropdown-menu"&g ...

jQuery - Enhancing User Experience with Dynamic Screen Updates

Is there a way to update the screen height when resizing or zooming the screen? Whenever I zoom the screen, the arrows break. I'm also curious if the method I'm using to display images on the screen is effective. It's supposed to be a paral ...

The scroll bar is visible on the scroll box, but unfortunately it is not functional in Internet Explorer 8

http://example.com/code <div style="width:625px;height:220px;overflow-y:hidden;overflow-x:scroll;z-index:10;"> <table width="1000" height="114" border="1"> <tr> <td width ...

Is it possible to rotate a group within an SVG without altering the orientation of the gradient fill it contains?

In my search on SO, I came across a lot of information about rotating gradients. However, my issue is the opposite. I have an icon created from multiple paths that I want to fill with a vertical gradient. I have successfully done this and it works well. ...

What is the process for removing a full row from a table in PHP?

Is there a way to add a delete button to delete an entire row in a MySQL database? Below you can find the code and instructions on how to delete a row using the ID, which is the primary key. <?php mysql_connect('localhost', 'off', ...

Error message "Undefined is not a function" occurred while using jQuery's .replace and scrollTop functions

I'm having issues with the scroll function in my code. It doesn't seem to be able to locate the ids in my HTML, even though I can't figure out why. I had a previous version that worked perfectly fine (unfortunately, I didn't save it D:) ...

Resolving issues with CSS placement and resizing

I have been considering developing a UI toolkit that offers an intuitive and powerful way of setting the position and size of elements/widgets. Here are some examples of how it could be used (although they are not currently implemented): ui("Panel").size( ...

Display different images based on user selection in vue.js

I am new to working with vue.js and I'm facing a challenge. My goal is to display images of NBA players based on the selected criteria using vue.js. For instance, if the Dunk contest champion is chosen, only images of Kobe and Jordan should be display ...

Removing the 'div' tag using jQuery in CodeIgniter

I need assistance in removing the added item. $(document).ready(function() { $("#appendex0").click(function() { $(".divcls0").append('<div class="col-sm-10 col-sm-offset-1"><div class="col-sm-3 col-sm-offset-1"><div class="form ...

"Utilize input within the div element to occupy the entire cell of the

There is a form input enclosed within a div which is nested inside a table cell. <td align="left" id="subformAttachments_Htmltable1td10" style="border:solid 1px black;width:316px;" class="tbldroppable ui-droppable ...

When the text is in motion, the ::before element will stay static

I'm working on creating a unique custom SVG underline for a header design. My goal is to achieve something similar to this: https://i.sstatic.net/EI1aT.png Here's what my code currently looks like: HTML: <div class="container"> ...

Navigating the web page and locating the appropriate Xpath element to extract

Here is the HTML that needs to be extracted: <input type="hidden" id="continueTo" name="continueTo" value="/oauth2/authz/?response_type=code&client_id=localoauth20&redirect_uri=http%3A%2F%2Fbg-sip-activemq%3A8080%2Fjbpm-console%2Foauth20Callbac ...

adjusting the font size based on the size of the screen

Hi everyone, I've encountered an issue with adjusting font sizes based on screen size. My framework is Bootstrap 3 and I'm using LESS. Here is the initial part of my LESS code: @font-size-base: 16px; @font-size-large: ceil((@fo ...

Creating trails by following the cursor's movement in KineticJS

Currently, I am working on a drawing application using KineticJS. While I have successfully used it to draw shapes and straight lines by following the method outlined in KineticJS - Drawing Lines with Mouse, I now need to draw a line along the mouse's ...

Issue with color display inconsistency in webgrid across various segments

https://i.sstatic.net/UW17M.png My goal is to display section status colors in a webgrid and divide them percentage-wise, but I am facing issues with some of the rows. Here is the cshtml script for the webgrid section status: webGrid.Column(header: "Sec ...