How to position a div in the middle of the screen when the page is bigger than the screen
just add position:fixed
and it will keep it in view even if you scroll down. see it at http://jsfiddle.net/XEUbc/1/
#mydiv {
position:fixed;
top: 50%;
left: 50%;
width:30em;
height:18em;
margin-top: -9em; /*set to a negative number 1/2 of your height*/
margin-left: -15em; /*set to a negative number 1/2 of your width*/
border: 1px solid #ccc;
background-color: #f3f3f3;
}
Positioning div element at center of screen
The easy way, if you have a fixed width and height:
#divElement{
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
width: 100px;
height: 100px;
}
Please don't use inline styles! Here is a working example http://jsfiddle.net/S5bKq/.
Center a 'div' in the middle of the screen, even when the page is scrolled up or down?
Change the position
attribute to fixed
instead of absolute
.
CSS - div always absolute middle left of screen even if page scroll
.timer {
position: fixed;
top: 50%;
left: 0px;
transform: translateY(-50%)
}
An element with position: fixed; is positioned relative to the viewport, which means it always stays in the same place even if the page is scrolled. The top, right, bottom, and left properties are used to position the element.
your idea is correct if needed you can try out all possible values to figure out which gives you the result.
https://www.w3schools.com/css/css_positioning.asp
How to align a div to the middle (horizontally/width) of the page
<body>
<div style="width:800px; margin:0 auto;">
centered content
</div>
</body>
Related Topics
How to Do Ie Conditionals in CSS
Continuously Adjust Element Size as Screen Size Changes
Finding "Equivalent" Color with Opacity
Differencebetween the Initial and Unset Values
How to Prevent CSS3 Animation Reset When Finished
Multiple Classes in CSS Selector
CSS Calc() - Multiplication and Division with Unit-Ed Values
Performance Differences Between Color Declarations
How to Include Common CSS Class in Another CSS Class
How to Change CSS Class for the Inputfield and Label When Validation Fails
Sass:Making Underscore File Names Actually Create CSS Files
My Z-Index Property Is Not Getting Set
Switching to Em-Based Media Queries
Why Doesn't a Child's Margin Affect a Parent's Height
Change Font-Weight of Fontawesome Icons