Published in: CSS
URL: http://blog.sympozium.gr/
Our need is very simple. We need a pop-up message, or a div element in general, to appear at the middle of the page anytime using only style sheets.
Our problem is that there is no “center” property in css, like “left” of “right” which can be used with “position:absolute”.
To make it align at the center of the x-axis you have to play the game of percentages. Give to your element a width in percentage (ex. width: 30%), remove the 30% from the 100% of the browsers window and you get the left 70%. Then divide this 70% into two pieces and you get 35%. That the equal left and right margin, our element will have.
<p class="hover_message">The image uploaded successfully</p> p.hover_message { position: absolute; width: 30%; left: 35%; }
Comments
Subscribe to comments
You need to login to post a comment.

Out of curiosity, why would you need a separate ie & moz tag? "margin:auto;" should work in both.
Out of curiosity, why would you need a separate ie & moz tag? "margin:auto;" should work in both.
<style type="text/css"> .ie-center { text-align:center; }
.moz-center { margin:auto; text-align:center; width:250px; }
.center-box { border:1px solid #efefef; background:#efefef; width:100%; height:125px; } </style>
<div class="ie-center"> <div class=""> <div class="center-box"></div> </div> </div>
center-box will be centered, will have 250px in width.
<style type="text/css"> .ie-center { text-align:center; }
.moz-center { margin:auto; text-align:center; width:250px; }
.center-box { border:1px solid #efefef; background:#efefef; width:100%; height:125px; } </style>
<div class="ie-center"> <div class=""> <div class="center-box"></div> </div> </div>
center-box will be centered, will have 250px in width.
Your code is correct, but you are wrong. Using percentages would result in elements that are larger than the ones the user actually needs.
Try this: .ie-center { text-align:center; } .moz-center { margin:auto; text-align:center; width:250px; } .center-box { border:1px solid #efefef; background:#efefef; }
center-box will be centered, will have 250px in width.