1

So I'm trying to keep my div centred when a page resizes. Like this for example https://accounts.spotify.com/en/login

But when I resize it currently resizes the whole div. Here's the relevant code:

<div id = "wrapper">
    <div id = "formwrap">
        <form action = "./PHP/main_login.php" method = "post">
            <input type = "text" name = "email" id = "email" placeholder = "Email"/><br>
            <input type = "password" name = "password" id = "password" placeholder = "Password"/><br>
            <input type = "submit" name = "submitlogin" id = "submitbut" value = "Login">
        </form>
    </div>
</div>
#wrapper{
    padding-left: 30%;
    padding-right: 30%;
    float: center;
}

#formwrap{
    width:100%;
    background-color: #383838;
    float: center;
    margin: 0 auto;
}

Essentially I'm trying to keep the div and the contents within it the same size while decreasing the horizontal screen size until I call a media query. All help much appreciated

Generic Snake
  • 575
  • 1
  • 5
  • 13
  • Instead of defining a percentage passing on the outer parent, use a fix width for the child instead. And there isn't such a thing as `float: center`. – Terry Mar 29 '16 at 23:28
  • Possible duplicate of [Horizontally center a div in a div](http://stackoverflow.com/questions/114543/horizontally-center-a-div-in-a-div) – Terry Mar 29 '16 at 23:29
  • 1
    Set a width for you wrapper and give it `margin: 0 auto` – Yuri Mar 29 '16 at 23:31
  • Refer to this: [How to keep a floating div centered on window resize](http://stackoverflow.com/questions/2956671/how-to-keep-a-floating-div-centered-on-window-resize-jquery-css) – Shady Alset Mar 29 '16 at 23:31

3 Answers3

2

Place it in a container div with text-align:center and add display:inline-block to it:

<div style="text-align:center;">
    <div style="display:inline-block;">


    </div>
</div>

Then your div will be at the center of page and its with won't change by resizing but with its content or the width given to it.

Here is the DEMO

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
0

Try something like this: https://jsfiddle.net/ogs0mfmn/

#wrapper{
 margin-left: auto;
 margin-right: auto;
}

#formwrap{
 margin-left: 50%;
 margin-right: 50%;
 background-color: #383838;
}
Frankely Diaz
  • 886
  • 1
  • 9
  • 16
0

Set a width to your #formwrapper and give it margin: 0 auto

#wrapper{
    padding-left: 30%;
    padding-right: 30%;
    float: center; background-color: blue
    height: 100vh; width: 40%;
}

#formwrap{
    width:200px;
    background-color: #383838;
    margin: 0 auto;
}

Check fiddle

Yuri
  • 3,082
  • 3
  • 28
  • 47