Changing the element to "position: absolute" prevents it from stretching to the full width

After styling my login page to perfection with the container div set to display: block and position: static, I encountered an issue when attempting to switch to display: inline-block or position: absolute. The container div stopped adhering to its maximum width of 500px, disrupting the desired layout. My goal is to center the div both vertically and horizontally using absolute positioning, while maintaining the same appearance as when it was positioned statically. How can I achieve this without compromising the layout?

* {
  -moz-box-sizing: border-box !important;
  -webkit-box-sizing: border-box !important;
  box-sizing: border-box !important;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

#login-box {
  max-width: 500px;
  min-width: 300px;
  box-shadow: #bbb 0 0 20px 0;
  display: block;
  position: static;
  /*position: absolute;*/
}

#HeaderForLoginForm {
  background-image: url('https://dummyimage.com/600x106/333333/fff.png&text=SOME+LOGO');
  background-repeat: no-repeat;
  background-size: 200px;
  background-position-x: center;
  background-position-y: 25px;
  background-color: #000;
  height: 110px;
  text-align: center;
}

#headerlinks {
  color: rgb(90, 90, 90);
  font-weight: bold;
  font-size: 12px;
  margin-top: 54px;
  display: inline-block;
}

@media (min-width: 450px) {
  #HeaderForLoginForm {
    background-position-x: 25px;
    background-position-y: center;
    text-align: right;
    height: 95px;
    line-height: 95px;
  }
  #headerlinks {
    margin-right: 20px;
    margin-top: 0;
  }
}

#DivForLoginForm {
  background: #b7d9ff;
  background: -webkit-linear-gradient(#b7d9ff, #fff);
  background: -o-linear-gradient(#b7d9ff, #fff);
  background: -moz-linear-gradient(#b7d9ff, #fff);
  background: linear-gradient(#b7d9ff, #fff);
  text-align: center;
}

#LoginForm {
  display: inline-block;
  width: 74%;
  margin-top: 20px;
  margin-bottom: 40px;
}

#LoginForm input.textField {
  display: inline-block;
  width: 100%;
  padding: 10px;
  margin-top: 18px;
  font-size: 14px;
  border-radius: 3px;
  border: 1px solid #999;
}

#terms-wrapper {
  margin-top: 16px;
  margin-bottom: 30px;
  text-align: left;
  font-size: 14px;
  font-weight: bold;
}

#terms-wrapper input {
  margin-left: 0;
  vertical-align: -2px;
}

a[href] {
  color: #0079dd;
  text-decoration: none;
}

a[href]:hover {
  text-decoration: underline;
}

input#btn-login {
  padding: 14px;
  height: auto;
  width: 40%;
  min-width: 100px;
  float: right;
  background-color: #1064d8;
  color: #fff;
  font-weight: bold;
  font-size: 16px;
  outline: none !important;
  border: none;
  border-radius: 3px;
  cursor: pointer;
}

input#btn-login:hover {
  background-color: #004BBF;
}

input#btn-login:active {
  background-color: #0031A5;
}

#loginfooter {
  background-image: url("http://images.naldzgraphics.net/2014/08/20-brushed-seamless-texture.jpg");
  color: rgb(100, 100, 100);
  padding: 12px;
  font-size: 11px;
}

[data-val-required] {
  background-color: #fff;
}
<section id="login-box-wrapper">
  <div id="login-box">
    <header id="HeaderForLoginForm">
      <div id="headerlinks">
        <a href="#">some link</a> |
        <a href="#">some other link</a>
      </div>
    </header>
    <div id="DivForLoginForm">
      <form method="post" id="LoginForm">
        <input class="textField" id="UserName" name="UserName" placeholder="Username" type="text">
        <input class="textField" id="Password" name="Password" placeholder="Password" type="password">
        <div id="terms-wrapper">
          <input id="HasAcceptedTermsConditions" name="HasAcceptedTermsConditions" type="checkbox">
          <label for="HasAcceptedTermsConditions">
            I agree to the <a id="terms-link" href="#" target="_blank">General Terms of Service</a>
          </label>
        </div>
        <input id="btn-login" type="submit" value="LOG IN">
      </form>
    </div>
    <footer id="loginfooter" style="text-align: center;">
      <span>© 2009-2017 Some Company, LLC — All rights reserved</span>
    </footer>
  </div>
</section>

Answer №1

To ensure that the #login-box takes up the max-width rule, add width: 100%; to it.

To center the login box both horizontally and vertically, utilize position: absolute; along with top, left, and transform using the translate function.

#login-box {
    width: 100%;
    max-width: 500px;
    min-width: 300px;
    box-shadow: #bbb 0 0 20px 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate( -50%, -50% );
}

* {
  -moz-box-sizing: border-box !important;
  -webkit-box-sizing: border-box !important;
  box-sizing: border-box !important;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

#login-box {
    width: 100%;
    max-width: 500px;
    min-width: 300px;
    box-shadow: #bbb 0 0 20px 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate( -50%, -50% );
}

