I'm having trouble aligning this div in the center of the screen. It looks like the position is slightly off

<!DOCTYPE html>
<html>

<head>
  <style>
    body {
      background-color: #7a64fa;
    }
    
    .container {
      background-color: #ffffff;
      position: fixed;
      top: 50%;
      left: 50%;
      margin-top: -50px;
      margin-left: -100px;
      padding-top: 75px;
      padding-right: 50px;
      padding-bottom: 75px;
      padding-left: 50px;
      border-radius: 10px;
    }
    
    label {
      font-size: 25px;
      font-family: "Lucida Console", "Courier New", monospace;
    }
    
    input {
      border-radius: 5px;
    }
    
    .ftm {
      display: inline-block;
    }
  </style>
  <meta charset="ISO-8859-1">
  <title>Feet to Meter</title>
</head>

<body>
  <div class="container">
    <div class="ftm">
      <label>Feet</label><br /> <input style="height: 40px;" type="number" name="feet">
    </div>
    <div class="ftm">
      <label>Meter</label> <br /> <input style="height: 40px;" type="number" name="meter">
    </div>
  </div>

</body>

</html>

I've been troubleshooting why the container DIV won't center properly on the screen. Despite adjusting the CSS properties for the container, I can't seem to get it perfectly centered. The issue might be related to the padding as well.

Answer №1

It seems like your attempt to center the div is not quite hitting the mark.

  top: 50%;
  left: 50%;
  margin-top: -50px;
  margin-left: -100px;

The reason this approach is not effective is because the dimensions of the div are not set to 100x200. A better solution would be to utilize relative transform:

  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);

Answer №2

Take out the margins from the .container class

margin-top: -50px; // delete
margin-left: -100px;  // delete

Include

transform: translate(-50%, -50%);

Answer №3

Eliminate the margin-left property from .container and insert transform: translateX(-50%); Check out translate() by mdn for further information on css translate

      .container {
    background-color: #ffffff;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translateX(-50%);
    margin-top: -50px;
    /* margin-left: -100px; */
    padding-top: 75px;
    padding-right: 50px;
    padding-bottom: 75px;
    padding-left: 50px;
    border-radius: 10px;
  }

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

Deploying static files with Django in a production environment

My Django application is functioning properly on Ubuntu 14.04 with nginx 1.10, Django 1.10.2, and uWSGI 2.0.14. It is able to load static files such as JavaScript, CSS, and images, but the CSS files are not being applied to my website. Below is the configu ...

Unable to access a hyperlink, the URL simply disregards any parameters

When I click an a tag in React, it doesn't take me to the specified href. Instead, it removes all parameters in the URL after the "?". For example, if I'm on http://localhost:6006/iframe.html?selectedKind=Survey&selectedStory=...etc, clicking ...

Basic node.js server that responds with HTML and CSS

I have successfully created a basic http server to send an HTML file as a response. However, I'm struggling with how to also send a CSS file so that the client can view the HTML page styled with CSS in their browser. Here is my current code: var htt ...

VUE 3 inexplicably failing to display the custom component's template, console remains error-free

I am utilizing flask, html, js for building a web application. I am currently facing an issue where the template defined in the component <add-movie> within movies.js is not being rendered into add_movies.html. The add_movies.html extends base_admin ...

Is it possible to develop Hybrid Apps specifically for Windows Tablets/Surface devices?

We have created a Hybrid App for Android devices and made it compatible with Windows by using Adobe PhoneGap Build to generate a .XAP package from the existing Source Code. We believe that this .XAP package can easily be deployed on a Windows Mobile devic ...

How can I force a variable width font to behave like a fixed width font in HTML?

Is there a method to emulate the appearance of a fixed-width font using a variable width font in HTML? For instance, if I were to display the phrase "The quick grey fox jumped over the lazy dog" in "Courier New," a fixed-width font, it would appear wider ...

When you press the submit button, the screen automatically scrolls down but no action is taken

I'm currently utilizing Selenium WebDriver (Java) with the Firefox driver. Upon trying to click the 'Submit' button on a particular page, it only results in scrolling down the screen slightly. The button itself does not register the click, c ...

Retrieve the html code from a PHP backend by utilizing core-ajax within the Polymer framework to store the information

Is there a way to retrieve JSON data from a PHP script that contains HTML tags, and display the data properly in an HTML format without seeing the tags? Currently, HTML tags are displayed as: <h1>hello</h1> instead of rendering as: hello I am ...

What is the best way to create underlined text that is either left-aligned or centered?

I am looking to replicate this specific style of decoration in a full width container. My goal is to have it aligned to the left of the text and restricted to a maximum width. https://i.stack.imgur.com/H8DXT.png ...

Images are not appearing correctly on Chrome browsers when using Windows operating system

img tags seem to have unusual margins in Chrome, Edge, and Opera browsers, while Firefox displays them correctly. Queries What is causing these margins specifically in Chrome? The Devtool does not detect any margin properties. Is there a straightforward s ...

stream a song with a font awesome symbol

Can an audio track be played using a font awesome icon, such as displaying the song name (mp3) with a play icon right next to it? When users click on the play icon, can the track start playing and be paused or stopped at will? The list will consist of app ...

What steps can I take to create a textbox that expands as more text is

Looking to create a unique textbook design that starts out with specific width and height dimensions, but expands downward as users type beyond the initial space. Wondering if CSS can help achieve this functionality? In a standard textbox, only a scroll ba ...

Tips for expanding the contentedible HTML element downwards within its container

When a user inputs content, I need the paragraph to expand only downward while the div with the ID of description also grows in the same direction. The main parent div labeled documentation should remain fixed so it doesn't expand upwards. This scenar ...

Identify a specific HTML template using jQuery

I am currently working on implementing datepickers in various HTML templates. The issue I am facing is that the functionality only seems to work in my index.html file. When I try to replicate the same setup in another template, it does not function proper ...

Can the color of text be adjusted (to either white or black) based on the background color (in any color and format)?

To achieve a text color that contrasts well with any background, I need to make sure it's either black or white. The background in my app can vary in color and format, so finding the perfect solution has been challenging. Using mix-blend-mode doesn&a ...

Having trouble with adding a class on scroll?

My challenge is to extract the header from this website, by adding an additional class when the user scrolls at a position greater than 0. I thought it would be easy, but Java always presents problems for me. Here’s the code I came up with: <!DOCTY ...

The final li element in the second unordered list is targeted by the selector ul li:last-child

Check out this JsFiddle Having trouble with the pseudo code :last-child. I want the style to apply to the li containing E, but it's affecting the last li in the .subNav. I've attempted a few solutions below, but none have worked. nav ul:first- ...

Creating a Rectangular Trapezoid Shape with CSS - Eliminating Unnecessary Spacing

I'm trying to create a trapezoid button using CSS. Here is the intended look: However, my current implementation looks like this: The button appears fine but there seems to be some excess space below it. It's almost like an unwanted margin, ev ...

Is it possible to rearrange column order in Bootstrap 4x according to different viewport sizes?

One of the great things about Bootstrap is its ability to rearrange columns regardless of the HTML structure. However, I am looking to have a different order of elements when the viewport size is smaller (e.g., Mobile). The current HTML structure looks li ...

Flaws in the implementation of Github pages deployment hindered its

When I attempted to run one of my repositories on GitHub for deployment, I encountered an issue where the CSS and JS contents were not loading correctly. You can access the repository through this link: https://github.com/vipin10100001/agecalculator If yo ...