Steps for modifying the width of a horizontal line element so that it aligns with an input element

I'm attempting to dynamically adjust the hr element's length based on the length of the input element by using CSS.

Additionally, I'd like to incorporate an animation that triggers when the input is focused.

I came across a solution for the h2 element on stackoverflow: How to do it with h2 element

Here is the code snippet:

h2{
  font-size:50px;
  padding:0 25%;
  text-align:center;
}


h2 hr{ width:100%;height:10px;background:#000;}
<h2>
This is a very big title
<hr />
</h2>

I tried implementing a similar approach for the input element, but without success.

My Attempted Code:

hr.underline {width: 0%;}


input.input:focus hr.underline{
    left:0;
    width:100%;
    height:2px;
    background-color:#17e13f;
    border:none;
    margin-top:5px;
    margin-left:0;
    margin-bottom:0px;
}
<div>
        <label for="1">Input</label>
        <input class="input" type="text" id="1">
            <hr class="underline">
        </input>
    </div>

However, this method doesn't seem to be working as expected. Does anyone have suggestions on how to achieve this, or perhaps a better alternative?

Answer №1

Your closing input tag is incorrect; the input tag is self-closing. Solution: Use max-content on the parent div, then apply width:100% to .underline when focused, making it 100% of the parent element. Include

transition: width 0.25s ease-in-out;
for a smooth animation.

    div{
        display:flex;
        gap:5px;
       }
    span {  
        display:inline-block;
        width: max-content;
        
    }
    
    .input:focus~.underline {
        width: 100%;
    }

    .underline {
        height: 2px;
        border: none;
        background-color: #17e13f;
        width: 0%;
        transition: width 0.25s ease-in-out;
    }
<div>
        <label for="1">Input</label>
        <span>
        <input class="input" type="text" id="1">
        <hr class="underline">
        </span>
        
</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

Adjust the baseline of text in <li> elements by shifting it 2 pixels upwards

My webpage menu is set up with inline "li" elements within a "ul". Each "li" has a colored border and contains an "a" tag. I'm looking to change the text color inside the "a" tag and move it 2px up when hovering over it without affecting the li border ...

Issue with scrolling to the bottom of collapsed sections in Bootstrap

I have a bootstrap collapse panel and I've added a toggle link at the bottom to allow users to expand and collapse the content with a click. The Issue My problem arises when the menu expands, causing it to scroll all the way to the bottom of the pag ...

PHP prepared statements do not seem to be effectively updating or inserting entire datasets

My website allows members to create and update posts using a text editor. However, I've run into an issue when the post content includes new lines or spaces such as <p>&nbsp;</p> or <div>&nbsp;</div>. The problem arises ...

Embedded Youtube code saved in database is not displaying on HTML form

My PHP update page allows me to modify a website's database that contains YouTube embed codes. However, when I try to edit the embed code on the update page, only "<iframe width=" displays instead of the complete original code. <html> <? ...

What is the method for importing .scss files throughout a Next.js application?

I'm looking to transform my React app into a Next.js app. In the current React setup, I have .scss CSS files being imported in various components. However, once I transfer the code to a Next.js environment, I encounter an error: Global CSS cannot be ...

What is the most effective way to align a thumb to the left and content to the right?

What is considered the optimal method for positioning an image to the left with two text fields to the right of it? Is using floats or positioning better? <li> <a href=""> <img src=""THUMB HERE /> </a> <a href=""> TITLE </ ...

Included Image Hyperlink with Limited Clickability in HTML

When trying to turn my image into a hyperlink, I enclosed it with the tag. Oddly enough, only the lower portion of the image is responsive as a link. The upper part seems unresponsive. Here's the code snippet I used: <a href="link-here"& ...

Avoid automatic background resizing to match the full width of the header text

I've been trying to use CSS to style my <h1> element in a specific way, but it seems the default browser behavior is getting in the way: Despite my efforts, the browser continues to display it like this: Anyone have any suggestions on how I ca ...

At what point does the browser begin fetching the background images specified in a stylesheet?

Are images mentioned in a valid CSS declaration with a background-image value downloaded when the stylesheet is parsed or when the declaration is applied on a page? In simpler terms, do I need to download all background images listed in my stylesheet even ...

Mobile devices seem to be constantly refreshing website images

I have a landing page consisting of multiple sections with images as the background, each section occupying the full screen. In one specific section, the images change every 5 seconds. The website works smoothly on desktop, but I encounter issues on mobil ...

Issue with the Z-Index property not functioning as expected on unordered lists within a dropdown menu

I have a dropdown menu that opens to the left, but it is displaying underneath the content. I attempted adjusting the z-index of the content to 1 and the dropdown to 2, however, this did not resolve the issue. Here is a sample in jsFiddle: https://jsfiddl ...

Utilizing the hcSticky plugin for creating a scrolling effect on webpage content

I'm attempting to utilize the JQuery plugin, hcSticky, in order to achieve a scrolling effect on my fixed content. However, I seem to be encountering some difficulty getting it to function properly. What could I possibly be doing incorrectly? JSFIDDL ...

Issues with modals appearing improperly after transitioning to NG-Bootstrap 15 and Bootstrap 5 upgrade

UPDATED following a thorough examination: In the process of upgrading our application from NG-Boostrap v11 with bootstrap 4 to NG-Boostrap v15 with Bootstrap 5 and Popper, we also made the switch from Angular 15 to Angular 16. Despite successfully resolvi ...

Conceal the Div containing the specified class

I was hoping to hide the first DIV if the second DIV is displayed on the front end, and vice versa upon page load. If the first DIV is set to 'block,' then the second DIV should be set to 'none.' And If the second DIV is set to &apos ...

Steps to create a clickable drop-down arrow with a label

I've designed a vertical navigation accordion menu that allows users to hover and click on any title (label) to expand a sub-menu. However, I'm facing an issue where my drop-down arrow is not clickable, unlike the rest of the label which works f ...

perfecting the animation of popovers

When I click on the button, a popover appears. However, the pointer of the popover seems to be coming from a different direction. I have been struggling to correct this issue. Any suggestions? .popover { position: absolute; top: 50px; left: -175px ...

Flip the Script: JQuery slideshow in reverse

Hey there, I've been tinkering with a JQuery slideshow and encountering an issue. All my images in the HTML are stacked on top of each other, and I attempted to modify the code so it starts with the last image. However, my attempts have not been succe ...

When CSS Display table is resized at media query breakpoints, it causes other cells to disappear

This is a discussion on creating a responsive image gallery website with images of different sizes and no margins or paddings. The issue at hand is that the .showcase class hides at a specific @media breakpoint when resizing, but there is a desire to displ ...

How do I insert a new column for the Name attribute in ASP.NET Core Identity without using the Username field?

I'm currently utilizing ASP.NET Core Identity within my project. I have a form that includes fields for Name and Username. The issue arises when attempting to save values from the Name field, as it triggers an error stating that they are not in the co ...

Creating an event within a class that is generated dynamically when hovering

I'm trying to create a hover effect where an image moves around the page in a pattern (top left corner -> top right corner -> bottom right corner -> bottom left corner -> then back up to the top left corner). To achieve this, I am adding ...