#HeaderForLoginForm {
  background-image: url('https://dummyimage.com/600x106/333333/fff.png&text=SOME+LOGO');
  background-repeat: no-repeat;
  background-size: 200px;
  background-position-x: center;
  background-position-y: 25px;
  background-color: #000;
  height: 110px;
  text-align: center;
}

#headerlinks {
  color: rgb(90, 90, 90);
  font-weight: bold;
  font-size: 12px;
  margin-top: 54px;
  display: inline-block;
}

@media (min-width: 450px) {
  #HeaderForLoginForm {
    background-position-x: 25px;
    background-position-y: center;
    text-align: right;
    height: 95px;
    line-height: 95px;
  }
  #headerlinks {
    margin-right: 20px;
    margin-top: 0;
  }
}

#DivForLoginForm {
  background: #b7d9ff;
  background: -webkit-linear-gradient(#b7d9ff, #fff);
  background: -o-linear-gradient(#b7d9ff, #fff);
  background: -moz-linear-gradient(#b7d9ff, #fff);
  background: linear-gradient(#b7d9ff, #fff);
  text-align: center;
}

#LoginForm {
  display: inline-block;
  width: 74%;
  margin-top: 20px;
  margin-bottom: 40px;
}

#LoginForm input.textField {
  display: inline-block;
  width: 100%;
  padding: 10px;
  margin-top: 18px;
  font-size: 14px;
  border-radius: 3px;
  border: 1px solid #999;
}

#terms-wrapper {
  margin-top: 16px;
  margin-bottom: 30px;
  text-align: left;
  font-size: 14px;
  font-weight: bold;
}

#terms-wrapper input {
  margin-left: 0;
  vertical-align: -2px;
}

a[href] {
  color: #0079dd;
  text-decoration: none;
}

a[href]:hover {
  text-decoration: underline;
}

input#btn-login {
  padding: 14px;
  height: auto;
  width: 40%;
  min-width: 100px;
  float: right;
  background-color: #1064d8;
  color: #fff;
  font-weight: bold;
  font-size: 16px;
  outline: none !important;
  border: none;
  border-radius: 3px;
  cursor: pointer;
}

input#btn-login:hover {
  background-color: #004BBF;
}

input#btn-login:active {
  background-color: #0031A5;
}

#loginfooter {
  background-image: url("http://images.naldzgraphics.net/2014/08/20-brushed-seamless-texture.jpg");
  color: rgb(100, 100, 100);
  padding: 12px;
  font-size: 11px;
}

[data-val-required] {
  background-color: #fff;
}
<section id="login-box-wrapper">
  <div id="login-box">
    <header id="HeaderForLoginForm">
      <div id="headerlinks">
        <a href="#">some link</a> |
        <a href="#">some other link</a>
      </div>
    </header>
    <div id="DivForLoginForm">
      <form method="post" id="LoginForm">
        <input class="textField" id="UserName" name="UserName" placeholder="Username" type="text">
        <input class="textField" id="Password" name="Password" placeholder="Password" type="password">
        <div id="terms-wrapper">
          <input id="HasAcceptedTermsConditions" name="HasAcceptedTermsConditions" type="checkbox">
          <label for="HasAcceptedTermsConditions">
            I agree to the <a id="terms-link" href="#" target="_blank">General Terms of Service</a>
          </label>
        </div>
        <input id="btn-login" type="submit" value="LOG IN">
      </form>
    </div>
    <footer id="loginfooter" style="text-align: center;">
      <span>© 2009-2017 Some Company, LLC — All rights reserved</span>
    </footer>
  </div>
</section>

Answer №2

Take caution when implementing width: 100% on elements that are no longer in the normal flow of the document.

Consider using right: 0 instead of width: 100% for consistency, depending on the desired outcome when applying margins to these elements.

Utilizing width: 100%.

div {
  height: 20vh;
  border: .2em solid violet;
  box-sizing: border-box;
}

.relative {
  position: relative;
}

.absolute {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  border-color: dodgerblue;
  margin: .5em;
}
<div class="relative">
  <div class="absolute"></div>
</div>

Opting for right: 0.

div {
  height: 20vh;
  border: .2em solid violet;
  box-sizing: border-box;
}

.relative {
  position: relative;
}

.absolute {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  border-color: dodgerblue;
  margin: .5em;
}
<div class="relative">
  <div class="absolute"></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

Strategies for Connecting CSS, JavaScript, and Image Files in Django

