Positioning an element within a div with a fixed width and height in a somewhat precise manner

Here is my current HTML markup:

<div id="content">
   <img src="some_content.jpg">
   <form action="...">
     <input type="text" ... >
     <input type="submit" ...>
   </form>

   <div id="forgotyourpassword">
     <a href="somelink.com">Forgot your password?</a>
   </div>
</div>

The form structure is generated by a CMS, hence it cannot be modified.

The styling for the children within the content div allows for centering both vertically and horizontally. The forgot your password link positioning is currently set as follows:

Below is the CSS code specifically for the forgot your password link:

#forgot-password{
    float: right;
    margin: 0; /* reset some stuff inherited from parent */
    padding: 0; /* reset some stuff inherited from parent */
    margin-right: 171px;
    margin-top: -20px;
}

Below are some relevant CSS styles being applied:

#content{
    position:absolute;
    width: 650px;
    left:50%;
    top:50%;
    height:242px;
    margin-top:-126px;
    margin-left: -325px;
    text-align: center;
}

#content > *{
  vertical-align: middle;
  display: inline-block;
  zoom:1; 
  *display:inline;
  margin-left: 20px;
  margin-right: 20px;
}

While this setup works in general, issues arise when an error message is appended to the form after submission. In such cases, I want the link alignment to adjust dynamically relative to the form changes.

The challenge lies in aligning the link with respect to the button rather than its parent container.

I initially planned to position the forgotyourpassword div directly under the form so that any change in form size adjusts the link position accordingly.

Adjusting margin-top should theoretically realign the forgotyourpassword div with the submit button regardless of form height variations.

However, testing reveals:

  • In Firefox 10, the forgotyourpassword div fails to shift as intended once overlapping with the form content area.
  • In IE9, the forgotyourpassword appears above the form.

Is there a CSS-only solution compatible with IE7+ and Firefox to address this alignment dilemma?

Answer №1

#content form{
    position: absolute;
    top: 80px;
    right: 0px;
}
#forgot-password{
    height:80px;
}

To achieve the desired layout, it's important to remove the form from the document flow by setting its position to absolute. This way, you can ensure that the link will be positioned above it without any adjustments needed. Simply make sure that the top value for #content form matches the height value for #forgot-password, and everything should align perfectly.

Although there may be concerns about clearing the absolutely positioned div in certain scenarios, setting the height for #content should prevent any issues in this case.

Answer №2

Due to restrictions in the form's markup, I am unable to directly insert the forgotyourpassword div as a child element.

To work around this limitation, I introduced a wrapper div:

<div id="content"> 
   <img src="some_content.jpg"> 
   <div id="wrapper>
       <form action="..."> 
         <input type="text" ... > 
         <input type="submit" ...> 
       </form> 

       <div id="forgotyourpassword"> 
         <a href="somelink.com">Forgot your password?</a> 
       </div> 
   </div>
</div> 

Next, I adjusted the margins for the forgotyourpassword div like so:

#forgotyourpassword{
   float: left;
   margin-top: 10px;
}

This solution may not contribute semantically to the page structure, but it serves its purpose given the current constraints. Until we have more advanced CSS capabilities, this workaround remains effective.

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

Is the homepage the only page rendering correctly?

After working on an MVC project for over 6 months, I consistently faced a problem with HTML and CSS. The homepage always rendered correctly, as shown here: http://prntscr.com/tucp1 However, all other pages had strange white spaces between words, like in t ...

Setting the z-index for a JavaScript plugin

I need help with embedding the LinkedIn plugin into a website that has stacked images using z-index for layout. I have tried assigning the plugin to a CSS class with a z-index and position properties, but it hasn't worked as expected. Can anyone sugge ...

When utilizing content: url() for a local image, it does not show up, whereas it will display for a web URL. What could be causing this discrepancy

I'm attempting to convert this image into a button. It works fine when acquiring the image through a website URL (not from my own website), but it doesn't work when using a relative path. The image just won't show up. For instance: .my-cla ...

Ensure that the div element spans the entire width of the screen

Having issues working with the div tag and encountering this problem: The left side doesn't extend as far as the right side, despite testing in multiple browsers. Here is the HTML code being used: <!DOCTYPE html> <html lang="en"> <h ...

