0

How can I have the following code centered vertically? It's already centered vertically, but I can't figure out how to have it center in into the middle of the screen. Also, I'd like to add some small links under the input field like "About | Report Image | Etc" but they're also not centering correctly.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="robots" content="noindex">

    <title>Upload</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

    <style type="text/css">
        @import url(http://fonts.googleapis.com/css?family=Open+Sans:400,700,300);
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,700,300);
body {
    font: 12px 'Open Sans';
}
.form-control, .thumbnail {
    border-radius: 2px;
}
.btn-danger {
    background-color: #B73333;
}

body {
  font-size: 80%;
  padding: 20px;
}

.center-block {
  float: none;
  margin: 0 auto;
}

main {
  position: relative;
  background: white;
  height: 200px;
  width: 60%;
  margin: 0 auto;
  padding: 20px;
  resize: both;
  overflow: auto;
}

main div {
  background: black;
  color: white;
  width: 200px;
  height: 100px;
  margin: -70px 0 0 -120px;
  position: absolute;
  top: 50%;
  left: 50%;
  padding: 20px;
}

/* File Upload */
.fake-shadow {
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}
.fileUpload {
    position: relative;
    overflow: hidden;
}
.fileUpload #upload_form {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    padding: 0;
    font-size: 33px;
    cursor: pointer;
    opacity: 0;
    filter: alpha(opacity=0);
}
.img-preview {
    width: 100%;
}

.container>.row>.col-md-6 {
  margin: 0 auto;
}
    </style>

    <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>
<body>
  <div class="container">
      <div class="row justify-content-center">
    <div class="col-md-6 center-block">
        <div class="form-group">
              <div class="main-img-preview center-block">
                <img class="thumbnail img-preview" src="http://farm4.static.flickr.com/3316/3546531954_eef60a3d37.jpg" title="Preview Logo">
              </div>
              <div class="input-group">
                <input id="fakeUploadLogo" class="form-control fake-shadow" placeholder="Select Image" disabled="disabled">
                <div class="input-group-btn">
                  <div class="fileUpload btn btn-danger fake-shadow">
                    <span><i class="glyphicon glyphicon-upload"></i> Upload Image</span>
                      <form id="image_form" action="upload_image" method="post">
                    <input class="attachment_upload" id="upload_form" name="image" type="file" enctype="multipart/form-data">
                    </form>
                  </div>
                </div>
              </div>
            </div>
    </div>
</div>
      </div>
  <script type="text/javascript">
  $(document).ready(function() {
    var brand = document.getElementById('upload_form');
    brand.className = 'attachment_upload';
    brand.onchange = function() {
        document.getElementById('fakeUploadLogo').value = this.value.substring(12);
    };

    function AKUpload(input) {
            var xmlHttpRequest = new XMLHttpRequest();
            xmlHttpRequest.onreadystatechange = function() {
                if (this.readyState == 4 && this.status == 200) {
                    window.location.replace(this.responseText);
               }
            };
            xmlHttpRequest.open("POST", '/upload_image', true);
            var formData = new FormData();
            formData.append("file", input.files[0]);
            xmlHttpRequest.send(formData);
            console.log(xmlHttpRequest.response);

    }
    $("#upload_form").change(function() {
        AKUpload(this);
    });
});



  </script>
</body>
</html>
Ryan
  • 21
  • 3
  • A couple things - you are loading bootstrap.css twice, it seems? If you are trying to override or customize any boostrap classes in your custom CSS, you are just overwriting that. Also, you are using Bootstrap 4 - there are many changes over bootstrap 3. In your `container`, you should not have any column class settings. – Ken H. Apr 18 '17 at 11:43
  • I think this you can get some solution right here https://www.w3schools.com/css/css_align.asp (CSS Layout - Horizontal & Vertical Align). It will help you. – Carl Angelo Nievarez Apr 19 '17 at 05:48

3 Answers3

1

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"/>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="robots" content="noindex">

    <title>Upload</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

    <style type="text/css">
        @import url(http://fonts.googleapis.com/css?family=Open+Sans:400,700,300);
        body {
            font: 12px 'Open Sans';
        }
        .form-control, .thumbnail {
            border-radius: 2px;
        }
        .btn-danger {
            background-color: #B73333;
        }


        /* File Upload */
        .fake-shadow {
            box-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
        }
        .fileUpload {
            position: relative;
            overflow: hidden;
        }
        .fileUpload #upload_form {
            position: absolute;
            top: 0;
            right: 0;
            margin: 0;
            padding: 0;
            font-size: 33px;
            cursor: pointer;
            opacity: 0;
            filter: alpha(opacity=0);
        }
        .img-preview {
            max-width: 100%;
        }
    </style>

    <script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
