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/.
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;
}
Center a position:fixed element
You basically need to set top
and left
to 50%
to center the left-top corner of the div. You also need to set the margin-top
and margin-left
to the negative half of the div's height and width to shift the center towards the middle of the div.
Thus, provided a <!DOCTYPE html>
(standards mode), this should do:
position: fixed;
width: 500px;
height: 200px;
top: 50%;
left: 50%;
margin-top: -100px; /* Negative half of height. */
margin-left: -250px; /* Negative half of width. */
Or, if you don't care about centering vertically and old browsers such as IE6/7, then you can instead also add left: 0
and right: 0
to the element having a margin-left
and margin-right
of auto
, so that the fixed positioned element having a fixed width knows where its left and right offsets start. In your case thus:
position: fixed;
width: 500px;
height: 200px;
margin: 5% auto; /* Will not center vertically and won't work in IE6/7. */
left: 0;
right: 0;
Again, this works only in IE8+ if you care about IE, and this centers only horizontally not vertically.
How do I position a div at the bottom center of the screen
align="center"
has no effect.
Since you have position:absolute
, I would recommend positioning it 50% from the left and then subtracting half of its width from its left margin.
#manipulate {
position:absolute;
width:300px;
height:300px;
background:#063;
bottom:0px;
right:25%;
left:50%;
margin-left:-150px;
}
Aligning a div to center of page while its position is absolute?
UPDATE: This is an old answer and the answer currently just below this gives a nicer solution which works even if your
div
has dynamic width. Another alternative, usingmargin: auto
, can be found here, on a different, but related, question.
You can do this if you know the width of the DIV
you want to centre.
CSS:
div
{
position: absolute;
top: 50%;
left: 50%;
width: 400px;
height: 300px;
margin-top: -150px;
margin-left: -200px;
}
You position the top left corner in the centre, and then use negative margins which are half of the width to centre it.
How can I center an absolutely positioned element in a div?
<body> <div style="position: absolute; left: 50%;"> <div style="position: relative; left: -50%; border: dotted red 1px;"> I am some centered shrink-to-fit content! <br /> tum te tum </div> </div></body>
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
JavaScript Dom: Find Element Index in Container
How to Get the Onclick Calling Object
How to Include CSS and Js Files Via Https When Needed
Changing a CSS Rule-Set from JavaScript
How to Remove a Style Added With .Css() Function
Is $(Document).Ready() Also CSS Ready
Inject CSS Stylesheet as String Using JavaScript
Why Is My Variable Unaltered After I Modify It Inside of a Function? - Asynchronous Code Reference
Difference Between "Let" and "Var"
Get Selected Value in Dropdown List Using JavaScript
JavaScript At Bottom/Top of Web Page
Get Json Data from External Url and Display It in a Div as Plain Text
Why Is ≪Marquee≫ Deprecated and What Is the Best Alternative
Check If an Element'S Content Is Overflowing
How to Get the Actual Rendered Font When It's Not Defined in Css
How to Convert Unordered List into Nicely Styled ≪Select≫ Dropdown Using Jquery
Why Does Jquery or a Dom Method Such as Getelementbyid Not Find the Element