The input and label are not experiencing any overflow

I've been following a tutorial to create an animation on an input and label. I successfully did it once today in school, but for some reason, it's not working now.

My objective is to have the label inside the "input" field, with the input taking up the full size of the div. However, when I use pos absolute on the label, it seems to go outside the "form" div and I can't figure out why.

.register {
  display: flex;
  justify-content: space-around;
  align-items: center;
  flex-direction: column;
}

.form {
  position: relative;
  width: 50%;
  height: 50px;
}

.form input {
  position: relative;
  height: 100%;
  width: 100%;
  color: white;
  border: none;
  padding-top: 20px;
}

.form label {
  position: absolute;
  bottom: 0px;
}
<div class="register">
  <h1>Register</h1>
  <div class="form">
    <input type="text" name="firstN" autocomplete="off" required>
    <label for="firstN"><span>Name</span></label>
  </div>
</div>

https://i.sstatic.net/Z84MM.png

Answer №1

Revise the css code in this manner:

  .form label {
   position: absolute;
   left:15px;
   top:15px;
 }

When using pos absolute, ensure to specify left or top.

.register {
    display: flex;
    justify-content: space-around;
    align-items: center;
    flex-direction: column;
}
.form {
    position: relative;
    width: 50%;
    height: 50px;
}
.form input {
    position: relative;
    height: 100%;
    width: 100%;
    color: white;
    border: none;
    padding-top: 20px;
    background-color:gray;
}
.form label {
    position: absolute;
  
    left:15px;
    top:25px;
   
}
<div class="register">
  <h1>Register</h1>
  <div class="form">
    <input type="text" name="firstN" autocomplete="off" required>
    <label for="firstN"><span>Name</span></label>
  </div>
</div>

Answer №2

It's more advisable to utilize the placeholder attribute instead:

<input placeholder="Name">

Alternatively, you can add more lines in CSS:

label{
    position: absolute;
    top:0;
    left:0;
}
.register {
  display: flex;
  justify-content: space-around;
  align-items: center;
  flex-direction: column;
}

.form {
  position: relative;
  width: 50%;
  height: 50px;
}

.form input {
  position: relative;
  height: 100%;
  width: 100%;
  color: white;
  border: none;
  padding-top: 20px;
}

.form label {
  position: absolute;
  top:0;
  left:0;
}
<div class="register">
  <h1>Register</h1>
  <div class="form">
    <input type="text" name="firstN" autocomplete="off" required>
    <label for="firstN"><span>Name</span></label>
  </div>
</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

tips for organizing the <div> element horizontally within a design

I have reviewed some similar questions that have been posted and attempted to apply their solutions to my code. However, I am still encountering difficulties. Below is the code that I need help with. I am looking to organize the main content along with t ...

Styling Material UI components with CSS is a great way to

I'm facing difficulties while working with Material UI components. Currently, I have a Material UI table and I want to center-align the text within it. The standard CSS of Material UI contains an element named MuiTableCell-root-60 which has a text-a ...

Avoid clicking in areas outside the perimeter of the blue circle in the SVG

Is there a way to restrict clicking outside the blue circle? The goal is to only allow opening by clicking on the svg itself, This includes everything, even white space inside the svg. How can this be achieved in the code? The current behavior is that ...

Add HTML code to the dataTable

I am working on an ajax function that receives HTML response containing table data like this: <tr><td>data1</td>td>data2</td>td>data3</td></tr> My question is, can I add this response to a dataTable row in the fo ...

Modifying the font style of text within an HTML string using SQL Server

I need to standardize font sizes in my database where strings are stored as html and users can modify the font size. However, for generating reports, I require all font sizes to be consistent. If I have the following html code, how can I change the font si ...

Press and hold feature using CSS or JavaScript

When working with a jQuery draggable element, my objective is to change the cursor to a move cursor when clicking and holding the header. I have attempted using CSS active and focus properties, but so far no changes are taking place. ...

Issues with Collision Detection between Canvas Rectangles and Balls

I am developing an HTML5 canvas game and encountering an issue with collision detection. The problem occurs when the ball collides with any platform - the ball's gravity should change to -1 and move upwards at the same velocity as the platforms. Howev ...

Is there a way to automatically load an entire Facebook profile in one go using a program?

Does anyone know a way to automatically download all photos from a Facebook profile, not just thumbnails? I'm thinking of using wget to fetch the page source code and then extract links to images from child a elements of all div with class="rq0e ...

Creating HTML code that is optimized for mobile devices

My front end design is based on the CSS stylesheet from W3Schools.com, which includes a class property called "w3-rightbar" for adding a thick right border. However, I encountered an issue where I needed to hide this "w3-rightbar" specifically on mobile vi ...

Show two spans, each underneath the other

I am looking to showcase two span elements, one below the other. Take a look at my HTML code snippet <img class="profile-photo margin-0" data-ng-if="!question.isOpen" ng-src="{{question.profilePicId ? que ...

Tips on adding CSS to a React component

I've successfully converted an HTML file into a component designed to resemble a 7-segment LED display. I have imported the necessary CSS styles from the index.css file, but I am unsure how to properly apply these styles within the component. My atte ...

Can you please explain the process of retrieving the value of an item from a drop-down menu using JavaScript?

I am currently developing a basic tax calculator that requires retrieving the value of an element from a drop-down menu (specifically, the chosen state) and then adding the income tax rate for that state to a variable for future calculations. Below is the ...

A guide to filling an irregular shape (such as a star) based on a percentage using CSS in a React project

I am currently working on developing a React component that showcases a rating percentage using a star icon with two different shades. For example, if the input rating is 79%, I want to display a star with 79% of it in gold color and the remaining 21% in ...

How to reach the Twitter share iframe with JavaScript/HTML

There seems to be an issue with the Twitter share button not displaying at its full width on certain pages. Upon investigation, I discovered that the iframe containing it is only set to a width of 24px, when it should actually be set to the correct width. ...

There is a collision of boxes occurring within the CSS

Help Needed: Overlapping Boxes in Responsive Design I am facing a problem with my CSS where the boxes on my website are overlapping when I shrink the browser window. The issue occurs as I resize the window, causing the boxes to overlap more and more. I ...

Conceal the div if it remains unhidden within the following 10 seconds

This piece of code is designed to show a loader image until the page finishes loading. Here is the code snippet: document.onreadystatechange = function () { var state = document.readyState if (state == 'interactive') { $('#unti ...

What is the best way to expand the navigation bar: should I add a side div or incorporate a background image?

I am encountering two issues that I need help solving. My first problem involves stretching out the navigation bar to fill the height and length of the adjacent div (div#textarea) while also keeping the text of the menu centered within that div. The seco ...

"Using a triangular background shape in JavaScript instead of a traditional circular

I want to add a unique effect to my site inspired by the AnimatedHeaderBackgrounds demo available at this link. My twist on this effect involves using upward-moving triangles instead of circles. I've explored various resources, including Stack Overfl ...

Reduce the height of the navigation bar

I used the header example from Bootstrap's documents here: https://getbootstrap.com/docs/5.3/examples/headers/# The code I have is: <div class="container fixed-top bg-white"> <header class="d-flex flex-wrap justify-conte ...

The scroll() function in jQuery does not bubble up

Recently, I encountered an issue with a Wordpress plugin that displays popups on scroll. The code I currently have is as follows: jQuery(window).scroll(function(){ //display popup }); The problem arises with a particular website that has specific CSS ...