</head>
<body>

  <div class="row">
  <div class="col-md-12">
    <div class="col-md-6 col-md-offset-3">
        <div class="form-group">
          <div class="main-img-preview center-block">
            <img class="thumbnail img-preview" src="http://farm4.static.flickr.com/3316/3546531954_eef60a3d37.jpg" title="Preview Logo">
        </div>
        <div class="input-group">
            <input id="fakeUploadLogo" class="form-control fake-shadow" placeholder="Select Image" disabled="disabled">
            <div class="input-group-btn">
              <div class="fileUpload btn btn-danger fake-shadow">
                <span><i class="glyphicon glyphicon-upload"></i> Upload Image</span>
                <form id="image_form" action="upload_image" method="post">
                    <input class="attachment_upload" id="upload_form" name="image" type="file" enctype="multipart/form-data">
                </form>
            </div>
        </div>
    </div>
</div>
</div>
</div>
</div>

<script type="text/javascript">
  $(document).ready(function() {
    var brand = document.getElementById('upload_form');
    brand.className = 'attachment_upload';
    brand.onchange = function() {
        document.getElementById('fakeUploadLogo').value = this.value.substring(12);
    };

    function AKUpload(input) {
        var xmlHttpRequest = new XMLHttpRequest();
        xmlHttpRequest.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                window.location.replace(this.responseText);
            }
        };
        xmlHttpRequest.open("POST", '/upload_image', true);
        var formData = new FormData();
        formData.append("file", input.files[0]);
        xmlHttpRequest.send(formData);
        console.log(xmlHttpRequest.response);

    }
    $("#upload_form").change(function() {
        AKUpload(this);
    });
});



</script>
</body>
</html>

i have made small changes in your grid col structure. best practice is to creat row than colum so i did that. i have created one column with col-md-6 and col-md-offset-3 (TESTED) on my device

Ken H.
  • 408
  • 1
  • 3
  • 11
Himesh Suthar
  • 562
  • 4
  • 14
  • It looks like you are loading Bootstrap.css multiple times? A 3.3.7 link at the top, which I just noticed when looking at the full view, and the original load of bootstrap 4 twice. Which one is the correct one for your version? – Ken H. Apr 18 '17 at 11:57
  • @KenH. I've updated my original code - I did indeed mean to use Bootstrap 4 over 3. – Ryan Apr 18 '17 at 12:20
0

Try to use the "justify-content-start"-Function of the grid view from bootstrap.

Your row should then start like this:

<body>
  <div class="container">
    <div class="row justify-content-center">
      <div class="col-6">
        <!-- put your content here -->
      </div>
    </div>
  </div>
</body>

This is new in Bootstrap 4 and a really nice feature.

Benjamin Schüller
  • 2,104
  • 1
  • 17
  • 29
0

Updated answer (vertical alignment): As your answer has been updated to ask for vertical centering, try adding the following to your CSS. There are many questions asking about vertical alignment.

html, body {
  height: 100%;
}
body {
  display:flex;
  align-items:center;
}

Original answer (horizontal alignment): Firstly, don't add column classes to your container div.

If you want to use Bootstrap 4, try switching the center-block class to the new mx-auto class. You can then apply this to your main column in place of the offset:

<div class="col-md-6 mx-auto">
...
</div>

Alternatively, for Bootstrap 3, check this answer.

Community
  • 1
  • 1
codemacabre
  • 1,112
  • 12
  • 20
  • 1
    `center-block` is a bootstrap 3 class - there is confusion in this original code between Bootstrap 3 (which might be what he wants to use) and the fact that he is loading Bootstrap 4. – Ken H. Apr 18 '17 at 12:15
  • 1
    Thanks @Ken; although a Bootstrap 3 class, `center-block` needed `float: none;` added to make it work in OP's original code (according to the v3.0.1 update to [this answer](http://stackoverflow.com/a/18153551/7584702)). As he seems to actually want Bootstrap 4, I've edited my answer to reflect the Boostrap 4 equivalent class, `mx-auto`. – codemacabre Apr 18 '17 at 12:34
  • @CodeMacabre I tried your changes but it still isn't centered vertically. – Ryan Apr 18 '17 at 12:45