The margin 0 auto feature is ineffective, and instead, the target is activated upon the page's initial loading

As a beginner in CSS, I am encountering some issues with the HTML page of my application. I am currently working on an app and aiming to create a login view where the login form appears either after a voice command or button click. I am facing two main problems - first, I am unable to center my form (although it works fine on jsbin), and secondly, my target event (animation) is running after the page is ready rather than after a link click.

    #przejscie
{
    width: 340px;
    display: block;
    opacity: 0;
    max-width: 100%;
    margin: 0 auto;
    position: relative;
    top: 500px;

 transition: transform 20s, background 50s;
 -o-transition: -o-transform 16s, background 0.5s;
 -moz-transition: -moz-transform 0.5s, background 0.5s;
 -webkit-transition: -webkit-transform 3s, opacity 2s, background 0.5s;
}
#przejscie:target
{
 transform: translate(-200);
 -o-transform: scale(1.2);
 -moz-transform: scale(1.2);
 -webkit-transform: translate(0px,-300px );

  opacity: 1;
}

This is my current CSS code for the form:

    <html>
<head>
     <script src="{% static 'js/artyom.min.js' %}"></script>
     <script src="{% static 'js/jquery-2.1.4.min.js' %}"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
        <link rel="stylesheet" type="text/css" href="{% static 'css/animacje.css' %}"/>
</head>

<h1><a href="#przejscie">Welcome register in our application</a></h1>

<div id="przejscie" class="container col-sm-3 srodek col-md-offset-2">

            <form method="post" action="/PPProject-web/register" class="form-signin">

                <label for="email" class="sr-only">Email Address</label>
                <input name="email" class="form-control" id="email" type="email" placeholder="Email" required>

                <label for="name" class="sr-only">Name</label>
                <input name="name" class="form-control" id="name"  placeholder="Name" required>

                <label for="surname" class="sr-only">Surname</label>
                <input name="surname" class="form-control" id="surname"  placeholder="Surname" required>

                <label for="username" class="sr-only">Username</label>
                <input name="username" class="form-control" id="username" placeholder="Username" required>

                <label for="password" class="sr-only">Password</label>
                <input name="password" type="password" class="form-control" id="password" placeholder="Password" required>

                <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>

            </form>
        </div>

</html>

Although the target function is working properly, I still need assistance in centering this div element.

Answer №1

When using absolute positioning, the automatic margin (margin: 0 auto) does not apply to the space being focused on. In such cases, it is recommended to change the position from "absolute" to "relative".

If you still need to utilize absolute positioning, consider implementing the following code:

#przejscie{
    display: block;
    max-width: 100%;
    width:400px;
    margin:0 auto;
    position: relative;
    background:salmon;
}

  
  #przejscie2{
    width:400px;
    position:relative;
    background:red;
}



  .cont-form{
    width:100%;
    height:auto;
    display: flex;
    justify-content: center;
    background:yellow;

  }
<div id="przejscie" class="container col-sm-3 srodek col-md-offset-2">
            <form method="post" action="/PPProject-web/register" class="form-signin">
                <label for="email" class="sr-only">Email Address</label>
                <input name="email" class="form-control" id="email" type="email" placeholder="Email" required>

                <label for="imie" class="sr-only">First Name</label>
                <input name="name" class="form-control" id="name"  placeholder="Name" required>

                <label for="nazwisko" class="sr-only">Last Name</label>
                <input name="last_name" class="form-control" id="last_name"  placeholder="Last Name" required>

                <label for="username" class="sr-only">Username</label>
                <input name="username" class="form-control" id="username" placeholder="Username" required>

                <label for="password" class="sr-only">Password</label>
                <input name="password" type="password" class="form-control" id="password" placeholder="Password" required>

                <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>

            </form>
        </div>


<br><br>

<div class="cont-form">
<div id="przejscie2" class="container col-sm-3 srodek col-md-offset-2">
            <form method="post" action="/PPProject-web/register" class="form-signin">
                <label for="email" class="sr-only">Email Address</label>
                <input name="email" class="form-control" id="email" type="email" placeholder="Email" required>

                <label for="imie" class="sr-only">First Name</label>
                <input name="name" class="form-control" id="name"  placeholder="Name" required>

                <label for="nazwisko" class="sr-only">Last Name</label>
                <input name="last_name" class="form-control" id="last_name"  placeholder="Last Name" required>

                <label for="username" class="sr-only">Username</label>
                <input name="username" class="form-control" id="username" placeholder="Username" required>

                <label for="password" class="sr-only">Password</label>
                <input name="password" type="password" class="form-control" id="password" placeholder="Password" required>

                <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>

            </form>
        </div>
</div>

Answer №2

It seems like your document is missing a crucial <body> tag.

Edit: After some review, it looks like the issue may be related to absolute positioning and the form taking up 100% of the width.

When using margin: auto, the element's width, padding, and borders are considered to calculate margin on both sides equally. If the form takes up the entire body width, there won't be any margin present.

Absolute positioning removes the element from the flow, causing it to ignore other elements. This could explain why the browser treats the margin as zero in this situation.

Consider trying position:relative instead or setting all position properties to zero for horizontal and vertical centering. You can find more information in this answer: css absolute position won't work with margin-left:auto margin-right: auto.

I experimented by adding position: relative and width:50% to #przejscie, which seemed to resolve the issue.

