6

Is there an option to find center position (top and left) of an actual screen position?

My main goal is to show div in the center of the screen no matter where i scroll or click

joc
  • 1,336
  • 12
  • 31
user6769617
  • 61
  • 1
  • 1
  • 2
  • And you are committed to using javascript/jquery for this? Or is a CSS-only solution valid as well? – roberrrt-s Aug 29 '16 at 09:50
  • 6
    I will answer with a question: what have you tried? If you google a bit you will find plenty of examples to start with. Then you can ask for help on your specific code – Lelio Faieta Aug 29 '16 at 09:50
  • Yes, there's an option: calculate the center using window.screen - https://developer.mozilla.org/en-US/docs/Web/API/Window/screen – Mike Scotty Aug 29 '16 at 09:51
  • 2
    FYI, pure css solution. http://codepen.io/lzl124631x/pen/JKgwvY – lzl124631x Aug 29 '16 at 09:53
  • `$(window).height() - $('div.yourelement').height()) / 2` you can figure out width. – vaso123 Aug 29 '16 at 09:55
  • Possible duplicate of [How to center a "position: absolute" element](http://stackoverflow.com/questions/8508275/how-to-center-a-position-absolute-element) – Ismail Farooq Aug 29 '16 at 10:01
  • please, only javascript. I've tried a lot of examples from google, non of them works. – user6769617 Aug 29 '16 at 10:07
  • by the way, the solution with pure css done by Moon is the best. Why don't use just css instead of javascript? – Jordi Flores Aug 29 '16 at 10:13

6 Answers6

8

You can use window attributes to get center coordinates in pure javascript:

var x = window.innerWidth / 2;
var y = window.innerHeight / 2;
joc
  • 1,336
  • 12
  • 31
1

With Jquery

jQuery.fn.center = function(parent) {
    if (parent) {
        parent = this.parent();
    } else {
        parent = window;
    }
    this.css({
        "position": "absolute",
        "top": ((($(parent).height() - this.outerHeight()) / 2) + $(parent).scrollTop() + "px"),
        "left": ((($(parent).width() - this.outerWidth()) / 2) + $(parent).scrollLeft() + "px")
    });
return this;
}
$("div.target:nth-child(1)").center(true);
$("div.target:nth-child(2)").center(false);
div.container{
    width:300px;
    height:300px;
    border:1px solid #555;
    position:relative;
    top:10px;
    left:10px;
}

div.target{
    width:50px;
    height:50px;
    color:white;
    background:rgba(30,30,30,.7);
    border-radius: 10px;
    text-align:center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
    <div class="target">1<br>parent</div>
    <div class="target">2<br>window</div>
</div>

With Pure CSS

div {
  background:green;
  position:fixed;
  color:#fff;
  width:50px;
  height:50px;
  top:50%;
  left:50%;
  -ms-transform: translateX(-50%) translateY(-50%);
  -webkit-transform: translate(-50%,-50%);
  transform: translate(-50%,-50%);
}
<div></div>
Ismail Farooq
  • 6,309
  • 1
  • 27
  • 47
0

please add these code to add center div in web page

div {
  background:red;
  position:absolute;
  color:#fff;
  top:50%;
  left:50%;
  padding:50px;
  -ms-transform: translateX(-50%) translateY(-50%);
  -webkit-transform: translate(-50%,-50%);
  transform: translate(-50%,-50%);
} 
<div></div>
0

Please tray this code.

$(document).ready(function(){
  $(window).resize(function(){
 var windowWidth = window.innerWidth;
 var windowHeight = window.innerHeight;
 $('.className').css({
  position: 'absolute',
  left: windowWidth / 2,
  top: windowHeight / 2
    }); 
  });
  // call `resize` to center elements
  $(window).resize();
});
div {
  background:red;  
  color:#fff;  
  padding:50px;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="className"></div>
0

You can write it like this:

div {
   display: block;
   position: fixed;
   height: // Your div height
   width: // Your div width
   top: // your window height / 2;
   left: // your window width / 2;`
}

The key of this problem is the position, where if you change it onto fixed, it means that element always sticks at that current position (even you scrolled).

Elijah Mock
  • 587
  • 8
  • 21
Lorenzo028
  • 17
  • 1
  • 4
0

See resize event documentation to get it to resize whenever the screen size is changed.

// grab el
const myEl = document.getElementById("yourId");
// initial styles
myEl.style.position = "absolute";
myEl.style.transform = "translate(-50%, -50%)"; // to counter
let x,y;
// update screen positioning
function updateCoords() {
  x = window.innerWidth / 2;
  y = window.innerHeight / 2;
}
function onResize() {
  updateCoords();
  myEl.style.top = x;
  myEl.style.left = y;
}
onResize(); // start in the middle
// when window size changes, update the element position
window.addEventListener("resize", onResize);
Elijah Mock
  • 587
  • 8
  • 21