I'm attempting to make this script function correctly when an image is loaded

Can anyone help me figure out how to make this script function on image load instead of onclick? I came across the script on stackoverflow, but it was originally set up for onclick. I want it to work for onload or .load HTML====== <div id="contai ...

What is causing the extra space on the right side of the box?

I've been struggling to align text next to an image inside a box. Desired outcome CSS: #roundedBox { border-radius: 25px; background: #363636; width: auto; height: auto; margin: 10%; display: flex; position: relative; ...

Toggle JavaScript Query Display

Welcome to my HTML test website where I am testing show/hide JavaScript functionality. Upon loading the page, you may notice that everything is hidden until a button is clicked. <html> <head><title>Test</title> <scr ...

Image in the background not showing up on Chrome or Internet Explorer

The issue I'm facing is that the background-image displays correctly in Firefox, but not in Chrome or IE. Despite trying various solutions found on Stackoverflow, such as disabling my ad-blocker, placing the code in the head of the file: <head> ...

When attempting to print using React, inline styles may not be effective

<div styleName="item" key={index} style={{ backgroundColor: color[index] }}> The hex color code stored in color[index] displays correctly in web browsers, but fails to work in print preview mode. Substituting 'blue' for color[index] succe ...

Innovative and responsive web design strategies

Although I have a solid understanding of html, css, and JavaScript, I struggle with combining these skills effectively. My expertise lies in developing native apps for Android, where I focus on creating dynamic layouts using relative positions and sizes li ...

Removing classes from multiple cached selectors can be achieved by using the .removeClass

Currently working on enhancing some JavaScript/jQuery code by storing selectors in variables when they are used more than once to improve performance, for example: var element = $("#element"); element.hide(); However, I am facing difficulties while tryin ...

Loading screen preceding page change

I've been searching everywhere and can't find a good tutorial on how to smoothly transition pages when clicking a link on my website. I want the page to fade out on click, display a preloader, then fade in the new page once the preloader finishes ...

Explore the interactive feature of hovering over a component within a div that is enhanced with a transformative translate3d effect upon hover. Discover the seamless event queuing

Within a div, there is a modal component that includes a transform: translate3d effect with hover transition. The div is created using vue.js list rendering method. Upon opening the modal and hovering over it, it jerks between the parent div and its intend ...

Tips for entering the lowest and highest values into the input field

I am looking to track the daily minimum and maximum prices of shares for various companies. Currently, I'm attempting to utilize this tag: <input type="number" name="number" placeholder="minprice"> <input type="number" name="number" place ...

Arranging elements in a Material-UI card in a horizontal layout

I am currently working on designing a user profile display. I want to create a card layout that showcases the details listed below. My goal is to have these items displayed side by side instead of being divided as shown in the image. Despite trying to giv ...

Pause jQuery at the conclusion and head back to the beginning

As a beginner in the world of jQuery, I am eager to create a slider for my website. My goal is to design a slideshow that can loop infinitely with simplicity. Should I manually count the number of <li> elements or images, calculate their width, and ...

Display the div without using javascript/jquery if the radio button is chosen

Could someone help me find a solution similar to the one mentioned here: If Radio Button is Selected Show Div I am specifically looking for a way to achieve this using only HTML and CSS, without involving JavaScript or jQuery. Any suggestions would be gre ...

Integrate SVG into the dropdown menu without duplicating the SVG element

I have an SVG of a right arrow: <svg xmlns="http://www.w3.org/2000/svg" height="24px" viewBox="0 0 24 24" width="24px" fill="#000000"> <path d="M0 0h24v24H0V0z" fill="none"/> ...

Unique Pie Chart Designs

As I attempt to create a dynamic arc-shaped pie graph using CSS, representing 100% of the data, I find myself facing challenges. Despite extensive research and Google searches, I have yet to come across a suitable solution. The following reference appears ...

Activate a side navigation bar in AdminLTE-3

I am using AdminLTE3 as my admin panel in Laravel, but I am facing an issue with the nav-item links not activating when clicked. Even though I have all the required assets for AdminLTE3 and used the starter.html file, the navigation items are not working p ...