Being new to Django 1.9.5 and working on Windows, I am facing difficulty in linking my CSS, images, and JS files to the Django template. This is how my project structure looks like: Here is my settings page: BASE_DIR = os.path.dirname(os.path.dirn ...

The ordering class is malfunctioning within the dropdown-menu of Bootstrap 5.2

I am having an issue with the order class when using a list-group inside a dropdown-menu in Bootstrap 5. I checked the documentation on ordering classes in Bootstrap 5 but I can't seem to get it to work inside the dropdown-menu. Here is a sample of t ...

Using Ajax to implement the Modal IF functionality

Currently in the process of registering a domain, utilizing two modals in the process. Through Ajax, I have set it up so that if the response is 1, an alert stating that the domain is available is displayed. If the response is 0, an alert stating that the ...

Lock the first row and a few lines in place for an HTML table

I'm facing a layout challenge on my webpage. I have a header line followed by a table, and I want both the header line and the top row of the table to be frozen while allowing the rest of the table to scroll. Despite trying to use 'position:fixed ...

The hamburger menu in Bootstrap collapses along with the navigation items when viewed on a mobile device

How can I resolve the issue of the hamburger menu icon collapsing with the nav-items when clicked? Navbar <header> <div class="container-fluid"> <div class="row"> <div class="col-12 col-sm-12"> ...

Angularjs directive retrieves infowindow DOM element from Google Maps

In order to apply some style fixes to the Infowindow, I am trying to select the element with the class 'gm-style-iw'. This selection process is taking place within an angularjs directive. <div ui-view="full-map" id="full-map" class="mainMap c ...

Display a snippet of each employee's biography along with a "Read More" button that will expand the content using Bootstrap, allowing users to learn more about each team

I have successfully developed a Google App Script that links to a Google Sheet serving as a database. The application iterates through each row, and I showcase each column as part of an employee bio page entry. ex. Picture | Name | Title | Phone | Email ...

Choose a procedure to reset to the original setting

I'm attempting to achieve something that seems straightforward, but I'm having trouble finding a solution. I have a <select> tag with 5 <option> elements. All I want to do is, when I click a button (which has a function inside of it), ...

Having trouble concealing images in HTML Emails on Outlook 2013

Struggling to properly hide images in Outlook 2013? Even when hidden, they are leaving a line of spacing that stretches the email. Here's the code I'm using to hide an image from the desktop version: <tr style="display: none; line-height: 0; ...

Creating a Sudoku puzzle grid with HTML and CSS - a step-by-step guide

Apologies for the basic inquiry, but I have created a sudoku grid with the following structure. Modified(Using Tables): <table id="grid" border="1" style="border-collapse: collapse;"> <tr class="row"> ...

How to conceal table cells on Safari 5?

My code works on Firefox and IE, but not on Safari. Here's an example: <table> <thead> <tr> <th style="display: none;">hi</th> </tr> </thead> <tr class="someClass"> <td style= ...

issues with conditional statement

I'm encountering an issue with the if statement not functioning as expected, and I can't seem to figure out why. The if condition always gets displayed regardless of the input. To troubleshoot, I initially used a temporary code snippet for the co ...

Search for text within the nearest <p> tag using HTML and jQuery

My table contains different td elements with the same ID, as shown in this foreach loop: <td class="AMLLeft" style="display:inline-block; !important">ID: <p class="important">${item.id}</p> </td> <td align="right" nowrap="tr ...

inconsistencies observed in flex layout between webkit browsers (Chrome and Safari) compared to moz (Firefox)

I am experiencing unexpected behavior with flex on Mozilla (-moz-) and Chrome/Safari (-webkit-) Referenced the Mozilla tutorial to understand flex layout /** { border: solid; border-width: 0 1px; }*/ html, body { width: 100%; height: 1 ...

Enable the Angular button only when the radio button has been selected

Just starting out with Angular and I have a query. This is a scenario for my project at work. https://i.stack.imgur.com/R3SxA.png In this screenshot, I want to enable the "ajouter" button when at least one radio button is selected in the secure chest (s ...

Retrieve the 'computed' CSS for the entire webpage

We operate multiple websites, each referencing 3-5 CSS files. Some of these files are shared across sites while others are specific to certain pages. Our aim is to streamline the CSS structure by consolidating into fewer files without altering the end res ...

What is the method to modify a section of a URL based on form input?

I am currently developing a website that allows users to interact with product images by clicking buttons, entering text into forms, and using a slider to view different angles of the product. My main goal is to extract user input from form fields and dyn ...

How can we target every nth child element universally?

I am working with an HTML structure that has a specific layout, and I need to target all odd <span> elements globally without considering their parent elements. Is there a way to style 1, 3, 5, 7, 9 in red color? I attempted to use :nth-child(odd) b ...

Is there a way to allow users to edit all the input fields within the td elements when they click the edit button for a specific row?

I am completely new to web dev and I'm struggling with what should be a simple task. My goal is to enable editing of all inputs in a table row when the edit link is clicked. Since I am not using jQuery, I prefer a pure JavaScript solution if possible. ...

Leveraging jQuery chosen for interactive form elements

This Table Row contains 5 input fields, with the first being a select box. I am utilizing the chosen jQuery plugin to enable search functionality for the select items. Since this is a dynamic form, I am duplicating these rows. However, I am facing an issu ...