Strange behavior is observed with CSS horizontal alignment when using width percentages

This situation may seem confusing to many, as it certainly does to me.

I am currently working on developing a login page where the credentials are positioned at the center of the page both horizontally and vertically.
I have successfully achieved this layout but along the way, I encountered something perplexing that I couldn't quite comprehend.

Below is my HTML code snippet:

<div class="LoginWrapper">
<div class="Login">
    <div id="Username" title="Register Number">
        <asp:Label ID="UsernameLabel" runat="server" Text="Username" AssociatedControlID="UserNameTextBox"></asp:Label>
        <asp:TextBox ID="UserNameTextBox" runat="server" CssClass="LoginTextBox"></asp:TextBox>
    </div>
    <div id="Password" title="Password">
        <asp:Label ID="PasswordLabel" runat="server" Text="Password" AssociatedControlID="PasswordTextBox"></asp:Label>
        <asp:TextBox ID="PasswordTextBox" TextMode="Password" runat="server"></asp:TextBox>
    </div>
    <div><asp:Button ID="Button1" runat="server" Text="Login" />
        </div>
</div>

And here are my CSS classes responsible for aligning the elements:

.LoginWrapper {
    width:1%;  //This part seems unconventional
    height:500px;
    display:table-cell;
    text-align:center;
    vertical-align:middle;
}

.Login {
    width:300px;
    height:auto;
    margin:10px;
    padding:20px;
    display:inline-block;
    text-align:center
}

One peculiar aspect is the width set to 1% in the LoginWrapper class. Strangely enough, this setting effectively centers my Login class horizontally. Even when zooming out significantly, the alignment remains consistent. Experimentation with different percentage values has yielded results that defy traditional expectations.
I have tested this across various browsers, consistently obtaining the same outcome.

Could someone shed light on what exactly is happening here?

Answer №1

Here is a glimpse into my response:

JSFIDDLE

HTML

<div class="LoginWrapper center">
    <div class="Login center">
        <div id="Username" title="Register Number">
            <asp:Label ID="UsernameLabel" runat="server" Text="Username" AssociatedControlID="UserNameTextBox"></asp:Label>
            <asp:TextBox ID="UserNameTextBox" runat="server" CssClass="LoginTextBox"></asp:TextBox>
        </div>
        <div id="Password" title="Password">
            <asp:Label ID="PasswordLabel" runat="server" Text="Password" AssociatedControlID="PasswordTextBox"></asp:Label>
            <asp:TextBox ID="PasswordTextBox" TextMode="Password" runat="server"></asp:TextBox>
        </div>
        <div>
            <asp:Button ID="Button1" runat="server" Text="Login" />
        </div>
    </div>
</div>

By the way, there seems to be a missing </div> in your code.

CSS

