Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript center div on the page include scrolling

In javascript I need to show div content on the center of the browser window; Content of the window is high therefore it has a scroll.

How can I set up div element on the center of the screen independently of the scroll ?

Thanks!

like image 464
ihorko Avatar asked Dec 21 '10 13:12

ihorko


People also ask

How do you center a div in the middle of the screen?

You can do this by setting the display property to “flex.” Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

How do you place an element in the center of the page?

To horizontally center a block element (like <div>), use margin: auto; Setting the width of the element will prevent it from stretching out to the edges of its container.

How do you center a container in the middle of the page?

Use a container element and set a specific max-width . A common width many websites use is 960px. To actually center the page, add margin: auto .


1 Answers

You can use this CSS (You don't need javascript for that):

#divID{
  width:500px;   /* adjust */
  height:500px;  /* adjust */
  top:50%;
  left:50%;
  margin-left: -250px;  /* half of the width */
  margin-top: -250px;  /* half of the height */
  position:fixed;
}

You can check out the demo here including scrolling

You can make the div pretty with additional CSS.

Check out demo

like image 173
Sarfraz Avatar answered Oct 20 '22 00:10

Sarfraz