For further insights, check out: https://developer.mozilla.org/en-US/docs/Web/CSS/position#Absolute_positioning

Answer №3

It is essential to specify the width of the element you want to center

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

Mobile display trapping Bootstrap navbar

Can anyone help me figure out why my navbar is stuck in its mobile display? Here is the link to the code: http://jsfiddle.net/enL35t1q/ Navbar <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul cla ...

Creating PNG images in a scripting language for web applications

Imagine giving your website user the ability to specify a radius size, let's say 5px, and in return receiving a PNG file with a circle of that radius. This question can be divided into two sections: In what language and using which technologies can ...

What is the best way to position the header at the top and the footer at the bottom of the page, with the main content in between using

Recently, I started using Bootstrap and incorporated Bootstrap 5.3 into this HTML page. My goal is to align the header at the top, footer at the bottom, and adjust the size of the main content in between. Here's the code I attempted: <!DOCTYPE html ...

Tracking progress with a dynamic bar from the beginning to the end

Struggling to develop a progress bar that corresponds to project start dates and end dates. With a roster of 100 projects, spanning from mid-2017 to future launches, the aim is to depict progress between 2018, 2019, and 2020. If a project commenced in 2017 ...

Troubleshooting Issue with Importing File into CSS Document

I'm currently working on building a website and I've been trying to import an image to use as the header. I want to add this .png picture to my CSS file, but despite extensive research and double checking everything, I still can't figure out ...

Using static typing in Visual Studio for Angular HTML

Is there a tool that can validate HTML and provide intellisense similar to TypeScript? I'm looking for something that can detect errors when using non-existent directives or undeclared scope properties, similar to how TypeScript handles compilation er ...

Can anyone suggest a stellar sprite generator for me?

I am in search of a sprite generator to streamline my web development process. With so many options available, I am seeking recommendations for the best tool. Ideally, I want a generator that can produce both a sprite image and corresponding CSS files wi ...

Utilizing Nginx with Angular2 for a seamless PathLocationStrategy implementation in html5

Angular2 is a single-page site, so all URL requests need to be redirected to index.html using Nginx. Below is the Nginx server block configuration: server { listen 8901; server_name my_server_ip; root /projects/my_site/dist; location /.+& ...

<container> rectangular form

.products { width: 100%; height: 500px; background: gray; } .box { width: 250px; height: 300px; background: darksalmon; border: 1px solid #000; } <div class="products"> <div class="box"> <p>Ola</p> </div> ...

Dynamic sidebar that adheres to the content and is placed alongside it using JavaScript

I am in need of creating a sticky sidebar that will float beside my content column. The purpose of this sidebar is to contain jump links on the page, similar to what can be seen on this page, but with the navigation buttons positioned directly next to the ...

Invoke a function to retrieve HTML content from a controller method

public ActionResult MyActionMethod(MyModel model) { //some code string myVar= ActionMethod2(model).toString(); //use myVar Method3(myVar, otherVar); } public ActionResult ActionMethod2()(MyModel model) { return View(model); } private ...

Issue with Modal Box: Datepicker not Displaying Correctly

I have implemented a modal box in my webpage. When I click on a text field where a datepicker is added, it does not display anything. However, when inspecting the element using Chrome's developer tools, I can see that the 'hasDatepicker' cla ...

Tips on activating the overlay solely on the image without affecting the entire column

Currently, I am working on a project locally and do not plan to release it. However, I am facing an issue with making the overlay only appear on the image rather than covering the entire column. I came across some pre-built code on the w3 schools website ...

Tips for customizing the appearance of a functional stateless component in Reactjs using class objects

I am looking to create a functional stateless component in ReactJs following the guidelines outlined here. const MyBlueButton = props => { const styles = { background: 'blue', color: 'white' }; return <button {...props} sty ...

Firefox does not allow contenteditable elements to be edited if they are nested inside a parent element that is draggable

Here is a basic HTML example: <div draggable=true> <div contenteditable=true>can't edit me</div> </div> When testing in Chrome, I noticed that I was able to edit the contents of the contenteditable div, however, this functi ...

Issues with jQuery animate not functioning properly when triggered on scroll position

I found a solution on Stack Overflow at this link, but I'm having trouble implementing it properly. The idea is to make an element change opacity from 0 to 1 when the page is scrolled to it, but it's not working as expected. The element is locat ...

Expand Navigation Bar upon Hover

My goal is to recreate a Navigation Bar similar to the one found on this website: I want the Navigation Block to expand and show Menu Items when hovered over, just like the example on the website mentioned above. I've attempted to use Bootstrap and ...

Utilizing Angular 2 for dynamic CSS binding: manipulating style.width and style.height properties

I am experiencing some unusual rendering issues when using plain HTML5 canvas and CSS binding with A2. Can anyone please explain the difference between the following code snippets: <canvas height="400" width="1200" #background> </canvas> AND ...

What is the best way to generate two <div> elements, with one of them being centrally fixed?

When it comes to CSS, I must admit that it's not my strong suit. My current challenge is to create two boxes or divs where one remains fixed in the center of the page, while the other adapts and positions itself right next to the first one. Essentiall ...

There is a gap found in the span element in Angular, but this gap is not seen in

Comparing two component templates, one in AngularJS and the other in modern Angular: <span ng-repeat="status in statusInformation.statusList | filter : whereFilter" class="show-on-hover"> <span class="app-icon ap ...