.LoginWrapper {
    width:100%; 
    height:500px;
    /*display:table-cell; */ not necessary
    text-align:center;
    vertical-align:middle;
    background:gray;
}
.Login {
    width:300px;
    height:200px;
    margin:10px;
    padding:20px;
    display:inline-block;
    text-align:center;
    background:blue;
}
.center {
  margin: auto;
  position: absolute;
  top: 0; left: 0; bottom: 0; right: 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

Unable to get CSS Filter to function properly in Firefox

I'm having trouble with the CSS filter on my Firefox (15.0) browser. Here is the HTML code: <div class="google"> <img src="https://www.google.com/images/srpr/logo3w.png"> </div> And here is the CSS code: .google{ -moz ...

"Learn the steps to toggle a sub menu using an onclick event and how to hide it using another

I created a sidebar navigation that displays submenus on mouseover, but I want them to open on click and close when clicking on the same tab. Please take a look at my code on this CodePen link. Thank you. <nav class="navigation"> <ul class="mai ...

Choose several items from the ASP.NET listbox programmatically

Currently, I am working with two databound listboxes. The primary listbox displays items assigned to my product, while the secondary listbox shows all available items. My goal is to automatically select all the items in listbox 2 that are also present in l ...

Determine the amount of interconnected nodes listed in a csv file depicting their relationships

I have a sizable CSV document formatted as follows: ServerID|AppID 01 | 01 01 | 02 02 | 02 02 | 03 02 | 04 03 | 04 The information from this file is being used in a d3 force layout, demonstrated in this example. The cr ...

Pattern for Ajax callback in Javascript

I'm facing an issue with the code snippet below. var bar = { ajaxcall : function(){ var _obj = {}; $.ajax({ headers: { 'Content-Type': "application/json; charset=utf-8", 'da ...

Ways to apply CSS for creating a circle with a "half border"

Someone named Simon Zhu was curious if CSS could be used to develop a circle with a "partial border," specifically a partial border that arced around more than 90 degrees of the circle. Check it out here: How to create partial circle border in CSS The re ...

PHP Mysqli - modifying data but encountering truncated items

I'm in the process of building a CRUD system, and I've encountered an issue on my edit page where the data being retrieved is getting cut off after the first space. For instance, if the company name in the database record is Stack Overflow, when ...

Transitioning from a see-through navigation bar to a vibrant colored menu as

I consider myself a novice when it comes to JavaScript. My goal is to create a Transparent Menu that smoothly transitions from transparent to white with a box shadow as I scroll down the page. However, the JavaScript code I have implemented does not seem ...

Ensure to verify the input's value by clicking the button

Upon clicking a button, I am trying to determine if there is any content in an input field. However, it seems that the check only happens once when the website first loads. Subsequent clicks of the button do not detect any new input values entered into the ...

What is the proper way to utilize CSS animation delays in order to design a collapsible HTML container?

I'm trying to implement responsive CSS animation delay to create an accordion effect when a user clicks on an arrow associated with a table, all without using JavaScript and only utilizing HTML/CSS. Check out the mobile view in action here: https://w ...

Creating a two-column list in C#

I have a SQL Server database that stores data in string format. The data appears like this: 08:00,11:00|11:00,13:00|13:00,16:00| As we can see, the string is divided by commas and pipes. The values separated by commas represent Start & End Time Slots, ...

Header sticking in place (Wordpress layout)

As a newcomer to HTML/CSS/PHP, I am taking on the challenge of building a website using an existing WP theme called onetone with a sticky header feature. Despite customizing it extensively, I have encountered a frustrating issue with the header. You can v ...

Ensure that you specify the scalar variable @TextBox1 in ASP.NET before proceeding

Here is a snippet of code that I am sharing for the solution to my latest application. Upon executing this code, an exception is thrown stating "Must declare the scalar variable @textBox1". public partial class About : Page { SqlCommand cmd = ...

What is the best way to create a miniaturized image of a webpage for sharing on Facebook?

I'm looking for a way to post a link to users' Facebook walls with a thumbnail image of their created page. The page is in HTML format and stored as a series of 'elements' in the database, each with its size, position, and content. Is i ...

Is there a way to access the badge hidden behind the collapsible menu in bootstrap 4?

After moving from bootstrap 3 to bootstrap 4, my items no longer align properly. I've scoured the entire Internet for a solution, but I've run out of options (and patience.. haha) This is how it currently looks: https://i.sstatic.net/ra22j.png ...

What is a straightforward method to choose a specific option in a <select> element based on the value provided by $_POST?

Is there a way to include the selected="selected" attribute in the option of an HTML <select> input directly from the received $_POST data without using an if statement for each option? ...

Looking for a straightforward way to incorporate a "Read more" and "Show Less" functionality into a Wordpress site using Javascript?

As a last resort, I am seeking a quick and simple fix. I have been experimenting with Javascript to implement a 'Read more...' & '... Read Less' feature on six different sections of a single page. The goal is to display only the fi ...

Ways to update Title label's CSS using JavaScript

These are my CSS rules: label[title="Hot"]{ display:none; } label[title="Cold"]{ display:none; } I am looking to convert this into JavaScript. Can you assist me with that? ...

Deploying ASP.NET Webforms with Markup Modifications

When making changes in ASP.NET Web Forms, it is common knowledge that when the C# code changes, the primary DLL also needs to be deployed. Additionally, if changes are made to the markup files (.aspx, .master) where new controls are added, the primary DLL ...

Pausing the Nivo slider when hovering over a specific non-slider element - a simple guide

As the saying goes, the title sums it up. I am trying to get the nivo slider to pause when I hover over the top menu in the Twenty Fourteen theme. It's been hours and I still can't figure out how to make it work... ...