Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

center div in the middle of the screen

Tags:

html

css

i have the "main" div here : http://tallevi.net/daniel/?page_id=5 if i change the screen resolution the div is not in the middle, currently the css of the "main" div is :

.#main {
width: 680px;
position: absolute;
right: 35%;
padding: 0 40px 5px 40px;
font: normal 14px Verdana, Arial, sans-serif;
line-height: 20px;
display: inline-block;
z-index: 2;
background: url(images/content_back.png) repeat-y;
}

wha'ts wrong?

like image 606
Tal Levi Avatar asked Jan 24 '26 20:01

Tal Levi


1 Answers

The best way to centre an element is using margin: 0 auto;, try changing your code to:

#main {
  width: 680px;
  margin: 0 auto;
  padding: 0 40px 5px 40px;
  font: normal 14px Verdana, Arial, sans-serif;
  line-height: 20px;
  z-index: 2;
  background: url(images/content_back.png) repeat-y;
}

Margin: 0 auto sets an "automatic" margin for the right and left, essentially centering it. (This only works if you specify a set width, which you have)

like image 155
Andy Avatar answered Jan 27 '26 10:01

Andy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!